Skip to content
Latchkey

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.

python
ValueError: Shapes (None, 10) and (None, 1) are incompatible

Common 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

  1. Print model.output_shape and the label array shape.
  2. Match the final layer units to the number of classes and pick the loss that fits the label encoding.
  3. Re-run the fit once the shapes agree.
python
# 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.

python
import keras
y = keras.utils.to_categorical(y, num_classes=10)

How to prevent it

  • Assert model.output_shape against 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.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →