Multi class sparse_categorical_crossentropy TruePositives metric Incompatible shapes 2,128 vs. 2,64
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This Keras shape error usually happens because sparse_categorical_crossentropy and TruePositives expect different label formats. In a sparse multiclass setup, y_true is typically a vector of integer class IDs, while TruePositives is designed for binary or same-shape label predictions, so it cannot compare a class-index vector against a full probability matrix directly.
Why the Shapes Do Not Match
With sparse multiclass classification, your model output often has shape:
batch_size, num_classes
For example, 2,64 means two samples and sixty-four class probabilities per sample.
But sparse labels usually have shape:
batch_size
or sometimes batch_size, 1
So if you compile a model like this:
Keras tries to feed TruePositives tensors that do not represent the same kind of object. One side is sparse integer labels, the other is dense per-class predictions.
That is why the metric raises an incompatible-shape error.
What TruePositives Is For
TruePositives is meant for binary-style comparisons where predictions and labels align elementwise after thresholding. It is a natural fit for:
- binary classification
- multilabel classification with same-shape targets and predictions
It is not the right built-in metric for ordinary sparse multiclass classification.
Use the Right Multiclass Metric Instead
For sparse multiclass problems, the most common built-in metric is SparseCategoricalAccuracy.
This metric understands that y_true is sparse class indices and y_pred is a probability distribution across classes.
That is why it works cleanly with sparse_categorical_crossentropy.
If You Need True Positives Per Class
If your real goal is class-wise precision, recall, or true-positive counts, convert the predictions into class IDs first and then compare class labels in a custom metric or after evaluation.
A simple custom sparse accuracy-style metric might look like this:
For per-class true positives, it is often cleaner to compute a confusion matrix after prediction rather than trying to force the generic TruePositives metric into a sparse multiclass training loop.
Example of a Correct Sparse Multiclass Setup
Here the output layer has 64 units, the labels are integer class IDs, and the metric matches that representation.
When One-Hot Metrics Are Appropriate
If you intentionally convert labels to one-hot vectors, then the setup changes. At that point you would usually use:
- '
categorical_crossentropyinstead ofsparse_categorical_crossentropy' - metrics that expect one-hot or same-shape tensors
The key is consistency. Loss, labels, model output, and metrics all have to agree on the representation.
Common Pitfalls
Mixing sparse labels with metrics designed for elementwise binary comparisons is the main cause of this error.
Using a softmax output with one-hot-oriented metrics while keeping integer labels is another representation mismatch.
Trying to measure multiclass true positives inside training with the wrong built-in metric can also obscure the actual evaluation goal. Often a confusion matrix after prediction is the better tool.
Finally, always inspect the shapes of y_true and y_pred before debugging deeper. Most of these errors become obvious once the tensor shapes are printed.
Summary
- '
sparse_categorical_crossentropyexpects sparse integer labels, whileTruePositivesexpects same-shape binary-style comparisons' - the shape error occurs because those expectations do not align in standard multiclass classification
- use
SparseCategoricalAccuracyor another sparse-compatible metric during training - if you need per-class true-positive counts, compute them from predicted class IDs or a confusion matrix
- keep loss, labels, output layer, and metrics consistent with the same label representation

