Integer-label classification
Multiclass classification with labels stored as scalar class IDs.
Keras · Class
SparseCategoricalCrossentropy computes classification crossentropy when targets are integer class IDs. Configure it for probability outputs or logits, and optionally ignore a class or choose the reduction.
This loss compares integer class labels with predictions for classification problems containing at least two classes. It interprets predictions as probabilities by default, or applies the appropriate logits handling when from_logits=True; reduction controls how per-example values become the returned loss.
keras.losses.SparseCategoricalCrossentropy(from_logits=False, ignore_class=None, reduction="sum_over_batch_size", axis=-1, name="sparse_categorical_crossentropy", dtype=None)| Name | Type | Description |
|---|---|---|
from_logits | bool | Whether predictions are logits rather than probabilities; defaults to false. |
ignore_class | int or None | Optional integer class ID to omit from loss computation. |
reduction | str | Reduction applied to per-example losses; defaults to sum_over_batch_size. |
axis | int | Axis along which categorical entropy is computed; defaults to -1. |
name | str or None | Optional loss name; defaults to sparse_categorical_crossentropy. |
dtype | str or None | Optional data type for the loss computation. |
A callable loss object that computes crossentropy values from y_true and y_pred; reduction determines the aggregate result.
import keras
loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model = keras.Sequential([
keras.layers.Input(shape=(4,)),
keras.layers.Dense(3),
])
model.compile(optimizer="adam", loss=loss)
model.fit(
[[0.0, 0.0, 0.0, 0.0], [1.0, 1.0, 1.0, 1.0]],
[0, 2],
epochs=1,
verbose=0,
)This compiles a three-class model with integer labels and tells the loss that the model outputs logits.
Multiclass classification with labels stored as scalar class IDs.
Semantic segmentation datasets reserve an ID for unlabeled or void pixels.
A classifier emits its final dense scores without softmax normalization.
It is appropriate when labels are integer class IDs rather than one-hot vectors, and it supports problems with two or more classes. Predictions contain one floating-point value per class for each feature.
Follow-up: What should replace it for one-hot targets? Use CategoricalCrossentropy, because this class expects integer labels.
With from_logits=False, predictions are treated as a probability distribution. Set from_logits=True when the final model output is logits, so the loss handles them as logits rather than already-normalized probabilities.
Follow-up: What configuration is needed for a softmax output? Leave from_logits at its default false setting.
ignore_class accepts an integer class ID whose entries are excluded from loss computation. This is useful for segmentation data containing a void or unlabeled class.
Follow-up: What happens when ignore_class is omitted? With None, no class is excluded.
keras.losses.CategoricalCrossentropykeras.losses.sparse_categorical_crossentropykeras.losses.Loss