【问题标题】:How can a LSTM have an output_dim different than the input_dim of the next layer?LSTM 的 output_dim 如何与下一层的 input_dim 不同?
【发布时间】:2018-08-11 04:59:24
【问题描述】:

我在看这段代码:

model = Sequential()
model.add(LSTM(input_shape = (1,), input_dim=1, output_dim=6, return_sequences=True))
model.add(LSTM(input_shape = (1,), input_dim=1, output_dim=6, return_sequences=False))
model.add(Dense(1))
model.add(Activation('linear'))

第一层怎么会有一个dim=6的输出,然后下一层有一个input_dim=1?

编辑

代码错误,Keras 只是尽力而为,如实际生成的模型所示(查看模型如何与代码不匹配):

【问题讨论】:

    标签: python machine-learning keras lstm keras-layer


    【解决方案1】:

    这段代码非常混乱,不应该这样写。

    在顺序模型中,Keras 仅尊重第一层的input_shape。所有后续层都使用前一层的输出进行初始化,有效地忽略了input_shape 规范。源代码:keras/models.py。在这种情况下,它是(None, None, 6)

    因此模型摘要如下所示:

    Layer (type)                 Output Shape              Param #   
    =================================================================
    lstm_1 (LSTM)                (None, None, 6)           192       
    _________________________________________________________________
    lstm_2 (LSTM)                (None, 6)                 312       
    =================================================================
    

    顺便说一句,keras 会在 LSTM 规范中发出警告,因为 input_dim 已被弃用:

    更新您对 Keras 2 API 的 LSTM 调用:LSTM(input_shape=(None, 1), return_sequences=True, units=6)

    将您的 LSTM 呼叫更新为 Keras 2 API:LSTM(input_shape=(None, 1), return_sequences=False, units=6)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2020-10-25
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      相关资源
      最近更新 更多