天天看點

pytorch動态調整學習率之epoch_step政策

上篇文章中講到《pytorch動态調整學習率之Poly政策》,本次講解另一個較為常用的政策,即訓練過程中随着epoch的增加根據設定的 e p o c h _ s t e p epoch\_step epoch_step對學習率進行衰減操作,具體公式如下所示:

l r = b a s e _ l r × 0. 1 e p o c h i n t ( n u m _ e p o c h / e p o c h _ s t e p ) lr = base\_lr \times {0.1^{\frac{{epoch}}{{{\mathop{\rm int}} (num\_epoch/epoch\_step)}}}} lr=base_lr×0.1int(num_epoch/epoch_step)epoch​

其中, l r lr lr為新的學習率, b a s e _ l r base\_lr base_lr為基準學習率, e p o c h epoch epoch為疊代次數, n u m _ e p o c h num\_epoch num_epoch為最大疊代次數, e p o c h _ s t e p epoch\_step epoch_step控制衰減的速率,其中 i n t ( ) int() int()為取整運算。

代碼具體如下所示:

def adjust_learning_rate_epoch_step(optimizer, epoch, num_epochs, base_lr, epoch_step)
    lr = base_lr * 0.1**(epoch/int(num_epochs/epoch_step))
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr
    return lr
           

以下為學習率随 e p o c h _ s t e p epoch\_step epoch_step變化而變化的曲線,假設 b a s e _ l r base\_lr base_lr=0.005, n u m _ e p o c h num\_epoch num_epoch=100。

pytorch動态調整學習率之epoch_step政策

繼續閱讀