mindspore.nn.WarmUpLR

View Source On Gitee
class mindspore.nn.WarmUpLR(learning_rate, warmup_steps)[source]

Gets learning rate warming up.

For current step, the formula of computing warmup learning rate is:

\[warmup\_learning\_rate = learning\_rate * tmp\_step / warmup\_steps\]

Where

\[tmp\_step= \min(current\_step, warmup\_steps)\]
Parameters
  • learning_rate (float) – The initial value of learning rate.

  • warmup_steps (int) – The warm up steps of learning rate.

Inputs:
  • global_step (Tensor) - The current step number.

Outputs:

Tensor. The learning rate value for the current step with shape \(()\).

Raises
  • TypeError – If learning_rate is not a float.

  • TypeError – If warmup_steps is not an int.

  • ValueError – If warmup_steps is less than 1.

  • ValueError – If learning_rate is less than or equal to 0.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import Tensor, nn
>>>
>>> learning_rate = 0.1
>>> warmup_steps = 2
>>> global_step = Tensor(2, mindspore.int32)
>>> warmup_lr = nn.WarmUpLR(learning_rate, warmup_steps)
>>> lr = warmup_lr(global_step)
>>> net = nn.Dense(2, 3)
>>> optim = nn.SGD(net.trainable_params(), learning_rate=lr)