timedidistributed (Conv1D)与timedidistributed的差异 &g



我无法理解TensorFlow Keras中TimeDistributed(Conv1D)Conv1D的区别。在这个例子中,假定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)

Timedistributed不适用于Conv1D

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

相关内容

  • 没有找到相关文章

最新更新