{"record":{"id":"73293b2d0cba4b02","repo":"tensorflow/models","slug":"slice-by-treatment-must-be-false-when-y-pred-is","errorCode":null,"errorMessage":"`slice_by_treatment` must be False when y_pred is a `tf.Tensor` or `np.ndarray`.","messagePattern":"`slice_by_treatment` must be False when y_pred is a `tf\\.Tensor` or `np\\.ndarray`\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/recommendation/uplift/metrics/loss_metric.py","lineNumber":150,"sourceCode":"      sample_weight: tf.Tensor | None = None,\n  ):\n    \"\"\"Updates the overall, control and treatment losses.\n\n    Args:\n      y_true: A `tf.Tensor` with the targets.\n      y_pred: Model outputs. If of type `TwoTowerTrainingOutputs`, the treatment\n        indicator tensor is used to slice the true logits or true predictions\n        into control and treatment losses.\n      sample_weight: Optional sample weight to compute weighted losses. If\n        given, the sample weight will also be sliced by the treatment indicator\n        tensor to compute the weighted control and treatment losses.\n\n    Raises:\n      TypeError: if `y_pred` is not of type `TwoTowerTrainingOutputs`.\n    \"\"\"\n    if isinstance(y_pred, (tf.Tensor, np.ndarray)):\n      if self._slice_by_treatment:\n        raise ValueError(\n            \"`slice_by_treatment` must be False when y_pred is a `tf.Tensor` or\"\n            \" `np.ndarray`.\"\n        )\n      pred = y_pred\n    elif isinstance(y_pred, types.TwoTowerTrainingOutputs):\n      pred = (\n          y_pred.true_logits if self._from_logits else y_pred.true_predictions\n      )\n    else:\n      raise TypeError(\n          \"y_pred must be of type `TwoTowerTrainingOutputs`, `tf.Tensor` or\"\n          f\" `np.ndarray` but got type {type(y_pred)} instead.\"\n      )\n\n    is_treatment = {}\n    if self._slice_by_treatment:\n      is_treatment[\"is_treatment\"] = y_pred.is_treatment\n","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/recommendation/uplift/metrics/loss_metric.py#L132-L168","documentation":"Error \"`slice_by_treatment` must be False when y_pred is a `tf.Tensor` or `np.ndarray`.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/recommendation/uplift/metrics/loss_metric.py:150 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}