【问题标题】:1D Convolution Layer dimension mismatch issue一维卷积层维度不匹配问题
【发布时间】:2021-09-23 18:09:53
【问题描述】:

我正在尝试了解卷积层在神经网络中的工作原理。我为类似问题找到了两个不同的相关帖子并尝试了这些建议,但无法解决。

  1. Keras dimensionality in convolutional layer mismatch
  2. ValueError: Input 0 is incompatible with layer conv_1: expected ndim=3, found ndim=4
import tensorflow as tf
model_valid = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(10,)),
    tf.keras.layers.Dense(16,  activation='relu'),
    tf.keras.layers.Conv1D(16, kernel_size=(2), activation='relu', padding='same'),     
    tf.keras.layers.MaxPooling1D(pool_size=(4), strides=3, padding='valid'),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(1, activation='softmax')
])
model_valid.summary()

我收到Input 0 of layer conv1d_37 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 16] 的不兼容问题。构建卷积层时会出现问题。

【问题讨论】:

    标签: python tensorflow keras neural-network conv-neural-network


    【解决方案1】:

    第一个 Conv1D 之前的输入形状不正确。正如用户 spb 所建议的,Conv1D 的输入必须是 3D 张量,但移除扁平层(如建议的那样)不会解决问题。

    相反,只需在第一个 Dense 层之后添加一个额外的维度。

    import tensorflow as tf
    model_valid = tf.keras.Sequential([
        tf.keras.layers.Flatten(input_shape=(10,)),
        tf.keras.layers.Dense(16,  activation='relu'),
        tf.keras.layers.Reshape((16, 1)), # ADD THIS LINE OF CODE
        tf.keras.layers.Conv1D(16, kernel_size=(2), activation='relu', padding='same'),     
        tf.keras.layers.MaxPooling1D(pool_size=(4), strides=3, padding='valid'),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(1, activation='softmax')
    ])
    model_valid.summary()
    

    现在模型总结是:

    Model: "sequential_2"
    _________________________________________________________________
    Layer (type)                 Output Shape              Param #
    =================================================================
    flatten_4 (Flatten)          (None, 10)                0
    _________________________________________________________________
    dense_4 (Dense)              (None, 16)                176
    _________________________________________________________________
    reshape (Reshape)            (None, 16, 1)             0
    _________________________________________________________________
    conv1d_1 (Conv1D)            (None, 16, 16)            48
    _________________________________________________________________
    max_pooling1d (MaxPooling1D) (None, 5, 16)             0
    _________________________________________________________________
    flatten_5 (Flatten)          (None, 80)                0
    _________________________________________________________________
    dense_5 (Dense)              (None, 1)                 81
    =================================================================
    Total params: 305
    Trainable params: 305
    Non-trainable params: 0
    _________________________________________________________________
    

    【讨论】:

      【解决方案2】:

      Conv1D 层的输入必须是 3D 张量(batch_size、steps、input_dim)。展平张量会将输入转换为二维张量(batch_size,input_dim)。尝试移除 Flatten 层。

      【讨论】:

        猜你喜欢
        • 2017-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-06
        相关资源
        最近更新 更多