神经网络向量化

From Ufldl

Jump to: navigation, search
Line 97: Line 97:
</math>
</math>
-
在这里 <math>\bullet</math>表示对两个向量按对应元素相乘的运算(译者注:其结果还是一个向量)。为了描述简单起见,我们这里暂时忽略对参数<math>b^{(l)}</math>.的求导, 不过在你真正实现反向传播时,还是需要计算关于它们的导数的。
+
在这里<math>\bullet</math>表示对两个向量按对应元素相乘的运算(译者注:其结果还是一个向量)。为了描述简单起见,我们这里暂时忽略对参数<math>b^{(l)}</math>的求导, 不过在你真正实现反向传播时,还是需要计算关于它们的导数的。
-
假定我们已经实现了向量化的正向传播方法,如前面9-5那样计算了矩阵形式的变量<tt>z2</tt>, <tt>a2</tt>, <tt>z3</tt>和<tt>h</tt>,那么反向传播的非向量化版本可如下实现:
+
假定我们已经实现了向量化的正向传播方法,如前面9-5那样计算了矩阵形式的变量<tt>z2</tt>, <tt>a2</tt>, <tt>z3</tt>和<tt>h</tt>,那么反向传播的非向量化版本可如下实现:
<syntaxhighlight>
<syntaxhighlight>
Line 115: Line 115:
在这个实现中,有一个<tt>for</tt>循环,而我们想要一个能同时处理所有样本、且去除这个<tt>for</tt>循环的向量化版本。
在这个实现中,有一个<tt>for</tt>循环,而我们想要一个能同时处理所有样本、且去除这个<tt>for</tt>循环的向量化版本。
-
To do so, we will replace the vectors <tt>delta3</tt> and <tt>delta2</tt> with matrices, where one column of each matrix corresponds to each training example.  We will also implement a function <tt>fprime(z)</tt> that takes as input a matrix <tt>z</tt>, and applies <math>f'(\cdot)</math> element-wise.  Each of the four lines of Matlab in the <tt>for</tt> loop above can then be vectorized and replaced with a single line of Matlab code (without a surrounding <tt>for</tt> loop).
+
为做到这一点,我们先把向量<tt>delta3</tt><tt>delta2</tt>替换为矩阵,其中每列对应一个训练样本。我们还要实现一个函数<tt>fprime(z)</tt>,该函数接受矩阵形式的输入<tt>z</tt>,并且对矩阵的每列分别执行<math>f'(\cdot)</math>。这样,上面<tt>for</tt>循环中的4行Matlab代码中每行都可单独向量化,以一行新的(向量化的)Matlab代码替换它(不再需要外层的<tt>for</tt>循环)。
-
'''初译:'''
+
在向量化练习([[Exercise:Vectorization|Vectorization exercise]])中,我们要求你自己去推导出这个算法的向量化版本。如果你已经能从上面的描述中了解如何去做,那么我们强烈建议你去实践一下。虽然我们已经为你准备了反向传播的向量化实现提示([[Backpropagation vectorization hints]]),但还是鼓励你在不看提示的情况下自己去推导一下。
-
为了实现它,我们将要把向量<tt>delta3</tt>和<tt>delta2</tt>替换为矩阵,矩阵中的每一列代表一个训练样本。我们将要实现一个方法<tt>fprime(z)</tt>,这个方法把矩阵<tt>z</tt>作为输入,并且对矩阵元素执行 。在上边的实现中的<tt>for</tt>循环中的4行中每一行都可以被向量化并且替换为1行Matlab代码(不需要被一个<tt>for</tt>循环所包围)
+
-
 
+
-
'''一审:'''
+
-
为做到这一点,我们先把向量<tt>delta3</tt>和<tt>delta2</tt>替换为矩阵,其中每列代表一个训练样本。我们还要实现一个函数<tt>fprime(z)</tt>,该函数接受矩阵形式的输入<tt>z</tt>,并且按其矩阵元素分别执行 。这样,上面<tt>for</tt>循环中的4行Matlab代码中每行都可单独向量化,而分别替之以一行新的(向量化的)Matlab代码(不再需要外层的<tt>for</tt>循环).
+
-
 
+
-
In the [[Exercise:Vectorization|Vectorization exercise]], we ask you to derive the vectorized version of this algorithm by yourself.  If you are able to do it from this description, we strongly encourage you to do so.  Here also are some [[Backpropagation vectorization hints]]; however, we encourage you to try to carry out the vectorization yourself without looking at the hints.
+
-
 
+
-
'''初译:'''
+
-
在[[向量化练习]]中,我们要求你自己去推导出这个算法的向量化版本。如果你能够按照描述去实现,我们强烈你去亲手实现它。我们给出了[[反向传导向量化提示]],但是我们鼓励你自己去导出向量化而不是求助于提示。
+
-
 
+
-
'''一审:'''
+
-
在[[向量化练习]]中,我们要求你自己去导出这个算法的向量化版本。如果你已经能从上面的描述中了解如何去做,那么我们强烈建议你去亲手实现它。虽然我们已经为你准备了[[反向传导的向量化实现提示]],但还是鼓励你自己去导出它,而不是求助于提示。
+
-
 
+
-
== Sparse autoencoder ==
+
-
 
+
-
The [[Autoencoders_and_Sparsity|sparse autoencoder]] neural network has an additional sparsity penalty that constrains neurons' average firing rate to be close to some target activation <math>\rho</math>.  When performing backpropagation on a single training example, we had taken into the account the sparsity penalty by computing the following:
+
==稀疏自编码网络==
==稀疏自编码网络==
-
'''初译:'''
+
[[自编码算法与稀疏性|稀疏自编码]]网络中包含一个额外的稀疏惩罚项,目的是限制神经元的平均激活率,使其接近某个(预设的)目标激活率<math>\rho</math>。 其实在对单个训练样本上执行反向传播时,我们已经考虑了如何计算这个稀疏惩罚项,如下所示:
-
[[稀疏自动编码]]神经网络有一个额外的稀疏惩罚去限制神经元的平均激活率使它接近目标激活<math>\rho</math>。 当在一个单一训练样本上执行反向传导时,我们在下边的算法中考虑了稀疏惩罚:
+
-
 
+
-
'''一审:'''
+
-
[[稀疏自编码]]网络中包含一个额外的稀疏惩罚项,目的是限制神经元的平均激活率,使其接近某个(预设的)目标激活率<math>\rho</math>。 其实在对单个训练样本上执行反向传导时,我们已经考虑了如何计算这个稀疏惩罚项,如下所示:
+
:<math>\begin{align}
:<math>\begin{align}
Line 149: Line 129:
\end{align}</math>
\end{align}</math>
-
In the ''unvectorized'' case, this was computed as:
 
-
 
-
'''初译:'''
 
-
在非向量化的实现中,计算代码如下:
 
-
 
-
'''一审:'''
 
在非向量化的实现中,计算代码如下:
在非向量化的实现中,计算代码如下:
<syntaxhighlight>
<syntaxhighlight>
-
% Sparsity Penalty Delta 稀疏惩罚Delta
+
% 稀疏惩罚Delta
sparsity_delta = - rho ./ rho_hat + (1 - rho) ./ (1 - rho_hat);
sparsity_delta = - rho ./ rho_hat + (1 - rho) ./ (1 - rho_hat);
for i=1:m,
for i=1:m,
Line 167: Line 141:
</syntaxhighlight>
</syntaxhighlight>
-
The code above still had a <tt>for</tt> loop over the training set, and <tt>delta2</tt> was a column vector.
+
但在上面的代码中,仍旧含有一个需要在整个训练集上运行的<tt>for</tt>循环,这里<tt>delta2</tt>是一个列向量。
-
 
+
-
'''初译:'''
+
-
在上边的代码中仍旧包含对训练集的<tt>for</tt>循环,以及一个列向量<tt>delta2</tt>。
+
-
 
+
-
'''一审:'''
+
-
但在上边的代码中,仍旧含有一个需要在整个训练集上运行的<tt>for</tt>循环,这里<tt>delta2</tt>是一个列向量。
+
-
 
+
-
In contrast, recall that in the vectorized case, <tt>delta2</tt> is now a matrix with <math>m</math> columns corresponding to the <math>m</math> training examples.  Now, notice that the <tt>sparsity_delta</tt> term is the same regardless of what training example we are processing.  This suggests that vectorizing the computation above can be done by simply adding the same value to each column when constructing the <tt>delta2</tt> matrix. Thus, to vectorize the above computation, we can simply add <tt>sparsity_delta</tt> (e.g., using <tt>repmat</tt>) to each column of <tt>delta2</tt>.
+
-
 
+
-
'''初译:'''做为对照,回想一下我们在向量化的示例中,对于<tt>m</tt>个训练样本,<tt>delta2</tt>变成了一个有<math>m</math>列的矩阵。在这里,<tt>Sparsity_delta</tt>项和我们正在处理的所有训练样本是一样的。这意味着上面的向量化计算可以通过对<tt>delta2</tt>矩阵的每一列添加一个相同的值来实现。因此对上面的向量化计算,我们可以简单的把<tt>Sparsity_delta</tt>增加到<tt>delta2</tt>的每一列上(使用<tt>repmat</tt>)。
+
-
'''一审:'''
+
作为对照,回想一下在向量化的情况下,<tt>delta2</tt>现在应该是一个有m列的矩阵,分别对应着<math>m</math>个训练样本。还要注意,稀疏惩罚项<tt>sparsity_delta</tt>对所有的训练样本一视同仁。这意味着要向量化实现上面的计算,只需在构造<tt>delta2</tt>时,往矩阵的每一列上分别加上相同的值即可。因此,要向量化上面的代码,我们只需简单的用<tt>repmat</tt>命令把<tt>sparsity_delta</tt>加到<tt>delta2</tt>的每一列上即可(译者注:这里原文描述得不是很清楚, 看似应加到上面代码中<tt>delta2</tt>行等号右边第一项,即<tt>W2'*delta3</tt>上)。
-
作为对照,回想一下在向量化的情况下, <tt>delta2</tt>现在应该是一个有m列的矩阵,分别对应着<math>m</math>个训练样本。还要注意, <tt>Sparsity_delta</tt>稀疏惩罚项对所有的训练样本一视同仁。这意味着要向量化实现上面的计算,只需在构造<tt>delta2</tt>时,往矩阵的每一列上分别加上相同的值即可。因此,要向量化上面的代码,我们只需简单的用<tt>repmat</tt>命令把<tt>sparsity_delta</tt>加到<tt>delta2</tt>的每一列上即可(这里原文描述得不是很清楚, 看似应加到上面代码中<tt>delta2</tt>行等号右边第一项,即<tt>W2'*delta3</tt>上—译者注)。
+

Revision as of 13:26, 16 March 2013

Personal tools