keras-team/keras · error · ValueError

Please provide at least one valid confusion matrix variable

Error message

Please provide at least one valid confusion matrix variable to update. Valid variable key options are: "{list(ConfusionMatrix)}". Received: "{variables_to_update.keys()}"

What it means

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.

Source

Thrown at keras/src/metrics/metrics_utils.py:419

        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

    y_true = ops.cast(y_true, dtype=variable_dtype)
    y_pred = ops.cast(y_pred, dtype=variable_dtype)

    if thresholds_distributed_evenly:
        # Check whether the thresholds has any leading or tailing epsilon added
        # for floating point imprecision. The leading and tailing threshold will
        # be handled bit differently as the corner case.  At this point,
        # thresholds should be a list/array with more than 2 items, and ranged
        # between [0, 1]. See is_evenly_distributed_thresholds() for more
        # details.

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Key the dict with ConfusionMatrix members: {ConfusionMatrix.TP: var_tp, ConfusionMatrix.FP: var_fp}.
  2. If nothing should be updated, pass variables_to_update=None - the function returns early instead of raising.

Example fix

# before
metrics_utils.update_confusion_matrix_variables(
    {'tp': self.true_positives}, y_true, y_pred)

# after
from keras.src.metrics.metrics_utils import ConfusionMatrix
metrics_utils.update_confusion_matrix_variables(
    {ConfusionMatrix.TP: self.true_positives}, y_true, y_pred)
Defensive patterns

Strategy: validation

Validate before calling

from keras.src.metrics.metrics_utils import ConfusionMatrix
def check_update_vars(d):
    if d is not None and not any(k in list(ConfusionMatrix) for k in d):
        raise ValueError('variables_to_update needs at least one ConfusionMatrix key')
    return d

Type guard

def has_valid_cm_keys(d) -> bool:
    from keras.src.metrics.metrics_utils import ConfusionMatrix
    return any(k in list(ConfusionMatrix) for k in (d or {}))

Prevention

When it happens

Trigger: Calling update_confusion_matrix_variables(variables_to_update={}) or variables_to_update={'tp': var} - no key that is a ConfusionMatrix member.

Common situations: Custom metrics building the variables dict with string keys instead of ConfusionMatrix enum members, or passing an empty dict when all variables were None.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/e89d5f0655c0025b. Report an issue: GitHub.