keras-team/keras · error · ValueError

Unknown loss function

Error message

Unknown loss function: '{loss_item}'.

What it means

Raised by _convert_loss_to_function in keras/src/distillation/distillation_loss.py: the loss argument was given as a string identifier and passed to keras.losses.get, which returned None — meaning the string is not a registered loss name (typo like 'categorical_cross_entropy', or an unregistered custom loss). No loss function could be resolved for distillation.

Solutions

  1. Use a valid loss identifier, e.g. "categorical_crossentropy" or "kl_divergence"
  2. Register custom losses with @keras.saving.register_keras_serializable() before referencing by name
  3. Pass a loss instance (e.g. keras.losses.MeanSquaredError()) instead of a string
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/distillation/distillation_loss.py:26 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/57da3cc280ccca96. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/distillation/distillation_loss.py:26

def _convert_loss_to_function(loss_item):
    """Convert a loss string identifier to a loss function.

    Arguments:
        loss_item: Either a string identifier, a loss function instance,
            or `None`.

    Returns:
        A loss function instance, or `None`.

    Raises:
        ValueError: If the loss string identifier is unknown.
    """
    if loss_item is None:
        return None
    elif isinstance(loss_item, str):
        loss_fn = keras.losses.get(loss_item)
        if loss_fn is None:
            raise ValueError(f"Unknown loss function: '{loss_item}'.")
        return loss_fn
    else:
        return loss_item


@keras_export("keras.distillation.DistillationLoss")
class DistillationLoss:
    """Base class for distillation loss computation.

    Distillation losses define how to compute the distillation loss
    between teacher and student outputs. Each loss implements a specific
    approach to knowledge transfer, from simple logits matching to feature-based
    distillation.

    To create custom distillation losses, subclass this class and
    override the `compute_loss` method.
    """

View on GitHub (pinned to 7a34a03db6)