gpt4 book ai didi

python - Keras LSTM input_shape : expected lstm_1_input to have shape (500, 2) 出现问题,但得到形状为 (500, 5) 的数组

转载 作者:行者123 更新时间:2023-12-02 09:30:59 25 4
gpt4 key购买 nike

x_trainy_train 是我的模型的输入和输出,形状为 (6508, 500, 5), (6508, 5)分别。

模型是这样的:

model = Sequential()
model.add(LSTM(units=96, return_sequences=True, input_shape=x_train.shape[1:]))
model.add(Dropout(0.2))
model.add(LSTM(units=96, return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=96))
model.add(Dropout(0.2))
model.add(Dense(units=5))

model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mse'])

model.fit(x_train, y_train, epochs=epochs, batch_size=batch_size)

模型摘要:

Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (None, 500, 96) 39168
_________________________________________________________________
dropout_1 (Dropout) (None, 500, 96) 0
_________________________________________________________________
lstm_2 (LSTM) (None, 500, 96) 74112
_________________________________________________________________
dropout_2 (Dropout) (None, 500, 96) 0
_________________________________________________________________
lstm_3 (LSTM) (None, 96) 74112
_________________________________________________________________
dropout_3 (Dropout) (None, 96) 0
_________________________________________________________________
dense_1 (Dense) (None, 5) 485
=================================================================
Total params: 187,877
Trainable params: 187,877
Non-trainable params: 0

问题是 lstm_1 需要 input_shape (500, 2),而我的数据形状是 (500, 5):

ValueError: Error when checking input: expected lstm_1_input to have shape (500, 2) but got array with shape (500, 5)

我打印图层的形状:

for layer in model.layers:
print(layer.input_shape, end='\t')

# (None, 500, 5) (None, 500, 96) (None, 500, 96) (None, 500, 96) (None, 500, 96) (None, 96) (None, 96)

它为 lstm_1 打印 (None, 500, 5),所以我无法找出问题所在。

Keras==2.3.0
tf==1.14.0

更新:

使用keras==2.2.5tf.keras解决问题。

最佳答案

在答案部分提及解决方案,以造福社区。

使用tf.keras而不是keras已解决问题。

关于python - Keras LSTM input_shape : expected lstm_1_input to have shape (500, 2) 出现问题,但得到形状为 (500, 5) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58013898/

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