'Input 0 of layer conv1d_27 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [1, None, 256, 3072]

Running the following code shows the dimension problem. As I look there is no direct input data to the model it's quite confusing. I play a lot with TensorFlow and Keras versions but the problem is the same. Thank for help.

Tensorflow==2.2.0 Keras==2.3.1

class NonMasking(keras.layers.Layer):   
    def __init__(self, **kwargs):   
        self.supports_masking = True  
        super(NonMasking, self).__init__(**kwargs)   
  
    def build(self, input_shape):   
        input_shape = input_shape   
  
    def compute_mask(self, input, input_mask=None):   
        # do not pass the mask to the next layers   
        return None   
  
    def call(self, x, mask=None):   
        return x   
  
    def get_output_shape_for(self, input_shape):   
        return input_shape  




   
    inputs = model.inputs
    bert_out = NonMasking()(model.outputs)
    
    bert_out = SpatialDropout1D(0.2)(bert_out)
    
    filter_lengths = [2, 3, 4, 5]
    conv_layers = []
    for filter_length in filter_lengths:
        conv_layer = Conv1D(filters=256, kernel_size=filter_length, padding='valid',
                            strides=1, activation='relu')(bert_out)
        maxpooling = MaxPool1D(pool_size=SEQ_LEN - filter_length + 1)(conv_layer)
        flatten = Flatten()(maxpooling)
        conv_layers.append(flatten)
    sentence_embed = Concatenate()(conv_layers)

dense_layer = Dense(256, activation='relu')(sentence_embed)
preds = Dense(1, activation='sigmoid')(dense_layer)
model1 = Model(inputs, preds)

ERROR

    ValueError                                Traceback (most recent call last)
<ipython-input-101-626bf205f5f3> in <module>()
      8 for filter_length in filter_lengths:
      9     conv_layer = Conv1D(filters=256, kernel_size=filter_length, padding='valid',
---> 10                         strides=1, activation='relu')(bert_out)
     11     maxpooling = MaxPool1D(pool_size=SEQ_LEN - filter_length + 1)(conv_layer)
     12     flatten = Flatten()(maxpooling)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    178                          'expected ndim=' + str(spec.ndim) + ', found ndim=' +
    179                          str(ndim) + '. Full shape received: ' +
--> 180                          str(x.shape.as_list()))
    181     if spec.max_ndim is not None:
    182       ndim = x.shape.ndims

ValueError: Input 0 of layer conv1d_27 is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: [1, None, 256, 3072]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source