【转】深度学习3: 梯度检验与高级优化

发布时间:2015-12-29  栏目:深度学习  评论:0 Comments

众所周知,反向传播算法很难调试得到正确结果,尤其是当实现程序存在很多难于发现的bug时。举例来说,索引的缺位错误(off-by-one error)会导致只有部分层的权重得到训练,再比如忘记计算偏置项。这些错误会使你得到一个看似十分合理的结果(但实际上比正确代码的结果要差)。因此,但从计算结果上来看,我们很难发现代码中有什么东西遗漏了。本节中,我们将介绍一种对求导结果进行数值检验的方法,该方法可以验证求导代码是否正确。另外,使用本节所述求导检验方法,可以帮助你提升写正确代码的信心。
缺位错误(Off-by-one error)举例说明:比如 \textstyle for 循环中循环 \textstyle m次,正确应该是 \textstyle for (i=1;~i<=m;~i++),但有时程序员疏忽,会写成 \textstyle for (i=1;~i<m;~i++),这就是缺位错误。
假设我们想要最小化以 \textstyle \theta 为自变量的目标函数\textstyle J(\theta)。假设 \textstyle J : \Re \mapsto \Re,则 \textstyle \theta \in \Re。在一维的情况下,一次迭代的梯度下降公式是

\begin{align} \theta := \theta - \alpha \frac{d}{d\theta}J(\theta). \end{align}

再假设我们已经用代码实现了计算 \textstyle \frac{d}{d\theta}J(\theta) 的函数 \textstyle g(\theta),接着我们使用 \textstyle \theta := \theta - \alpha g(\theta) 来实现梯度下降算法。那么我们如何检验 \textstyle g 的实现是否正确呢?

回忆导数的数学定义:

\begin{align} \frac{d}{d\theta}J(\theta) = \lim_{\epsilon \rightarrow 0} \frac{J(\theta+ \epsilon) - J(\theta-\epsilon)}{2 \epsilon}. \end{align}

那么对于任意 \textstyle \theta 值,我们都可以对等式左边的导数用:

\begin{align} \frac{J(\theta+{\rm EPSILON}) - J(\theta-{\rm EPSILON})}{2 \times {\rm EPSILON}} \end{align}

来近似。
实际应用中,我们常将 \textstyle EPSILON 设为一个很小的常量,比如在\textstyle 10^{-4} 数量级(虽然 \textstyle EPSILON 的取值范围可以很大,但是我们不会将它设得太小,比如 \textstyle 10^{-20},因为那将导致数值舍入误差。)
给定一个被认为能计算 \textstyle \frac{d}{d\theta}J(\theta) 的函数\textstyle g(\theta),我们可以用下面的数值检验公式

\begin{align} g(\theta) \approx \frac{J(\theta+{\rm EPSILON}) - J(\theta-{\rm EPSILON})}{2 \times {\rm EPSILON}}. \end{align}

计算两端是否一样来检验函数是否正确。
上式两端值的接近程度取决于 \textstyle J 的具体形式。但是在假定\textstyle {\rm EPSILON} = 10^{-4} 的情况下,你通常会发现上式左右两端至少有4位有效数字是一样的(通常会更多)。
现在,考虑 \textstyle \theta \in \Re^n 是一个向量而非一个实数(那么就有\textstyle n个参数要学习得到),并且 \textstyle J: \Re^n \mapsto \Re。在神经网络的例子里我们使用 \textstyle J(W,b),可以想象为把参数 \textstyle W,b 组合扩展成一个长向量 \textstyle \theta。现在我们将求导检验方法推广到一般化,即 \textstyle \theta 是一个向量的情况。

假设我们有一个用于计算 \textstyle \frac{\partial}{\partial \theta_i} J(\theta)的函数 \textstyle g_i(\theta);我们想要检验 \textstyle g_i 是否输出正确的求导结果。我们定义 \textstyle \theta^{(i+)} = \theta + {\rm EPSILON} \times \vec{e}_i,其中

\begin{align} \vec{e}_i = \begin{bmatrix}0 \\ 0 \\ \vdots \\ 1 \\ \vdots \\ 0\end{bmatrix} \end{align}

是第 \textstyle i 个基向量(维度和 \textstyle \theta 相同,在第 \textstyle i 行是“\textstyle 1”而其他行是“\textstyle 0”)。所以,\textstyle \theta^{(i+)}\textstyle \theta 几乎相同,除了第 \textstyle i 行元素增加了 \textstyle EPSILON。类似地,\textstyle \theta^{(i-)} = \theta - {\rm EPSILON} \times \vec{e}_i 得到的第 \textstyle i 行减小了 \textstyle EPSILON。然后我们可以对每个 \textstyle i 检查下式是否成立,进而验证 \textstyle g_i(\theta) 的正确性:

\begin{align} g_i(\theta) \approx \frac{J(\theta^{(i+)}) - J(\theta^{(i-)})}{2 \times {\rm EPSILON}}. \end{align}

当用反射传播算法求解神经网络时,正确算法实现会得到:

\begin{align} \nabla_{W^{(l)}} J(W,b) &= \left( \frac{1}{m} \Delta W^{(l)} \right) + \lambda W^{(l)} \\ \nabla_{b^{(l)}} J(W,b) &= \frac{1}{m} \Delta b^{(l)}. \end{align}

以上结果与反向传播算法中的最后一段伪代码一致,都是计算梯度下降。为了验证梯度下降代码的正确性,使用上述数值检验方法计算 \textstyle J(W,b) 的导数,然后验证 \textstyle \left(\frac{1}{m}\Delta W^{(l)} \right) + \lambda W\textstyle \frac{1}{m}\Delta b^{(l)} 是否能够给出正确的求导结果。
迄今为止,我们的讨论都集中在使用梯度下降法来最小化 \textstyle J(\theta)。如果你已经实现了一个计算 \textstyle J(\theta)\textstyle \nabla_\theta J(\theta) 的函数,那么其实还有更精妙的算法来最小化 \textstyle J(\theta)。举例来说,可以想象这样一个算法:它使用梯度下降,并能够自动调整学习速率 \textstyle \alpha,以得到合适的步长值,最终使 \textstyle \theta 能够快速收敛到一个局部最优解。还有更妙的算法:比如可以寻找一个Hessian矩阵的近似,得到最佳步长值,使用该步长值能够更快地收敛到局部最优(和牛顿法类似)。此类算法的详细讨论已超出了这份讲义的范围,但是L-BFGS算法我们以后会有论述(另一个例子是共轭梯度算法)。你将在编程练习里使用这些算法中的一个。使用这些高级优化算法时,你需要提供关键的函数:即对于任一个 \textstyle \theta,需要你计算出 \textstyle J(\theta)\textstyle \nabla_\theta J(\theta)。之后,这些优化算法会自动调整学习速率/步长值 \textstyle \alpha 的大小(并计算Hessian近似矩阵等等)来自动寻找 \textstyle J(\theta) 最小化时\textstyle \theta 的值。诸如L-BFGS和共轭梯度算法通常比梯度下降法快很多。

中英文对照

off-by-one error 缺位错误

bias term 偏置项

numerically checking 数值检验

numerical roundoff errors 数值舍入误差

significant digits 有效数字

unrolling 组合扩展

learning rate 学习速率

Hessian matrix Hessian矩阵

Newton’s method 牛顿法

conjugate gradient 共轭梯度

step-size 步长值

留下评论

You must be logged in to post a comment.

相册集

pix pix pix pix pix pix

关于自己

杨文龙,微软Principal Engineering Manager, 曾在各家公司担任影像技术资深总监、数据科学团队资深经理、ADAS算法总监、资深深度学习工程师等职位,热爱创新发明,专注于人工智能、深度学习、图像处理、机器学习、算法、自然语言处理及软件等领域,目前发明有国际专利19篇,中国专利28篇。

联系我

个人技术笔记

welonshen@gmail.com

2015 in Shanghai