From: https://liudongdong1.github.io/
a. 有序調整:等間隔調整(Step),按需調整學習率(MultiStep),指數衰減調整(Exponential)和 余弦退火CosineAnnealing,
b. 自適應調整:自適應調整學習率 ReduceLROnPlateau,
c. 自定義調整:自定義調整學習率 LambdaLR,
#得到當前學習率
lr = next(iter(optimizer.param_groups))['lr']
#multiple learning rates for different layers.
all_lr = []
for param_group in optimizer.param_groups:
all_lr.append(param_group['lr'])
#學習率衰減
#Reduce learning rate when validation accuarcy plateau.
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', patience=5, verbose=True)
for t in range(0, 80):
train(...); val(...)
scheduler.step(val_acc)
#Cosine annealing learning rate.
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=80)
#Reduce learning rate by 10 at given epochs.
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=[50, 70], gamma=0.1)
for t in range(0, 80):
scheduler.step()
train(...); val(...)
#Learning rate warmup by 10 epochs.
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda t: t / 10)
for t in range(0, 10):
scheduler.step()
train(...); val(...)
1. 針對不同的層
model = torchvision.models.resnet101(pretrained=True)
large_lr_layers = list(map(id,model.fc.parameters()))
small_lr_layers = filter(lambda p:id(p) not in large_lr_layers,model.parameters())
optimizer = torch.optim.SGD([
{"params":large_lr_layers},
{"params":small_lr_layers,"lr":1e-4}
],lr = 1e-2,momenum=0.9)
2. 等間隔調整學習率 StepLR
torch.optim.lr_scheduler.StepLR(optimizer, step_size, gamma=0.1, last_epoch=-1)
step_size(int)- 學習率下降間隔數,若為 30,則會在 30、 60、 90…個 step 時,將學習率調整為lr*gamma,- gamma(float)- 學習率調整倍數,默認為 0.1 倍,即下降 10 倍,
- last_epoch(int)- 上一個 epoch 數,這個變數用來
指示學習率是否需要調整,當last_epoch 符合設定的間隔時,就會對學習率進行調整,當為-1 時,學習率設定為初始值,調整倍數為 gamma 倍,調整間隔為 step_size,間隔單位是step,需要注意的是, step 通常是指 epoch,不要弄成 iteration 了,
3. 按需調整學習率 MultiStepLR
torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones, gamma=0.1, last_epoch=-1)
- milestones(list)- 一個 list,
每一個元素代表何時調整學習率,list 元素必須是遞增的,如 milestones=[30,80,120]- gamma(float)- 學習率調整倍數,默認為 0.1 倍,即下降 10 倍,
按設定的間隔調整學習率,這個方法適合后期除錯使用,觀察 loss 曲線,為每個實驗定制學習率調整時機,
4. 指數衰減調整學習率 ExponentialLR
torch.optim.lr_scheduler.ExponentialLR(optimizer, gamma, last_epoch=-1)
gamma- 學習率調整倍數的底,指數為 epoch,即 gamma**epoch
5. 余弦退火調整學習率 CosineAnnealingLR
torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max, eta_min=0, last_epoch=-1)
- T_max(int)- 一次學習率周期的迭代次數,即
T_max 個 epoch 之后重新設定學習率,- eta_min(float)- 最小學習率,即在一個周期中,
學習率最小會下降到 eta_min,默認值為 0,以余弦函式為周期,并在每個周期最大值時重新設定學習率,以初始學習率為最大學習率,以 2 ? T m a x 2*Tmax2?Tmax 為周期,在一個周期內先下降,后上升,
epochs = 60
optimizer = optim.SGD(model.parameters(),lr = config.lr,momentum=0.9,weight_decay=1e-4)
scheduler = lr_scheduler.CosineAnnealingLR(optimizer,T_max = (epochs // 9) + 1)
for epoch in range(epochs):
scheduler.step(epoch)
6. 自適應調整學習率 ReduceLROnPlateau
torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=10, verbose=False, threshold=0.0001, threshold_mode='rel', cooldown=0, min_lr=0, eps=1e-08)
- mode(str)- 模式選擇,有 min 和 max 兩種模式,
min 表示當指標不再降低(如監測loss),max 表示當指標不再升高(如監測 accuracy),- factor(float)- 學習率調整倍數(等同于其它方法的 gamma),即學習率更新為
lr = lr * factor- patience(int)-
忍受該指標多少個 step 不變化,當忍無可忍時,調整學習率,- verbose(bool)-
是否列印學習率資訊, print(‘Epoch {:5d}: reducing learning rate of group {} to {:.4e}.’.format(epoch, i, new_lr))- threshold_mode(str)- 選擇判斷指標是否達最優的模式,有兩種模式, rel 和 abs,
當 threshold_mode == rel,并且 mode == max 時, dynamic_threshold = best * ( 1 +threshold );
當 threshold_mode == rel,并且 mode == min 時, dynamic_threshold = best * ( 1 -threshold );
當 threshold_mode == abs,并且 mode== max 時, dynamic_threshold = best + threshold ;
當 threshold_mode == rel,并且 mode == max 時, dynamic_threshold = best - threshold;- threshold(float)- 配合 threshold_mode 使用,
cooldown(int)- “冷卻時間“,當調整學習率之后,讓學習率調整策略冷靜一下,讓模型再訓練一段時間,再重啟監測模式,- min_lr(float or list)-
學習率下限,可為 float,或者 list,當有多個引陣列時,可用 list 進行設定,- eps(float)-
學習率衰減的最小值,當學習率變化小于 eps 時,則不調整學習率,
optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)scheduler = ReduceLROnPlateau(optimizer, 'max',verbose=1,patience=3)for epoch in range(10): train(...) val_acc = validate(...) # 降低學習率需要在給出 val_acc 之后 scheduler.step(val_acc)
7. 自定義調整學習率 LambdaLR

torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda, last_epoch=-1)
- lr_lambda(function or list)- 一個
計算學習率調整倍數的函式,輸入通常為 step,當有多個引陣列時,設為 list,
8. 手動設定
def adjust_learning_rate(optimizer, lr): for param_group in optimizer.param_groups: param_group['lr'] = lrfor epoch in range(60): lr = 30e-5 if epoch > 25: lr = 15e-5 if epoch > 30: lr = 7.5e-5 if epoch > 35: lr = 3e-5 if epoch > 40: lr = 1e-5 adjust_learning_rate(optimizer, lr)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/296290.html
標籤:其他
上一篇:一句話告訴別人什么是CNN
