keras-team/keras · error · ValueError

Argument `recall` must be in the range [0, 1]. Received: rec

Error message

Argument `recall` must be in the range [0, 1]. Received: recall={recall}

What it means

Raised by keras.metrics.PrecisionAtRecall's __init__ when the recall target is outside [0, 1]. The metric finds the threshold at which recall reaches the given value, so recall must be a valid probability.

Source

Thrown at keras/src/metrics/confusion_metrics.py:937

    ...                sample_weight=[2, 2, 2, 1, 1])
    >>> m.result()
    0.33333333

    Usage with `compile()` API:

    ```python
    model.compile(
        optimizer='sgd',
        loss='binary_crossentropy',
        metrics=[keras.metrics.PrecisionAtRecall(recall=0.8)])
    ```
    """

    def __init__(
        self, recall, num_thresholds=200, class_id=None, name=None, dtype=None
    ):
        if recall < 0 or recall > 1:
            raise ValueError(
                "Argument `recall` must be in the range [0, 1]. "
                f"Received: recall={recall}"
            )
        self.recall = recall
        self.num_thresholds = num_thresholds
        super().__init__(
            value=recall,
            num_thresholds=num_thresholds,
            class_id=class_id,
            name=name,
            dtype=dtype,
        )

    def result(self):
        recalls = ops.divide_no_nan(
            self.true_positives,
            ops.add(self.true_positives, self.false_negatives),
        )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Pass recall as a fraction in [0, 1], e.g. 0.8.
  2. Validate sweep/config ranges before constructing the metric.
  3. Convert percentages explicitly: recall_pct / 100.0.

Example fix

# before
m = keras.metrics.PrecisionAtRecall(recall=80)

# after
m = keras.metrics.PrecisionAtRecall(recall=0.8)
Defensive patterns

Strategy: validation

Validate before calling

if not (0.0 <= recall <= 1.0):
    raise ValueError(f'recall must be in [0,1], got {recall}')

Type guard

def is_probability(v) -> bool:
    return isinstance(v, (int, float)) and 0.0 <= v <= 1.0

Prevention

When it happens

Trigger: keras.metrics.PrecisionAtRecall(recall=1.1); recall as percentage (80 for 80%); uninitialized config defaulting to -1.

Common situations: Config sweeps with -1 sentinels; percent/fraction mix-ups; ported sklearn-style code.

Related errors


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