梯度检验与高级优化

From Ufldl

Jump to: navigation, search
Line 209: Line 209:
是否成立来数学上验证<math>\textstyle g_i(\theta)</math>的正确性。
是否成立来数学上验证<math>\textstyle g_i(\theta)</math>的正确性。
:【一审】:
:【一审】:
 +
假设我们有一个用于计算<math>\textstyle \frac{\partial}{\partial \theta_i} J(\theta)</math>的函数<math>\textstyle g_i(\theta)</math>;我们想要检验<math>\textstyle g_i</math>是否输出正确的求导结果。定义<math>\textstyle \theta^{(i+)} = \theta +
 +
{\rm EPSILON} \times \vec{e}_i</math>,其中
 +
:<math>\begin{align}
 +
\vec{e}_i = \begin{bmatrix}0 \\ 0 \\ \vdots \\ 1 \\ \vdots \\ 0\end{bmatrix}
 +
\end{align}</math>
 +
是第<math>\textstyle i</math>个基向量(维度和<math>\textstyle \theta</math>相同,在第<math>\textstyle i</math>行是“1”而其他行是“0”)。所以,除非第<math>\textstyle i</math>行元素增加了<math>{\rm EPSILON}</math>,否则<math>\textstyle \theta^{(i+)}</math>和<math>\textstyle \theta</math>相同。类似地,第<math>\textstyle i</math>行减小了<math>{\rm EPSILON}</math>的相应向量是<math>\textstyle \theta^{(i-)} = \theta - {\rm EPSILON} \times \vec{e}_i</math>。现在我们可以通过对于每个<math>\textstyle i</math>检查下式的数学计算结果是否成立来验证<math>\textstyle g_i(\theta)</math>的正确性:
 +
:<math>\begin{align}
 +
g_i(\theta) \approx
 +
\frac{J(\theta^{(i+)}) - J(\theta^{(i-)})}{2 \times {\rm EPSILON}}.
 +
\end{align}</math>
:【二审】:
:【二审】:
-
 
+
假设我们有一个用于计算<math>\textstyle \frac{\partial}{\partial \theta_i} J(\theta)</math>的函数<math>\textstyle g_i(\theta)</math>;我们想要检验<math>\textstyle g_i</math>是否输出正确的求导结果。定义<math>\textstyle \theta^{(i+)} = \theta +
 +
{\rm EPSILON} \times \vec{e}_i</math>,其中
 +
:<math>\begin{align}
 +
\vec{e}_i = \begin{bmatrix}0 \\ 0 \\ \vdots \\ 1 \\ \vdots \\ 0\end{bmatrix}
 +
\end{align}</math>
 +
是第<math>\textstyle i</math>个基向量(维度和<math>\textstyle \theta</math>相同,在第<math>\textstyle i</math>行是“1”而其他行是“0”)。所以,<math>\textstyle \theta^{(i+)}</math>和<math>\textstyle \theta</math>几乎相同,除了第<math>\textstyle i</math>行元素增加了<math>{\rm EPSILON}</math>。类似地,第<math>\textstyle i</math>行减小了<math>{\rm EPSILON}</math>的相应向量是<math>\textstyle \theta^{(i-)} = \theta - {\rm EPSILON} \times \vec{e}_i</math>。现在我们可以通过对于每个<math>\textstyle i</math>检查下式的数学计算结果是否成立来验证<math>\textstyle g_i(\theta)</math>的正确性:
 +
:<math>\begin{align}
 +
g_i(\theta) \approx
 +
\frac{J(\theta^{(i+)}) - J(\theta^{(i-)})}{2 \times {\rm EPSILON}}.
 +
\end{align}</math>
:【原文】:
:【原文】:
Line 220: Line 239:
\end{align}</math>
\end{align}</math>
:【初译】:
:【初译】:
 +
在一个正确通过反向传播算法训练神经网络的实现中,我们将得到:
 +
:<math>\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}</math>
:【一审】:
:【一审】:
 +
在一个正确通过反向传播算法训练神经网络的实现中,我们将得到:
 +
:<math>\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}</math>
:【二审】:
:【二审】:
 +
当用反射传播算法求解神经网络时,正确的计算方法应该是:
 +
:<math>\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}</math>
:【原文】:
:【原文】:
Line 232: Line 266:
indeed giving the derivatives you want.
indeed giving the derivatives you want.
:【初译】:
:【初译】:
 +
这个结果说明反向传播算法的伪代码最后一块的确实现了梯度下降。为了检验你的梯度下降实现的正确性,通过使用上述方法会带来很大帮助,即计算<math>\textstyle J(W,b)</math>的导数从而验证你对<math>\textstyle \left(\frac{1}{m}\Delta W^{(l)} \right) + \lambda W</math>和<math>\textstyle \frac{1}{m}\Delta b^{(l)}</math>的计算确实给出了你要的求导结果。
:【一审】:
:【一审】:
 +
这个结果说明反向传播算法的伪代码实际上是在最后的代码段实现了梯度下降。为了检验你的梯度下降实现的正确性,通过使用上述方法会带来很大帮助,即计算<math>\textstyle J(W,b)</math>的近似导数从而验证你对<math>\textstyle \left(\frac{1}{m}\Delta W^{(l)} \right) + \lambda W</math>和<math>\textstyle \frac{1}{m}\Delta b^{(l)}</math>的计算确实给出了你要的求导结果。
:【二审】:
:【二审】:
 +
以上结果表明,在反向传播算法一课中,最后一段伪代码的确执行了梯度下降。为验证梯度下降代码的正确性,使用以上所述方法计算<math>\textstyle J(W,b)</math>的导数是非常有用的,从而确认<math>\textstyle \left(\frac{1}{m}\Delta W^{(l)} \right) + \lambda W</math>与<math>\textstyle \frac{1}{m}\Delta b^{(l)}</math>确实是你想要的导数。
:【原文】:
:【原文】:
Line 253: Line 290:
such as L-BFGS and conjugate gradient can often be much faster than gradient descent.
such as L-BFGS and conjugate gradient can often be much faster than gradient descent.
:【初译】:
:【初译】:
 +
最后,迄今为止我们的讨论都集中在使用梯度下降来最小化<math>\textstyle J(\theta)</math>。如果你已经实现了一个函数来计算<math>\textstyle J(\theta)</math>和<math>\textstyle \nabla_\theta J(\theta)</math>,会发现其实有更复杂的算法来尝试最小化<math>\textstyle J(\theta)</math>。举例来说,可以想象这样的一个算法——使用梯度下降,但使之自动调整学习率<math>\textstyle \alpha</math>以致尝试一步步地让<math>\textstyle \theta</math>尽快到达一个局部最优。还有其他算法比这更复杂;比如寻找一个Hessian矩阵的近似,以便它能以更快的步伐到达一个局部最优(和牛顿方法类似)。此类算法的详细讨论超出了这份讲义的范围,但是一个例子是L-BFGS算法(另一个例子是共轭梯度算法)。你将在编程联系里使用这些算法中的一个。对于任意一个<math>\textstyle \theta</math>,你需要提供给这些高级优化算法的东西主要是<math>\textstyle J(\theta)</math>和<math>\textstyle \nabla_\theta J(\theta)</math>。然后这些优化算法会内部调整学习率/步伐大小<math>\textstyle \alpha</math>(和对Hessian的近似等等)来自动寻找一个最小化<math>\textstyle J(\theta)</math>的<math>\textstyle \theta</math>值。诸如L-BFGS和共轭梯度的算法通常比梯度下降更快。
:【一审】:
:【一审】:
 +
最后,迄今为止我们的讨论都集中在使用梯度下降来最小化<math>\textstyle J(\theta)</math>。如果你已经实现了一个函数来计算<math>\textstyle J(\theta)</math>和<math>\textstyle \nabla_\theta J(\theta)</math>,会发现其实有更复杂的算法来尝试最小化<math>\textstyle J(\theta)</math>。举例来说,可以想象这样的一个算法:它使用梯度下降,但可以自动调整学习率<math>\textstyle \alpha</math>,以便尝试使用新的步长值,使<math>\textstyle \theta</math>尽快到达一个局部最优。还有其他算法比这更复杂;比如寻找一个Hessian矩阵的近似,以便它能以更快的步伐到达一个局部最优(和牛顿方法类似)。此类算法的详细讨论超出了这份讲义的范围,但是一个例子是L-BFGS算法(另一个例子是共轭梯度算法)。你将在编程练习里使用这些算法中的一个。对于任意一个<math>\textstyle \theta</math>,你需要提供给这些高级优化算法的东西主要是<math>\textstyle J(\theta)</math>和<math>\textstyle \nabla_\theta J(\theta)</math>。然后这些优化算法会内部调整学习率/步伐大小<math>\textstyle \alpha</math>(来计算它自己的近似Hessian矩阵等等)来自动寻找一个最小化<math>\textstyle J(\theta)</math>的<math>\textstyle \theta</math>值。诸如L-BFGS和共轭梯度的算法通常比梯度下降快很多。
:【二审】:
:【二审】:
-
 
+
最后,迄今为止我们的讨论都集中在使用梯度下降来最小化<math>\textstyle J(\theta)</math>。如果你已经实现了一个函数来计算<math>\textstyle J(\theta)</math>和<math>\textstyle \nabla_\theta J(\theta)</math>,会发现其实有更绝妙的算法来尝试最小化<math>\textstyle J(\theta)</math>。举例来说,可以想象这样的一个算法:它使用梯度下降,但可以自动调整学习率<math>\textstyle \alpha</math>,以便尝试使用新的步长值,使<math>\textstyle \theta</math>尽快到达一个局部最优。还有其他算法比这更妙;比如寻找一个Hessian矩阵的近似,以便它能以更快的步伐到达一个局部最优(和牛顿方法类似)。此类算法的详细讨论超出了这份讲义的范围,但是L-BFGS算法我们以后会有论述(另一个例子是共轭梯度算法)。你将在编程练习里使用这些算法中的一个。最关键的一点是,对于这些更高级更优化的算法,对于任一个<math>\textstyle \theta</math>,你都能计算出<math>\textstyle J(\theta)</math>和<math>\textstyle \nabla_\theta J(\theta)</math>的最小值,然后这些优化算法会通过内部调整学习率/步长 <math>\textstyle \alpha</math>的大小(来计算它自己的近似Hessian矩阵等等)来自动寻找一个最小化<math>\textstyle J(\theta)</math>的<math>\textstyle \theta</math>值。诸如L-BFGS和共轭梯度的算法通常比梯度下降快很多。
{{Sparse_Autoencoder}}
{{Sparse_Autoencoder}}

Revision as of 11:53, 9 March 2013

Personal tools