iT邦幫忙

0

LSTM 分類問題

  • 分享至 

  • xImage

我的問題是我使用16個感光元件使用掃描IC依序掃描電壓變化 去判斷是什麼方向0、1 演算法是用LSTM 但是準確率只有58% 不知道哪裡有問題

import pandas as pd
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, LSTM, TimeDistributed, RepeatVector
from keras.layers.normalization import BatchNormalization
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, ModelCheckpoint
import matplotlib.pyplot as plt
#matplotlib inline

def readTrain():
train = pd.read_csv("rawdata.csv")
#print(train)

return train

def normalize(train):
train = train.drop(["Time"], axis=1)
#train = train.drop(["Time"], axis=1)
temp = train['Dir']
train_norm = train.apply(lambda x: (x - np.mean(x)) / (np.max(x) - np.min(x)))
train_norm['Dir'] = temp
return train_norm

def buildTrain(train, pastDay=30, futureDay=1):
X_train, Y_train = [], []
for i in range(train.shape[0]-futureDay-pastDay):
X_train.append(np.array(train.iloc[i:i+pastDay]))
Y_train.append(np.array(train.iloc[i+pastDay:i+pastDay+futureDay]["Dir"]))
return np.array(X_train), np.array(Y_train)

def shuffle(X,Y):
np.random.seed(10)
randomList = np.arange(X.shape[0])
np.random.shuffle(randomList)
return X[randomList], Y[randomList]

def splitData(X,Y,rate):
X_train = X[int(X.shape[0]*rate):]
Y_train = Y[int(Y.shape[0]*rate):]
X_val = X[:int(X.shape[0]*rate)]
Y_val = Y[:int(Y.shape[0]*rate)]
return X_train, Y_train, X_val, Y_val

train = readTrain()

train_norm = normalize(train)

X_train, Y_train = buildTrain(train_norm, 1, 1)

X_train, Y_train = shuffle(X_train, Y_train)

X_train, Y_train, X_val, Y_val = splitData(X_train, Y_train, 0.1)

def buildOneToOneModel(shape):
model = Sequential()
model.add(LSTM(10, input_length=shape[1], input_dim=shape[2],return_sequences=True))

model.add(TimeDistributed(Dense(1))) # or use model.add(Dense(1))
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=['accuracy'])
model.summary()
return model

Y_train = Y_train[:,np.newaxis]
Y_val = Y_val[:,np.newaxis]

model = buildOneToOneModel(X_train.shape)
callback = EarlyStopping(monitor="loss", patience=1000, verbose=1, mode="auto")
model.fit(X_train, Y_train, epochs=3000, batch_size=128, validation_data=(X_val, Y_val), callbacks=[callback])
model.save('my_model.h5')

資料有400多筆
Voltage0 Voltage1 Voltage2 Voltage3 Voltage4 Voltage5 Voltage6 Voltage7 Voltage8 Voltage9 Voltage10 Voltage11 Voltage12 Voltage13 Voltage14 Voltage15 Dir Time
1422 1842 1542 1749 1548 1863 1683 1884 951 1809 1209 1803 1230 1764 1845 1461 1 0
1422 1842 1542 1752 1551 1863 1686 1896 1182 1368 1203 1800 1236 1770 1848 1458 1 1610
1413 1830 1530 1737 1539 1848 1674 1884 1161 918 1188 1788 1218 1752 1833 1449 1 3220

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答