LossScaleOptimizer
classkeras.optimizers.LossScaleOptimizer(
inner_optimizer, initial_scale=32768.0, dynamic_growth_steps=2000, **kwargs
)
一个动态缩放损失以防止下溢的优化器.
损失缩放是一种防止在使用float16时中间梯度数值下溢的技术.为了防止下溢,损失乘以(或"缩放”)一个称为"损失缩放因子”的特定因子,这导致中间梯度也按损失缩放因子进行缩放.最终梯度通过损失缩放因子进行除法(或"反缩放”)以恢复其原始值.
LossScaleOptimizer
包装另一个优化器并对其应用动态损失缩放.这个损失缩放因子会随着时间的推移动态更新,如下所示:
- 在任何训练步骤中,如果遇到非有限梯度,损失缩放因子会减半,并且跳过该训练步骤.
- 如果在上次更新损失缩放因子后发生了dynamic_growth_steps
次,并且没有发生非有限梯度,损失缩放因子会加倍.
参数:
inner_optimizer: 要包装的keras.optimizers.Optimizer
实例.
initial_scale: 浮点数.初始损失缩放因子.这个缩放因子会在训练过程中更新.建议这个值非常高,因为过高的损失缩放因子下降的速度远快于过低的损失缩放因子上升的速度.
dynamic_growth_steps: 整数.更新缩放因子的频率.在每次有限梯度的dynamic_growth_steps
步之后,损失缩放因子会加倍.
name: String. The name to use
for momentum accumulator weights created by
the optimizer.
weight_decay: Float. If set, weight decay is applied.
clipnorm: Float. If set, the gradient of each weight is individually
clipped so that its norm is no higher than this value.
clipvalue: Float. If set, the gradient of each weight is clipped to be
no higher than this value.
global_clipnorm: Float. If set, the gradient of all weights is clipped
so that their global norm is no higher than this value.
use_ema: Boolean, defaults to False
.
If True
, exponential moving average
(EMA) is applied. EMA consists of computing an exponential moving
average of the weights of the model (as the weight values change
after each training batch), and periodically overwriting the
weights with their moving average.
ema_momentum: Float, defaults to 0.99. Only used if use_ema=True
.
This is the momentum to use when computing
the EMA of the model's weights:
new_average = ema_momentum * old_average + (1 - ema_momentum) *
current_variable_value
.
ema_overwrite_frequency: Int or None, defaults to None. Only used if
use_ema=True
. Every ema_overwrite_frequency
steps of iterations,
we overwrite the model variable by its moving average.
If None, the optimizer
does not overwrite model variables in the middle of training,
and you need to explicitly overwrite the variables
at the end of training by calling
optimizer.finalize_variable_values()
(which updates the model
variables in-place). When using the built-in fit()
training loop,
this happens automatically after the last epoch,
and you don't need to do anything.
loss_scale_factor: Float or None
. If a float, the scale factor will
be multiplied the loss before computing gradients, and the inverse
of the scale factor will be multiplied by the gradients before
updating variables. Useful for preventing underflow during
mixed precision training. Alternately,
keras.optimizers.LossScaleOptimizer
will
automatically set a loss scale factor.
gradient_accumulation_steps: Int or None
. If an int, model & optimizer
variables will not be updated at every step; instead they will be
updated every gradient_accumulation_steps
steps, using the average
value of the gradients since the last update. This is known as
"gradient accumulation". This can be useful
when your batch size is very small, in order to reduce gradient
noise at each update step. EMA frequency will look at "accumulated"
iterations value (optimizer steps // gradient_accumulation_steps).
Learning rate schedules will look at "real" iterations value
(optimizer steps).