【问题标题】:Dense vs. TimeDistributed(Dense)密集与时间分布式(密集)
【发布时间】:2020-04-22 09:38:16
【问题描述】:

这两种使用Dense层的方法有什么区别吗?似乎输出形状相同,参数数量相同。

  1. 如果我们使用固定权重,输出是否相同?
  2. 训练时的结果会不会一样?
def test_rnn_output_v1():

    max_seq_length = 10
    n_features = 8
    rnn_dim = 64
    dense_dim = 16

    input = Input(shape=(max_seq_length, n_features))
    out = LSTM(rnn_dim, return_sequences=True)(input)
    out = Dense(dense_dim)(out)

    model = Model(inputs=[input], outputs=out)

    print(model.summary())

    # (None, max_seq_length, n_features)
    # (None, max_seq_length, dense_dim)

def test_rnn_output_v2():

    max_seq_length = 10
    n_features = 8
    rnn_dim = 64
    dense_dim = 16

    input = Input(shape=(max_seq_length, n_features))
    out = LSTM(rnn_dim, return_sequences=True)(input)
    out = TimeDistributed(Dense(dense_dim))(out)

    model = Model(inputs=[input], outputs=out)

    print(model.summary())

    # (None, max_seq_length, n_features)
    # (None, max_seq_length, dense_dim)

【问题讨论】:

    标签: python tensorflow keras lstm tf.keras


    【解决方案1】:

    TimeDistributed(Dense(...))Dense(...) 之间没有区别,它们的输出维度和连接性完全相同。那是因为Dense 层应用在其输入的最后一个轴上;因此,它是否被包裹在TimeDistributed 层中并没有什么区别。 This answer 更详细地解释了Dense 层的工作原理。

    【讨论】:

      猜你喜欢
      • 2017-11-20
      • 1970-01-01
      • 2011-06-03
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      相关资源
      最近更新 更多