keras-team/keras

Feature request: Add SparseCategoricalFocalCrossentropy

Aperta

#23.442 aperta il 15 ago 2026

 (3 commenti) (0 reazioni) (2 assegnatari)Python (19.760 fork)batch import
Good first issuepr-approved

Metriche repository

Star
 (64.249 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

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.

Guida contributor