Jeremy Howard 和他在 USF 數(shù)據(jù)研究所的團(tuán)隊(duì)開發(fā)了 fast.ai。這是一個(gè)基于 PyTorch 的高級(jí)抽象的深度學(xué)習(xí)庫(kù)。fast.ai 是一個(gè)簡(jiǎn)單而強(qiáng)大的工具集,可以用于訓(xùn)練最先進(jìn)的深度學(xué)習(xí)模型。Jeremy 在他最新的深度學(xué)習(xí)課程()中使用了這個(gè)庫(kù)。
fast.ai 提供了學(xué)習(xí)率搜索器的一個(gè)實(shí)現(xiàn)。你只需要寫幾行代碼就能繪制模型的損失函數(shù)-學(xué)習(xí)率的圖像(來自 GitHub:plot_loss.py):
# learn is an instance of Learnerclass or one of derived classes like ConvLearner
learn.lr_find()
learn.sched.plot_lr()
庫(kù)中并沒有提供代碼繪制損失函數(shù)變化率的圖像,但計(jì)算起來非常簡(jiǎn)單(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)
請(qǐng)注意:只在訓(xùn)練之前選擇一次學(xué)習(xí)率是不夠的。訓(xùn)練過程中,最優(yōu)學(xué)習(xí)率會(huì)隨著時(shí)間推移而下降。你可以定期重新運(yùn)行相同的學(xué)習(xí)率搜索程序,以便在訓(xùn)練的稍后時(shí)間查找學(xué)習(xí)率。
使用其他庫(kù)實(shí)現(xiàn)本方案
我還沒有準(zhǔn)備好將這種學(xué)習(xí)率搜索方法應(yīng)用到諸如 Keras 等其他庫(kù)中,但這應(yīng)該不是什么難事。只需要做到:
多次運(yùn)行訓(xùn)練,每次只訓(xùn)練一個(gè)小批量;
在每次分批訓(xùn)練之后通過乘以一個(gè)小的常數(shù)的方式增加學(xué)習(xí)率;
當(dāng)損失函數(shù)值高于先前觀察到的最佳值時(shí),停止程序。(例如,可以將終止條件設(shè)置為「當(dāng)前損失 > *4 最佳損失」)
學(xué)習(xí)計(jì)劃
選擇學(xué)習(xí)率的初始值只是問題的一部分。另一個(gè)需要優(yōu)化的是學(xué)習(xí)計(jì)劃(learning schedule):如何在訓(xùn)練過程中改變學(xué)習(xí)率。傳統(tǒng)的觀點(diǎn)是,隨著時(shí)間推移學(xué)習(xí)率要越來越低,而且有許多方法進(jìn)行設(shè)置:例如損失函數(shù)停止改善時(shí)逐步進(jìn)行學(xué)習(xí)率退火、指數(shù)學(xué)習(xí)率衰退、余弦退火等。
我上面引用的論文描述了一種循環(huán)改變學(xué)習(xí)率的新方法,它能提升卷積神經(jīng)網(wǎng)絡(luò)在各種圖像分類任務(wù)上的性能表現(xiàn)。?
評(píng)論