Standard multiclass classification
A model ends with softmax probabilities and training targets are one-hot encoded.
Keras · Class
CategoricalCrossentropy computes classification loss for one-hot labels across two or more classes. Configure logits handling, label smoothing, class axis, and reduction through its constructor.
CategoricalCrossentropy compares one-hot class targets with predictions over a class axis and returns a reduced loss by default. Set from_logits=True when predictions are raw logits, and use label_smoothing to soften target distributions before computing the loss.
keras.losses.CategoricalCrossentropy(from_logits=False, label_smoothing=0.0, axis=-1, reduction="sum_over_batch_size", name="categorical_crossentropy")| Name | Type | Description |
|---|---|---|
from_logits | bool | Whether y_pred contains logits or probabilities; defaults to False. |
label_smoothing | float | A smoothing amount applied to the target distribution; defaults to 0.0. |
axis | int | The dimension over which class entropy is computed; defaults to -1. |
reduction | string or None | Reduction mode for aggregating per-example losses. |
name | string or None | Optional loss name. |
A categorical crossentropy loss value.
import keras
import numpy as np
loss_fn = keras.losses.CategoricalCrossentropy()
y_true = np.array([[1., 0., 0.], [0., 0., 1.]])
y_pred = np.array([[0.9, 0.1, 0.0], [0.1, 0.2, 0.7]])
print(loss_fn(y_true, y_pred).numpy())Creates the loss for one-hot targets and evaluates it on class-probability predictions.
A model ends with softmax probabilities and training targets are one-hot encoded.
A model emits raw scores instead of applying softmax in its final layer.
Training benefits from less certainty in the target distribution.
It is a loss for classification with two or more classes where targets are represented as one-hot vectors. It compares those targets with the model predictions and reduces the result by default when used standalone.
Follow-up: How would you handle integer labels? Use SparseCategoricalCrossentropy rather than converting the labels to one-hot vectors.
Set from_logits=True when the model output contains unnormalized logits. Leave it false when the output already represents probabilities, such as a softmax output.
Follow-up: Why does this distinction matter? The loss must interpret the numerical representation correctly before evaluating crossentropy.
label_smoothing modifies the target distribution so the loss does not train against completely hard one-hot targets. axis identifies the dimension containing the class values and defaults to the last dimension.
Follow-up: What should be checked for nonstandard tensor layouts? Pass an axis matching the actual class dimension.
keras.losses.SparseCategoricalCrossentropykeras.losses.CategoricalFocalCrossentropykeras.losses.categorical_crossentropykeras.losses.Loss