我有不同的長度輸入。 (下面是樣本輸入)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)
感謝您的答覆。我嘗試了你所描述的,但仍然面臨一些錯誤。我已經更新了這個問題。你能告訴我什麼是錯的嗎? – xlax
我編輯了我的答案:) –
感謝Nassim。知道什麼是錯的。完美工作! – xlax