keras-team/keras · error · ValueError
Argument `specificity` must be in the range [0, 1]. Received
Error message
Argument `specificity` must be in the range [0, 1]. Received: specificity={specificity} What it means
Raised in the constructor of keras.metrics.SpecificityAtSpecificity when the specificity argument falls outside [0, 1]. Specificity is a probability-like quantity (true negative rate), so Keros rejects out-of-range values immediately at construction time.
Source
Thrown at keras/src/metrics/confusion_metrics.py:743
```python
model.compile(
optimizer='sgd',
loss='binary_crossentropy',
metrics=[keras.metrics.SensitivityAtSpecificity(specificity=0.5)])
```
"""
def __init__(
self,
specificity,
num_thresholds=200,
class_id=None,
name=None,
dtype=None,
):
if specificity < 0 or specificity > 1:
raise ValueError(
"Argument `specificity` must be in the range [0, 1]. "
f"Received: specificity={specificity}"
)
self.specificity = specificity
self.num_thresholds = num_thresholds
super().__init__(
specificity,
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
- Pass a fraction in [0, 1], e.g. 0.9 instead of 90.
- Validate computed values: assert 0 <= v <= 1.
- Clamp drifting floats with min(max(v, 0.0), 1.0).
Example fix
# before m = keras.metrics.SpecificityAtSpecificity(specificity=90) # after m = keras.metrics.SpecificityAtSpecificity(specificity=0.9)
Defensive patterns
Strategy: validation
Validate before calling
if not (0.0 <= specificity <= 1.0):
raise ValueError(f'specificity must be in [0,1], got {specificity}') Type guard
def is_probability(v) -> bool:
return isinstance(v, (int, float)) and 0.0 <= v <= 1.0 Prevention
- Store targets as fractions, never percentages.
- Clamp computed ratios before passing to metrics.
When it happens
Trigger: keras.metrics.SpecificityAtSpecificity(specificity=1.2) or (-0.1); passing a percentage like 90 instead of 0.9; unclamped computed values.
Common situations: Percent-vs-fraction confusion; values from unnormalized expressions; floating-point drift slightly above 1.0.
Related errors
- Argument `sensitivity` 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/a22b04b56403b78f.
Report an issue: GitHub.