mindflow.common.get_warmup_cosine_annealing_lr

mindflow.common.get_warmup_cosine_annealing_lr(lr_init, steps_per_epoch, last_epoch, warmup_epochs=0, warmup_lr_init=0.0, eta_min=1e-06)[source]

Calculates learning rate base on cosine decay function. If warmup epoch is specified, the warmup epoch will be warmed up by linear annealing.

For the i-th step, the formula of computing cosine decayed_learning_rate[i] is:

\[decayed\_learning\_rate[i] = eta\_min + 0.5 * (lr\_init - eta\_min) * (1 + cos(\frac{current\_epoch}{last\_epoch}\pi))\]

Where \(current\_epoch = floor(\frac{i}{steps\_per\_epoch})\).

If warmup epoch is specified, for the i-th step in waramup epoch, the formula of computing

warmup_learning_rate[i] is:

\[warmup\_learning\_rate[i] = (lr\_init - warmup\_lr\_init) * i / warmup\_steps + warmup\_lr\_init\]
Parameters
  • lr_init (float) – init learning rate, positive float value.

  • steps_per_epoch (int) – number of steps to each epoch, positive int value.

  • last_epoch (int) – total epoch of training, positive int value.

  • warmup_epochs (int) – total epoch of warming up, default: 0.

  • warmup_lr_init (float) – warmup init learning rate, default: 0.0.

  • eta_min (float) – minimum learning rate, default: 1e-6.

Returns

Numpy.array, learning rate array.

Raises
  • TypeError – If lr_init or warmup_lr_init or eta_min is not a float.

  • TypeError – If steps_per_epoch or warmup_epochs or last_epoch is not an int.

Supported Platforms:

Ascend GPU CPU

Examples

>>> from mindflow import get_warmup_cosine_annealing_lr
>>> lr_init = 0.001
>>> steps_per_epoch = 3
>>> last_epoch = 5
>>> warmup_epochs = 1
>>> lr = get_warmup_cosine_annealing_lr(lr_init, steps_per_epoch, last_epoch, warmup_epochs=warmup_epochs)
>>> print(lr)
[3.3333333e-04 6.6666666e-04 1.0000000e-03 9.0460398e-04 9.0460398e-04
 9.0460398e-04 6.5485400e-04 6.5485400e-04 6.5485400e-04 3.4614600e-04
 3.4614600e-04 3.4614600e-04 9.6396012e-05 9.6396012e-05 9.6396012e-05]