ValueError
Shape Mismatch
Debugging
Machine Learning
Tensor Shape

ValueError Shape mismatch The shape of labels received 15, should equal the shape of logits except for the last dimension received 5, 3

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This error means your model outputs and your target labels do not describe the same batch in the same shape. If logits are shaped (5, 3), the model is saying "I have predictions for 5 samples across 3 classes." Labels shaped (15,) do not match that expectation, so the loss function refuses to continue.

How to Read the Shapes

The logits shape (5, 3) usually means:

  • batch size is 5
  • number of classes is 3

So the loss function expects one of these label layouts:

  • integer class ids shaped (5,) when using sparse categorical loss
  • one-hot labels shaped (5, 3) when using categorical loss

Labels shaped (15,) often mean that one-hot labels were flattened accidentally. 5 * 3 equals 15, which is a strong clue that the class dimension got merged into the batch dimension somewhere in preprocessing.

The Most Common Fix

If you intended sparse labels, make sure each sample has one integer class id:

python
1import tensorflow as tf
2
3logits = tf.random.normal((5, 3))
4labels = tf.constant([0, 2, 1, 0, 2], dtype=tf.int32)
5
6loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
7loss = loss_fn(labels, logits)
8print(loss.numpy())

Here the labels are shaped (5,), which matches the batch dimension of the logits.

If you intended one-hot labels, make sure the labels keep their class dimension:

python
1import tensorflow as tf
2
3logits = tf.random.normal((5, 3))
4labels = tf.constant([
5    [1, 0, 0],
6    [0, 0, 1],
7    [0, 1, 0],
8    [1, 0, 0],
9    [0, 0, 1],
10], dtype=tf.float32)
11
12loss_fn = tf.keras.losses.CategoricalCrossentropy(from_logits=True)
13loss = loss_fn(labels, logits)
14print(loss.numpy())

In that case the label shape is (5, 3), exactly aligned with the logits.

Where the Shape Usually Breaks

This mismatch often appears in one of three places.

Flattening Labels by Mistake

Code like this is dangerous:

python
labels = labels.reshape(-1)

If labels started as (5, 3), flattening turns it into (15,) and destroys the class structure.

Using the Wrong Loss Function

If labels are integer class ids but you compile with categorical cross-entropy, TensorFlow expects one-hot targets. If labels are one-hot but you use sparse categorical cross-entropy, the shapes also stop making sense.

python
1model.compile(
2    optimizer="adam",
3    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
4)

Choose the loss that matches the label format you actually have.

Mismatched Dataset Batching

Sometimes features are batched correctly but labels are still coming from a different preprocessing path. Print the shapes right before training:

python
for x_batch, y_batch in dataset.take(1):
    print("x:", x_batch.shape)
    print("y:", y_batch.shape)

That is often faster than guessing where the mismatch came from.

A Useful Mental Check

Ask one simple question: "How many samples are in this batch?" If the logits say 5, the labels must also describe exactly 5 samples. The representation can be sparse or one-hot, but the batch dimension still has to agree.

This mental check catches most label-shape bugs before they become loss-function errors.

Common Pitfalls

The biggest mistake is flattening a one-hot label matrix into one long vector. That converts valid class information into the wrong batch shape.

Another issue is mixing sparse labels with categorical loss or one-hot labels with sparse loss. The label format and the loss function must agree.

Developers also inspect only the model output shape and forget to inspect the dataset output shape right before the training step.

Summary

  • Logits shaped (5, 3) mean 5 samples and 3 classes.
  • Labels should usually be (5,) for sparse targets or (5, 3) for one-hot targets.
  • A label shape of (15,) often means a one-hot matrix was flattened accidentally.
  • Match the loss function to the label format you are using.
  • Print batch shapes before training to find the mismatch quickly.

Course illustration
Course illustration

All Rights Reserved.