torch.optim.lr_scheduler.
MultiStepLR
(optimizer, milestones, gamma=0.1, last_epoch=-1, verbose=False)import torch
import torchvision
learing_rate = 0.1
model = torchvision.models.resnet18()
optimizer = torch.optim.SGD(model.parameters(), lr=learing_rate,
momentum=0.9,
weight_decay=5e-5)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[3, 6], gamma=0.1)
for epoch in range(9):
optimizer.step()
scheduler.step()
# print(optimizer.get_lr())
print(epoch, scheduler.get_lr())
返回:
0 [0.1]
1 [0.1]
2 [0.0010000000000000002] # 此处乘的是gamma的平方
3 [0.010000000000000002]
4 [0.010000000000000002]
5 [0.00010000000000000003] # 此处乘的是gamma的平方
6 [0.0010000000000000002]
7 [0.0010000000000000002]
8 [0.0010000000000000002]
和前面1、是一样的,因为函数的默认值就是last_epoch=-1
import torch
import torchvision
learing_rate = 0.1
model = torchvision.models.resnet18()
optimizer = torch.optim.SGD(model.parameters(), lr=learing_rate,
momentum=0.9,
weight_decay=5e-5)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[3, 6], gamma=0.1)
scheduler.last_epoch = -1
for epoch in range(9):
optimizer.step()
scheduler.step()
# print(optimizer.get_lr())
print(epoch, scheduler.get_lr())
返回:
0 [0.1]
1 [0.1]
2 [0.1]
3 [0.0010000000000000002]
4 [0.010000000000000002]
5 [0.010000000000000002]
6 [0.00010000000000000003]
7 [0.0010000000000000002]
8 [0.0010000000000000002]
把第1个epoch的learning_rate设置为0.1,但是按照模型已经更新到了第4个epoch开始执行。
后来理解了一下,感觉就是:在last_epoch处将learning_rate重新设置为初始值,而且也是从last_epoch处继续进行运行;所以就要求你手动把learning_rate设为上一次模型停止的时候对应的learning_rate值,即last_epoch处对应的learning_rate。
import torch
import torchvision
learing_rate = 0.1
model = torchvision.models.resnet18()
optimizer = torch.optim.SGD(model.parameters(), lr=learing_rate,
momentum=0.9,
weight_decay=5e-5)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[3, 6], gamma=0.1)
scheduler.last_epoch = 4
for epoch in range(9):
optimizer.step()
scheduler.step()
# print(optimizer.get_lr())
print(epoch, scheduler.get_lr())
返回:
0 [0.1] # 0相当于第4个eopch
1 [0.0010000000000000002] # 1相当于第5个epoch所以乘以gamma的平方
2 [0.010000000000000002]
3 [0.010000000000000002]
4 [0.010000000000000002]
5 [0.010000000000000002]
6 [0.010000000000000002]
7 [0.010000000000000002]
8 [0.010000000000000002] # 因为4在3的后面,只有一个6这个milestones,所以只更新了一次
import torch
import torchvision
learing_rate = 0.1
model = torchvision.models.resnet18()
optimizer = torch.optim.SGD(model.parameters(), lr=learing_rate,
momentum=0.9,
weight_decay=5e-5)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[3, 6], gamma=0.1)
scheduler.last_epoch = 4
for epoch in range(9):
optimizer.step()
scheduler.step(epoch)
# print(optimizer.get_lr())
print(epoch, scheduler.get_lr())
返回:
0 [0.0010000000000000002]
1 [0.0010000000000000002]
2 [0.0010000000000000002]
3 [0.00010000000000000003]
4 [0.0010000000000000002]
5 [0.0010000000000000002]
6 [0.00010000000000000003]
7 [0.0010000000000000002]
8 [0.0010000000000000002]
import torch
import torchvision
learing_rate = 0.1
model = torchvision.models.resnet18()
optimizer = torch.optim.SGD(model.parameters(), lr=learing_rate,
momentum=0.9,
weight_decay=5e-5)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[5, 8], gamma=0.1)
scheduler.last_epoch = 4
for epoch in range(9):
optimizer.step()
# scheduler.step(epoch)
scheduler.step()
# print(optimizer.get_lr())
print(epoch, scheduler.get_lr())
返回:
0 [0.0010000000000000002]
1 [0.010000000000000002]
2 [0.010000000000000002]
3 [0.00010000000000000003]
4 [0.0010000000000000002]
5 [0.0010000000000000002]
6 [0.0010000000000000002]
7 [0.0010000000000000002]
8 [0.0010000000000000002]
get_last_lr
():Return the last computed learning rate by current scheduler.(获取scheduler计算的最后学习速率)
print_lr
(is_verbose, group, lr, epoch=None):# 这个怎么用,还没空研究Display the current learning rate.(返回当前的学习率)
看来,现在没有get_lr()这个函数了
import torch
import torchvision
learing_rate = 0.1
model = torchvision.models.resnet18()
optimizer = torch.optim.SGD(model.parameters(), lr=learing_rate,
momentum=0.9,
weight_decay=5e-5)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[3, 6], gamma=0.1)
for epoch in range(9):
optimizer.step()
scheduler.step()
# print(optimizer.get_lr())
print(epoch, scheduler.get_last_lr())
返回:
0 [0.1]
1 [0.1]
2 [0.010000000000000002]
3 [0.010000000000000002]
4 [0.010000000000000002]
5 [0.0010000000000000002]
6 [0.0010000000000000002]
7 [0.0010000000000000002]
8 [0.0010000000000000002]
可以看到:没有“乘以gamma平方”这个问题了。
ps: 是评论区的 angleboy8 朋友发现的。