keras-team/keras · error · ValueError
`label_weights` for multilabel data should be handled outsid
Error message
`label_weights` for multilabel data should be handled outside of `update_confusion_matrix_variables` when `multi_label` is True.
What it means
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.
Source
Thrown at keras/src/metrics/metrics_utils.py:409
`variables_to_update` must have a second dimension equal to the number
of labels in y_true and y_pred, and those tensors must not be
RaggedTensors.
label_weights: (optional) tensor of non-negative weights for multilabel
data. The weights are applied when calculating TP, FP, FN, and TN
without explicit multilabel handling (i.e. when the data is to be
flattened).
thresholds_distributed_evenly: Boolean, whether the thresholds are evenly
distributed within the list. An optimized method will be used if this is
the case. See _update_confusion_matrix_variables_optimized() for more
details.
Raises:
ValueError: If `y_pred` and `y_true` have mismatched shapes, or if
`sample_weight` is not `None` and its shape doesn't match `y_pred`, or
if `variables_to_update` contains invalid keys.
"""
if multi_label and label_weights is not None:
raise ValueError(
"`label_weights` for multilabel data should be handled "
"outside of `update_confusion_matrix_variables` when "
"`multi_label` is True."
)
if variables_to_update is None:
return
if not any(
key for key in variables_to_update if key in list(ConfusionMatrix)
):
raise ValueError(
"Please provide at least one valid confusion matrix "
"variable to update. Valid variable key options are: "
f'"{list(ConfusionMatrix)}". '
f'Received: "{variables_to_update.keys()}"'
)
variable_dtype = list(variables_to_update.values())[0].dtype
View on GitHub (pinned to 7a34a03db6)
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.
Example fix
# before
metrics_utils.update_confusion_matrix_variables(
variables, y_true, y_pred, multi_label=True, label_weights=w)
# after
weighted = y_true * w # apply per-label weights outside
metrics_utils.update_confusion_matrix_variables(
variables, weighted, y_pred, multi_label=True) Defensive patterns
Strategy: validation
Validate before calling
def update_safe(**kw):
if kw.get('multi_label') and kw.get('label_weights') is not None:
kw.pop('label_weights') # apply weights outside instead
return metrics_utils.update_confusion_matrix_variables(**kw) Prevention
- Never forward label_weights together with multi_label=True.
When it happens
Trigger: Calling update_confusion_matrix_variables(..., multi_label=True, label_weights=<array>) directly, e.g. from a custom multilabel metric that forwards both parameters.
Common situations: Writing a custom multilabel Precision/Recall variant and copying the single-label label_weights logic into the multilabel path.
Related errors
- Please provide at least one valid confusion matrix variable
- Invalid keys: "{invalid_keys}". Valid variable key options a
- ConvNeXt does not support the `channels_first` image data fo
- Invalid tensor type: {tensor_type}
- Layer `add_metric()` method is deprecated. Add your metric i
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/585d4f624ac3f952.
Report an issue: GitHub.