神经网络向量化

From Ufldl

Jump to: navigation, search
Line 36: Line 36:
</syntaxhighlight>  
</syntaxhighlight>  
-
In this implementation, <tt>z2</tt>, <tt>a2</tt>, and <tt>z3</tt> are all matrices, with one column per training example.  A common design pattern in vectorizing across training examples is that whereas previously we had a column vector (such as <tt>z2</tt>) per training example, we can often instead try to compute a matrix so that all of these column vectors are stacked together to form a matrix.  Concretely, in this example, <tt>a2</tt> becomes a <math>s_2</math> by <math>m</math> matrix (where <math>s_2</math> is the number of units in layer 2 of the network, and <math>m</math> is the number of training examples).  And, the <math>i</math>-th column of <tt>a2</tt> contains the activations of the hidden units (layer 2 of the network) when the <math>i</math>-th training example <tt>x(:,i)</tt> is input to the network.
+
在这个实现中,<tt>z2</tt>,<tt>a2</tt>,<tt>z3</tt>都是矩阵,每个训练样本对应矩阵的一列。在对多个训练样本实现向量化时常用的设计模式是,虽然前面每个样本对应一个列向量(比如<tt>z2</tt>),但我们可把这些列向量堆叠成一个矩阵以充分享受矩阵运算带来的好处。这样,在这个例子中,<tt>a2</tt>就成了一个<math>s_2</math>X<math>m</math>的矩阵(<math>s_2</math>是网络第二层中的神经元数,<math>m</math>是训练样本个数)。矩阵<tt>a2</tt>的物理含义是,当第<math>i</math>个训练样本<tt>x(:i)</tt>输入到网络中时,它的第<math>i</math>列就表示这个输入信号对隐神经元 (网络第二层)的激励结果。
-
 
+
在上面的实现中,我们假定激活函数 <tt>f(z)</tt>接受矩阵形式的输入<tt>z</tt>,并对输入矩阵按列分别施以激活函数。需要注意的是,你在实现<tt>f(z)</tt>的时候要尽量多用Matlab/Octave的矩阵操作,并尽量避免使用for循环。假定激活函数采用Sigmoid函数,则实现代码如下所示:
-
'''初译:'''
+
-
在这个实现中,<tt>z2</tt>,<tt>a2</tt>,<tt>z3</tt>都是矩阵,每个训练样本是一个矩阵中的的一列。对于多个训练样本的向量化的一个常用的设计模式是我们对每个训练样本一个列向量(比如<tt>z2</tt>),我们可以转而用矩阵计算从而所有的列向量可以放在一起变成一个矩阵。具体在这个例子中,<tt>a2</tt>变成了一个<math>s_2</math>X<math>m</math>的一个矩阵(<math>s_2</math>是第二层网络中的神经元数,<math>m</math>是训练样本数量)。当第<math>i</math>个训练样本<tt>x(:i)</tt>输入到网络中时,<tt>a2</tt>的第<math>i</math>列包含隐含神经元的激活(网络的第二层)。
+
-
 
+
-
'''一审:'''
+
-
在这个实现中,<tt>z2</tt>,<tt>a2</tt>,<tt>z3</tt>都是矩阵,每个训练样本是一个矩阵中的一列。在对多个训练样本实现向量化时常用的设计模式是,虽然前面每个样本对应一个列向量(比如<tt>z2</tt>),但我们可把这些列向量堆叠成一个矩阵以充分享受矩阵运算带来的好处。这样,在这个例子中,<tt>a2</tt>就成了一个<math>s_2</math>X<math>m</math>的矩阵(<math>s_2</math>是网络第二层中的神经元数,<math>m</math>是训练样本个数)。矩阵<tt>a2</tt>的物理含义是,当第<math>i</math>个训练样本<tt>x(:i)</tt>输入到网络中时,它的第<math>i</math>列就表示这个输入信号对隐神经元 (网络第二层)的激励结果。
+
-
 
+
-
In the implementation above, we have assumed that the activation function <tt>f(z)</tt> takes as input a matrix <tt>z</tt>, and applies the activation function component-wise to the input.  Note that your implementation of <tt>f(z)</tt> should use Matlab/Octave's matrix operations as much as possible, and avoid <tt>for</tt> loops as well.  We illustrate this below, assuming that <tt>f(z)</tt> is the sigmoid activation function:
+
-
 
+
-
 
+
-
'''初译:'''
+
-
在上面的实现中,我们假定激活函数 <tt>f(z)</tt>把一个矩阵<tt>z</tt>作为输入,并且把激活函数应用到输入矩阵的每个组成部分。需要注意的是<tt>f(z)</tt>的实现要尽量多的使用Matlab/Octave的矩阵操作并且避免使用<tt>for</tt>循环。下边我们要进行举例说明,这里假定<tt>f(z)</tt>是S型激活函数:
+
-
 
+
-
'''一审:'''
+
-
在上面的实现中,我们假定激活函数 <tt>f(z)</tt>实现的功能是:它接受一个输入矩阵<tt>z</tt>,然后按列分别施以激活函数(这个激活函数相当于上面用的激活函数--译者注)。需要注意的是,你在实现<tt>f(z)</tt>的时候要尽量多用Matlab/Octave的矩阵操作,并尽量避免使用for循环,不妨设它是Sigmoid函数,则实现代码如下所示:
+
<syntaxhighlight>
<syntaxhighlight>
Line 64: Line 50:
end;
end;
end
end
-
+
 
% 高效的、向量化激活函数实现
% 高效的、向量化激活函数实现
function output = vectorized_f(z)
function output = vectorized_f(z)
Line 71: Line 57:
</syntaxhighlight>
</syntaxhighlight>
-
 
+
最后,我们上面的正向传播向量化实现中忽略了<tt>b1</tt>和<tt>b2</tt>,现在要把他们包含进来,我们需要用到Matlab/Octave的内建函数<tt>repmat</tt>:
-
Finally, our vectorized implementation of forward propagation above had ignored <tt>b1</tt> and <tt>b2</tt>.  To incorporate those back in, we will use Matlab/Octave's built-in <tt>repmat</tt> function.  We have:
+
-
 
+
-
'''初译:'''
+
-
最后,我们前面实现的正向传播的向量化忽略了<tt>b1</tt>和<tt>b2</tt>。为了包含他们进来,我们将使用Matlab/Octave的内建的函数<tt>repmat</tt>:
+
-
 
+
-
'''一审:'''
+
-
最后,我们前面的正向传播向量化实现中忽略了<tt>b1</tt>和<tt>b2</tt>。现在要把他们包含进来,我们将用到Matlab/Octave的内建的函数<tt>repmat</tt>:
+
<syntaxhighlight>
<syntaxhighlight>
Line 88: Line 67:
</syntaxhighlight>
</syntaxhighlight>
-
The result of <tt>repmat(b1,1,m)</tt> is a matrix formed by taking the column vector <tt>b1</tt> and stacking <math>m</math> copies of them in columns as follows
 
-
 
-
'''初译:'''
 
-
<tt>repmat(b1,1,m)</tt>的结果是由<math>m</math>个列向量<tt>b1</tt>做为列组成的一个矩阵如下:
 
-
 
-
'''一审:'''
 
<tt>repmat(b1,1,m)</tt>的运算效果是,它把列向量<tt>b1</tt>拷贝<math>m</math>份,然后堆叠成如下矩阵:
<tt>repmat(b1,1,m)</tt>的运算效果是,它把列向量<tt>b1</tt>拷贝<math>m</math>份,然后堆叠成如下矩阵:

Revision as of 01:20, 16 March 2013

Personal tools