我有不同长度的输入。(以下是示例输入)
[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)
现在我需要将输入重塑为 [samples, time step, features] 以根据 keras 文档实现 LSTM 层。
但是当我将输入填充数组重塑为 -
X_reshape = 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)
在将数据拟合到模型时,以下是我得到的错误-
异常:检查模型输入时出错:预期 lstm_input_4 具有形状 (None, 10418, 694) 但得到的数组具有形状 (10418, 694, 1)