keras-team/keras · error · ValueError

Invalid value for argument `reduction`. Expected one of {all

Error message

Invalid value for argument `reduction`. Expected one of {allowed}. Received: reduction={reduction}

What it means

Error "Invalid value for argument `reduction`. Expected one of {allowed}. Received: reduction={reduction}" thrown in keras-team/keras.

Source

Thrown at keras/src/losses/loss.py:111

    @classmethod
    def from_config(cls, config):
        return cls(**config)

    def _obj_type(self):
        return "Loss"


def standardize_reduction(reduction):
    allowed = {
        "sum_over_batch_size",
        "sum",
        None,
        "none",
        "mean",
        "mean_with_sample_weight",
    }
    if reduction not in allowed:
        raise ValueError(
            "Invalid value for argument `reduction`. "
            f"Expected one of {allowed}. Received: "
            f"reduction={reduction}"
        )
    return reduction


def squeeze_or_expand_to_same_rank(x1, x2, expand_rank_1=True):
    """Squeeze/expand last dim if ranks differ from expected by exactly 1."""
    x1_rank = len(x1.shape)
    x2_rank = len(x2.shape)
    if x1_rank == x2_rank:
        return x1, x2
    if x1_rank == x2_rank + 1:
        if x1.shape[-1] == 1:
            if x2_rank == 1 and expand_rank_1:
                x2 = ops.expand_dims(x2, axis=-1)
            else:

View on GitHub (pinned to 7a34a03db6)

When it happens

Trigger: Thrown at keras/src/losses/loss.py:111 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/4758db0918844611. Report an issue: GitHub.