keras-team/keras

Feature request: Add SparseCategoricalFocalCrossentropy

オープン

#23,442 opened on 2026/08/15

 (3 件のコメント) (0 件のリアクション) (2 人の担当者)Python (19,760 件のフォーク)batch import
Good first issuepr-approved

Repository metrics

Stars
 (64,249 個のスター)
PR merge metrics
 (PR metrics pending)

説明

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.

コントリビューターガイド