{"record":{"id":"413feef2325e050b","repo":"keras-team/keras","slug":"argument-sensitivity-must-be-in-the-range-0-1","errorCode":null,"errorMessage":"Argument `sensitivity` must be in the range [0, 1]. Received: sensitivity={sensitivity}","messagePattern":"Argument `sensitivity` must be in the range \\[0, 1\\]\\. Received: sensitivity=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/metrics/confusion_metrics.py","lineNumber":847,"sourceCode":"\n    ```python\n    model.compile(\n        optimizer='sgd',\n        loss='binary_crossentropy',\n        metrics=[keras.metrics.SpecificityAtSensitivity(sensitivity=0.3)])\n    ```\n    \"\"\"\n\n    def __init__(\n        self,\n        sensitivity,\n        num_thresholds=200,\n        class_id=None,\n        name=None,\n        dtype=None,\n    ):\n        if sensitivity < 0 or sensitivity > 1:\n            raise ValueError(\n                \"Argument `sensitivity` must be in the range [0, 1]. \"\n                f\"Received: sensitivity={sensitivity}\"\n            )\n        self.sensitivity = sensitivity\n        self.num_thresholds = num_thresholds\n        super().__init__(\n            sensitivity,\n            num_thresholds=num_thresholds,\n            class_id=class_id,\n            name=name,\n            dtype=dtype,\n        )\n\n    def result(self):\n        sensitivities = ops.divide_no_nan(\n            self.true_positives,\n            ops.add(self.true_positives, self.false_negatives),\n        )","sourceCodeStart":829,"sourceCodeEnd":865,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/metrics/confusion_metrics.py#L829-L865","documentation":"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__.","triggerScenarios":"keras.metrics.SensitivityAtSpecificity(sensitivity=1.5); passing 95 (percent) instead of 0.95; values from unnormalized computations.","commonSituations":"Percent/fraction mix-ups; ported code where targets were percentages; config typos.","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."],"exampleFix":"# before\nm = keras.metrics.SensitivityAtSpecificity(specificity=0.5, sensitivity=95)\n\n# after\nm = keras.metrics.SensitivityAtSpecificity(specificity=0.5, sensitivity=0.95)","handlingStrategy":"validation","validationCode":"if not (0.0 <= sensitivity <= 1.0):\n    raise ValueError(f'sensitivity must be in [0,1], got {sensitivity}')","typeGuard":"def is_probability(v) -> bool:\n    return isinstance(v, (int, float)) and 0.0 <= v <= 1.0","tryCatchPattern":null,"preventionTips":["Convert percent configs with /100.0 at the boundary.","Add unit-range assertions for metric args in tests."],"tags":["keras","metrics","sensitivity","range-validation"],"backgroundTag":"probability-out-of-range","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}