{"record":{"id":"585d4f624ac3f952","repo":"keras-team/keras","slug":"label-weights-for-multilabel-data-should-be-hand","errorCode":null,"errorMessage":"`label_weights` for multilabel data should be handled outside of `update_confusion_matrix_variables` when `multi_label` is True.","messagePattern":"`label_weights` for multilabel data should be handled outside of `update_confusion_matrix_variables` when `multi_label` is True\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/metrics/metrics_utils.py","lineNumber":409,"sourceCode":"        `variables_to_update` must have a second dimension equal to the number\n        of labels in y_true and y_pred, and those tensors must not be\n        RaggedTensors.\n      label_weights: (optional) tensor of non-negative weights for multilabel\n        data. The weights are applied when calculating TP, FP, FN, and TN\n        without explicit multilabel handling (i.e. when the data is to be\n        flattened).\n      thresholds_distributed_evenly: Boolean, whether the thresholds are evenly\n        distributed within the list. An optimized method will be used if this is\n        the case. See _update_confusion_matrix_variables_optimized() for more\n        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","sourceCodeStart":391,"sourceCodeEnd":427,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/metrics/metrics_utils.py#L391-L427","documentation":"update_confusion_matrix_variables() is the shared engine behind Precision/Recall/confusion-matrix metrics. When multi_label=True it refuses a label_weights argument, because per-label weighting must be applied by the caller before state updates. Passing both raises this ValueError.","triggerScenarios":"Calling update_confusion_matrix_variables(..., multi_label=True, label_weights=<array>) directly, e.g. from a custom multilabel metric that forwards both parameters.","commonSituations":"Writing a custom multilabel Precision/Recall variant and copying the single-label label_weights logic into the multilabel path.","solutions":["Drop label_weights from the call and apply per-label weights yourself (multiply y_true or sample_weight per column before updating).","Alternatively pre-multiply sample_weight by the label weights and pass the result as sample_weight."],"exampleFix":"# before\nmetrics_utils.update_confusion_matrix_variables(\n    variables, y_true, y_pred, multi_label=True, label_weights=w)\n\n# after\nweighted = y_true * w  # apply per-label weights outside\nmetrics_utils.update_confusion_matrix_variables(\n    variables, weighted, y_pred, multi_label=True)","handlingStrategy":"validation","validationCode":"def update_safe(**kw):\n    if kw.get('multi_label') and kw.get('label_weights') is not None:\n        kw.pop('label_weights')  # apply weights outside instead\n    return metrics_utils.update_confusion_matrix_variables(**kw)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never forward label_weights together with multi_label=True."],"tags":["keras","metrics","confusion-matrix","multilabel","internal-api"],"backgroundTag":"unsupported-argument-combination","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}