What are the actual class labels while using SparseCategoricalCrossEntropy loss for multiclass classification in keras?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you use SparseCategoricalCrossentropy in Keras, the true class labels are integer class indices, not one-hot vectors. Those integers must line up directly with the positions of the model's output units, so for N classes the valid labels are normally 0 through N - 1.
What the Labels Actually Mean
Suppose your model ends with:
That output has three positions:
- index
0 - index
1 - index
2
With SparseCategoricalCrossentropy, the ground-truth label for each example must be one of those integer indices.
So the labels are not:
- '
"cat","dog","bird"' - one-hot vectors such as
[0, 1, 0]
They are:
- '
0' - '
1' - '
2'
Concrete Example
Imagine these semantic classes:
A valid label encoding is:
If the model predicts:
then the true sparse label should be 1 if the correct class is dog, because dog is at output index 1.
Minimal Keras Example
The label array y contains integer class IDs, not one-hot rows.
Difference from CategoricalCrossentropy
This is the essential distinction:
- '
CategoricalCrossentropyexpects one-hot encoded targets' - '
SparseCategoricalCrossentropyexpects integer class indices'
So for three classes:
Both represent the same semantic class, but they are fed to the model differently.
Class Names Are Your Responsibility
Keras does not know that 0 means cat unless you define that mapping yourself. The loss function only sees integer indices and prediction positions.
A common pattern is:
This external mapping is what turns raw model outputs into human-readable labels.
Output Layer and from_logits
If your final layer uses softmax, the model outputs probabilities and from_logits=False is the normal default.
If your final layer omits softmax and returns raw scores, configure the loss accordingly:
This changes how the loss interprets the predictions, but it does not change what the labels are. The labels are still integer class indices.
Common Pitfalls
The biggest mistake is using one-hot encoded targets with SparseCategoricalCrossentropy. That is the wrong target format for this loss and will often produce shape or semantic mismatches.
Another issue is using labels that do not start at 0 or do not match the output width. If your output layer has 3 units, labels such as 1, 2, and 3 are wrong unless you first remap them to 0, 1, and 2.
Finally, do not confuse class IDs with class names. The loss function works with indices only. Human-readable names belong in a separate mapping layer in your code or data pipeline.
Summary
- '
SparseCategoricalCrossentropyexpects integer class labels, not one-hot vectors.' - Those labels must match the output indices of the model, typically
0tonum_classes - 1. - Class names such as
"cat"or"dog"must be mapped to integer IDs before training. - '
from_logitschanges prediction interpretation, not label format.' - Use
CategoricalCrossentropyinstead if your targets are one-hot encoded.

