Multiclass model training
A neural network predicts one probability or logit per class and training labels are one-hot encoded.
TensorFlow · Class
Computes categorical crossentropy between one-hot labels and predictions for multiclass classification. Predictions may be probabilities or logits, with optional label smoothing, feature-axis selection, and configurable reduction.
CategoricalCrossentropy compares one-hot class labels with predictions and returns categorical crossentropy. It can interpret predictions as probabilities or logits, optionally smooth target labels, compute along a chosen feature axis, and reduce per-example losses according to the selected reduction mode.
tf.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 is expected to be a logits tensor; defaults to treating it as a probability distribution. |
label_smoothing | float | A value from 0 through 1 that relaxes confidence in the target labels. |
axis | int | The feature axis along which to calculate crossentropy; defaults to -1. |
reduction | str or None | Loss aggregation mode: "sum", "sum_over_batch_size", or None. |
name | str or None | Optional name for the loss instance. |
Calling the loss with y_true and y_pred returns the categorical crossentropy loss, reduced according to reduction; with reduction=None it returns per-example losses.
import tensorflow as tf
loss = tf.keras.losses.CategoricalCrossentropy(
from_logits=True,
label_smoothing=0.1,
)
y_true = [[1., 0., 0.], [0., 0., 1.]]
logits = [[3., 1., -2.], [0., 2., 4.]]
print(loss(y_true, logits).numpy())Creates a loss for one-hot labels and raw class logits, then evaluates the batch loss.
A neural network predicts one probability or logit per class and training labels are one-hot encoded.
A model outputs raw, unnormalized class scores.
Training benefits from less certainty in hard target labels.
An optimizer or analysis requires one loss value for each example.
It is appropriate for classification with at least two classes when each target is one-hot encoded. The target and prediction tensors contain one floating-point value per class for each feature.
Follow-up: What should be used for integer labels? Use SparseCategoricalCrossentropy, because this class expects one-hot labels.
from_logits controls how y_pred is interpreted. False means y_pred represents a probability distribution; True means it contains logits, so the loss handles them as logits.
Follow-up: Why avoid applying softmax twice? If the model already emits probabilities, use from_logits=False; if it emits logits, let the loss process them directly with from_logits=True.
label_smoothing relaxes confidence in the target labels. With a value such as 0.1, target and non-target class values are adjusted using the number of classes.
Follow-up: Which dimension does the loss treat as classes by default? The final dimension, because axis defaults to -1.
tf.keras.losses.SparseCategoricalCrossentropytf.keras.losses.categorical_crossentropytf.keras.losses.get