当前位置:网站首页>pytorch余弦退火学习率CosineAnnealingLR的使用
pytorch余弦退火学习率CosineAnnealingLR的使用
2022-08-05 08:45:00 【冬日and暖阳】
一、背景
再次使用CosineAnnealingLR的时候出现了一点疑惑,这里记录一下,其使用方法和参数含义
后面的代码基于 pytorch 版本 1.1, 不同版本可能代码略有差距,但是含义是差不多的
二、余弦退火的目的和用法
2.1 为啥用cosineAnnealingLR策略
原因:因为懒… 这样就不用像使用其他类似于StepLR策略 进行调参了,而且总会取得不错的结果。
余弦函数如下(两个)
考虑cosine函数的四分之一个周期,如下图所示
我们希望学习率能像四分之一个cosine的周期
一样下降:所以有了cosineAnnealingLR
学习率的策略。如果想每个batch 更新学习率,则
torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max, eta_min=0, last_epoch=- 1, verbose=False
``
这里面主要就介绍一下参数T_max
,这个参数指的是cosine 函数 经过多少次更新完成四分之一个周期。
2.2 如果 希望 learning rate 每个epoch更新一次
import numpy as np
import torch
from torchvision import models
import matplotlib.pyplot as plt
net = models.resnet18(pretrained=False)
max_epoch=50 # 一共50 epoch
iters=200 # 每个epoch 有 200 个 bach
optimizer = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer = optimizer,
T_max = max_epoch) # * iters
lr = []
for epoch in range(max_epoch):
for batch in range(iters):
optimizer.step()
lr.append(scheduler.get_lr()[0])
scheduler.step() # 注意 每个epoch 结束, 更新learning rate
plt.plot(np.arange(len(lr)), lr)
plt.savefig('aa.jpg')
每个epoch内,learning rate 是一样的
2.3 每个batch 迭代都改变学习率
max_epoch=50 # 一共50 epoch
iters=200 # 每个epoch 有 200 个 bach
optimizer = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer = optimizer,
T_max = max_epoch * iters ) # 调整了四分之一周期的长度
lr = []
for epoch in range(max_epoch):
for batch in range(iters):
optimizer.step()
lr.append(scheduler.get_lr()[0])
scheduler.step() # 注意 每个batch 结束, 更新learning rate
每个batch都改变学习率
边栏推荐
- egg框架
- thinkPHP5 实现点击量(数据自增/自减)
- 数据源对象管理Druid和c3p0
- Moonbeam团队发布针对整数截断漏洞的紧急安全修复
- Data source object management Druid and c3p0
- 基因数据平台
- Iptables implementation under the network limited (NTP) synchronization time custom port
- 学习笔记14--机器学习在局部路径规划中的应用
- How to replace colors in ps, self-study ps software photoshop2022, replace one color of a picture in ps with another color
- 原型&原型链
猜你喜欢
How to make a puzzle in PS, self-study PS software photoshop2022, PS make a puzzle effect
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(下)
XCODE12 在使用模拟器(SIMULATOR)时编译错误的解决方法
【结构体内功修炼】结构体实现位段(二)
苹果官网商店新上架Mophie系列Powerstation Pro、GaN充电头等产品
嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)
Dynamic memory development (C language)
[Structural Internal Power Cultivation] The Mystery of Enumeration and Union (3)
工程制图知识点
Spark cluster deployment (third bullet)
随机推荐
行走社会100绝招
手机上流行的各类谜语
How to make a puzzle in PS, self-study PS software photoshop2022, PS make a puzzle effect
XSS靶机通关以及XSS介绍
CVPR 2022 | 将X光图片用于垃圾分割,港中大(深圳)探索大规模智能垃圾分类
spark集群部署(第三弹)
php向mysql写入数据失败
ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色
嵌入式实操----基于RT1170 移植memtester做SDRAM测试(二十五)
Long-term recruitment embedded development-Shenzhen Baoan
宝塔实测-搭建中小型民宿酒店管理源码
Data source object management Druid and c3p0
The Secrets of the Six-Year Team Leader | The Eight Most Important Soft Skills of Programmers
Comprehensively explain what is the essential difference between GET and POST requests?Turns out I always misunderstood
mySQL数据库初始化失败,有谁可以指导一下吗
What is the connection and difference between software system testing and acceptance testing? Professional software testing solution recommendation
DPU — 功能特性 — 网络系统的硬件卸载
Constellation ideal lover
全面讲解GET 和 POST请求的本质区别是什么?原来我一直理解错了
最 Cool 的 Kubernetes 网络方案 Cilium 入门教程