An optimizer that applies loss scaling in backprop.
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 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_manger = tf.contrib.mixed_precision.FixedLossScaleManager(5000) # Wraps the original optimizer in a LossScaleOptimizer. loss_scale_optimizer = 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
Notice the following way of using LossScaleOptimizer is not intended. Always
loss_scale_optimizer.compute_gradients() to compute gradients instead of
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)
__init__( opt, loss_scale_manager )
Construct a loss scaling optimizer.
opt: The actual optimizer that will be used to compute and apply the gradients. Must be an implementation of the
loss_scale_manager: A LossScaleManager object.
apply_gradients( grads_and_vars, global_step=None, name=None )
Apply gradients. See base class
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
get_slot( var, name )
Return a slot named
name created for
var by the Optimizer.
Optimizer subclasses use additional variables. For example
Adagrad use variables to accumulate updates. This method
gives access to these
Variable objects if for some reason you need them.
get_slot_names() to get the list of slot names created by the
var: A variable passed to
name: A string.
Variable for the slot if it was created,
Return a list of the names of slots created by the
A list of strings.
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
This method simply combines calls
apply_gradients(). If you want to process the gradient before applying
apply_gradients() explicitly instead
of using this function.
Tensorcontaining the value to minimize.
Variableto increment by one after the variables have been updated.
var_list: Optional list or tuple of
Variableobjects to update to minimize
loss. Defaults to the list of variables collected in the graph under the key
gate_gradients: How to gate the computation of gradients. Can be
aggregation_method: Specifies the method used to combine gradient terms. Valid values are defined in the class
colocate_gradients_with_ops: If True, try colocating gradients with the corresponding op.
name: Optional name for the returned operation.
grad_loss: Optional. A
Tensorholding the gradient computed for
An Operation that updates the variables in
None, that operation also increments
ValueError: If some of the variables are not
When eager execution is enabled,
loss should be a Python function that
takes elements of
var_list as arguments and computes the value to be
var_list is None,
loss should take no arguments.
Minimization (and gradient computation) is done with respect to the
var_list if not None, else with respect to any trainable
variables created during the execution of the
grad_loss are ignored when eager execution is enabled.
A list of variables which encode the current state of
Includes slot variables and additional global variables created by the optimizer in the current default graph.
A list of variables.