close

[Solved] ValueError: Shapes (None, 1) and (None, 3) are incompatible

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ValueError: Shapes (None, 1) and (None, 3) are incompatible in Python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ValueError: Shapes (None, 1) and (None, 3) are incompatible Error Occurs?

Today I get the following error ValueError: Shapes (None, 1) and (None, 3) are incompatible in Python.

How To Solve ValueError: Shapes (None, 1) and (None, 3) are incompatible Error ?

  1. How To Solve ValueError: Shapes (None, 1) and (None, 3) are incompatible Error ?

    To Solve ValueError: Shapes (None, 1) and (None, 3) are incompatible Error Check if the last Dense Layer(output) has same number of classes as the number of target classes in the training dataset. I made similar mistake while training the dataset and correcting it helped me.

Solution 1

The first problem is with the LSTM input_shape. input_shape = (20,85,1).

From the doc: https://keras.io/layers/recurrent/

LSTM layer expects 3D tensor with shape (batch_size, timesteps, input_dim).

model.add(tf.keras.layers.Dense(nb_classes, activation='softmax')) – this suggets you’re doing a multi-class classification.

So, you need your y_train and y_test have to be one-hot-encoded. That means they must have dimension (number_of_samples, 3), where 3 denotes number of classes.

You need to apply tensorflow.keras.utils.to_categorical to them.

y_train = to_categorical(y_train, 3)
y_test = to_categorical(y_test, 3)

ref: https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical

tf.keras.callbacks.History() – this callback is automatically applied to every Keras model. The History object gets returned by the fit method of models.

ref: https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/History

Solution 2

Check if the last Dense Layer(output) has same number of classes as the number of target classes in the training dataset. I made similar mistake while training the dataset and correcting it helped me.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment