<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.21.0/tensorflow/python/ops/nn_impl_distribute.py#L70-L142) |

Scales per-example losses with sample_weights and computes their average.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.nn.compute_average_loss`](https://www.tensorflow.org/api_docs/python/tf/nn/compute_average_loss)

<br />

    tf.nn.compute_average_loss(
        per_example_loss, sample_weight=None, global_batch_size=None
    )

### Used in the notebooks

| Used in the guide | Used in the tutorials |
|---|---|
| - [Use TPUs](https://www.tensorflow.org/guide/tpu) - [Distributed training with TensorFlow](https://www.tensorflow.org/guide/distributed_training) | - [Custom training with tf.distribute.Strategy](https://www.tensorflow.org/tutorials/distribute/custom_training) - [Custom training loop with Keras and MultiWorkerMirroredStrategy](https://www.tensorflow.org/tutorials/distribute/multi_worker_with_ctl) - [Parameter server training with ParameterServerStrategy](https://www.tensorflow.org/tutorials/distribute/parameter_server_training) |

Usage with distribution strategy and custom training loop:

    with strategy.scope():
      def compute_loss(labels, predictions, sample_weight=None):

        # If you are using a `Loss` class instead, set reduction to `NONE` so that
        # we can do the reduction afterwards and divide by global batch size.
        per_example_loss = tf.keras.losses.sparse_categorical_crossentropy(
            labels, predictions)

        # Compute loss that is scaled by sample_weight and by global batch size.
        return tf.nn.compute_average_loss(
            per_example_loss,
            sample_weight=sample_weight,
            global_batch_size=GLOBAL_BATCH_SIZE)

<br />

<br />

<br />

| ## Args ||
|---|---|
| `per_example_loss` | Per-example loss. |
| `sample_weight` | Optional weighting for each example. |
| `global_batch_size` | Optional global batch size value. Defaults to (size of first dimension of `losses`) \* (number of replicas). |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| Scalar loss value, obtained by summing the `per_example_loss` and dividing by `global_batch_size`. If `global_batch_size` is zero, the result is zero. ||

<br />