【问题标题】:Why is TimeDistributed not needed in my Keras LSTM?为什么我的 Keras LSTM 不需要 TimeDistributed?
【发布时间】:2019-08-27 04:44:51
【问题描述】:

我知道这个话题有很多问题,但我不明白为什么在我的情况下这两种选择都是可能的。 我在 LSTM 中的输入形状是 (10,24,2),我的 hidden_​​size 是 8。

model = Sequential()    
model.add(LSTM(hidden_size, return_sequences=True, stateful = True, 
               batch_input_shape=((10, 24, 2))))
model.add(Dropout(0.1))

为什么可以在下面添加这一行:

model.add(TimeDistributed(Dense(2))) # Option 1

或者这个:

model.add(Dense(2)) # Option 2

Option 2 不应该导致编译错误,因为它需要二维输入吗?

【问题讨论】:

    标签: python tensorflow keras lstm


    【解决方案1】:

    在您的情况下,您定义的 2 个模型是相同的。

    这是因为您使用了return_sequences=True 参数,这意味着Dense 层像TimeDistributedDense 一样应用于每个时间步,但是如果您切换到False,那么这两个模型并不相同并且在 TimeDistributedDense 版本的情况下会引发错误,但不会在 Dense 版本中引发错误。

    here 也对类似情况提供了更彻底的解释。

    【讨论】:

      猜你喜欢
      • 2021-10-25
      • 2018-04-04
      • 2019-03-12
      • 1970-01-01
      • 2020-10-05
      • 2020-08-03
      • 2018-04-28
      • 2017-10-01
      • 2016-07-04
      相关资源
      最近更新 更多