本文介绍了TimeDistributed层在Keras中的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解 TimeDistributed 包装器在 Keras 中的作用.

I am trying to grasp what TimeDistributed wrapper does in Keras.

我知道 TimeDistributed 将一个层应用于输入的每个时间切片."

I get that TimeDistributed "applies a layer to every temporal slice of an input."

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

But I did some experiment and got the results that I cannot understand.

简而言之,就LSTM层而言,TimeDistributed和just Dense层的结果是一样的.

In short, in connection to LSTM layer, TimeDistributed and just Dense layer bear same results.

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) 的输出形状.

For both models, I got output shape of (None, 10, 1).

谁能解释一下 RNN 层之后的 TimeDistributed 层和 Dense 层之间的区别?

Can anyone explain the difference between TimeDistributed and Dense layer after an RNN layer?

推荐答案

keras 中 - 在构建顺序模型时 - 通常是第二个维度(在样本维度之后) - 与一个 时间维度.这意味着,例如,如果您的数据是 5-dim(sample, time, width, length, channel),您可以使用 TimeDistributed 应用卷积层(适用于4-dim with (sample, width, length, channel))沿时间维度(对每个时间片应用相同的层) 以获得 5-d 输出.

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.

Dense 的情况是,在 keras 2.0 版中 Dense 默认仅应用于最后一个维度(例如,如果您应用 Dense(10) 输入形状 (n, m, o, p) 你会得到形状 (n, m, o, 10)) 所以在你的情况下 DenseTimeDistributed(Dense) 是等价的.

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.

这篇关于TimeDistributed层在Keras中的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 16:09