[英]What is the correct input shape of multivariate time series for LSTM in keras?
[英]LSTM input shape for multivariate time series?
我知道這個問題已經問過很多次了,但是我確實無法解決我的情況下的這種輸入形狀問題。
我的x_train shape ==(5523000,13)//(13個時間序列的長度5523000)
我的y_train形狀==(5523000,1)
類數== 2
重塑x_train和y_train:
x_train= x_train.values.reshape(27615,200,13) # 5523000/200 = 27615
y_train= y_train.values.reshape((5523000,1)) # I know I have a problem here but I dont know how to fix it
這是我的lstm網絡:
def lstm_baseline(x_train, y_train):
batch_size=200
model = Sequential()
model.add(LSTM(batch_size, input_shape=(27615,200,13),
activation='relu', return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(128, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='softmax'))
model.compile(
loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(x_train,y_train, epochs= 15)
return model
每當我運行代碼時,都會出現此錯誤:
ValueError:輸入0與層lstm_10不兼容:預期ndim = 3,找到的ndim = 4
我的問題是我在這里想念的是什么?
PS:該項目的想法是,我有13個來自人體13個點的信號,我想用它們來檢測某種類型的疾病(一種喚醒)。 通過使用LSTM,我希望我的模型根據這13個信號來定位具有喚醒功能的區域。
。
整個數據為993位患者,每位患者我都使用13個信號來檢測疾病區域。
如果要我將數據放入3D尺寸中:
(500000 ,13, 993)
500000,13,993 (500000 ,13, 993)
#(nb_recods,nb_signals,nb_ Patient)
對於每個患者,我對13個信號有500000次觀察。 nb_ Patient是993
值得注意的是,500000大小無關緊要! 因為我可以讓患者得到更多或更少的觀察結果。
更新:這是一名患者的樣本數據。
您可以嘗試以下修改:
x_train = x_train.reshape(1999, 1, 13)
# double-check dimensions
x_train.shape
def lstm_baseline(x_train, y_train, batch_size):
model = Sequential()
model.add(LSTM(batch_size, input_shape=(None, 13),
activation='relu', return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(128, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='softmax'))
model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
好的,我對您的代碼做了一些更改。 首先,我現在仍然不了解嘗試重塑數據時使用“ 200”表示什么,因此,我將為您提供一個有效的代碼,讓我們看看是否可以使用它或可以對其進行修改以使代碼正常工作。 輸入數據的大小和目標必須匹配。 輸入x_train的行數為27615(x_train [0] = 27615的含義),目標集y_train的值為5523000。
我從您為該示例提供的數據示例中獲取了前兩行:
x_sample = [[-17, -7, -7, 0, -5, -18, 73, 9, -282, 28550, 67],
[-21, -16, -7, -6, -8, 15, 60, 6, -239, 28550, 94]]
y_sample = [0, 0]
讓我們重塑x_sample :
x_train = np.array(example)
#Here x_train.shape = (2,11), we want to reshape it to (2,11,1) to
#fit the network's input dimension
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1)
您正在使用分類損失,因此必須將目標更改為分類(檢查https://keras.io/utils/ )
y_train = np.array(target)
y_train = to_categorical(y_train, 2)
現在您有兩個類別,在您提供的所有目標值均為0的數據中,我假設有兩個類別,所以我不知道目標可以采用多少個可能的值。 如果目標可以取4個可能的值,則to_categorical函數中的類別數將為4。最后一個密集層的每個輸出將代表一個類別,並且該輸出的值即您的輸入屬於該類別的概率。
現在,我們只需要稍微修改您的LSTM模型即可:
def lstm_baseline(x_train, y_train):
batch_size = 200
model = Sequential()
#We are gonna change input shape for input_dim
model.add(LSTM(batch_size, input_dim=1,
activation='relu', return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(128, activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))
#We are gonna set the number of outputs to 2, to match with the
#number of categories
model.add(Dense(2, activation='softmax'))
model.compile(
loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=15)
return model
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.