tf.keras.losses.Reduction
Stay organized with collections
Save and categorize content based on your preferences.
Types of loss reduction.
Contains the following values:
AUTO
: Indicates that the reduction option will be determined by the usage
context. For almost all cases this defaults to SUM_OVER_BATCH_SIZE
. When
used with tf.distribute.Strategy
, outside of built-in training loops such
as tf.keras
compile
and fit
, we expect reduction value to be
SUM
or NONE
. Using AUTO
in that case will raise an error.
NONE
: Weighted losses with one dimension reduced (axis=-1, or axis
specified by loss function). When this reduction type used with built-in
Keras training loops like fit
/evaluate
, the unreduced vector loss is
passed to the optimizer but the reported loss will be a scalar value.
SUM
: Scalar sum of weighted losses.
SUM_OVER_BATCH_SIZE
: Scalar SUM
divided by number of elements in losses.
This reduction type is not supported when used with
tf.distribute.Strategy
outside of built-in training loops like tf.keras
compile
/fit
.
You can implement 'SUM_OVER_BATCH_SIZE' using global batch size like:
with strategy.scope():
loss_obj = tf.keras.losses.CategoricalCrossentropy(
reduction=tf.keras.losses.Reduction.NONE)
....
loss = tf.reduce_sum(loss_object(labels, predictions)) *
(1. / global_batch_size)
Please see
https://www.tensorflow.org/alpha/tutorials/distribute/training_loops for
more details on this.
Methods
all
View source
@classmethod
all()
validate
View source
@classmethod
validate(
key
)
Class Variables
AUTO = 'auto'
NONE = 'none'
SUM = 'sum'
SUM_OVER_BATCH_SIZE = 'sum_over_batch_size'
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.keras.losses.Reduction\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/losses/loss_reduction.py#L21-L68) |\n\nTypes of loss reduction.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tf.losses.Reduction`](/api_docs/python/tf/keras/losses/Reduction)\n\n\u003cbr /\u003e\n\nContains the following values:\n\n- `AUTO`: Indicates that the reduction option will be determined by the usage context. For almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When used with [`tf.distribute.Strategy`](../../../tf/distribute/Strategy), outside of built-in training loops such as [`tf.keras`](../../../tf/keras) `compile` and `fit`, we expect reduction value to be `SUM` or `NONE`. Using `AUTO` in that case will raise an error.\n- `NONE`: Weighted losses with one dimension reduced (axis=-1, or axis specified by loss function). When this reduction type used with built-in Keras training loops like `fit`/`evaluate`, the unreduced vector loss is passed to the optimizer but the reported loss will be a scalar value.\n- `SUM`: Scalar sum of weighted losses.\n- `SUM_OVER_BATCH_SIZE`: Scalar `SUM` divided by number of elements in losses.\n This reduction type is not supported when used with\n [`tf.distribute.Strategy`](../../../tf/distribute/Strategy) outside of built-in training loops like [`tf.keras`](../../../tf/keras)\n `compile`/`fit`.\n\n You can implement 'SUM_OVER_BATCH_SIZE' using global batch size like:\n\n\u003e with strategy.scope():\n\u003e loss_obj = tf.keras.losses.CategoricalCrossentropy(\n\u003e reduction=tf.keras.losses.Reduction.NONE)\n\u003e ....\n\u003e loss = tf.reduce_sum(loss_object(labels, predictions)) *\n\u003e (1. / global_batch_size)\n\nPlease see\n\u003chttps://www.tensorflow.org/alpha/tutorials/distribute/training_loops\u003e for\nmore details on this.\n\nMethods\n-------\n\n### `all`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/losses/loss_reduction.py#L61-L63) \n\n @classmethod\n all()\n\n### `validate`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.0.0/tensorflow/python/ops/losses/loss_reduction.py#L65-L68) \n\n @classmethod\n validate(\n key\n )\n\nClass Variables\n---------------\n\n- `AUTO = 'auto'`\n- `NONE = 'none'`\n- `SUM = 'sum'`\n- `SUM_OVER_BATCH_SIZE = 'sum_over_batch_size'`"]]