Keras "ValueError: Shapes ... are incompatible" in CI
Keras compared the shape of the model output against the labels (or a layer input) during build or fit and they do not align. The mismatch raises a ValueError before or during the first training step.
What this error means
A training or evaluation test fails with "ValueError: Shapes (32, 10) and (32, 1) are incompatible", naming the two tensor shapes that do not match.
ValueError: Shapes (None, 10) and (None, 1) are incompatibleCommon causes
Output units do not match the label encoding
A dense layer emits 10 logits but the labels are a single column, or labels are one-hot while the loss expects integer classes.
A loss/metric that assumes a different label shape
Using categorical_crossentropy with integer labels (instead of sparse_categorical_crossentropy) produces a shape conflict.
How to fix it
Align the output layer, labels, and loss
- Print
model.output_shapeand the label array shape. - Match the final layer units to the number of classes and pick the loss that fits the label encoding.
- Re-run the fit once the shapes agree.
# integer labels -> sparse loss; one-hot labels -> categorical loss
model.compile(loss="sparse_categorical_crossentropy", metrics=["accuracy"])One-hot encode when the loss expects it
If you keep categorical_crossentropy, convert integer labels to one-hot so shapes match.
import keras
y = keras.utils.to_categorical(y, num_classes=10)How to prevent it
- Assert
model.output_shapeagainst label shape in a unit test. - Choose the loss that matches your label encoding.
- Use small deterministic fixtures so shape contracts are exercised in CI.