ValueError: Shapes (None, 1) and (None, 26) are incompatible

this is my code, now I am getting this error:

I tried setting loss as ‘sparse_categorical_crossentropy’ but even that is giving an error.
Please help

Why do you have 26 outputs in the final output layer?

I put 26 as there are 26 possible outputs. I also tried with 3 and 1 but still the same error

when I tried 1:

when I tried 3:

You are right it hasn’t got to do with the that, it should be 26 but I am not that familiar with these assignments here. However, I can see where the problem is and I think if you search in the forum this problem has come up a few times by now.

The problem is in the choice of your loss, think a bit about it!

1 Like

I mentioned this earlier that I tried setting the loss as ‘sparse_categorical_crossentropy’ but even that is giving an error.

Number of units in the final dense layer in a multi-class classification problem should equal to the number of classes. This number is not 3 for this assignment.

Loss function should be categorical_crossentropy for a multiclass classification problem only if output classes are one-hot encoded. If not, use the loss function where the classes are represented as integers.


I am also having the same error in my assignment. The data shapes printed out as expected.

Images of training generator have shape: (27455, 28, 28, 1)
Labels of training generator have shape: (27455,)
Images of validation generator have shape: (7172, 28, 28, 1)
Labels of validation generator have shape: (7172,)

I looked a general solution to the problem on stack overflow at python - ValueError: Shapes (None, 1) and (None, 3) are incompatible - Stack Overflow.
Which recommended to alter the training and validation labels in their respective ImageDataGenerator objects. The solution worked for me.

1 Like

I had a similar error, and using the [snippet removed by mentor] helped.

and we have 25 unique labels, not 26

You should be okay since there is no training data for the missing class.

1 Like

This was very helpful, thanks.

It’d be useful for this to be addressed and explained upfront in the assignment. I believe that is a shortcoming for an otherwise very clear material.

For future students with this problem note that for a multiclass classification, you need to apply tf.keras.utils.to_categorical to the training and validation labels to convert them into a ‘one-hot-encoded’ vector before feeding them into the ImageDataGenerator. In this way the Softmax output (which has a shape of ‘one-hot-encoded’ vector) can be compared to those labels.

For more details use the stack overflow link provided by @Kendrick_Mitchell


its only 24. J and Z is missing see the images which is there in data folder.

I encountered this error and fixed doing the following.
The first output layer must have at least 24 units (26 is ideal) since there is 24 different character in the data.
The second one is tricky, you should make the label’s shape as 7172, 24 or 7172, 26. That means instead of ‘2’, it should be [0,0,1,0,0,0,…0,0,0,0] array.

After that training will work without any problem and accuracy will be almost 99% on training and 95% on validation set.

Good luck :slight_smile: