梯度检验与高级优化
From Ufldl
Line 26: | Line 26: | ||
:【二审】: | :【二审】: | ||
众所周知,反向传播算法(我之前都译为“传导”,大家基本都译为“传播”就先传播)是很难调试成功的,特别是,代码中存在很多易于出错的细微之处――比如说,索引的缺位错误(off-by-one error,二审注:如果仅是为了翻译,只要选个合适的词翻译就可,但为了让读者理解,我觉得有必要加上具体说明),因此可能就只求解了一部分层中的权重参数;或者代码中疏略了偏置项――学习的结果很可能是即让人吃惊又觉得很可信(事实是比正确的代码差多了)。因此,即使这代码是错误的,我们也很难轻易发现有什么东西遗漏了。本节中,我们将讨论一种对导数(导数的运算由代码实现)进行数值检测的方法,以确定你的代码是否正确。使用本节所述导数检测的方法,将非常有助于提升对你所写代码正确性的信心。 | 众所周知,反向传播算法(我之前都译为“传导”,大家基本都译为“传播”就先传播)是很难调试成功的,特别是,代码中存在很多易于出错的细微之处――比如说,索引的缺位错误(off-by-one error,二审注:如果仅是为了翻译,只要选个合适的词翻译就可,但为了让读者理解,我觉得有必要加上具体说明),因此可能就只求解了一部分层中的权重参数;或者代码中疏略了偏置项――学习的结果很可能是即让人吃惊又觉得很可信(事实是比正确的代码差多了)。因此,即使这代码是错误的,我们也很难轻易发现有什么东西遗漏了。本节中,我们将讨论一种对导数(导数的运算由代码实现)进行数值检测的方法,以确定你的代码是否正确。使用本节所述导数检测的方法,将非常有助于提升对你所写代码正确性的信心。 | ||
+ | |||
缺位错误(Off-by-one error)说明:指的是,比如for 循环中循环m次,则应该是for(i=1; i<=m ;i++),但有时程序员疏忽,会写成for(i=1;i<m;i++),这就是缺位错误。 | 缺位错误(Off-by-one error)说明:指的是,比如for 循环中循环m次,则应该是for(i=1; i<=m ;i++),但有时程序员疏忽,会写成for(i=1;i<m;i++),这就是缺位错误。 | ||
Line 167: | Line 168: | ||
checking procedure to the case where <math>\textstyle \theta</math> may be a vector. | checking procedure to the case where <math>\textstyle \theta</math> may be a vector. | ||
:【初译】: | :【初译】: | ||
+ | 现在,考虑<math>\textstyle \theta \in \Re^n</math>是一个向量而非单个实数(我们有<math>\textstyle n</math>个参数要学习),并且<math>\textstyle J: \Re^n \mapsto \Re</math>。在神经网络的例子里我们使用<math>\textstyle J(W,b)</math>,可以想象把参数<math>\textstyle W,b</math>扩展到一个长向量<math>\textstyle \theta</math>。现在我们将求导检验方法推广到一般化,即<math>\textstyle \theta</math>可能是一个向量的情况。 | ||
:【一审】: | :【一审】: | ||
+ | 现在,考虑<math>\textstyle \theta \in \Re^n</math>是一个向量而非单个实数(因为我们有<math>\textstyle n</math>个参数要学习),并且<math>\textstyle J: \Re^n \mapsto \Re</math>。在神经网络的例子里我们使用<math>\textstyle J(W,b)</math>,可以想象把参数<math>\textstyle W,b</math>扩展到一个长向量<math>\textstyle \theta</math>。现在我们将求导检验方法推广到一般化,即<math>\textstyle \theta</math>可能是一个向量的情况。 | ||
:【二审】: | :【二审】: | ||
+ | 现在,考虑<math>\textstyle \theta \in \Re^n</math>是一个向量而非单个实数(那么我们就要求解<math>\textstyle n</math>个参数),并且<math>\textstyle J: \Re^n \mapsto \Re</math>。在神经网络的例子里我们使用<math>\textstyle J(W,b)</math>,可以想象把参数<math>\textstyle W,b</math>扩展到一个长向量<math>\textstyle \theta</math>。现在我们将求导检验方法推广到一般化,即<math>\textstyle \theta</math>可能是一个向量的情况。 | ||
Line 193: | Line 197: | ||
\end{align}</math> | \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>{\rm EPSILON}</math>减小了第<math>\textstyle i</math>位的相应向量是<math>\textstyle \theta^{(i-)} = \theta - {\rm EPSILON} \times \vec{e}_i</math>。现在我们可以通过对于每个<math>\textstyle i</math>检查 | ||
+ | :<math>\begin{align} | ||
+ | g_i(\theta) \approx | ||
+ | \frac{J(\theta^{(i+)}) - J(\theta^{(i-)})}{2 \times {\rm EPSILON}}. | ||
+ | \end{align}</math> | ||
+ | 是否成立来数学上验证<math>\textstyle g_i(\theta)</math>的正确性。 | ||
:【一审】: | :【一审】: | ||
:【二审】: | :【二审】: |