本文主要爲深度之眼pytorch訓練營二期學習筆記,詳細課程內容移步:深度之眼 https://ai.deepshare.net/index
目錄
1.torch.optim.lr_scheduler.StepLR()
2.torch.optim.lr_scheduler.MultiStepLR()
3.torch.optim.lr_scheduler.ReduceLROnPlateau()
背景知識
學習率是每一次反向傳播過程中梯度更新的步長大小。假如學習率一直不變,如果學習率設置的過大,每一次更新的步長就很大,更新速度雖然很快,不容易收斂到極值點(可能上一次,非常接近極小值了,但是更新的步長太大就‘跳’過去了),或者在極值點來回震盪,無法收斂到極值。但是如果學習率設置的很小,那麼每一次更新只能挪動一點點,則完整的訓練時間就會變得很長。
因此如果能夠我們隨着模型訓練的推進,能夠控制或者調整學習率大小,那模型說要的訓練時間、模型獲得的訓練效果是不是就更好了呢。(前期學習率大,後期學習率小)
舉例如下圖:一開始學習率大,然後到了第50個epoch學習率減小,然後到100再減小,到了150再減小。(在這個過程中,也就是損失函數越靠近極小值的時候,更新的步伐就越小)
Pytorch中的學習率調整
主要的學習率調整策略都是繼承這個類。
1.torch.optim.lr_scheduler.
StepLR()
功能:等間隔調整學習率
重要參數:step_size:每隔多少個調整一次,gamma:調整係數(lr = lr * gamma)
"""
答案來自深度之眼訓練營學習課程
"""
import torch
import torch.optim as optim
import numpy as np
import matplotlib.pyplot as plt
torch.manual_seed(1)
LR = 0.1
iteration = 10
max_epoch = 200
# ------------------------------ fake data and optimizer ------------------------------
weights = torch.randn((1), requires_grad=True)
target = torch.zeros((1))
optimizer = optim.SGD([weights], lr=LR, momentum=0.9)
# ------------------------------ Step LR ------------------------------
#flag = 0
flag = 1
if flag:
scheduler_lr = optim.lr_scheduler.StepLR(optimizer, step_size=50, gamma=0.1) # 設置學習率下降策略
lr_list, epoch_list = list(), list()
for epoch in range(max_epoch):
lr_list.append(scheduler_lr.get_lr())
epoch_list.append(epoch)
for i in range(iteration):
loss = torch.pow((weights - target), 2)
loss.backward()
optimizer.step()
optimizer.zero_grad()
scheduler_lr.step()
plt.plot(epoch_list, lr_list, label="Step LR Scheduler")
plt.xlabel("Epoch")
plt.ylabel("Learning rate")
plt.legend()
plt.show()
2.torch.optim.lr_scheduler.
MultiStepLR()
功能:給定間隔調整學習率
主要參數:
milestones:指定需要調整的epoch位置, gamma:調整係數(lr = lr * gamma)
flag = 1
if flag:
milestones = [50,80,130,150] #指定間隔
scheduler_lr = optim.lr_scheduler.MultiStepLR(optimizer, milestones=milestones, gamma=0.3)
lr_list, epoch_list = list(), list()
for epoch in range(max_epoch):
lr_list.append(scheduler_lr.get_lr())
epoch_list.append(epoch)
for i in range(iteration):
loss = torch.pow((weights - target), 2)
loss.backward()
optimizer.step()
optimizer.zero_grad()
scheduler_lr.step() #調整學習率
plt.plot(epoch_list, lr_list, label="Multi Step LR Scheduler\nmilestones:{}".format(milestones))
plt.xlabel("Epoch")
plt.ylabel("Learning rate")
plt.legend()
plt.show()
3.torch.optim.lr_scheduler.
ReduceLROnPlateau()
Keras中的學習率調整