keras-team/keras

Feature request: Add SparseCategoricalFocalCrossentropy

Open

#23,442 opened on Aug 15, 2026

 (3 comments) (0 reactions) (2 assignees)Python (19,760 forks)batch import
Good first issuepr-approved

Repository metrics

Stars
 (64,249 stars)
PR merge metrics
 (PR metrics pending)

Description

Summary

Add sparse-label support for categorical focal crossentropy through:

  • keras.losses.sparse_categorical_focal_crossentropy
  • keras.losses.SparseCategoricalFocalCrossentropy

Keras currently provides CategoricalFocalCrossentropy, which expects one-hot labels, and SparseCategoricalCrossentropy, which accepts integer labels. There is no corresponding focal loss for sparse categorical labels.

Motivation

Sparse labels are common in semantic segmentation, token classification, and multiclass problems with many classes. Converting these labels to one-hot tensors solely to use focal loss increases memory usage and adds unnecessary preprocessing.

A native sparse implementation would provide focal loss while preserving integer labels and Keras' backend-agnostic behavior.

Proposed API

keras.losses.SparseCategoricalFocalCrossentropy(
    alpha=0.25,
    gamma=2.0,
    from_logits=False,
    ignore_class=None,
    axis=-1,
    reduction="sum_over_batch_size",
    name="sparse_categorical_focal_crossentropy",
    dtype=None,
)

I would like to work on this if the contribution makes sense.

Contributor guide