'ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5)

i use a model with a combinaison of GRu and Conv1D .And when i want to fit the model i get an error in ValueError: Input 0 of layer "sequential_8" is incompatible with the layer: expected shape=(None, 223461, 5), found shape=(None, 5). this is my code : the shape of X_train is (223461, 5) , whereas the y_train is (223461,)

  verbose, epochs, batch_size = 0, 100, 64
    n_timesteps, n_features, n_outputs = X_train.shape[0], X_train.shape[1], y_train.shape[0]
    model = Sequential()
    model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))
    model.add(MaxPooling1D(pool_size=2))
    model.add(GRU(64))
    model.add(Dropout(0.4))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(n_outputs, activation='softmax'))
    opt = Adam(learning_rate=0.01)
    model.compile(loss='categorical_crossentropy', optimizer=opt , metrics=['accuracy'])
    model.summary()

the output of summary is :

Model: "sequential_8"
_________________________________________________________________  Layer (type)                Output Shape              Param #   
=================================================================  conv1d_8 (Conv1D)           (None, 223459, 64)        1024      
                                                                   max_pooling1d_8 (MaxPooling  (None, 111729, 64)       0           1D) 

                                                                   gru_7 (GRU)                 (None, 64)                24960     
                                                                   dropout_14 (Dropout)        (None, 64)                0         
                                                                   flatten_6 (Flatten)         (None, 64)                0         
                                                                   dense_14 (Dense)            (None, 128)               8320      
                                                                   dropout_15 (Dropout)        (None, 128)               0         
                                                                   dense_15 (Dense)            (None, 223461)            28826469  
                                                                 
================================================================= Total params: 28,860,773 Trainable params: 28,860,773 Non-trainable params: 0
_________________________________________________________________

and here where I face the error:

model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size, verbose=verbose)
_, accuracy = model.evaluate(X_test, y_test, batch_size=batch_size, verbose=0)


Solution 1:[1]

According to your model, your training data x_train and y_train is just a piece of data.

So your training data have to expand the dimension, like this:

X_train = X_train[None,:]
y_train = y_train[None,:]

Or use tensorflow function to do this :

X_train = tf.expand_dims(X_train, axis=0)
y_train = tf.expand_dims(y_train, axis=0)

The output shape of the model will be (1,223461)

If the output is not what you expected, it means your model design is wrong.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Denis Savenko