keras-team/keras · error · ValueError
Argument `sensitivity` must be in the range [0, 1]. Received
Error message
Argument `sensitivity` must be in the range [0, 1]. Received: sensitivity={sensitivity} What it means
Raised in the constructor of keras.metrics.SensitivityAtSpecificity when the sensitivity argument is below 0 or above 1. Sensitivity (recall / true positive rate) is a probability, so Keras validates the range eagerly in __init__.
Source
Thrown at keras/src/metrics/confusion_metrics.py:847
```python
model.compile(
optimizer='sgd',
loss='binary_crossentropy',
metrics=[keras.metrics.SpecificityAtSensitivity(sensitivity=0.3)])
```
"""
def __init__(
self,
sensitivity,
num_thresholds=200,
class_id=None,
name=None,
dtype=None,
):
if sensitivity < 0 or sensitivity > 1:
raise ValueError(
"Argument `sensitivity` must be in the range [0, 1]. "
f"Received: sensitivity={sensitivity}"
)
self.sensitivity = sensitivity
self.num_thresholds = num_thresholds
super().__init__(
sensitivity,
num_thresholds=num_thresholds,
class_id=class_id,
name=name,
dtype=dtype,
)
def result(self):
sensitivities = ops.divide_no_nan(
self.true_positives,
ops.add(self.true_positives, self.false_negatives),
)View on GitHub (pinned to 7a34a03db6)
Solutions
- Use a fraction in [0, 1], e.g. sensitivity=0.95.
- Validate config values before metric construction.
- Ensure upstream calculations produce probabilities, not percentages.
Example fix
# before m = keras.metrics.SensitivityAtSpecificity(specificity=0.5, sensitivity=95) # after m = keras.metrics.SensitivityAtSpecificity(specificity=0.5, sensitivity=0.95)
Defensive patterns
Strategy: validation
Validate before calling
if not (0.0 <= sensitivity <= 1.0):
raise ValueError(f'sensitivity must be in [0,1], got {sensitivity}') Type guard
def is_probability(v) -> bool:
return isinstance(v, (int, float)) and 0.0 <= v <= 1.0 Prevention
- Convert percent configs with /100.0 at the boundary.
- Add unit-range assertions for metric args in tests.
When it happens
Trigger: keras.metrics.SensitivityAtSpecificity(sensitivity=1.5); passing 95 (percent) instead of 0.95; values from unnormalized computations.
Common situations: Percent/fraction mix-ups; ported code where targets were percentages; config typos.
Related errors
- Argument `specificity` must be in the range [0, 1]. Received
- Argument `recall` must be in the range [0, 1]. Received: rec
- Argument `precision` must be in the range [0, 1]. Received:
- Invalid `beta` argument value. It should be > 0. Received: b
- Invalid `threshold` argument value. It should verify 0 < thr
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/413feef2325e050b.
Report an issue: GitHub.