【问题标题】:Keras Concatenate outputs of Dense into a MatrixKeras 将 Dense 的输出连接成一个矩阵
【发布时间】:2021-03-11 08:08:03
【问题描述】:

我正在尝试使用 API 实现一个结构相当复杂的 Keras 模型(至少与我习惯的一样)。 该模型应该以单个 CNN 为特征,以从一系列帧中提取特征(所有帧本质上应该由同一个 CNN 解析),然后应该将 CNN 的输出连接起来再次形成一个矩阵。

假设输入帧是 64x64 的图像,CNN 的输出是 Dense(16)

input_frame = tf.keras.Input(shape=(64, 64, 3))
x = tf.keras.layers.Conv2D(16,(4,4), activation='relu')(input_frame)
x = tf.keras.layers.MaxPooling2D(pool_size=(2,2))(x)
x = tf.keras.layers.Conv2D(32,(4,4), activation='relu')(x)
x = tf.keras.layers.MaxPooling2D(pool_size=(2,2))(x)
x = tf.keras.layers.Conv2D(32,(4,4), activation='relu')(x)
x = tf.keras.layers.MaxPooling2D(pool_size=(2,2))(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(16)(x)

features_out = [x for i in range(num_frames)]

然后我尝试将输出与

连接起来
tcn_input = tf.keras.layers.Concatenate(axis=-1)(features_out)

在我得到的总结中

dense_27 (Dense)                (None, 16)           33296       flatten_13[0][0]                 
__________________________________________________________________________________________________
concatenate_10 (Concatenate)    (None, 240)          0           dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
                                                                 dense_27[0][0]                   
==================================================================================================

我认为这是正确的。不过,就我而言,我更喜欢矩阵,所以我尝试这样做

tcn_input = tf.keras.layers.Concatenate(axis=0)(features_out)

但是改变轴给了我这个结果

dense_31 (Dense)                (None, 16)           33296       flatten_17[0][0]                 
__________________________________________________________________________________________________
concatenate_14 (Concatenate)    (None, 16)           0           dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
                                                                 dense_31[0][0]                   
==================================================================================================

输出形状为 16,我觉得令人困惑(我预计会有 16xnum_frames)。 我相信我误解了图层的连接方式,但我无法弄清楚。 任何人都可以解释为什么它给出了我期望的输出?

【问题讨论】:

  • 您正在沿批处理轴 (axis=0) 连接。在将训练数据放入模型之前,批量大小是未知的(即无)。没有时间和整数是无,这就是为什么你看到 (None, 16) 作为输出。如果您将数据放入模型中,batch_size 为 B 并将 num_frames 设置为 N,则输出的大小为 (B*N, 16)

标签: python tensorflow keras deep-learning


【解决方案1】:

正如在 cmets 中指出的那样,您正在沿特征或批次维度进行连接,而不是在新维度上进行连接。您可以进行以下更改以获得 [batch size, num_frames, 16] 大小的张量。


input_frame = tf.keras.Input(shape=(64, 64, 3))
x = tf.keras.layers.Conv2D(16,(4,4), activation='relu')(input_frame)
x = tf.keras.layers.MaxPooling2D(pool_size=(2,2))(x)
x = tf.keras.layers.Conv2D(32,(4,4), activation='relu')(x)
x = tf.keras.layers.MaxPooling2D(pool_size=(2,2))(x)
x = tf.keras.layers.Conv2D(32,(4,4), activation='relu')(x)
x = tf.keras.layers.MaxPooling2D(pool_size=(2,2))(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(16)(x)

# Add a new dimension to x so that we can concatenate over that
features_out = [tf.keras.layers.Lambda(lambda x: x[:, tf.newaxis, :])(x) for i in range(num_frames)]

# We concatenate over the new dimension
tcn_input = tf.keras.layers.Concatenate(axis=-2)(features_out)

model = tf.keras.models.Model(input_frame, tcn_input)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多