首页 > 解决方案 > ValueError:输入 0 与层 lstm_55 不兼容:预期 ndim=3,发现 ndim=2

问题描述

我使用 2 个 LSTM 多层堆栈和密集层,它向我显示了一个错误。
这是我的代码:

model.add(LSTM(5, batch_input_shape=(batch_size, X.shape[1], X.shape[2]), stateful=True))
model.add(Dropout(0.2))
model.add(LSTM(5,return_sequences=False))
model.add(Dropout(0.2))
model.add(Dense(units=1))
model.add(Activation('relu'))
batch_input_shape=(1,1,4)

它向我显示以下错误:

ValueError: Input 0 is incompatible with layer lstm_57: expected ndim=3, found ndim=2

标签: pythonkeraslstm

解决方案


您的第二个 LSTM 允许输入 shape [batch_size, time_steps, features]。第一个 LSTM 产生 shape[batch_size, output_units]的输出,因为参数return_sequences默认为False

您需要return_sequences = True在第一个 LSTM 中显式设置以使两个循环层兼容。


推荐阅读