tensorflow/models · error · TypeError

y_pred must be of type `TwoTowerTrainingOutputs` but got…

Error message

y_pred must be of type `TwoTowerTrainingOutputs` but got type {type(y_pred)} instead.

What it means

Type guard in LabelVariance.update_state: variance-by-treatment-group requires the treatment indicator inside TwoTowerTrainingOutputs; the received y_pred type carries none, so slicing into control/treatment groups cannot proceed.

Solutions

  1. Pass TwoTowerTrainingOutputs from the two-tower model as y_pred
  2. Ensure the model's train_step returns the full outputs tuple, not just logits
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at official/recommendation/uplift/metrics/label_variance.py:92 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/e88f00e671f0fed3. Report an issue: GitHub.

Appendix: source

Thrown at official/recommendation/uplift/metrics/label_variance.py:92

      y_pred: types.TwoTowerTrainingOutputs,
      sample_weight: tf.Tensor | None = None,
  ):
    """Updates the overall, control and treatment label variances.

    Args:
      y_true: tensor labels.
      y_pred: two tower training outputs. The treatment indicator tensor is used
        to slice the labels into control and treatment groups.
      sample_weight: optional sample weight to compute weighted label variances.
        If given, the sample weight will also be sliced by the treatment
        indicator tensor to compute the weighted control and treatment label
        variances.

    Raises:
      TypeError: if y_pred is not of type `TwoTowerTrainingOutputs`.
    """
    if not isinstance(y_pred, types.TwoTowerTrainingOutputs):
      raise TypeError(
          "y_pred must be of type `TwoTowerTrainingOutputs` but got type"
          f" {type(y_pred)} instead."
      )

    self._sliced_variance.update_state(
        values=y_true,
        is_treatment=y_pred.is_treatment,
        sample_weight=sample_weight,
    )

  def result(self) -> dict[str, tf.Tensor]:
    return self._sliced_variance.result()

View on GitHub (pinned to e006f5f0d5)