keras-team/keras · error · ValueError

Invalid `beta` argument value. It should be > 0. Received: b

Error message

Invalid `beta` argument value. It should be > 0. Received: beta={beta}

What it means

Raised by FBetaScore's __init__ when beta is a float but <= 0.0. beta weights recall against precision via beta^2, so it must be strictly positive.

Source

Thrown at keras/src/metrics/f_score_metrics.py:92

        super().__init__(name=name, dtype=dtype)
        # Metric should be maximized during optimization.
        self._direction = "up"

        if average not in (None, "micro", "macro", "weighted"):
            raise ValueError(
                "Invalid `average` argument value. Expected one of: "
                "{None, 'micro', 'macro', 'weighted'}. "
                f"Received: average={average}"
            )

        if not isinstance(beta, float):
            raise ValueError(
                "Invalid `beta` argument value. "
                "It should be a Python float. "
                f"Received: beta={beta} of type '{type(beta)}'"
            )
        if beta <= 0.0:
            raise ValueError(
                "Invalid `beta` argument value. "
                "It should be > 0. "
                f"Received: beta={beta}"
            )

        if threshold is not None:
            if not isinstance(threshold, float):
                raise ValueError(
                    "Invalid `threshold` argument value. "
                    "It should be a Python float. "
                    f"Received: threshold={threshold} "
                    f"of type '{type(threshold)}'"
                )
            if threshold > 1.0 or threshold <= 0.0:
                raise ValueError(
                    "Invalid `threshold` argument value. "
                    "It should verify 0 < threshold <= 1. "
                    f"Received: threshold={threshold}"

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Use a positive float: beta=0.5 favors precision, beta=2.0 favors recall.
  2. For precision-only behavior use keras.metrics.Precision.
  3. Validate sweep ranges to beta > 0.

Example fix

# before
m = keras.metrics.FBetaScore(beta=0.0)

# after
m = keras.metrics.FBetaScore(beta=0.5)  # or use keras.metrics.Precision()
Defensive patterns

Strategy: validation

Validate before calling

if not (beta > 0.0):
    raise ValueError(f'beta must be > 0, got {beta}')

Type guard

def is_positive_float(v) -> bool:
    return isinstance(v, float) and v > 0.0

Prevention

When it happens

Trigger: keras.metrics.FBetaScore(beta=0.0); beta=-1.0; beta computed as 0.0 by a misconfigured expression.

Common situations: Sweeps including 0 as boundary; sign errors; misunderstanding that beta=0 is not a valid precision-only mode.

Related errors


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