当前位置:网站首页>深度学习学习记录-优化器的学习率的更新
深度学习学习记录-优化器的学习率的更新
2022-07-23 13:43:00 【七月的和弦】
摘要
以前优化器的学习率总是设置为恒定不变的,现在想让优化器学习率随着epoch进行改变。本文使用Torch框架和Paddle框架对优化器的学习率进行更新。
1. Torch优化器的学习率更新
建立一个简单的模型
import torch
model = torch.nn.Conv2d(3,3,1)
optim = torch.optim.Adam(model.parameters(),lr=1e-2)
optim.param_groups[0]["lr"] # 可以看到当前优化器的学习率

对模型设置Adam优化器,默认学习率为1e-2。现在想让学习率在训练的时候,进行改变。那么可以借助,torch.optim.lr_scheduler来实现
# 对上边的优化器创建优化策略,每2个epoch进行更新一次,缩放因子为0.1
scheduler = torch.optim.lr_scheduler.StepLR(optim, step_size=2, gamma=0.1, last_epoch=-1)
for epoch in range(10):
scheduler.step(epoch)
print("Epoch : {}/{} | lr : {}".format(epoch+1,10,optim.param_groups[0]["lr"]))

可以观察到,每2个epoch,学习率按缩放因子进行一次改变。
2. Paddle优化器学习率更新
目标是实现与Torch相同的功能
import paddle
paddle.set_device("cpu")
model = paddle.nn.Conv2D(3,3,1)
# paddle要先定义一个可变化的学习率,传给优化器。与torch不太一样
scheduler = paddle.optimizer.lr.StepDecay(learning_rate=1e-2,step_size=2,gamma=0.2)
optim = paddle.optimizer.Adam(learning_rate=scheduler,parameters=model.parameters())
print(optim.get_lr())
for epoch in range(10):
scheduler.step(epoch)
print("Epoch : {}/{} | lr : {:.8f}".format(epoch+1,10,optim.get_lr()))

参考资料
边栏推荐
- 低代码搭建校园信息化管理系统案例分析
- IE盒模型和标准盒模型
- 微信小程序wx.hideLoading()会关闭toast提示框
- Surface family purchase reference
- Taishan Office Technology Lecture: layout drawing analysis of paragraph borders
- 48:第五章:开发admin管理服务:1:创建子工程【imooc-news-dev-service-admin】,管理服务模块;
- opencv之打开摄像头、边缘检测
- YOLOV7
- Is it safe for online account managers to open accounts when choosing securities companies in flush
- 系统内存介绍和内存管理
猜你喜欢

灰色关联分析(MATLAB)

Heartless sword English Chinese bilingual poem 006. to my wife

Using "soup.h1.text" crawler to extract the title will be one more\

JMeter之函数二次开发/插件开发(细版)

Priyanka Sharma, general manager of CNCF Foundation: read CNCF operation mechanism

Case analysis of building campus information management system with low code

Acquisition of positional reliability in accurate target detection

Wechat applet wx.hideloading() will close the toast prompt box

系统内存介绍和内存管理

距离IoU损失:包围盒回归更快更好的学习(Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression)
随机推荐
【Flutter -- 布局】弹性布局(Flex 和 Expanded)
JMeter之函数二次开发/插件开发(细版)
已解决(selenium 操作火狐Firefox浏览器报错)AttributeError: ‘WebDriver’ object has no attribute ‘execute_cdp_cmd’
Frequently asked questions about MySQL
【Web漏洞探索】SQL注入漏洞
Weisfeiler-Lehman图同构测试及其他
Direct exchange
【Flutter -- 布局】线性布局(Row 和 Column)
怎么正确设置路由器
IE盒模型和标准盒模型
ROS2自学笔记:Rviz可视化工具
ROS2自学笔记:RQT可视化工具
Heartless sword English Chinese bilingual poem 006. to my wife
uni-app进阶之认证【day12】
Eureka notes
零基础怎么自学软件测试?十年测试老鸟最强软件测试学习路线图
Lake Shore—EMPX-H2 型低温探针台
一款非常棒的开源微社区轻论坛类源码
Visual analysis of real-time epidemic data
tensorflow一层神经网络训练手写体数字识别