Jeremy Howard 和他在 USF 數(shù)據(jù)研究所的團隊開發(fā)了 fast.ai。這是一個基于 PyTorch 的高級抽象的深度學習庫。fast.ai 是一個簡單而強大的工具集,可以用于訓練最先進的深度學習模型。Jeremy 在他最新的深度學習課程()中使用了這個庫。
fast.ai 提供了學習率搜索器的一個實現(xiàn)。你只需要寫幾行代碼就能繪制模型的損失函數(shù)-學習率的圖像(來自 GitHub:plot_loss.py):
# learn is an instance of Learnerclass or one of derived classes like ConvLearner
learn.lr_find()
learn.sched.plot_lr()
庫中并沒有提供代碼繪制損失函數(shù)變化率的圖像,但計算起來非常簡單(plot_change_loss.py):
def plot_loss_change(sched, sma=1, n_skip=20, y_lim=(-0.01,0.01)):
"""
Plots rate of change of the loss function.
sched - learning rate scheduler, an instance of LR_Finder class.
sma - number of batches for simple moving average to smooth out the curve.
n_skip - number of batches to skip on the left.
y_lim - limits for the y axis.
"""
derivatives = [0] * (sma + 1)
for i in range(1 + sma, len(learn.sched.lrs)):
derivative = (learn.sched.losses[i] - learn.sched.losses[i - sma]) / sma
derivatives.append(derivative)
plt.ylabel("d/loss")
plt.xlabel("learning rate (log scale)")
plt.plot(learn.sched.lrs[n_skip:], derivatives[n_skip:])
plt.xscale('log')
plt.ylim(y_lim)
plot_loss_change(learn.sched, sma=20)
請注意:只在訓練之前選擇一次學習率是不夠的。訓練過程中,最優(yōu)學習率會隨著時間推移而下降。你可以定期重新運行相同的學習率搜索程序,以便在訓練的稍后時間查找學習率。
使用其他庫實現(xiàn)本方案
我還沒有準備好將這種學習率搜索方法應用到諸如 Keras 等其他庫中,但這應該不是什么難事。只需要做到:
多次運行訓練,每次只訓練一個小批量;
在每次分批訓練之后通過乘以一個小的常數(shù)的方式增加學習率;
當損失函數(shù)值高于先前觀察到的最佳值時,停止程序。(例如,可以將終止條件設置為「當前損失 > *4 最佳損失」)
學習計劃
選擇學習率的初始值只是問題的一部分。另一個需要優(yōu)化的是學習計劃(learning schedule):如何在訓練過程中改變學習率。傳統(tǒng)的觀點是,隨著時間推移學習率要越來越低,而且有許多方法進行設置:例如損失函數(shù)停止改善時逐步進行學習率退火、指數(shù)學習率衰退、余弦退火等。
我上面引用的論文描述了一種循環(huán)改變學習率的新方法,它能提升卷積神經網絡在各種圖像分類任務上的性能表現(xiàn)。?
評論