keras-team/keras · error · ValueError

Invalid `threshold` argument value. It should verify 0 < thr

Error message

Invalid `threshold` argument value. It should verify 0 < threshold <= 1. Received: threshold={threshold}

What it means

Raised by FBetaScore's __init__ when threshold is a float outside (0, 1.0], i.e. <= 0.0 or > 1.0. The threshold converts probabilities to hard decisions, so it must lie in that interval.

Source

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

                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}"
                )

        self.average = average
        self.beta = beta
        self.threshold = threshold
        self.axis = None
        self._built = False

        if self.average != "micro":
            self.axis = 0

    def _build(self, y_true_shape, y_pred_shape):
        if len(y_pred_shape) != 2 or len(y_true_shape) != 2:
            raise ValueError(
                "FBetaScore expects 2D inputs with shape "

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Use a value in (0, 1], e.g. 0.5.
  2. Convert percentages: t = pct / 100.0.
  3. For hard 0/1 labels leave threshold=None and supply integer labels.

Example fix

# before
m = keras.metrics.FBetaScore(beta=1.0, threshold=50)

# after
m = keras.metrics.FBetaScore(beta=1.0, threshold=0.5)
Defensive patterns

Strategy: validation

Validate before calling

if threshold is not None and not (0.0 < threshold <= 1.0):
    raise ValueError(f'threshold must be in (0, 1], got {threshold}')

Type guard

def is_valid_threshold(v) -> bool:
    return v is None or (isinstance(v, float) and 0.0 < v <= 1.0)

Prevention

When it happens

Trigger: keras.metrics.FBetaScore(beta=1.0, threshold=1.5); threshold=0.0; -0.5; percent-style values like 50.

Common situations: Percent/fraction confusion; not knowing 0.0 is invalid but 1.0 is valid; config typos.

Related errors


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