Python 尝试使用KERA实现堆叠LSTM层时出现的问题

Python 尝试使用KERA实现堆叠LSTM层时出现的问题,python,keras,Python,Keras,我正试图向我的神经网络添加更多的LSTM层,但我不断得到以下错误: ValueError: Error when checking target: expected dense_4 to have 2 dimensions, but got array with shape (385, 128, 1) 我的模型的代码如下所示: model = Sequential() model.add(LSTM(60, return_sequences=True, input_shape=(128, 1

我正试图向我的神经网络添加更多的LSTM层,但我不断得到以下错误:

ValueError: Error when checking target: expected dense_4 to have 2 dimensions, but got array with shape (385, 128, 1) 
我的模型的代码如下所示:

model = Sequential()

model.add(LSTM(60, return_sequences=True, input_shape=(128, 14)))

model.add(LSTM(60, return_sequences=False))

model.add(Dense(1))

model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(data_train, RUL_train, epochs=number_epochs, batch_size=batch_size, verbose=1)
_________________________________________________________________
   Layer (type)                 Output Shape              Param #   
=================================================================
lstm_15 (LSTM)               (None, 128, 60)           18000     
_________________________________________________________________
lstm_16 (LSTM)               (None, 60)                29040     
_________________________________________________________________
dense_7 (Dense)              (None, 1)                 61        
=================================================================
Total params: 47,101
Trainable params: 47,101
Non-trainable params: 0
_________________________________________________________________
当我移除第二个LSTM层时,它可以正常工作。或者如果我添加更密集的层。只是当我添加LSTM层时没有。RUL_火车有形状(385128,1)。 model.summary的输出如下:

model = Sequential()

model.add(LSTM(60, return_sequences=True, input_shape=(128, 14)))

model.add(LSTM(60, return_sequences=False))

model.add(Dense(1))

model.compile(loss='mean_squared_error', optimizer='adam')

model.fit(data_train, RUL_train, epochs=number_epochs, batch_size=batch_size, verbose=1)
_________________________________________________________________
   Layer (type)                 Output Shape              Param #   
=================================================================
lstm_15 (LSTM)               (None, 128, 60)           18000     
_________________________________________________________________
lstm_16 (LSTM)               (None, 60)                29040     
_________________________________________________________________
dense_7 (Dense)              (None, 1)                 61        
=================================================================
Total params: 47,101
Trainable params: 47,101
Non-trainable params: 0
_________________________________________________________________

感谢您的帮助。

这是Keras 2.1.0中引入的一个bug(在2.1.1中没有完全修复)。尝试安装Keras 2.0.9或更早版本:

pip uninstall keras
pip install keras==2.0.9

标签数组有三个维度:
(385128,1)

那么,你的目的是什么

  • 对每个序列中的所有步骤进行分类?-所有LSTM必须使用
    return\u sequence=True
  • 整个序列只有一个类?-以某种方式将标签数组修复为
    (示例,1)

您使用哪种版本的KERA?
return\u sequences=True
在最后一层感谢您的建议。我实际上是在运行Keras2.0.8。但我还是试着升级到2.0.9,我仍然遇到同样的问题。