name | about | labels |
---|---|---|
Task | Use this template for task tracking | kind/task |
class CosineDecayLR(LearningRateSchedule):
def __init__(self, min_lr, max_lr, decay_steps):
super(CosineDecayLR, self).__init__()
self.min_lr = min_lr
self.max_lr = max_lr
self.decay_steps = decay_steps
self.math_pi = math.pi
self.delta = 0.5 * (max_lr - min_lr)
self.cos = P.Cos()
self.min = P.Minimum()
self.cast = P.Cast()
def construct(self, global_step):
p = self.cast(self.min(global_step, self.decay_steps), mstype.float32)
return self.min_lr + self.delta * (1.0 + self.cos(self.math_pi * p / self.decay_steps))
LearningRateSchedule is subclass of nn.Cell.
min_lr = 0.01
max_lr = 0.1
decay_steps = 4
global_steps = Tensor(2, mstype.int32)
cosine_decay_lr = CosineDecayLR(min_lr, max_lr, decay_steps)
result = cosine_decay_lr(global_steps)
print(result)
0.055
cosine_decay_lr
to a optimizer. Then, during the training process, the optimizer call cosine_decay_lr
with global_step as input to get the current learning rate.global_step
and invocation process described above are defined in the nn.Optimizer.net = Net()
loss = nn.SoftmaxCrossEntropyWithLogits()
optim = nn.Momentum(net.trainable_params(), learning_rate=cosine_decay_lr)
model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)
dataset = create_custom_dataset()
model.train(1, dataset)
Before developing the learning rate, you need to make sure that the operators provided by MindSpore meet your requirements. Click here to view the operators currently supported by MindSpore.
Hey wangnan39, Welcome to MindSpore Community.
All of the projects in MindSpore Community are maintained by @mindspore-ci-bot.
That means the developers can comment below every pull request or issue to trigger Bot Commands.
Please follow instructions at https://gitee.com/mindspore/community/blob/master/command.md to find the details.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
hello, @wangnan39 , Has this problem been resolved? If yes, please close this issue, thanks!
你好, @wangnan39 , 这个问题是否已经解决了呢? 如果是的,请关闭这个issue, 谢谢!
hello, @wangnan39 , Has this problem been resolved? If yes, please close this issue, thanks!
你好, @wangnan39 , 这个问题是否已经解决了呢? 如果是的,请关闭这个issue, 谢谢!
hello, @wangnan39 , Has this problem been resolved? If it is in progress, please change the status to WIP. If the issue was solved, please close this issue, thanks!
你好, @wangnan39 , 这个问题是否已经解决了呢? 如果正在进行中,请把issue状态设置为WIP;如果问题已解决,请关闭这个issue, 谢谢!
登录 后才可以发表评论