Home > Software engineering >  ValueError: Input 0 of layer "sequential_13" is incompatible with the layer: expected shap
ValueError: Input 0 of layer "sequential_13" is incompatible with the layer: expected shap

Time:05-22

I don't know why this error keep coming when I run the code below

CNN.fit(X_train_vector, y_train, epochs=10)

My CNN code is this:

CNN = tf.keras.models.Sequential()
CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (21367, 9000)))
CNN.add(tf.keras.layers.MaxPooling1D(2))
CNN.add(tf.keras.layers.Dropout(0.2))
CNN.add(tf.keras.layers.Flatten())

CNN.add(tf.keras.layers.Dense(200, activation='relu'))
CNN.add(tf.keras.layers.Dense(20, activation='relu'))
CNN.add(tf.keras.layers.Dense(1, activation='softmax'))

My "X_train_vector" has a shape:

(21367, 9000)

My "y_train" has a shape:

(21367, 1)

The Error I am getting:

Epoch 1/10
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-108-895976bf38cd> in <module>()
----> 1 CNN.fit(X_train_vector, y_train, epochs=10)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

    ValueError: Input 0 of layer "sequential_13" is incompatible with the layer: expected shape=(None, 21367, 9000), found shape=(None, 9000)

I have tried several solutions, including changing my first line of CNN to this:

CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000)))

But running it says:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-109-dd8d734d0a9f> in <module>()
      1 CNN = tf.keras.models.Sequential()
----> 2 CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000)))
      3 CNN.add(tf.keras.layers.MaxPooling1D(2))
      4 CNN.add(tf.keras.layers.Dropout(0.2))
      5 CNN.add(tf.keras.layers.Flatten())

3 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/base_layer.py in __init__(self, trainable, name, dtype, dynamic, **kwargs)
    441         else:
    442           batch_size = None
--> 443         batch_input_shape = (batch_size,)   tuple(kwargs['input_shape'])
    444       self._batch_input_shape = batch_input_shape
    445 

TypeError: 'int' object is not iterable

Can anyone help me. I have been looking for the solution for two days, it should work the way I am trying. Is there a mistake I am making? Please let me know. Thanks In Advance.

CodePudding user response:

The input array should have the shape (None, shape_0, shape_1), where None represent the batch size, and (shape_0, shape_1) represents the shape of the feature. So, you should reshape your input array:

X_train_vector = X_train_vector.reshape(-1, 9000, 1)

And you don't really need to specify the batch size when building the model, so remove that and just use (9000, 1) as the input_shape. Try this:

CNN = tf.keras.models.Sequential()
CNN.add(tf.keras.layers.Conv1D(120, kernel_size=3, padding='valid', activation='relu', input_shape = (9000, 1)))
CNN.add(tf.keras.layers.MaxPooling1D(2))
CNN.add(tf.keras.layers.Dropout(0.2))
CNN.add(tf.keras.layers.Flatten())

CNN.add(tf.keras.layers.Dense(200, activation='relu'))
CNN.add(tf.keras.layers.Dense(20, activation='relu'))
CNN.add(tf.keras.layers.Dense(1, activation='softmax'))

And this should solve the problem the same error would not appear again.

  • Related