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

503人阅读 评论(0)

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:

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 of neural networks-------------------------

（一）、为什么引入神经网络？——Nonlinear hypothesis

===============================

（二）、神经元与大脑（neurons and brain）

===============================

（三）、神经网络的表示形式

z(2)(1)x

a(2)=g(z(2))

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

z(3)(2)a(2)

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

===============================

（四）、怎样用神经网络实现逻辑表达式？

a21 = x1 && x2

a22 = （﹁x1）&&（﹁x2）

a31 =a2||a21 =(x1 && x2) ||  （﹁x1）&&（﹁x2） = x1 XNOR x2；

===============================

（五）、分类问题（Classification）

===============================

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

（一）、Cost function

（二）、Backpropagation algorithm

（三）、Backpropagation intuition

（四）、Implementation note: Unrolling parameters

（六）、Random initialization

（七）、Putting it together

===============================

（一）、Cost function

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

===============================

（二）、Backpropagation algorithm

$E = \frac{1}{2}\sum_{i}{(y_i-a_i)^2}$

$\Delta W \propto -\frac{\partial E}{\partial W}$

$\Theta_{ij}^{(l)} = \Theta_{ij}^{(l)}+\Delta\Theta_{ij}^{(l)}=\Theta_{ij}^{(l)}-\alpha \frac{\partial E(\Theta)}{\partial \Theta_{ij}^{(l)}}$

$\delta_{i}^{(l)} = \frac{\partial E}{\partial z_i^{(l)}}=\sum_{j}^{N^{(l+1)}} \frac{\partial E}{\partial z_j^{(l+1)}} \cdot \frac{\partial z_j^{(l+1)}}{\partial z_i^{(l)}}\\ = \sum_{j}^{N^{(l+1)}} \delta_{j}^{(l+1)}\cdot \frac{\partial [\sum_{k}^{N^{l}}\Theta_{jk}\cdot g(z_k^{(l)})] }{\partial z_i^{(l)}},i\in k\\ =\sum_{j}^{N^{(l+1)}} (\delta_{j}^{(l+1)}\cdot \Theta_{ji}) \cdot g{'}(z_i)$

$\fn_cm \frac{\partial E}{\partial \Theta_{ji}^{l}} = \frac{\partial E}{\partial z_i^{(l+1)}}\cdot \frac {\partial z_i^{(l+1)}}{\partial \Theta_{ji}^{l}} =\delta_i^{(l+1)}\cdot a_j^{(l)}$

$\fn_cm \Theta_{ji}^{l} = \Theta_{ji}^{l}-\alpha\cdot \delta_i^{(l+1)}\cdot a_j^{(l)}$

============================================================
Example of logistical cost:

$\Delta \Theta_{k-1} = \frac{\partial E}{\partial a_k}\cdot \frac{\partial a_k}{\partial z_k} \cdot \frac{\partial z_k}{\Theta _{k-1}}$

$\frac{\partial E}{\partial a_k} = a_k-y \\ \frac{\partial a_k}{\partial z_k} = \frac{\partial g(z_k))}{\partial z_k} = \frac{e^{-z}}{(1+e^{-z})^2} = a_k(1-a_k)\\ \frac{\partial z_k}{\partial \Theta _{k-1}} = a_{k-1}$

$\Delta\Theta_{k} = \xi (y-a_k)a_k(1-a_k)a_{k-1}$

$\delta_{k} = (y-a_k)a_k(1-a_k)$

$\Delta\Theta_k = \xi \delta_k \cdot a_{k-1}$

\begin{align*} \frac{\partial E}{\partial a_{j}}=\sum_{k} \frac{\partial E}{\partial a_k}\cdot \frac{\partial a_k}{\partial z_k}\cdot \frac{\partial z_k}{\partial a_{j}}\\ = \sum_{k}(y-a_k)\cdot a_k(1-a_k)\cdot \Theta_j \end{align*}

ps：最后一步之所以写+=而非直接赋值是把Δ看做了一个矩阵，每次在相应位置上做修改。

===============================

（三）、Backpropagation intuition

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

$\delta_k = \frac{\partial J(\Theta)}{\partial z_k} = \frac{\partial J(\Theta)}{\partial a_k}\frac{\partial a_k}{\partial z_k} = \Theta_{k}\delta_{k+1}\cdot g'(z_k) \\ \Delta w_{ij} = \Delta w_{ij} + \frac{\partial J(\Theta)}{\partial w_{ij}} = \Delta w_{ij} + a_j^l \cdot \delta_k^(l+1)\\ \frac{\partial J(\Theta)}{\partial w_{ij}} = \frac{\partial J(\Theta)}{\partial z_k} \cdot \frac{\partial z_k}{\partial w_{ij}}$

===============================

(四)、Implementation note: Unrolling parameters

optTheta = fminunc(@costFunction, initialTheta, options)

===============================

Summary: 有以下几点需要注意

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

-看是否得到相同（or相近）的结果

-（这一点非常重要）停止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. 选择神经网络结构

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

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：29660次
• 积分：548
• 等级：
• 排名：千里之外
• 原创：25篇
• 转载：14篇
• 译文：0篇
• 评论：4条