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

Keras · Class

losses.SparseCategoricalCrossentropy

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.

How it works

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.

Syntax

keras.losses.SparseCategoricalCrossentropy(from_logits=False, ignore_class=None, reduction="sum_over_batch_size", axis=-1, name="sparse_categorical_crossentropy", dtype=None)

Parameters

NameTypeDescription
from_logitsboolWhether predictions are logits rather than probabilities; defaults to false.
ignore_classint or NoneOptional integer class ID to omit from loss computation.
reductionstrReduction applied to per-example losses; defaults to sum_over_batch_size.
axisintAxis along which categorical entropy is computed; defaults to -1.
namestr or NoneOptional loss name; defaults to sparse_categorical_crossentropy.
dtypestr or NoneOptional data type for the loss computation.

Returns

A callable loss object that computes crossentropy values from y_true and y_pred; reduction determines the aggregate result.

Key points

  • Use it for classification with at least two classes.
  • Targets are integer class IDs, not one-hot vectors.
  • Predictions provide one floating-point score for every class.
  • Instantiate the class to configure options such as logits handling, ignored classes, reduction, axis, name, and dtype.
  • Keras also exposes a function form, although the class is the configurable object documented here.

Common mistakes

  • Do not pass one-hot labels; use CategoricalCrossentropy for that label format.
  • Set from_logits=True when the model returns unnormalized logits instead of probabilities.

Example: Sparse categorical loss with logits

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.

When to use it

Integer-label classification

Multiclass classification with labels stored as scalar class IDs.

Ignored segmentation class

Semantic segmentation datasets reserve an ID for unlabeled or void pixels.

Logit-based training

A classifier emits its final dense scores without softmax normalization.

Interview questions

QWhen should SparseCategoricalCrossentropy be used?

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.

QWhat does from_logits control?

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.

QHow can a class be excluded from the loss?

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.CategoricalCrossentropy
  • keras.losses.sparse_categorical_crossentropy
  • keras.losses.Loss

Source: Probabilistic losses. Checked 2026-10-15.