【问题标题】:Difference between TimeDistributed(Conv1D) and TimeDistributedTimeDistributed(Conv1D) 和 TimeDistributed 之间的区别
【发布时间】:2021-10-12 21:47:24
【问题描述】:

我无法理解 TensorFlow Keras 中 TimeDistributed(Conv1D)Conv1D 之间的区别。在这个example 中,假定TimeDistrbuted(Dense)Dense 是等价的,因为它适用于最后一个维度。我可以看到 TimeDistributed(Conv1D)Conv1D 的相同输出,但形状除外(如下面的代码所示)。是否也适用于Conv1D

a, b, c = 2, 3, 2
# inputs = np.random.random([a, b, c]).astype(np.float32)
inputs = np.arange(a*b*c).reshape(a, b, c)
input_shape = np.shape(inputs)
print(f'Input                     :{inputs.tolist()}')

def init_cnn_weights(model, layer):
  wt = model.layers[layer].get_weights()
  wt[0] = 2*np.ones(wt[0].shape)
  wt[1] = np.zeros_like(wt[1])
  model.layers[layer].set_weights(wt)

def get_model1_and_output(nf, nk):
  inp = tf.keras.Input(shape=(a, b, c))
  curr_layer = tf.keras.layers.TimeDistributed(tf.keras.layers.Conv1D(nf,nk))(inp)
  model = tf.keras.models.Model(inputs=inp, outputs=curr_layer)
  init_cnn_weights(model, 1)
  # print(model.summary())
  output = model.predict(np.expand_dims(inputs, axis=0))
  return model, output

def get_model2_and_output(nf, nk):
  inp = tf.keras.Input(shape=(b, c))
  curr_layer = tf.keras.layers.Conv1D(nf,nk)(inp)
  model = tf.keras.models.Model(inputs=inp, outputs=curr_layer)
  init_cnn_weights(model, 1)
  # print(model.summary())
  output = model.predict(inputs)
  # output = model.predict(np.concatenate((inputs, inputs), axis=0))
  return model, output

nf = 2
nk = 2
model1, output1 = get_model1_and_output(nf, nk)
print(f'With TD: Output         :{output1.tolist()}')

model2, output2 = get_model2_and_output(nf, nk)
print(f'Without TD: Output      :{output2.tolist()}')

print(f'With TD: weights,bias   :{model1.get_weights()}')
print(f'Without TD: weights,bias:{model2.get_weights()}')

print(f'With TD: Output.shape   :{output1.shape}')
print(f'Without TD: Output.shape:{output2.shape}')
print(f'With TD: param shape    :{model1.get_weights()[0].shape}')
print(f'Without TD: param shape :{model2.get_weights()[0].shape}')

输出:

Input                     :[[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9],
[10, 11]]]
With TD: Output         :[[[[12.0, 12.0], [28.0, 28.0]], [[60.0, 60.0], [76.0, 76.0]]]]
Without TD: Output      :[[[12.0, 12.0], [28.0, 28.0]], [[60.0, 60.0], [76.0, 76.0]]]
With TD: weights,bias   :[array([[[2., 2.],
            [2., 2.]],
            [[2., 2.],
            [2., 2.]]], dtype=float32), array([0., 0.], dtype=float32)]
Without TD: weights,bias:[array([[[2., 2.],
            [2., 2.]],
            [[2., 2.],
            [2., 2.]]], dtype=float32), array([0., 0.], dtype=float32)]
With TD: Output.shape   :(1, 2, 2, 2)
Without TD: Output.shape:(2, 2, 2)
With TD: param shape    :(2, 2, 2)
Without TD: param shape :(2, 2, 2)

【问题讨论】:

    标签: tensorflow convolution tf.keras


    【解决方案1】:

    Timedistributed 不适用于Conv1D

    Timedistributed 主要用于 2D 具有时间步长的数据(例如:LSTM 或任何其他循环形式)。

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 2022-10-20
      • 2022-06-10
      • 2017-07-12
      • 2017-10-01
      • 1970-01-01
      • 2020-05-15
      • 2018-04-04
      • 2020-02-21
      相关资源
      最近更新 更多