Tensorflow Model Input Shape Error: Input 0 of layer sequential_11 incompatible with layer: rank undefined, but the layer requires a defined rank

Viewed 628

I am trying to train a 1D CNN model in TensorFlow with data of the input shape (14400,1), but I am receiving an error that the input shape is incompatible with the model. I have ensured that my input data is of the correct shape. I am using TensorFlow version 2.3.0

Batches Snippet (32 examples per batch, data shape - (14400,1), label shape - (1,1) )

batch:  0
Data shape:  (32, 14400, 1) (32, 1, 1)
batch:  1
Data shape:  (32, 14400, 1) (32, 1, 1)
batch:  2
Data shape:  (32, 14400, 1) (32, 1, 1)
batch:  3
Data shape:  (32, 14400, 1) (32, 1, 1)
batch:  4
Data shape:  (32, 14400, 1) (32, 1, 1)
batch:  5
Data shape:  (32, 14400, 1) (32, 1, 1)

CNN Model

model = Sequential()
model.add(Conv1D(128, kernel_size=5, activation='relu', input_shape=(14400,1)))
model.add(BatchNormalization())
model.add(Dropout(.2))
model.add(Conv1D(32, kernel_size=5, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(.2))
model.add(Dense(64, activation='relu'))
model.add(Dropout(.2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
model.summary()

Model Summary

Model: "sequential_11"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_19 (Conv1D)           (None, 14396, 128)        768       
_________________________________________________________________
batch_normalization_10 (Batc (None, 14396, 128)        512       
_________________________________________________________________
dropout_40 (Dropout)         (None, 14396, 128)        0         
_________________________________________________________________
conv1d_20 (Conv1D)           (None, 14392, 32)         20512     
_________________________________________________________________
batch_normalization_11 (Batc (None, 14392, 32)         128       
_________________________________________________________________
dropout_41 (Dropout)         (None, 14392, 32)         0         
_________________________________________________________________
flatten_8 (Flatten)          (None, 460544)            0         
_________________________________________________________________
dense_32 (Dense)             (None, 128)               58949760  
_________________________________________________________________
dropout_42 (Dropout)         (None, 128)               0         
_________________________________________________________________
dense_33 (Dense)             (None, 64)                8256      
_________________________________________________________________
dropout_43 (Dropout)         (None, 64)                0         
_________________________________________________________________
dense_34 (Dense)             (None, 32)                2080      
_________________________________________________________________
dropout_44 (Dropout)         (None, 32)                0         
_________________________________________________________________
dense_35 (Dense)             (None, 1)                 33        
=================================================================
Total params: 58,982,049
Trainable params: 58,981,729
Non-trainable params: 320
_________________________________________________________________

Code that causes the error

history = model.fit(train_ds, validation_data=val_ds, epochs=10)

Error Message

ValueError: in user code:

    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:806 train_function  *
        return step_function(self, iterator)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:796 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
        return fn(*args, **kwargs)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:789 run_step  **
        outputs = model.train_step(data)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:747 train_step
        y_pred = self(x, training=True)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py:976 __call__
        self.name)
    /data/anaconda3/envs/py36/lib/python3.6/site-packages/tensorflow/python/keras/engine/input_spec.py:168 assert_input_compatibility
        layer_name + ' is incompatible with the layer: '

    ValueError: Input 0 of layer sequential_11 is incompatible with the layer: its rank is undefined, but the layer requires a defined rank.

I would greatly appreciate the help.

2 Answers

I figured out my problem. It was coming from the custom generator I built with the tf.data.Dataset.from_generator function. Since I didn't specify the output shapes of the data and labels, then these shapes were defined as unknown, and the input layer of the network couldn't figure out the shape of the data.

you can see the documentation of Conv1D here [https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv1D]

The input shape of your first layer should include the batch size. (32,14440,1) if you try this script below you will have the same error

input_shape = (14440,1)
x = tf.random.normal(input_shape)
y = tf.keras.layers.Conv1D(128, 5,   activation='relu',input_shape=input_shape)(x)
y.shape

but with the input shape (32,14440,1) it works.

Related