keras-team/keras · error · ValueError

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

Error message

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

What it means

Raised by the constructor of sensitivity/specificity-style confusion metrics (keras/src/metrics/confusion_metrics.py:573) when num_thresholds is zero or negative. Keras discretizes the ROC/PR curve into num_thresholds buckets, so it must be a positive integer. The check runs eagerly in __init__, before any data is seen.

Source

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

        return {**base_config, **config}


class SensitivitySpecificityBase(Metric):
    """Abstract base class for computing sensitivity and specificity.

    For additional information about specificity and sensitivity, see
    [the following](https://en.wikipedia.org/wiki/Sensitivity_and_specificity).
    """

    def __init__(
        self, value, num_thresholds=200, class_id=None, name=None, dtype=None
    ):
        super().__init__(name=name, dtype=dtype)
        # Metric should be maximized during optimization.
        self._direction = "up"

        if num_thresholds <= 0:
            raise ValueError(
                "Argument `num_thresholds` must be an integer > 0. "
                f"Received: num_thresholds={num_thresholds}"
            )
        self.value = value
        self.class_id = class_id

        # Compute `num_thresholds` thresholds in [0, 1]
        if num_thresholds == 1:
            self.thresholds = [0.5]
            self._thresholds_distributed_evenly = False
        else:
            thresholds = [
                (i + 1) * 1.0 / (num_thresholds - 1)
                for i in range(num_thresholds - 2)
            ]
            self.thresholds = [0.0] + thresholds + [1.0]
            self._thresholds_distributed_evenly = True

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Set num_thresholds to a positive integer, typically 200 (default) or 500-1000 for finer resolution.
  2. Validate config values before constructing the metric.
  3. Constrain sweeps to num_thresholds >= 2.

Example fix

# before
m = keras.metrics.SpecificityAtSensitivity(0.5, num_thresholds=0)

# after
m = keras.metrics.SpecificityAtSensitivity(0.5, num_thresholds=200)
Defensive patterns

Strategy: validation

Validate before calling

nt = int(num_thresholds)
if nt <= 0:
    raise ValueError(f'num_thresholds must be > 0, got {nt}')

Type guard

def is_valid_num_thresholds(v) -> bool:
    return isinstance(v, int) and not isinstance(v, bool) and v > 0

Prevention

When it happens

Trigger: keras.metrics.SpecificityAtSensitivity(0.5, num_thresholds=0); negative values; values computed from config that evaluate to 0 (e.g. int(cfg['steps'])).

Common situations: Hyperparameter sweeps including 0 or -1 sentinels; YAML/JSON configs where num_thresholds is missing and defaults to 0; code ported from older TF that used num_thresholds=1.

Related errors


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