tensorflow/models · error · TypeError
`loss_fn` cannot be a Keras `Loss` object, pass a non-reduci
Error message
`loss_fn` cannot be a Keras `Loss` object, pass a non-reducing loss function or a metric instance instead.
What it means
Error "`loss_fn` cannot be a Keras `Loss` object, pass a non-reducing loss function or a metric instance instead." thrown in tensorflow/models.
Source
Thrown at official/recommendation/uplift/metrics/loss_metric.py:92
`loss_fn` is a Keras metric.
from_logits: When `y_pred` is of type `TwoTowerTrainingOutputs`, specifies
whether the true logits or true predictions should be used to compute
the loss (defaults to using the true logits). Othwerwise, this argument
will be ignored if `y_pred` is of type `tf.Tensor`.
slice_by_treatment: Specifies whether the loss should be sliced by the
treatment indicator tensor. If `True`, `loss_fn` will be wrapped in a
`TreatmentSlicedMetric` to report the loss values sliced by the
treatment group.
name: Optional name for the instance. If `loss_fn` is a Keras metric then
its name will be used instead.
dtype: Optional data type for the instance. If `loss_fn` is a Keras metric
then its `dtype` will be used instead.
**loss_fn_kwargs: The keyword arguments that are passed on to `loss_fn`.
These arguments will be ignored if `loss_fn` is a Keras metric.
"""
# Do not accept Loss objects as they reduce tensors before weighting.
if isinstance(loss_fn, tf_keras.losses.Loss):
raise TypeError(
"`loss_fn` cannot be a Keras `Loss` object, pass a non-reducing loss"
" function or a metric instance instead."
)
if isinstance(loss_fn, tf_keras.metrics.Metric):
name = loss_fn.name
dtype = loss_fn.dtype
super().__init__(name=name, dtype=dtype)
self._loss_fn = loss_fn
self._from_logits = from_logits
self._loss_fn_kwargs = loss_fn_kwargs
self._slice_by_treatment = slice_by_treatment
if isinstance(loss_fn, tf_keras.metrics.Metric):
metric_from_logits = loss_fn.get_config().get("from_logits", from_logits)
if from_logits != metric_from_logits:View on GitHub (pinned to e006f5f0d5)
When it happens
Trigger: Thrown at official/recommendation/uplift/metrics/loss_metric.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/5ea6a19b97a205c7.
Report an issue: GitHub.