學(xué)習(xí)率
學(xué)習(xí)速率(learning rate)是指導(dǎo)我們?cè)撊绾瓮ㄟ^損失函數(shù)的梯度調(diào)整網(wǎng)絡(luò)權(quán)重的超參數(shù)坊萝。學(xué)習(xí)率越低宋下,損失函數(shù)的變化速度就越慢咙好。雖然使用低學(xué)習(xí)率可以確保我們不會(huì)錯(cuò)過任何局部極小值造挽,但也意味著我們將花費(fèi)更長(zhǎng)的時(shí)間來進(jìn)行收斂碱璃,特別是在被困在高原區(qū)域的情況下。
new_weight = existing_weight — learning_rate * gradient
image.png
圖1采用較小的學(xué)習(xí)率饭入,梯度下降的速度慢嵌器;
圖2采用較大的學(xué)習(xí)率,梯度下降太快越過了最小值點(diǎn)圣拄,導(dǎo)致不收斂嘴秸,甚至震蕩毁欣。
image.png
目的
- 了解pytorch中學(xué)習(xí)率調(diào)整的方法
測(cè)試環(huán)境
- windows 10
- Ananconda 3, python3.7
- pytorch 1.0
- pycharm
實(shí)驗(yàn)/測(cè)試
pytorch中相關(guān)的API
關(guān)于學(xué)習(xí)率調(diào)整庇谆,pytorch提供了torch.optim.lr_scheduler
image.png
主要提供了幾個(gè)類:
torch.optim.lr_scheduler.LambdaLr
torch.optim.lr_scheduler.StepLR
torch.optim.lr_scheduler.MultiStepLR
torch.optim.lr_scheduler.ExponentialLR
torch.optim.lr_sheduler.CosineAnneaingLR
torch.optim.lr_scheduler.ReduceLROnPlateau
1. torch.optim.lr_scheduler.StepLR
- 代碼
import torch
import torch.optim as optim
from torch.optim import lr_scheduler
from torchvision.models import AlexNet
import matplotlib.pyplot as plt
model = AlexNet(num_classes=2)
optimizer = optim.SGD(params=model.parameters(), lr=0.05)
# lr_scheduler.StepLR()
# Assuming optimizer uses lr = 0.05 for all groups
# lr = 0.05 if epoch < 30
# lr = 0.005 if 30 <= epoch < 60
# lr = 0.0005 if 60 <= epoch < 90
scheduler = lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1)
plt.figure()
x = list(range(100))
y = []
for epoch in range(100):
scheduler.step()
lr = scheduler.get_lr()
print(epoch, scheduler.get_lr()[0])
y.append(scheduler.get_lr()[0])
plt.plot(x, y)
image.png
0<epoch<30, lr = 0.05
30<=epoch<60, lr = 0.005
60<=epoch<90, lr = 0.0005
torch.optim.lr_scheduler.MultiStepLR
與StepLR
相比,MultiStepLR
可以設(shè)置指定的區(qū)間
- 代碼
# ---------------------------------------------------------------
# 可以指定區(qū)間
# lr_scheduler.MultiStepLR()
# Assuming optimizer uses lr = 0.05 for all groups
# lr = 0.05 if epoch < 30
# lr = 0.005 if 30 <= epoch < 80
# lr = 0.0005 if epoch >= 80
print()
plt.figure()
y.clear()
scheduler = lr_scheduler.MultiStepLR(optimizer, [30, 80], 0.1)
for epoch in range(100):
scheduler.step()
print(epoch, 'lr={:.6f}'.format(scheduler.get_lr()[0]))
y.append(scheduler.get_lr()[0])
plt.plot(x, y)
plt.show()
image.png
torch.optim.lr_scheduler.ExponentialLR
指數(shù)衰減
- 代碼
scheduler = lr_scheduler.ExponentialLR(optimizer, gamma=0.9)
print()
plt.figure()
y.clear()
for epoch in range(100):
scheduler.step()
print(epoch, 'lr={:.6f}'.format(scheduler.get_lr()[0]))
y.append(scheduler.get_lr()[0])
plt.plot(x, y)
plt.show()
image.png
End
參考:
https://pytorch.org/docs/master/optim.html#how-to-adjust-learning-rate