mindspore.nn.PolynomialDecayLR

View Source On Gitee
class mindspore.nn.PolynomialDecayLR(learning_rate, end_learning_rate, decay_steps, power, update_decay_steps=False)[source]

Calculates learning rate base on polynomial decay function.

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

\[\begin{split}decayed\_learning\_rate = &(learning\_rate - end\_learning\_rate) *\\ &(1 - tmp\_step / tmp\_decay\_steps)^{power}\\ &+ end\_learning\_rate\end{split}\]

Where :

\[tmp\_step= \min(current\_step, decay\_steps)\]

If update_decay_steps is true, update the value of \(tmp\_decay\_steps\) every decay_steps. The formula is :

\[tmp\_decay\_steps = decay\_steps * ceil(current\_step / decay\_steps)\]
Parameters
  • learning_rate (float) – The initial value of learning rate.

  • end_learning_rate (float) – The end value of learning rate.

  • decay_steps (int) – Number of steps to decay over.

  • power (float) – The power of polynomial. It must be greater than 0.

  • update_decay_steps (bool) – If true , learning rate is decayed once every decay_steps time. Default: False .

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, end_learning_rate or power is not a float.

  • TypeError – If decay_steps is not an int or update_decay_steps is not a bool.

  • ValueError – If end_learning_rate is less than 0 or decay_steps is less than 1.

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

Supported Platforms:

Ascend GPU

Examples

>>> import mindspore
>>> from mindspore import Tensor, nn
>>>
>>> learning_rate = 0.1
>>> end_learning_rate = 0.01
>>> decay_steps = 4
>>> power = 0.5
>>> global_step = Tensor(2, mindspore.int32)
>>> polynomial_decay_lr = nn.PolynomialDecayLR(learning_rate, end_learning_rate, decay_steps, power)
>>> lr = polynomial_decay_lr(global_step)
>>> net = nn.Dense(2, 3)
>>> optim = nn.SGD(net.trainable_params(), learning_rate=lr)