gpt4 book ai didi

deep-learning - Keras- LSTM- 输入大小错误

转载 作者:行者123 更新时间:2023-12-02 01:15:55 24 4
gpt4 key购买 nike

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

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

现在我需要将输入 reshape 为 [样本、时间步长、特征] 以根据 keras 文档实现 LSTM 层。

但是当我将输入的填充数组 reshape 为 -

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)

在将数据拟合到模型时,以下是我得到的错误-

异常:检查模型输入时出错:预期 lstm_input_4 的形状为 (None, 10418, 694) 但得到的数组形状为 (10418, 694, 1)

最佳答案

据我了解,您这里没有功能。你有数字序列,而不是向量序列。您的形状是 (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) reshape 为 (13189,694,694)。第二个有更多的值(value),因此提示。

希望对你有帮助

编辑:

您的训练数据在整形后的形状为 (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]

关于deep-learning - Keras- LSTM- 输入大小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42640212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com