tf.keras.losses.SparseCategoricalFocalCrossentropy

Computes the sparse categorical focal crossentropy loss.

Inherits From: Loss

Use this crossentropy loss function when there are two or more label classes and labels are provided as integers. If you want to provide labels using a one-hot representation, please use CategoricalFocalCrossentropy loss.

Focal loss applies a modulating factor to down-weight easy examples and focus more on hard examples. For each example, the loss is:

FL(p_t) = -alpha_t * (1 - p_t) ** gamma * log(p_t)

where p_t is the predicted probability of the true class, gamma is the focusing parameter, and alpha_t is the weight for the true class. When gamma=0, there is no focal effect on the crossentropy loss.

alpha A weight balancing factor for all classes. Defaults to 0.25. It can be a list of floats or a scalar. If a list is provided, it must have the same length as the number of classes.
gamma A focusing parameter. Defaults to 2.0. It gradually reduces the importance given to easy examples.
from_logits Whether y_pred is expected to be a logits tensor. By default, y_pred is expected to encode a probability distribution.
ignore_class Optional integer. The ID of a class to be ignored during loss computation. This is useful, for example, in segmentation problems featuring a "void" class (commonly -1 or 255). By default (ignore_class=None), all classes are considered.
axis The axis along which to compute crossentropy (the features axis). Defaults to -1.
reduction Type of reduction to apply to the loss. In almost all cases this should be "sum_over_batch_size". Supported options are "sum", "sum_over_batch_size", "mean", "mean_with_sample_weight" or None. "sum" sums the loss, "sum_over_batch_size" and "mean" sum the loss and divide by the sample size, and "mean_with_sample_weight" sums the loss and divides by the sum of the sample weights. "none" and None perform no aggregation. Defaults to "sum_over_batch_size".
name Optional name for the loss instance.
dtype The dtype of the loss's computations. Defaults to None, which means using keras.backend.floatx(). keras.backend.floatx() is a "float32" unless set to a different value (via keras.backend.set_floatx()). If a keras.DTypePolicy is provided, then the compute_dtype will be utilized.

Examples:

y_true = np.array([1, 2])
y_pred = np.array([[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
loss = keras.losses.SparseCategoricalFocalCrossentropy(
    reduction=None
)
loss(y_true, y_pred)
array([3.2058e-05, 4.6627e-01], dtype=float32)

Usage with the compile() API:

model.compile(
    optimizer="adam",
    loss=keras.losses.SparseCategoricalFocalCrossentropy(),
)

An out-of-range, non-ignored label produces a NaN loss value. An alpha vector with a statically known wrong length raises ValueError; if its length or the class dimension is only known at runtime, a mismatch produces NaN loss values. This prevents backend-dependent index selection in compiled and symbolic execution, where Keras does not have a portable runtime assertion primitive.

dtype

Methods

call

View source

from_config

View source

get_config

View source

__call__

View source

Call self as a function.