{"record":{"id":"e89d5f0655c0025b","repo":"keras-team/keras","slug":"please-provide-at-least-one-valid-confusion-matrix","errorCode":null,"errorMessage":"Please provide at least one valid confusion matrix variable to update. Valid variable key options are: \"{list(ConfusionMatrix)}\". Received: \"{variables_to_update.keys()}\"","messagePattern":"Please provide at least one valid confusion matrix variable to update\\. Valid variable key options are: \"(.+?)\"\\. Received: \"(.+?)\"","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/metrics/metrics_utils.py","lineNumber":419,"sourceCode":"        details.\n\n    Raises:\n      ValueError: If `y_pred` and `y_true` have mismatched shapes, or if\n        `sample_weight` is not `None` and its shape doesn't match `y_pred`, or\n        if `variables_to_update` contains invalid keys.\n    \"\"\"\n    if multi_label and label_weights is not None:\n        raise ValueError(\n            \"`label_weights` for multilabel data should be handled \"\n            \"outside of `update_confusion_matrix_variables` when \"\n            \"`multi_label` is True.\"\n        )\n    if variables_to_update is None:\n        return\n    if not any(\n        key for key in variables_to_update if key in list(ConfusionMatrix)\n    ):\n        raise ValueError(\n            \"Please provide at least one valid confusion matrix \"\n            \"variable to update. Valid variable key options are: \"\n            f'\"{list(ConfusionMatrix)}\". '\n            f'Received: \"{variables_to_update.keys()}\"'\n        )\n\n    variable_dtype = list(variables_to_update.values())[0].dtype\n\n    y_true = ops.cast(y_true, dtype=variable_dtype)\n    y_pred = ops.cast(y_pred, dtype=variable_dtype)\n\n    if thresholds_distributed_evenly:\n        # Check whether the thresholds has any leading or tailing epsilon added\n        # for floating point imprecision. The leading and tailing threshold will\n        # be handled bit differently as the corner case.  At this point,\n        # thresholds should be a list/array with more than 2 items, and ranged\n        # between [0, 1]. See is_evenly_distributed_thresholds() for more\n        # details.","sourceCodeStart":401,"sourceCodeEnd":437,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/metrics/metrics_utils.py#L401-L437","documentation":"update_confusion_matrix_variables() requires its variables_to_update dict to contain at least one key from the ConfusionMatrix enum (TP, FP, TN, FN). If no key is a valid enum member it raises this ValueError listing the valid options. An empty dict or a dict keyed by plain strings like 'true_positives' triggers it.","triggerScenarios":"Calling update_confusion_matrix_variables(variables_to_update={}) or variables_to_update={'tp': var} - no key that is a ConfusionMatrix member.","commonSituations":"Custom metrics building the variables dict with string keys instead of ConfusionMatrix enum members, or passing an empty dict when all variables were None.","solutions":["Key the dict with ConfusionMatrix members: {ConfusionMatrix.TP: var_tp, ConfusionMatrix.FP: var_fp}.","If nothing should be updated, pass variables_to_update=None - the function returns early instead of raising."],"exampleFix":"# before\nmetrics_utils.update_confusion_matrix_variables(\n    {'tp': self.true_positives}, y_true, y_pred)\n\n# after\nfrom keras.src.metrics.metrics_utils import ConfusionMatrix\nmetrics_utils.update_confusion_matrix_variables(\n    {ConfusionMatrix.TP: self.true_positives}, y_true, y_pred)","handlingStrategy":"validation","validationCode":"from keras.src.metrics.metrics_utils import ConfusionMatrix\ndef check_update_vars(d):\n    if d is not None and not any(k in list(ConfusionMatrix) for k in d):\n        raise ValueError('variables_to_update needs at least one ConfusionMatrix key')\n    return d","typeGuard":"def has_valid_cm_keys(d) -> bool:\n    from keras.src.metrics.metrics_utils import ConfusionMatrix\n    return any(k in list(ConfusionMatrix) for k in (d or {}))","tryCatchPattern":null,"preventionTips":["Build variables_to_update dicts only with ConfusionMatrix enum members.","Use None instead of {} when nothing should update."],"tags":["keras","metrics","confusion-matrix","dict-keys","internal-api"],"backgroundTag":"invalid-dict-key","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}