Neural Networks Part 1:Setting up the Architecture

来源:互联网 发布:c语言获取系统日期 编辑:程序博客网 时间:2024/05/16 01:36

原文地址:http://cs231n.github.io/neural-networks-1/


####################################


内容列表:

1.快速介绍(Quick intro without brain analogies)

2.一个神经元模型(modeling one neuron)

2.1 . 生理动机和连接(biological motivation and connections)

2.2 . 单个神经元作为一个线性分类器

2.3 . 常用的激活函数

3.神经网络架构

3.1 . 分层组织

3.2 . 前馈计算实例

3.3 . 表象性(representational power)

3.4 . 设置层数以及每层大小

4.总结

5.其他参考资料


############################################


Quick intro

本小节我们将不通过和大脑的类比来介绍神经网络。在线性分类中,我们使用公式判断测试图像属于哪个类别的,其中,是一个矩阵,是一个列向量,表示图像的全部像素数据。在CIFAR-10的例子中,是一个大小为的列向量,而是一个大小为的行向量,所以输出成绩是10个类别成绩的集合。


神经网络可以实现上述功能,它的公式为。其中,大小为,可以将图像转换为一个100维的中间向量。The function  is a non-linearity that is applied elementwise。在下面我们还会学习几个类似max的函数,但max是最常用的方法。矩阵的大小为,所以最终我们可以得到10个数字的向量,我们把这个向量解释为图像相对于每个类别的成绩。注意,这个非线性(这里指max)是至关重要的-如果我们忽略了它,那么矩阵又将变成一个矩阵,那么整个函数将再次变成输入的线性函数。The non-linearity is where we get the wiggle. 参数通过随机梯度下降(stochastic gradient descent)学习得到,并且它们的梯度通过链式法则进行推导(通过反向传播进行计算)。


一个三层神经网络的公式如下:,其中,都是参数,通过学习得到。中间隐藏向量的大小是网络的超参数,之后我们会设置它。下面让我们从神经元/神经网络的视角来解释这些计算。


Modeling one neuron

神经网络的出现主要原因是对生理神经系统的建模,但马上就和最初目的分道扬镳,成为了一个工程问题并且在机器学习任务中达到了良好的效果。尽管如此,我们在一开始还是先对生理神经系统(that a large portion of this area has been inspired by)进行一个简短的粗略的描述,因为神经网络的很大一部分受其启发。


Biological motivation and connections

大脑基本的计算单位是神经元neuron)。人类神经系统中大约有860亿个神经元,它们之间被大约10^14 - 10^15次个突触synapse)所连接。下图左边展示了一个神经元,右图展示了一个通用的数学模型。每一个神经元都从树突dendrite)中接受输入信号,计算后,沿着轴突axon)输出信号,其中,树突有多个,轴突仅有一个。轴突会有分支,其分支通过突触和不同的神经元的树突相连。在一个神经元的计算模型中,沿着突触的信号(e.g.)传向其他神经元的树突,并且基于一定的突触的强度(e.g.),所以树突接收到的是处理过后的信号(e.g. )。突触强度是可以通过学习得到,主要为了控制信号影响其他神经元的强度(和它的方向:兴奋(正的权重)或者抑制(负的权重))。在基本模型中,树突把信号传给神经元,在神经元中求和所有信号。If the final sum is above a certain threshold, the neuron can fire, sending a spike along its axon. In the computational model, we assume that the precise timings of the spikes do not matter, and that only the frequency of the firing communicates information.  Based on this rate code interpretation, we model the firing rate of the neuron with anactivation function , which represents the frequency of the spikes along the axon. Historically, a common choice of activation function is the sigmoid function , since it takes a real-valued input (the signal strength after the sum) and squashes it to range between 0 and 1. We will see details of these activation functions later in this section.


单个神经元的前向传播代码如下:

class Neuron(object):  # ...   def forward(inputs):    """ assume inputs and weights are 1-D numpy arrays and bias is a number """    cell_body_sum = np.sum(inputs * self.weights) + self.bias    firing_rate = 1.0 / (1.0 + math.exp(-cell_body_sum)) # sigmoid activation function    return firing_rate
换句话说,每个神经元都对输入和权重执行点积操作,加上偏置,最后应用在非线性函数上(或叫做激活函数)。本例中激活函数就是sigmoid函数:。在本节最后会详细结束不同的激活函数。

Carse model. 需要强调的是,这个生理神经元的模型是很粗略的,事实上还有很多不同类型的神经元,每一个都有不同的特性。生理神经元的树突执行了复杂的非线性运算。突触也不仅仅只有一组权重,它们有一个复杂的动态系统。The exact timing of the output spikes in many systems in known to be important, suggesting that the rate code approximation may not hold. Due to all these and many other simplifications, be prepared to hear groaning sounds from anyone with some neuroscience background if you draw analogies between Neural Networks and real brains. See thisreview(pdf),or more recently this review if you are interested.


Single neuron as a linear classifier

你可能对神经元的前向计算公式很熟悉。就像线性份分类器,神经元可以“喜欢”(激活为1)或“不喜欢”(激活为0)其输入空间的若干线性区域。因此,对神经元的输出设置一个损失函数,我们就可以把单个神经元变成一个线性分类器:


Binary Softmax classifier. 举个例子,我们把公式得到的值解释为图像属于第i个类别的可能性,而其他类别的可能性就是,因为类别可能性之和为1。基于这个解释,在之前的线性分类小节中我们构造出交叉熵损失(cross-entropy loss),优化它将生成一个二类Softmax分类器(也被称为逻辑回归logistic regression))。因为sigmoid函数值约束在0-1之间,所以这个分类器的预测结果将基于神经元的输出值是否大于0.5。


Binary SVM classifier. 或者,我们可以使用a max-margin hinge loss,这样分类器就可以训练成为二类支持向量机(SVM)。


Regularization interpretation. 从生理观点上看,在SVM/Softmax函数中的规则化损失(regularization loss)可以被解释为逐步遗忘gradual forgetting),因为,在每次权重更新后,它会影响突触的权重,有变为0的可能性(since it would have the effect of driving all synaptic weights ww towards zero after every parameter update.)。


Commonly used activation functions

每一个激活函数(或者称为非线性函数)得到一个数字后(a single number),很执行一次固定的数学操作。下面是几个你在实际使用中会遇到的激活函数:


Sigmoid. the sigmoid non-linearity的数学公式为:,函数分布如图左所示。在上一节提到的,它得到一个实数,计算结果在0-1之间。特别的,如果得到大的负数,那么结果趋向于0;反之,得到大的正数,则结果趋向于1。sigmoid函数曾经很受欢迎,因为它对神经元的发射率(the firing rate of a neuron)有一个很好的解释:from not firing at all (0) to fully-saturated firing at an assumed maximum frequency (1)。实际上,the sigmoid non-linearity已经失去了人们的信任和支持,现在很少被使用。它有两个主要的缺点:

1)Sigmoids saturate and kill gradients. sigmoid神经元有一个很不好的特性,就是当神经元的激活饱和在0或1上时,这些区域的梯度几乎都为0(when the neuron’s activation saturates at either tail of 0 or 1, the gradient at these regions is almost zero)。在反向传播的时候,局部梯度将和门的输出的梯度相乘( this (local) gradient will be multiplied to the gradient of this gate’s output for the whole objective)。Therefore, if the local gradient is very small, it will effectively “kill” the gradient and almost no signal will flow through the neuron to its weights and recursively to its data。另外,必须额外关注信号神经元初始化权重,以避免饱和。举个例子,如果初始权重太大,那么所有神经元将变得饱和,网络也将无法学习。

2)Sigmoid outputs are not zero-centered. 这是不可取的,因为下一层神经元处理的输入数据并不是零中心的(zero-centered).在梯度下降的时候会产生影响,因为如果神经元接收到的数据总是正的(e.g. 在中的输入),那么在反向传播的时候,权重的梯度值会全部是正的或者全部是负的(这依赖于整个表达式的梯度)。这对于在权重的梯度更新过程中会产生不稳定的动态影响。 However, notice that once these gradients are added up across a batch of data the final update for the weights can have variable signs, somewhat mitigating this issue. 因此,第一个问题没有第一个问题严重。


Tanh. Tanh函数如图右所示。它转换一个实数到-1和1之间。和sigmoid神经元类似,它的激活函数是饱和的,但它的输出和sigmoid神经元不同,是零中心的。因此,在实际使用中tanh函数总是优先于sigmoid函数使用。注意,tanh神经元仅仅是sigmoid神经元的扩展形式,其公式如下:


ReLU. 整流线性单元(the Rectified Linear Unit)最近几年很受欢迎。它的计算公式为:。换句话说,激活函数仅仅是下阈值为0(如图左所示)。下面是ReLUs的优缺点:

1)(+)和sigmoid/tanh函数相比,ReLU有助于促进随机梯度下降的收敛(e.g. a factor of 6 in Krizhevsky et al.)。有人认为这是由于它的线性,非饱和的格式。

2)(+)tanh/sigmoid神经元都有计算量大的特点(比如,指数操作),而ReLU函数仅仅需要对激活矩阵进行max函数判断即可。

3)(-)ReLU在训练的时候是脆弱的。举个例子,当ReLU神经元得到一个大的梯度后,权重更新会导致一种结果,就是这个神经元在永远不会在激活(For example, a large gradient flowing through a ReLU neuron could cause the weights to update in such a way that the neuron will never activate on any datapoint again)。如果这个事情发生的话,那么梯度流经过这个神经元后将变为0。That is, the ReLU units can irreversibly die during training since they can get knocked off the data manifold. 举个例子,如果你的学习速率太高,你可能会发现在你的神经网络中40%是“死的”(i.e. 对全部的训练数据都不会激活)。所以设置好学习速率有助于减小这种问题出现的概率。


Leaky ReLU. Leaky ReLus对ReLU出现的问题进行了改进。max函数的下阈值不再是0,而会使用一个很小的负斜率(比如0.01)。因此,函数公式为:,其中是一个很小的常数。但这种激活函数的形式得到的结果并不稳定,有时好有时差。The slope in the negative region can also be made into a parameter of each neuron, as seen in PReLU neurons, introduced inDelving Deep into Rectifiers, by Kaiming He et al., 2015. However, the consistency of the benefit across tasks is presently unclear.


Maxout. 其中出现过得激活函数类型格式并不像上面的:。但最近有一种比较受欢迎的选择是Maxout神经元(introduced recently byGoodfellow eg al.),其泛化了ReLU和LeakyReLU。Maxout神经元的计算公式为:。ReLU和Leaky ReLU都是这一种格式的特例(比如,当德时就是ReLU)。因此,maxout神经元拥有ReLU的所有优点(线性操作,非饱和),并且摒弃了它的缺点。然而,每一个神经元它加倍了参数的数目,这直接导致了总的参数的数目。


上面就是最常见的神经元和激活函数的类型。最后一个建议,不要在一个神经网络中混合多种上述神经元,即使这样做并没有根本性的问题。


TLDR:“What neuron type should I use?” 使用ReLU函数,要注意学习速率,以及要监督你的神经网络中“dead”的神经元的百分比。如果你担心这个问题,使用Leaky ReLU或者Maxout函数。永远不要使用sigmoid函数。如果想要使用tanh函数,那么做好效率低的准备。


Neural Network architecture

Layer-wise organization

Neural Networks as neurons in graphs. 神经网络是一组神经元的集合,并且是一个无循环图。换句话说,一些神经元的输出另一些神经元的输入。循环是不允许的,因为这将导致一个无限循环的网络。神经元之间并不是无组织的连接,神经网络模型通常被组织成不同的神经元层。对于常规的神经网络,最常见的层的类型是全连接层(thefully-connected layer),表示相邻层之间的神经元是两两全部连接,同一层的神经元没有任何连接。下面是典型神经网络的拓扑结构:


Naming conventions(命名约定). 注意,当我们说N层神经网络时,我们并没有统计输入层。因此,一个单层网络表示一个没有隐藏层的神经网络(输入直接和输出相连)。从这个意义上来说,逻辑回归和SVMs都是单层神经网络的一个特例。你也可能听到这些网络被称为“人工神经网络”(ANN)或“多层感知器”(MLP)。Many people do not like the analogies between Neural Networks and real brains and prefer to refer to neurons as units.


Output layer. 不想神经网络的其他层,输出层神经元通常没有激活函数(或者,你可以想象它们有一个线性标志激活函数)。这是因为输出层的输出通常表示类别的成绩(e.g. 在分类中),可以是任意实数,或some kind of real-valued target(e.g. in regression).


Sizing neural networks. 两个常用来衡量神经网络大小的度量标准是神经元的个数,或者更常用的是参数的数目。使用上面的两个典型神经网络进行计算:

1)左边的神经网络:神经元数:4+2 = 6(不统计输入层),权重数:[3 x 4] + [4 x 2] = 20,偏置数:4 + 2 = 6,所以共有26个可学习的参数。

2)右边的神经网络:神经元数:4+4+1 = 9,权重数:[3 x 4] + [4 x 4] + [4 x 1] = 12 + 16 + 4 = 32,偏置数:4 + 4 + 1 = 9,所以共有41个可学习的参数。


注意,现在的卷积神经网络拥有超过了1亿个参数,通常为10-20层(因此称为深度学习deep learning)。However, as we will see the number ofeffective connections is significantly greater due to parameter sharing. More on this in the Convolutional Neural Networks module.


Example feed-forward computation

Repeated matrix multiplications interwoven(交织) with activation function. 神经网络的分层组织的主要原因之一是可以使用矩阵向量操作来有效计算。使用图右所示的3层神经网络,输入是大小为[3 x 1]的向量。每一个层的所有连接强度可以被存储在一个矩阵中。举个例子,第一个隐藏层的权重 大小为[4 x 3],并且所有神经元的偏置值组成一个向量,大小为[4 x 1]。每个神经元的所有权重就是的一行,所以,矩阵向量乘法计算了该隐藏层。同理,的大小为[4 x 4],存储了第二个隐藏层的所有连接,的大小为[1 x 4],存储了最后输出层的权重。所以,3层神经网络的全部前向操作就是3个矩阵乘法,然后和激活函数进行交互:

# forward-pass of a 3-layer neural network:f = lambda x: 1.0/(1.0 + np.exp(-x)) # activation function (use sigmoid)x = np.random.randn(3, 1) # random input vector of three numbers (3x1)h1 = f(np.dot(W1, x) + b1) # calculate first hidden layer activations (4x1)h2 = f(np.dot(W2, h1) + b2) # calculate second hidden layer activations (4x1)out = np.dot(W3, h2) + b3 # output neuron (1x1)
在上面代码中,是神经网络的可学习参数。注意,输入并不一定只是单一一列,变量可以包含整个训练数据,所有的训练都可以并行计算。注意,最后的输出层通常没有激活函数(e.g. 在分类问题中,它表示类别成绩)


Representational power

全连接层神经网络定义了一个函数簇,可以被网络权重参数化。一个很自然的问题是:What is the representational power of this family of functions?特别的,有没有函数可以被神经网络建模?

事实证明,至少一个隐藏层的神经网络是通用逼近器universal approximators)。可以证明(e.g. seeApproximation by Superpositions of Sigmoidal Function from 1989 (pdf), or thisintuitive explanation from Michael Nielsen),给予任何连续函数以及参数,存在一个一层神经网络(拥有一个合理的激活函数,比如,sigmoid(with a reasonable choice of non-linearity, e.g. sigmoid)),会有。换句话说,神经网络可以近似任何连续函数。


如果一个隐藏层的神经网络就足够近似任何函数,为什么要使用更多的层呢?The answer is that the fact that a two-layer Neural Network is a universal approximator is, while mathematically cute, a relatively weak and useless statement in practice. In one dimension, the “sum of indicator bumps” function  where are parameter vectors is also a universal approximator, but noone would suggest that we use this functional form in Machine Learning. Neural Networks work well in practice because they compactly express nice, smooth functions that fit well with the statistical properties of data we encounter in practice, and are also easy to learn using our optimization algorithms (e.g. gradient descent). Similarly, the fact that deeper networks (with multiple hidden layers) can work better than a single-hidden-layer networks is an empirical observation, despite the fact that their representational power is equal.


从另一个方面来说,在实际使用中,3层神经网络的性能比2层神经网络更强,但是更深的网络(4/5/6层)就很少有帮助了。这和卷积神经网络恰恰相反,对于一个好的识别系统来说,深度是一个极端重要的组成部分(e.g. on order of 10 learnable layers)。有人认为图像包含层次结构(hierarchical structure,e.g. 脸上有眼睛,眼睛有edges,etc.),so several layers of processing make intuitive sense for this data domain.


当然,完整的理论包括了更多的内容,以及最新的研究。如果你感兴趣,推荐以下阅读:

1)Deep Learning book in press by Bengio, Goodfellow, Courville, in practicularChapter 6.4.

2)Do Deep Nets Really Need to be Deep?

3)FitNets: Hints for Thin Deep Nets


Setting number of layers and their sizes

当面对一个实际问题的时候,我们应该选择哪一种结构的神经网络?是1层结构吗?还是2层结构?还是3层结构?每一层的大小如何?首先,当我们增加神经网络的层数和大小的时候,网络的性能会增加。That is, the space of representable functions grows since the neurons can collaborate to express many different functions.比如,我们在一个2维空间中有一个2类分类问题,我们训练3个不同的神经网络,每一个都是一个隐藏层,但是隐藏层大小不同,得到以下分类:


由上图可知,更多神经元的神经网络可以表示更复杂的函数。然而,这既是一个赞美(因为我们可以分类更复杂的数据),也是一个诅咒(因为它会更容易对训练数据过拟合)。当一个高性能的模型拟合数据的噪声而不是底层的关系,就会发生过拟合Overfitting)。举个例子,有20个隐藏神经元的模型拟合了所有的训练数据,but at the cost of segmenting the space into many disjoint red and green decision regionsThe model with 3 hidden neurons only has the representational power to classify the data in broad strokes. It models the data as two blobs and interprets the few red points inside the green cluster asoutliers(离群点) (noise). In practice, this could lead to better generalization(泛化) on the test set.


基于上述描述,如果数据并不复杂,那么更小的神经网络有助于避免过拟合。然而,这是不正确的-我们在后面会提到更多的避免过拟合的手段(比如,L2规则化,dropout,input noise)。实际上,这些方法比控制神经元数量更能够避免过拟合。


The subtle reason behind this is that smaller networks are harder to train with local methods such as Gradient Descent: 很显然,它们的损失函数有相对较少的局部最小值,而事实证明这些极小值更容易收敛,and that they are bad (i.e. with high loss)。相反,更大的神经网络包含了明显更多的局部最小值,but these minima turn out to be much better in terms of their actual loss. 因为神经网络是非凸(non-convex),所以很难在数学上研究这些属性,但也有些人尝试去理解已经出现的损失函数,比如,最近的文章是:The Loss Surfaces of Multilayer Networks。事实上,如果你训练一个小的网络,你会发现最终的损失会难以预料-有时候,你可以收敛的很好,而有时候,你会困在一个坏的极小值上(you get trapped in one of the bad minima)。换句话说,如果你训练一个大型网络,you’ll start to find many different solutions, but the variance in the final achieved loss will be much smaller. In other words, all solutions are about equally as good, and rely less on the luck of random initialization.


重申一遍,规则化强度(the regularization strength)是更好的控制神经网络过拟合的方式。我们可以看看下面3中不同强度达到的效果:


道理是你不应该因为害怕过拟合而使用更小的网络。相反,你应该使用你计算能力允许下的最大的神经网络,然后使用其他规则花方法来控制过拟合。


Summary

1)介绍了一个生理神经元(biological neuron)的粗略模型

2)讨论了实际使用的几种激活函数activation functions),其中ReLU是最常用的

3)介绍了全连接层神经网络:相连层的神经元全部两两连接,但是同一层的神经元之间没有连接

4)基于矩阵乘法以及激活函数的交互,分层结构有助于神经网络的快速计算

5)神经网络是通用函数逼近器,but we also discussed the fact that this property has little to do with their ubiquitous(无处不在的) use. They are used because they make certain “right” assumptions about the functional forms of functions that come up in practice

6)我们讨论了更大的神经网络总比小的神经网络工作更好, but their higher model capacity must be appropriately(适当的) addressed(处理) with stronger regularization (such as higher weight decay(衰退)),或者它们可能过拟合。我们将在后面看到更多不同格式的规则化方法(特别是dropout)


Additional References

1)deeplearning.net tutorial with Theano

2)ConvNetJS demos for intuitions

3)Michael Nielsen’s tutorials

0 0