Deep learning-------------Neural networks

I explain it in English.

Neural Networks

Consider a supervised learning problem where we have access to labeled training examples (x(i),y(i)). Neural networks give a way of defining a complex, non-linear form of hypotheses hW,b(x), with parameters W,b that we can fit to our data.

To describe neural networks, we will begin by describing the simplest possible neural network, one which comprises a single "neuron." We will use the following diagram to denote a single neuron:


This "neuron" is a computational unit that takes as input x1,x2,x3 (and a +1 intercept term), and outputs \textstyle h_{W,b}(x) = f(W^Tx) = f(\sum_{i=1}^3 W_{i}x_i +b), where f : \Re \mapsto \Re is called the activation function. In these notes, we will choose f(\cdot) to be the sigmoid function:

f(z) = \frac{1}{1+\exp(-z)}.

Thus, our single neuron corresponds exactly to the input-output mapping defined by logistic regression.

Although these notes will use the sigmoid function, it is worth noting that another common choice for f is the hyperbolic tangent, or tanh, function:

f(z) = \tanh(z) = \frac{e^z - e^{-z}}{e^z + e^{-z}},

Here are plots of the sigmoid and tanh functions:

Sigmoid activation function. Tanh activation function.

The tanh(z) function is a rescaled version of the sigmoid, and its output range is [ − 1,1] instead of [0,1].

Note that unlike some other venues (including the OpenClassroom videos, and parts of CS229), we are not using the convention here of x0 = 1. Instead, the intercept term is handled separately by the parameter b.

Finally, one identity that'll be useful later: If f(z) = 1 / (1 + exp( − z)) is the sigmoid function, then its derivative is given by f'(z) = f(z)(1 − f(z)). (If f is the tanh function, then its derivative is given by f'(z) = 1 − (f(z))2.) You can derive this yourself using the definition of the sigmoid (or tanh) function.

Neural Network model

A neural network is put together by hooking together many of our simple "neurons," so that the output of a neuron can be the input of another. For example, here is a small neural network:


In this figure, we have used circles to also denote the inputs to the network. The circles labeled "+1" are called bias units, and correspond to the intercept term. The leftmost layer of the network is called the input layer, and the rightmost layer the output layer (which, in this example, has only one node). The middle layer of nodes is called thehidden layer, because its values are not observed in the training set. We also say that our example neural network has 3input units (not counting the bias unit), 3 hidden units, and 1 output unit.

We will let nl denote the number of layers in our network; thus nl = 3 in our example. We label layer l as Ll, so layer L1is the input layer, and layer L_{n_l} the output layer. Our neural network has parameters (W,b) = (W(1),b(1),W(2),b(2)), where we write W^{(l)}_{ij} to denote the parameter (or weight) associated with the connection between unit j in layer l, and unit i in layer l + 1. (Note the order of the indices.) Also, b^{(l)}_i is the bias associated with unit i in layer l + 1. Thus, in our example, we have W^{(1)} \in \Re^{3\times 3}, and W^{(2)} \in \Re^{1\times 3}. Note that bias units don't have inputs or connections going into them, since they always output the value +1. We also let sl denote the number of nodes in layer l (not counting the bias unit).

We will write a^{(l)}_i to denote the activation (meaning output value) of unit i in layer l. For l = 1, we also use a^{(1)}_i = x_i to denote the i-th input. Given a fixed setting of the parameters W,b, our neural network defines a hypothesis hW,b(x) that outputs a real number. Specifically, the computation that this neural network represents is given by:

\begin{align}a_1^{(2)} &= f(W_{11}^{(1)}x_1 + W_{12}^{(1)} x_2 + W_{13}^{(1)} x_3 + b_1^{(1)})  \\a_2^{(2)} &= f(W_{21}^{(1)}x_1 + W_{22}^{(1)} x_2 + W_{23}^{(1)} x_3 + b_2^{(1)})  \\a_3^{(2)} &= f(W_{31}^{(1)}x_1 + W_{32}^{(1)} x_2 + W_{33}^{(1)} x_3 + b_3^{(1)})  \\h_{W,b}(x) &= a_1^{(3)} =  f(W_{11}^{(2)}a_1^{(2)} + W_{12}^{(2)} a_2^{(2)} + W_{13}^{(2)} a_3^{(2)} + b_1^{(2)}) \end{align}

In the sequel, we also let z^{(l)}_i denote the total weighted sum of inputs to unit i in layer l, including the bias term (e.g., \textstyle z_i^{(2)} = \sum_{j=1}^n W^{(1)}_{ij} x_j + b^{(1)}_i), so that a^{(l)}_i = f(z^{(l)}_i).

Note that this easily lends itself to a more compact notation. Specifically, if we extend the activation function f(\cdot) to apply to vectors in an element-wise fashion (i.e., f([z1,z2,z3]) = [f(z1),f(z2),f(z3)]), then we can write the equations above more compactly as:

\begin{align}z^{(2)} &= W^{(1)} x + b^{(1)} \\a^{(2)} &= f(z^{(2)}) \\z^{(3)} &= W^{(2)} a^{(2)} + b^{(2)} \\h_{W,b}(x) &= a^{(3)} = f(z^{(3)})\end{align}

We call this step forward propagation. More generally, recalling that we also use a(1) = x to also denote the values from the input layer, then given layer l's activations a(l), we can compute layer l + 1's activations a(l + 1) as:

\begin{align}z^{(l+1)} &= W^{(l)} a^{(l)} + b^{(l)}   \\a^{(l+1)} &= f(z^{(l+1)})\end{align}

By organizing our parameters in matrices and using matrix-vector operations, we can take advantage of fast linear algebra routines to quickly perform calculations in our network.

We have so far focused on one example neural network, but one can also build neural networks with other architectures(meaning patterns of connectivity between neurons), including ones with multiple hidden layers. The most common choice is a\textstyle n_l-layered network where layer \textstyle 1 is the input layer, layer \textstyle n_l is the output layer, and each layer \textstyle l is densely connected to layer \textstyle l+1. In this setting, to compute the output of the network, we can successively compute all the activations in layer\textstyle L_2, then layer \textstyle L_3, and so on, up to layer \textstyle L_{n_l}, using the equations above that describe the forward propagation step. This is one example of a feedforward neural network, since the connectivity graph does not have any directed loops or cycles.

Neural networks can also have multiple output units. For example, here is a network with two hidden layers layers L2 and L3and two output units in layer L4:


To train this network, we would need training examples (x(i),y(i)) where y^{(i)} \in \Re^2. This sort of network is useful if there're multiple outputs that you're interested in predicting. (For example, in a medical diagnosis application, the vector x might give the input features of a patient, and the different outputs yi's might indicate presence or absence of different diseases.)

本节内容: The representation and learning of neural networks

-----------------------The representation of neural networks-------------------------

(一)、为什么引入神经网络?——Nonlinear hypothesis

之前我们讨论的ML问题中,主要针对Regression做了分析,其中采用梯度下降法进行参数更新。然而其可行性基于假设参数不多,如果参数多起来了怎么办呢?比如下图中这个例子:从100*100个pixels中选出所有XiXj作为logistic regression的一个参数,那么总共就有5*10^7个feature,即x有这么多维。

所以引入了Nonlinear hypothesis,应对高维数据和非线性的hypothesis(如下图所示):


(二)、神经元与大脑(neurons and brain)


神经网络的逻辑单元:输入向量x(input layer),中间层a(2,i)(hidden layer), 输出层h(x)(output layer)。

其中,中间层的a(2,i)中的2表示第二个级别(第一个级别是输入层),i表示中间层的第几个元素。或者可以说,a(j,i) is the activation of unit i in layer j.









h(x)= a(3)=g(z(3))


通过以上这种神经元的传递方式(input->activation->output)来计算h(x), 叫做Forward propagation, 向前传递。

这里我们可以发现,其实神经网络就像是logistic regression,只不过我们把logistic regression中的输入向量[x1~x3]变成了中间层的[a(2)1~a(2)3], 即

h(x)=g(Θ(2)0 a(2)0(2)1 a(2)1(2)2 a(2)2(2)3 a(2)3)

而中间层又由真正的输入向量通过Θ(1)学习而来,这里呢,就解放了输入层,换言之输入层可以是original input data的任何线性组合甚至是多项式组合如set x1*x2 as original x1...另外呢,具体怎样利用中间层进行更新下面会更详细地讲;此外,还有一些其他模型,比如:






以上两个例子只是单层传递,下面我们再给出一个更复杂的例子,用来实现逻辑表达< x1 XNOR x2 >, 即逻辑同或关系,它由前面几个例子共同实现:

将AND、NOT AND和 OR分别放在下图中输入层和输出层的位置,即可得到x1 XNOR x2,道理显而易见:

a21 = x1 && x2

a22 = (﹁x1)&&(﹁x2) 

a31 =a2||a21 =(x1 && x2) ||  (﹁x1)&&(﹁x2) = x1 XNOR x2;





输入向量x有三个维度,两个中间层,输出层4个神经元分别用来表示4类,也就是每一个数据在输出层都会出现[a b c d]T,且a,b,c,d中仅有一个为1,表示当前类。




----------------------- The learning of neural networks------------------

(一)、Cost function

(二)、Backpropagation algorithm

(三)、Backpropagation intuition

(四)、Implementation note: Unrolling parameters

(五)、Gradient checking

(六)、Random initialization

(七)、Putting it together


(一)、Cost function



卐二类分类:SL=1, y=0 or 1表示哪一类;

卐K类分类:SL=K, y= 1表示分到第i类;(K>2)

我们在前几章中已经知道,Logistic hypothesis的Cost Function如下定义:

其中,前半部分表示hypothesis与真实值之间的距离,后半部分为对参数进行regularization的bias项,神经网络的cost function同理:

hypothesis与真实值之间的距离为 每个样本-每个类输出 的加和,对参数进行regularization的bias项处理所有参数的平方和


(二)、Backpropagation algorithm

前面我们已经讲了cost function的形式,下面我们需要的就是最小化J(Θ)

想要根据gradient descent的方法进行参数optimization,首先需要得到cost function和一些参数的表示。根据forward propagation,我们首先进行training dataset 在神经网络上的各层输出值:




对于最后一层,我们可以直接算出网络产生的输出与实际值之间的差距,我们将这个差距定义为。对于隐藏单元我们如何处理呢?我们将通过计算各层节点残差的加权平均值计算hidden layer的残差。读者可以自己验证下,其实就是E对b求导的结果。





不断迭代直到落入local optima,就是backpropagation的算法过程。

Example of logistical cost:

下面我们针对logistical cost给出计算的例子:



如果将误差对激励函数(activation function)的导数记做δ,则有:

对于前面一层 ,更新同理,,只是上一层\Theta梯度的第一个分量E对a_k求导有所变化,





从后向前此计算每层δ,用Δ表示全局误差,每一层都对应一个Δ(l)。再引入D作为cost function对参数的求导结果。下图左边j是否等于0影响的是是否有最后的bias regularization项。左边是定义,右边可证明(比较繁琐)。


(三)、Backpropagation intuition


首先根据forward propagation方法从前往后计算z(j),a(j) ;

然后将原cost function 进行简化,去掉下图中后面那项regularization项,


Cost(i)=y(i)log(hθ(x(i)))+(1- y(i))log(1- hθ(x(i)))


其中J就是cost。那么将其进行简化,暂时不考虑g'(zk) = ak(1-ak)的部分,就有:


换句话说, 对于每一层来说,δ分量都等于后面一层所有的δ加权和,其中权值就是参数Θ


(四)、Implementation note: Unrolling parameters

这一节讲述matlab中如何实现unrolling parameter。


function [jVal, gradient] = costFunction(theta)
optTheta = fminunc(@costFunction, initialTheta, options)

与linear regression和logistic regression不同,在神经网络中,参数非常多,每一层j有一个参数向量Θj和Derivative向量Dj。那么我们首先将各层向量连起来,组成大vectorΘ和D,传入function,再在计算中进行下图中的reshape,分别取出进行计算。



(五)、Gradient checking

神经网络中计算起来数字千变万化难以掌握,那我们怎么知道它里头工作的对不对呢?不怕,我们有法宝,就是gradient checking,通过check梯度判断我们的code有没有问题,ok?怎么做呢,看下边:




Summary: 有以下几点需要注意

-在back propagation中计算出J(θ)对θ的导数D,并组成vector(Dvec)

-用numerical gradient check方法计算大概的梯度gradApprox=(J(Θ+ε)-J(Θ-ε))/(2ε)


-(这一点非常重要)停止check,只用back propagation 来进行神经网络学习(否则会非常慢,相当慢)


(六)、Random Initialization


this means all of your hidden units are computing all of the exact same function of the input. So this is a highly redundant representation. 因为一层内的所有计算都可以归结为1个,而这使得一些interesting的东西被ignore了。



(七)、Putting it together

1. 选择神经网络结构

我们有很多choices of network :


No. of input units: Dimension of features
No. output units: Number of classes
Reasonable default: 1 hidden layer, or if >1 hidden layer, have same no. of hidden units in every layer (usually the more the better)

2. 神经网络的训练

① Randomly initialize weights
② Implement forward propagation to get hθ(x(i)) for anyx(i)
③ Implement code to compute cost function J(θ)
④ Implement backprop to compute partial derivatives

个人分类: Deep Learning
想对作者说点什么? 我来说一句



Deep learning-------------Neural networks