keras-team/keras · error · ValueError

Argument `num_thresholds` must be an integer > 1. Received:

Error message

Argument `num_thresholds` must be an integer > 1. Received: num_thresholds={num_thresholds}

What it means

Raised by keras.metrics.AUC's __init__ when num_thresholds <= 1 and no explicit thresholds list was given. AUC needs at least two thresholds to interpolate the curve.

Source

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

                "Invalid `summation_method` "
                f'argument value "{summation_method}". '
                f"Expected one of: {list(metrics_utils.AUCSummationMethod)}"
            )

        # Update properties.
        self._init_from_thresholds = thresholds is not None
        if thresholds is not None:
            # If specified, use the supplied thresholds.
            self.num_thresholds = len(thresholds) + 2
            thresholds = sorted(thresholds)
            self._thresholds_distributed_evenly = (
                metrics_utils.is_evenly_distributed_thresholds(
                    np.array([0.0] + thresholds + [1.0])
                )
            )
        else:
            if num_thresholds <= 1:
                raise ValueError(
                    "Argument `num_thresholds` must be an integer > 1. "
                    f"Received: num_thresholds={num_thresholds}"
                )

            # Otherwise, linearly interpolate (num_thresholds - 2) thresholds in
            # (0, 1).
            self.num_thresholds = num_thresholds
            thresholds = [
                (i + 1) * 1.0 / (num_thresholds - 1)
                for i in range(num_thresholds - 2)
            ]
            self._thresholds_distributed_evenly = True

        # Add an endpoint "threshold" below zero and above one for either
        # threshold method to account for floating point imprecisions.
        self._thresholds = np.array(
            [0.0 - backend.epsilon()] + thresholds + [1.0 + backend.epsilon()]
        )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Set num_thresholds >= 2 (default 200).
  2. For a single decision threshold use keras.metrics.Precision/Recall with threshold=... instead of AUC.
  3. Pass thresholds=[...] explicitly when you need specific cutoffs.

Example fix

# before
auc = keras.metrics.AUC(num_thresholds=1)

# after
auc = keras.metrics.AUC(num_thresholds=200)
# or explicit cutoffs:
auc = keras.metrics.AUC(thresholds=[0.1, 0.3, 0.5, 0.7, 0.9])
Defensive patterns

Strategy: validation

Validate before calling

if thresholds is None and (not isinstance(num_thresholds, int) or num_thresholds <= 1):
    raise ValueError('num_thresholds must be an integer > 1')

Type guard

def is_valid_auc_thresholds(nt) -> bool:
    return isinstance(nt, int) and nt > 1

Prevention

When it happens

Trigger: keras.metrics.AUC(num_thresholds=1) or 0; intending a single operating point; config values derived from len(list) that evaluate to 1.

Common situations: Sweeps starting at 1; misunderstanding that internal endpoints 0.0/1.0 are added so num_thresholds must exceed 1; legacy TF 1.x code.

Related errors


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