时间分布层在 Keras 的作用是什么?

我试图了解 Timedistribution 包装器在 Keras 做什么。

我知道 Timedistribution“对输入的每个时间片都应用了一个层”

但是我做了一些实验,得到了我无法理解的结果。

总之,在连接到 LSTM 层时,Timedistribution 和公正的 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)

对于两个模型,我得到了 (无,10,1)的输出形状。

有人能解释一下在 RNN 层之后的时间分布层和密集层之间的区别吗?

53488 次浏览

In keras - while building a sequential model - usually the second dimension (one after sample dimension) - is related to a time dimension. This means that if for example, your data is 5-dim with (sample, time, width, length, channel) you could apply a convolutional layer using TimeDistributed (which is applicable to 4-dim with (sample, width, length, channel)) along a time dimension (applying the same layer to each time slice) in order to obtain 5-d output.

The case with Dense is that in keras from version 2.0 Dense is by default applied to only last dimension (e.g. if you apply Dense(10) to input with shape (n, m, o, p) you'll get output with shape (n, m, o, 10)) so in your case Dense and TimeDistributed(Dense) are equivalent.