PyTorch常用学习率调整策略

神经网络在进行参数优化的过程中,经常需要对学习率进行动态调整。那么PyTorch的torch.optim.lr_scheduler接口提供了很多策略实现动态调整。我们选取一些常用的进行介绍。

1. torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda, last_epoch=- 1, verbose=False)

Sets the learning rate of each parameter group to the initial lr times a given function.

这个策略也比较简单,就是每次调用scheduler.step()之后,新的lr会乘以lr_lambda的结果。举例如下,其中network可以换成我们要优化的模型:

optim = torch.optim.Adam(network.parameters(), lr=0.001)
lf = lambda x: ((1 + math.cos(x * math.pi / num_epochs)) / 2) * (1 - 0.2) + 0.2
scheduler = torch.optim.lr_scheduler.LambdaLR(optim, lr_lambda=lf)
for epoch in range(10):
    optim.step()
    scheduler.step()
    print(scheduler.get_last_lr())

输出结果如下:
[0.0009804226065180616]
[0.0009236067977499791]
[0.0008351141009169894]
[0.0007236067977499789]
[0.0006000000000000001]
[0.00047639320225002107]
[0.00036488589908301085]
[0.0002763932022500211]
[0.00021957739348193862]
[0.0002]
可以看到每次调用step之后,lr = lr * lr_lambda(epoch)。

2.torch.optim.lr_scheduler.MultiplicativeLR(optimizer, lr_lambda, last_epoch=- 1, verbose=False)

Multiply the learning rate of each parameter group by the factor given in the specified fu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值