keras-team/keras · error · ValueError
Threshold values must be in [0, 1]. Received: {invalid_thres
Error message
Threshold values must be in [0, 1]. Received: {invalid_thresholds} What it means
Keras validates that every threshold used by thresholded metrics (Precision, Recall, AUC with explicit thresholds) lies in [0,1]. parse_init_thresholds() calls assert_thresholds_range(), which collects offending values and raises this ValueError. Thresholds below 0, above 1, or None entries inside the list trigger it.
Source
Thrown at keras/src/metrics/metrics_utils.py:20
from enum import Enum
import numpy as np
from keras.src import backend
from keras.src import ops
from keras.src.losses.loss import squeeze_or_expand_to_same_rank
from keras.src.utils.python_utils import to_list
NEG_INF = -1e10
def assert_thresholds_range(thresholds):
if thresholds is not None:
invalid_thresholds = [
t for t in thresholds if t is None or t < 0 or t > 1
]
if invalid_thresholds:
raise ValueError(
"Threshold values must be in [0, 1]. "
f"Received: {invalid_thresholds}"
)
def parse_init_thresholds(thresholds, default_threshold=0.5):
if thresholds is not None:
assert_thresholds_range(to_list(thresholds))
thresholds = to_list(
default_threshold if thresholds is None else thresholds
)
return thresholds
class ConfusionMatrix(Enum):
TRUE_POSITIVES = "tp"
FALSE_POSITIVES = "fp"
TRUE_NEGATIVES = "tn"View on GitHub (pinned to 7a34a03db6)
Solutions
- Clamp or filter thresholds to [0,1]: [t for t in thresholds if 0 <= t <= 1].
- If you have logit-scale scores, convert to probabilities with a sigmoid before using them as thresholds.
- Remove None entries from the thresholds list; pass thresholds=None to use the default 0.5.
Example fix
# before metric = keras.metrics.Precision(thresholds=[0.5, 1.2]) # after metric = keras.metrics.Precision(thresholds=[0.5, 0.9])
Defensive patterns
Strategy: validation
Validate before calling
def check_thresholds(thresholds):
if thresholds is not None:
bad = [t for t in thresholds if t is None or t < 0 or t > 1]
if bad:
raise ValueError(f'thresholds outside [0,1]: {bad}')
return thresholds Prevention
- Validate config-sourced thresholds against [0,1] before constructing metrics.
- Convert logit-scale values with a sigmoid before using them as thresholds.
When it happens
Trigger: Constructing keras.metrics.Precision(thresholds=[0.2, 1.5]), Recall(thresholds=[-0.1]), or AUC(thresholds=[None, 0.5]) - any value <0, >1, or None inside the thresholds list.
Common situations: Reading thresholds from a config file or hyperparameter sweep where values escape [0,1]; mixing logit-scale values (e.g. 5.0) into probability thresholds.
Related errors
- Could not interpret activation function identifier: {identif
- If using `weights="imagenet"` as true, `classes` should be 1
- The number of repeats in `EfficientNet` must be > 0. Receive
- If using `weights="imagenet"` as true, `classes` should be 1
- The number of repeats in `EfficientNetV2` must be > 0. Recei
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/4531c0a9060d7a38.
Report an issue: GitHub.