keras-team/keras · error · ValueError
Argument `precision` must be in the range [0, 1]. Received:
Error message
Argument `precision` must be in the range [0, 1]. Received: precision={precision} What it means
Raised by keras.metrics.RecallAtPrecision's __init__ when the precision argument is outside [0, 1]. Precision is a probability-like score; Keras validates the range at construction time.
Source
Thrown at keras/src/metrics/confusion_metrics.py:1032
```python
model.compile(
optimizer='sgd',
loss='binary_crossentropy',
metrics=[keras.metrics.RecallAtPrecision(precision=0.8)])
```
"""
def __init__(
self,
precision,
num_thresholds=200,
class_id=None,
name=None,
dtype=None,
):
if precision < 0 or precision > 1:
raise ValueError(
"Argument `precision` must be in the range [0, 1]. "
f"Received: precision={precision}"
)
self.precision = precision
self.num_thresholds = num_thresholds
super().__init__(
value=precision,
num_thresholds=num_thresholds,
class_id=class_id,
name=name,
dtype=dtype,
)
def result(self):
recalls = 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. precision=0.99.
- Clamp computed values: min(max(v, 0.0), 1.0).
- Validate config entries before building the model.
Example fix
# before m = keras.metrics.RecallAtPrecision(precision=99) # after m = keras.metrics.RecallAtPrecision(precision=0.99)
Defensive patterns
Strategy: validation
Validate before calling
if not (0.0 <= precision <= 1.0):
raise ValueError(f'precision must be in [0,1], got {precision}') Type guard
def is_probability(v) -> bool:
return isinstance(v, (int, float)) and 0.0 <= v <= 1.0 Prevention
- Clamp tp/(tp+fp) style ratios to [0,1].
- Keep percent-to-fraction conversion in one place.
When it happens
Trigger: keras.metrics.RecallAtPrecision(precision=1.05); passing 99 (percent); a computed tp/(tp+fp) that is unclamped above 1.
Common situations: Percent/fraction confusion; unclamped ratios; config typos.
Related errors
- Argument `specificity` must be in the range [0, 1]. Received
- Argument `sensitivity` must be in the range [0, 1]. Received
- Argument `recall` must be in the range [0, 1]. Received: rec
- 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/4a03bd894c37426c.
Report an issue: GitHub.