Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Class

losses.CategoricalCrossentropy

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.

How it works

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.

Syntax

tf.keras.losses.CategoricalCrossentropy(
    from_logits=False,
    label_smoothing=0.0,
    axis=-1,
    reduction='sum_over_batch_size',
    name='categorical_crossentropy'
)

Parameters

NameTypeDescription
from_logitsboolWhether y_pred is expected to be a logits tensor; defaults to treating it as a probability distribution.
label_smoothingfloatA value from 0 through 1 that relaxes confidence in the target labels.
axisintThe feature axis along which to calculate crossentropy; defaults to -1.
reductionstr or NoneLoss aggregation mode: "sum", "sum_over_batch_size", or None.
namestr or NoneOptional name for the loss instance.

Returns

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.

Key points

  • The class measures disagreement between labels and model predictions using categorical crossentropy.
  • It targets multiclass classification with two or more possible classes.
  • Each feature needs one floating-point value for every class in both target and prediction data.
  • Positive label smoothing softens target confidence rather than keeping a hard one-hot target.
  • axis selects the feature dimension over which the class-wise crossentropy is computed.
  • The default reduction averages the summed loss over the batch size.

Common mistakes

  • Pass one-hot encoded labels; integer class IDs require SparseCategoricalCrossentropy instead.
  • Set from_logits=True when predictions are raw logits rather than probabilities.
  • Reduction accepts only sum, sum_over_batch_size, or None; None preserves individual losses.

Example: Categorical crossentropy from logits

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.

When to use it

Multiclass model training

A neural network predicts one probability or logit per class and training labels are one-hot encoded.

Logit-based loss evaluation

A model outputs raw, unnormalized class scores.

Label smoothing

Training benefits from less certainty in hard target labels.

Per-example loss computation

An optimizer or analysis requires one loss value for each example.

Interview questions

QWhen should CategoricalCrossentropy be used?

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.

QWhat does from_logits change?

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.

QHow do label_smoothing and axis affect the calculation?

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.SparseCategoricalCrossentropy
  • tf.keras.losses.categorical_crossentropy
  • tf.keras.losses.get

Source: tf.keras.losses.CategoricalCrossentropy. Checked 2026-10-15.