3

我有不同长度的输入。(以下是示例输入)

[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)

4

1 回答 1

2

据我了解,您这里没有功能。你有数字序列,而不是向量序列。你的形状是(n_samples, time_step)

所以如果你想制作一个 3D 张量来输入:

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]
于 2017-03-07T05:55:01.187 回答