Home > Software design >  1D Convolution Layer dimension mismatch issue
1D Convolution Layer dimension mismatch issue

Time:09-24

I'm trying to learn how the convolution layer works in neural networks. I found two different related posts for a similar issue and tried the suggestions, but could not get around.

  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()

I am receiving an incompatiblity issue as Input 0 of layer conv1d_37 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 16]. The issue arises when building up the convolution layer.

CodePudding user response:

The input's shape before the first Conv1D is incorrect. As user spb suggested, the input to Conv1D must be a 3D tensor, though removing the flatten layer (as suggested) won't fix the problem.

Instead, simply add an extra dimension after the first Dense layer.

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()

Now the model summary is:

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
_________________________________________________________________

CodePudding user response:

Input to Conv1D layer must be 3D Tensor (batch_size, steps, input_dim). Flattening the tensor will convert input into 2D tensor (batch_size, input_dim). Try removing the Flatten layer.

  • Related