keras-team/keras · error · ValueError

`num_labels` is needed only when `multi_label` is True.

Error message

`num_labels` is needed only when `multi_label` is True.

What it means

Raised by keras.metrics.AUC's __init__ when num_labels is supplied while multi_label=False. num_labels only defines the output shape [None, num_labels] in multi-label mode; for single-label AUC it is rejected.

Source

Thrown at keras/src/metrics/confusion_metrics.py:1286

        self.multi_label = multi_label
        self.num_labels = num_labels
        if label_weights is not None:
            label_weights = ops.array(label_weights, dtype=self.dtype)
            self.label_weights = label_weights

        else:
            self.label_weights = None

        self._from_logits = from_logits

        self._built = False
        if self.multi_label:
            if num_labels:
                shape = [None, num_labels]
                self._build(shape)
        else:
            if num_labels:
                raise ValueError(
                    "`num_labels` is needed only when `multi_label` is True."
                )
            self._build(None)

    @property
    def thresholds(self):
        """The thresholds used for evaluating AUC."""
        return list(self._thresholds)

    def _build(self, shape):
        """Initialize TP, FP, TN, and FN tensors, given the shape of the
        data."""
        if self.multi_label:
            if len(shape) != 2:
                raise ValueError(
                    "`y_pred` must have rank 2 when `multi_label=True`. "
                    f"Found rank {len(shape)}. "
                    f"Full shape received for `y_pred`: {shape}"

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Add multi_label=True if you have a (batch, num_labels) output with independent per-label AUCs.
  2. Remove num_labels for single-label/binary AUC.
  3. Treat multi_label and num_labels as a paired config option.

Example fix

# before
auc = keras.metrics.AUC(num_labels=3)

# after (multi-label):
auc = keras.metrics.AUC(multi_label=True, num_labels=3)
# after (binary):
auc = keras.metrics.AUC()
Defensive patterns

Strategy: validation

Validate before calling

if num_labels is not None and not multi_label:
    raise ValueError('num_labels requires multi_label=True')

Prevention

When it happens

Trigger: keras.metrics.AUC(num_labels=3) without multi_label=True; copying a multi-label AUC config and dropping only the multi_label flag.

Common situations: Adapting multi-label example code to binary problems; leftover config keys from earlier experiments.

Related errors


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