tensorflow/models · error · TypeError

y_pred must be of type `TwoTowerTrainingOutputs`, `tf.Tensor

Error message

y_pred must be of type `TwoTowerTrainingOutputs`, `tf.Tensor` or `np.ndarray` but got type {type(y_pred)} instead.

What it means

Error "y_pred must be of type `TwoTowerTrainingOutputs`, `tf.Tensor` or `np.ndarray` but got type {type(y_pred)} instead." thrown in tensorflow/models.

Source

Thrown at official/recommendation/uplift/metrics/loss_metric.py:160

        given, the sample weight will also be sliced by the treatment indicator
        tensor to compute the weighted control and treatment losses.

    Raises:
      TypeError: if `y_pred` is not of type `TwoTowerTrainingOutputs`.
    """
    if isinstance(y_pred, (tf.Tensor, np.ndarray)):
      if self._slice_by_treatment:
        raise ValueError(
            "`slice_by_treatment` must be False when y_pred is a `tf.Tensor` or"
            " `np.ndarray`."
        )
      pred = y_pred
    elif isinstance(y_pred, types.TwoTowerTrainingOutputs):
      pred = (
          y_pred.true_logits if self._from_logits else y_pred.true_predictions
      )
    else:
      raise TypeError(
          "y_pred must be of type `TwoTowerTrainingOutputs`, `tf.Tensor` or"
          f" `np.ndarray` but got type {type(y_pred)} instead."
      )

    is_treatment = {}
    if self._slice_by_treatment:
      is_treatment["is_treatment"] = y_pred.is_treatment

    if isinstance(self._loss_fn, tf_keras.metrics.Metric):
      self._loss.update_state(
          y_true,
          y_pred=pred,  # pyrefly: ignore[unexpected-keyword]
          sample_weight=sample_weight,
          **is_treatment,
      )
    else:
      self._loss.update_state(
          values=self._loss_fn(y_true, pred, **self._loss_fn_kwargs),  # pyrefly: ignore[bad-argument-type]

View on GitHub (pinned to e006f5f0d5)

When it happens

Trigger: Thrown at official/recommendation/uplift/metrics/loss_metric.py:160 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/a2cbfac23ceb4fe3. Report an issue: GitHub.