我试图掌握 TimeDistributed 包装器在 Keras 中的作用。
我得到 TimeDistributed “将一个层应用于输入的每个时间切片”。
但是我做了一些实验,得到了我无法理解的结果。
简而言之,对于 LSTM 层,TimeDistributed 和 just Dense 层具有相同的结果。
model = Sequential()
model.add(LSTM(5, input_shape = (10, 20), return_sequences = True))
model.add(TimeDistributed(Dense(1)))
print(model.output_shape)
model = Sequential()
model.add(LSTM(5, input_shape = (10, 20), return_sequences = True))
model.add((Dense(1)))
print(model.output_shape)
对于这两个模型,我得到的输出形状都是 (None, 10, 1)。
谁能解释 RNN 层之后 TimeDistributed 和 Dense 层之间的区别?
Dense
层将输入展平然后重新整形,从而连接不同的时间步并具有更多参数,而 TimeDistributed
保持时间步分离(因此具有更少的参数)。在您的情况下 Dense
应该有 500 个参数,TimeDistributed
只有 50
在 keras
- 在构建顺序模型时 - 通常是第二个维度(样本维度之后的一个) - 与 time
维度相关。这意味着,例如,如果您的数据是 5-dim
和 (sample, time, width, length, channel)
,您可以使用 TimeDistributed
(适用于 4-dim
和 (sample, width, length, channel)
)沿时间维度应用卷积层(将同一层应用于每个时间片)以获得5-d
输出。
Dense
的情况是,在 2.0 版的 keras
中,Dense
默认仅应用于最后一个维度(例如,如果您将 Dense(10)
应用于形状为 (n, m, o, p)
的输入,您将得到形状为 (n, m, o, 10)
的输出}) 所以在你的情况下 Dense
和 TimeDistributed(Dense)
是等价的。
Input
张量时,与仅将模型的map
应用于包含Input
的每个切片的列表相比,有什么不同吗?