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

Keras · Class

losses.CategoricalCrossentropy

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.

How it works

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.

Syntax

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 contains logits or probabilities; defaults to False.
label_smoothingfloatA smoothing amount applied to the target distribution; defaults to 0.0.
axisintThe dimension over which class entropy is computed; defaults to -1.
reductionstring or NoneReduction mode for aggregating per-example losses.
namestring or NoneOptional loss name.

Returns

A categorical crossentropy loss value.

Key points

  • Use it for classification problems with at least two classes.
  • Each feature should provide one floating-point value for every class.
  • Keras exposes this loss both as a configurable class and as a function-style API.
  • Calling an instance directly produces a reduced loss unless reduction behavior is configured otherwise.
  • A typical logits-based setup constructs the loss with from_logits=True.

Common mistakes

  • Targets must use one-hot encoding; integer class IDs require SparseCategoricalCrossentropy instead.
  • Instantiate the class when you need constructor options such as logits handling or label smoothing.

Example: Evaluate categorical crossentropy

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.

When to use it

Standard multiclass classification

A model ends with softmax probabilities and training targets are one-hot encoded.

Logit-based classification

A model emits raw scores instead of applying softmax in its final layer.

Smoothed targets

Training benefits from less certainty in the target distribution.

Interview questions

QWhat problem does CategoricalCrossentropy solve?

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.

QWhen should from_logits be enabled?

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.

QWhat do label_smoothing and axis control?

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.SparseCategoricalCrossentropy
  • keras.losses.CategoricalFocalCrossentropy
  • keras.losses.categorical_crossentropy
  • keras.losses.Loss

Source: Losses - Keras. Checked 2026-10-15.