# tf.contrib.mixed_precision.LossScaleOptimizer

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/mixed_precision/python/loss_scale_optimizer.py#L28-L174) |

An optimizer that applies loss scaling in backprop.

Inherits From: [`Optimizer`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/train/Optimizer)

    tf.contrib.mixed_precision.LossScaleOptimizer(
        opt, loss_scale_manager
    )

This class is useful for "mixed precision training" on GPUs (or other
potential accelerators), an approach to improve compute throughput without
compromising model quality.

The canonical way to perform mixed precision training is the following:

- Model variables are kept in high precision (e.g. float32).
- Computations are done in lower precision (e.g. float16), which enjoys performance speedup by virtue of hardware support. Variables are casted to lower precision before they're used.
- Final gradients are casted back to high precision dtype, then used to update variables.

The side-effect of performing computation in lower precision, is that it comes
with smaller numerical range. During backproping, small gradients might
underflow in the reduced numerical range, causing a model to converge at
suboptimal level.

To prevent underflow, this optimizer multiplies the loss by a factor before
backprop starts. Consequently, the gradients are linearly scaled up by the
same factor, thus not falling into the underflow zone. After that, to perserve
the correctness of backprop, the gradients are down-scaled by the same factor,
casted to the (higher) variable precision, then applied on the variables.

See [Nvidia's manual on mixed precision training](https://docs.nvidia.com/deeplearning/sdk/mixed-precision-training/index.html)
for more details.

To use loss scale optimizer, one only needs choose a loss scale strategy and
wrap a regular optimizer. See examples below.

    loss = loss_fn()
    opt = tf.AdamOptimizer(learning_rate=...)

    # Choose a loss scale manager which decides how to pick the right loss scale
    # throughout the training process.
    loss_scale_manager = tf.contrib.mixed_precision.FixedLossScaleManager(5000)

    # Wraps the original optimizer in a LossScaleOptimizer.
    loss_scale_optimizer =
        tf.contrib.mixed_precision.LossScaleOptimizer(opt, loss_scale_manager)

    # Call minimize() on the loss scale optimizer.
    train_op = loss_scale_optimizer.minimize(loss)

If gradients clipping is applied, one can call
`optimizer.compute_gradients()` and `optimizer.apply_gradients()`
separately.

Notice the following way of using LossScaleOptimizer is not intended. Always
use `loss_scale_optimizer.compute_gradients()` to compute gradients instead of
[`tf.gradients()`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/gradients) if doing mixed precision training.

    # The following is a wrong way to use LossScaleOptimizer along with
    # tf.gradients().

    # Always use loss_scale_optimizer.compute_gradients() to compute grads, or
    # loss scale is not correctly applied.
    grads = tf.gradients(loss, ...)

    # Do some custom grad clipping.
    grads = clip_grads(grads, ...)

    loss_scale_optimizer.apply(grads_and_vars)

<br />

<br />

<br />

| ## Args ||
|---|---|
| `opt` | The actual optimizer that will be used to compute and apply the gradients. Must be an implementation of the [`tf.compat.v1.train.Optimizer`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/train/Optimizer) interface. |
| `loss_scale_manager` | A LossScaleManager object. |

<br />

## Methods

### `apply_gradients`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/mixed_precision/python/loss_scale_optimizer.py#L144-L163)

    apply_gradients(
        grads_and_vars, global_step=None, name=None
    )

Apply gradients. See base class [`tf.compat.v1.train.Optimizer`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/train/Optimizer).

### `compute_gradients`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/contrib/mixed_precision/python/loss_scale_optimizer.py#L114-L142)

    compute_gradients(
        loss, var_list=None, gate_gradients=optimizer.Optimizer.GATE_OP,
        aggregation_method=None, colocate_gradients_with_ops=False, grad_loss=None
    )

Compute gradients. See base class [`tf.compat.v1.train.Optimizer`](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/train/Optimizer).

### `get_name`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/training/optimizer.py#L352-L353)

    get_name()

### `get_slot`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/training/optimizer.py#L735-L771)

    get_slot(
        var, name
    )

Return a slot named `name` created for `var` by the Optimizer.

Some `Optimizer` subclasses use additional variables. For example
`Momentum` and `Adagrad` use variables to accumulate updates. This method
gives access to these `Variable` objects if for some reason you need them.

Use `get_slot_names()` to get the list of slot names created by the
`Optimizer`.

<br />

<br />

<br />

| Args ||
|---|---|
| `var` | A variable passed to `minimize()` or `apply_gradients()`. |
| `name` | A string. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The `Variable` for the slot if it was created, `None` otherwise. ||

<br />

### `get_slot_names`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/training/optimizer.py#L773-L781)

    get_slot_names()

Return a list of the names of slots created by the `Optimizer`.

See `get_slot()`.

<br />

<br />

<br />

| Returns ||
|---|---|
| A list of strings. ||

<br />

### `minimize`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/training/optimizer.py#L355-L413)

    minimize(
        loss, global_step=None, var_list=None, gate_gradients=GATE_OP,
        aggregation_method=None, colocate_gradients_with_ops=False, name=None,
        grad_loss=None
    )

Add operations to minimize `loss` by updating `var_list`.

This method simply combines calls `compute_gradients()` and
`apply_gradients()`. If you want to process the gradient before applying
them call `compute_gradients()` and `apply_gradients()` explicitly instead
of using this function.

<br />

<br />

<br />

| Args ||
|---|---|
| `loss` | A `Tensor` containing the value to minimize. |
| `global_step` | Optional `Variable` to increment by one after the variables have been updated. |
| `var_list` | Optional list or tuple of `Variable` objects to update to minimize `loss`. Defaults to the list of variables collected in the graph under the key `GraphKeys.TRAINABLE_VARIABLES`. |
| `gate_gradients` | How to gate the computation of gradients. Can be `GATE_NONE`, `GATE_OP`, or `GATE_GRAPH`. |
| `aggregation_method` | Specifies the method used to combine gradient terms. Valid values are defined in the class `AggregationMethod`. |
| `colocate_gradients_with_ops` | If True, try colocating gradients with the corresponding op. |
| `name` | Optional name for the returned operation. |
| `grad_loss` | Optional. A `Tensor` holding the gradient computed for `loss`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| An Operation that updates the variables in `var_list`. If `global_step` was not `None`, that operation also increments `global_step`. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If some of the variables are not `Variable` objects. |

<br />

#### Eager Compatibility

When eager execution is enabled, `loss` should be a Python function that
takes no arguments and computes the value to be minimized. Minimization (and
gradient computation) is done with respect to the elements of `var_list` if
not None, else with respect to any trainable variables created during the
execution of the `loss` function. `gate_gradients`, `aggregation_method`,
`colocate_gradients_with_ops` and `grad_loss` are ignored when eager
execution is enabled.

### `variables`

[View source](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/training/optimizer.py#L783-L809)

    variables()

A list of variables which encode the current state of `Optimizer`.

Includes slot variables and additional global variables created by the
optimizer in the current default graph.

<br />

<br />

<br />

| Returns ||
|---|---|
| A list of variables. ||

<br />

## Class Variables

- `GATE_GRAPH = 2`
- `GATE_NONE = 0`
- `GATE_OP = 1`