在炼丹的过程中,学习率的调整是必不可少的,下面给出scheduler模块的调学习率的方法,后面会慢慢补充
调整学习率:PyTorch官方文档
一、CyclicLR
torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr, max_lr, step_size_up=2000, step_size_down=None)
- optimizer: wrapped optimizer
- base_lr: 学习率的最低值
- max_lr:学习率的最大值
- step_size_up: lr从最小值到最大值所需的步骤(即半个循环)
- step_size_down: lr从最大值到最小值所需的步骤(即半个循环)
二、CosineAnnealingLR
torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max, eta_min=0, last_epoch=-1, verbose=False)
- optimizer: wrapped optimizer
- T_max: 最大迭代次数(就是学习率降到最小值的迭代次数)
- eta_min:学习率的最小值,默认为0
- last_epoch:
- verbose:如果为True,每次更新lr的时候都会打印出来
学习率会随着epoch成周期性的变化,例如最大迭代次数为100,一共有300个epoch,那么前100个epoch学习率从0.0009降到0,然后100-200个周期从0又升到0.0009,200-300个epoch从0.0009降到0
更新公式:
η t = η m i n + 1 2 ( η m a x − η m i n ) ( 1 + c o s ( T c u r T m a x π ) ) \eta_{t} = \eta_{min} + \frac{1}{2}(\eta_{max}-\eta_{min})(1+cos(\frac{T_{cur}}{T_{max}}\pi)) ηt=ηmin+21(ηmax−ηmin)(1+cos(TmaxTcurπ))
import torch.optim as optim
from torch.optim import lr_scheduler
from torchvision.models import AlexNet
import matplotlib.pyplot as plt
def plot(lr_list):
f = plt.figure()
plt.plot(lr_list)
plt.show()
epochs = 50
model = AlexNet()
optimizer = optim.Adam(model.parameters(), lr=1e-3, weight_decay=1e-5)
scheduler = lr_scheduler.CosineAnnealingLR(optimizer, epochs, eta_min=1e-4, last_epoch=-1)
# scheduler = lr_scheduler.CosineAnnealingLR(optimizer, epochs - 10, eta_min=1e-4, last_epoch=-1)
# this zero gradient update is needed to avoid a warning message, issue #8.
optimizer.zero_grad()
lr_list = list()
for epoch in range(epochs*3):
optimizer.step()
scheduler.step()
print('{} - {}'.format(epoch, scheduler.get_last_lr()))
lr_list.append(scheduler.get_last_lr()[0])
plot(lr_list)