3

我有不同的長度輸入。 (下面是樣本輸入)Keras-LSTM-輸入大小錯誤

[0.501757009346, 0.554708349218] 
[0.460997102135, 0.554708349218] 
[0.377844867627] 
[0.328125, 0.554708349218] 
[-0.266091572661, 0.554708349218, 0.554708349218] 
[0.514723203769] 
[0.104587155963, 0.554708349218] 
[0.247003647733, 0.554708349218] 
[0.586212380233] 
[0.559979406212, 0.554708349218] 
[0.412262156448, 0.554708349218] 

因此,我已經填補輸入序列如下 -

In [115]: from keras.preprocessing.sequence import pad_sequences 

In [116]: max_sequence_length = max([len(i) for i in X]) 

In [117]: padded_sequences = pad_sequences(X, max_sequence_length).tolist() 

In [118]: X_padd=np.array(padded_sequences) 


In [119]: X_padd.shape 
Out[119]: (13189, 694) 

現在我需要重塑輸入要的[樣品,時間步驟,特徵]根據keras文檔實現LSTM層。

但是,當我重塑輸入襯墊陣列 -

X_reshaped = X_padd.reshape(X_padd.shape [1],max_sequence_length,X_padd.shape [0])

它將引發下面的錯誤。請幫我解決這個問題。謝謝。

In [120]: X_reshaped = X_padd.reshape(X_padd.shape[1], max_sequence_length, X_padd.shape[0]) 
--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-120-86980292fb31> in <module>() 
----> 1 X_reshaped = X_padd.reshape(X_padd.shape[1], max_sequence_length, X_padd.shape[0]) 

ValueError: total size of new array must be unchanged 

------ -----更新

max_sequence_length = max([len(i) for i in X]) 
padded_sequences = pad_sequences(X, max_sequence_length).tolist() 
X_padd=np.array(padded_sequences) # shape -> (13023, 694) 

X_reshaped = X_padd.reshape(X_padd.shape[0],X_padd.shape[1],1) 

X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(X_reshaped,Y,test_size=0.2,random_state=42) 

input_length = X_train.shape[0] 
input_dim = X_train.shape[1] 

model=Sequential() 
model.add(LSTM(4, input_dim=input_dim, input_length=input_length)) 
model.add(Dropout(0.5)) 
model.add(Dense(1)) 
model.add(Activation('sigmoid')) 
model.compile(loss='mean_squared_error', optimizer='adam') 
model.fit(X_train, Y_train, nb_epoch=50, batch_size=12) 
的數據擬合模型

,下面是錯誤,我getting-

例外:當錯誤檢查模型輸入:預計lstm_input_4有形狀(無,10418,694),但有形狀的陣列(10418,694,1)

回答

1

據我瞭解,你沒有這裏的功能。你有數字序列,而不是向量序列。你的形狀是(n_samples, time_step)

所以,如果你想使一個三維張量輸入:

X_Reshaped = X_pad.reshape(X_pad[0], X_pad[1], 1) 

記住X_pad[1]是你max_sequence_length。所以你試圖將張量形狀(13189,694)重塑成(13189,694,694)。第二個有更多的價值,因此抱怨。

我希望這有助於

編輯:

你的訓練數據具有重塑後的形狀(n_samples, time_steps, num_feat)。 因此,您的lstm的輸入數據將具有(batch_size, time_steps, features)的形狀。因此,當您指定input_lengthinput_dim時,應該放置time_steps和num_feat值而不是n_samples和time_steps。

因此改變:

input_length = X_train.shape[1] 
input_dim = X_train.shape[2] 
+0

感謝您的答覆。我嘗試了你所描述的,但仍然面臨一些錯誤。我已經更新了這個問題。你能告訴我什麼是錯的嗎? – xlax

+1

我編輯了我的答案:) –

+0

感謝Nassim。知道什麼是錯的。完美工作! – xlax