Jeremy Howard 和他在 USF 数据研究所的团队开发了 fast.ai。这是一个基于 PyTorch 的高级抽象的深度学习库。fast.ai 是一个简单而强大的工具集,可以用于训练最先进的深度学习模型。Jeremy 在他最新的深度学习课程()中使用了这个库。
fast.ai 提供了学习率搜索器的一个实现。你只需要写几行代码就能绘制模型的损失函数-学习率的图像(来自 GitHub:plot_loss.py):
# learn is an instance of Learnerclass or one of derived classes like ConvLearner
learn.lr_find()
learn.sched.plot_lr()
库中并没有提供代码绘制损失函数变化率的图像,但计算起来非常简单(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)
请注意:只在训练之前选择一次学习率是不够的。训练过程中,最优学习率会随着时间推移而下降。你可以定期重新运行相同的学习率搜索程序,以便在训练的稍后时间查找学习率。
使用其他库实现本方案
我还没有准备好将这种学习率搜索方法应用到诸如 Keras 等其他库中,但这应该不是什么难事。只需要做到:
多次运行训练,每次只训练一个小批量;
在每次分批训练之后通过乘以一个小的常数的方式增加学习率;
当损失函数值高于先前观察到的最佳值时,停止程序。(例如,可以将终止条件设置为「当前损失 > *4 最佳损失」)
学习计划
选择学习率的初始值只是问题的一部分。另一个需要优化的是学习计划(learning schedule):如何在训练过程中改变学习率。传统的观点是,随着时间推移学习率要越来越低,而且有许多方法进行设置:例如损失函数停止改善时逐步进行学习率退火、指数学习率衰退、余弦退火等。
我上面引用的论文描述了一种循环改变学习率的新方法,它能提升卷积神经网络在各种图像分类任务上的性能表现。
评论