【问题标题】:Issue trying to implement stacked LSTM layers with Keras尝试使用 Keras 实现堆叠 LSTM 层的问题
【发布时间】:2018-04-29 07:34:44
【问题描述】:

我正在尝试向我的神经网络添加更多 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)

当我删除第二个 LSTM 层时它工作正常。或者,如果我添加更密集的层。只是在我添加 LSTM 层时没有。 RUL_train 的形状为 (385, 128, 1)。 model.summary 的输出如下:

_________________________________________________________________
   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?
  • return_sequences=True 在最后一层

标签: python keras


【解决方案1】:

您的标签数组具有三个维度:(385,128,1)

那么,你的目的是什么?

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

【讨论】:

    【解决方案2】:

    这是 Keras 2.1.0 中引入的错误(在 2.1.1 中并未完全修复)。尝试安装 Keras 2.0.9 或更早版本:

    pip uninstall keras
    pip install keras==2.0.9
    

    https://github.com/fchollet/keras/issues/8481

    【讨论】:

    • 感谢您的建议。我实际上正在运行 Keras 2.0.8。但我还是尝试更新到 2.0.9,但我仍然遇到同样的问题。
    猜你喜欢
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2017-03-12
    • 2019-06-29
    • 2021-07-26
    相关资源
    最近更新 更多