{"record":{"id":"5ea6a19b97a205c7","repo":"tensorflow/models","slug":"loss-fn-cannot-be-a-keras-loss-object-pass-a","errorCode":null,"errorMessage":"`loss_fn` cannot be a Keras `Loss` object, pass a non-reducing loss function or a metric instance instead.","messagePattern":"`loss_fn` cannot be a Keras `Loss` object, pass a non-reducing loss function or a metric instance instead\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"official/recommendation/uplift/metrics/loss_metric.py","lineNumber":92,"sourceCode":"        `loss_fn` is a Keras metric.\n      from_logits: When `y_pred` is of type `TwoTowerTrainingOutputs`, specifies\n        whether the true logits or true predictions should be used to compute\n        the loss (defaults to using the true logits). Othwerwise, this argument\n        will be ignored if `y_pred` is of type `tf.Tensor`.\n      slice_by_treatment: Specifies whether the loss should be sliced by the\n        treatment indicator tensor. If `True`, `loss_fn` will be wrapped in a\n        `TreatmentSlicedMetric` to report the loss values sliced by the\n        treatment group.\n      name: Optional name for the instance. If `loss_fn` is a Keras metric then\n        its name will be used instead.\n      dtype: Optional data type for the instance. If `loss_fn` is a Keras metric\n        then its `dtype` will be used instead.\n      **loss_fn_kwargs: The keyword arguments that are passed on to `loss_fn`.\n        These arguments will be ignored if `loss_fn` is a Keras metric.\n    \"\"\"\n    # Do not accept Loss objects as they reduce tensors before weighting.\n    if isinstance(loss_fn, tf_keras.losses.Loss):\n      raise TypeError(\n          \"`loss_fn` cannot be a Keras `Loss` object, pass a non-reducing loss\"\n          \" function or a metric instance instead.\"\n      )\n\n    if isinstance(loss_fn, tf_keras.metrics.Metric):\n      name = loss_fn.name\n      dtype = loss_fn.dtype\n\n    super().__init__(name=name, dtype=dtype)\n\n    self._loss_fn = loss_fn\n    self._from_logits = from_logits\n    self._loss_fn_kwargs = loss_fn_kwargs\n    self._slice_by_treatment = slice_by_treatment\n\n    if isinstance(loss_fn, tf_keras.metrics.Metric):\n      metric_from_logits = loss_fn.get_config().get(\"from_logits\", from_logits)\n      if from_logits != metric_from_logits:","sourceCodeStart":74,"sourceCodeEnd":110,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/recommendation/uplift/metrics/loss_metric.py#L74-L110","documentation":"Error \"`loss_fn` cannot be a Keras `Loss` object, pass a non-reducing loss function or a metric instance instead.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/recommendation/uplift/metrics/loss_metric.py:92 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"}