Yongxing tensorflow notes -6 activation function

Here Insert Picture Description

First, the basic neuron:

Neuron model: mathematically expressed as:
Here Insert Picture Description
F is the activation function, w is a weight, b is the bias. Artificial neural networks are composed of neurons.
Here Insert Picture Description

Second, what is the activation function?

The non-linear function into a linear function, is responsible for mapping the input to the output neuron.
Activation function (Activation functions) for the artificial neural network model of learning, understanding very complex and non-linear function is a very important role. They will be introduced to the nonlinear characteristics of our network. In neurons, Inputs entered by a weighted, after summation, a further effect of the function that the function is activated. Activation function is introduced to increase the non-linear neural network model. No activation function of each layer corresponds to matrix multiplication. Even after you overlay a number of layers, or simply a matrix multiplication nothing.

Third, the role of activation functions:

If the activation function is not used, the output of each layer we undertake only one linear transformation on the input function, regardless of how many layer neural network, the output is a linear combination of inputs. If used, the activation function of neurons to the introduction of non-linear factors, making the neural network can approximate any nonlinear function, so that the neural network can be applied to the non-linear model, the ability to greatly increase the model.

Fourth, the activation function:

The most popular three activation function:

  • Logistic function or functions 1.Sigmoid
    Sigmoid function is a common S-shaped function in biology, also known as S-shaped growth curve. In the information science, and by the nature of their single inverse function monocytogenes like, the Sigmoid function is often used as a threshold value function neural network mapping variables to between 0 and 1. Formula is as follows
    Here Insert Picture Description
tf.nn.sigmoid()  #sigmoid激活函数

特点:
它能够把输入的连续实值变换为0和1之间的输出,特别的,如果是非常大的负数,那么输出就是0;如果是非常大的正数,输出就是1.
sigmoid函数曾经被使用的很多,不过近年来,用它的人越来越少了。主要是因为它固有的一些 缺点。
缺点:在深度神经网络中梯度反向传递时导致梯度爆炸和梯度消失,其中梯度爆炸发生的概率非常小,而梯度消失发生的概率比较大(如果神经网络隐层特别多,那么梯度在穿过多层后将变得非常小接近于0,即出现梯度消失现象;当网络权值初始化为 (1,+∞) 区间内的值,则会出现梯度爆炸情况)。

  • 2.Tanh — Hyperbolic tangent(双曲正切函数)
    Tanh是双曲函数中的一个,Tanh()为双曲正切。在数学中,双曲正切“Tanh”是由基本双曲函数双曲正弦和双曲余弦推导而来。公式如下
    Here Insert Picture Description
tf.nn.tanh() #tanh激活函数

sigmoid函数和tanh函数是研究早期被广泛使用的2种激活函数。两者都为S 型饱和函数。 当sigmoid 函数输入的值趋于正无穷或负无穷时,梯度会趋近零,从而发生梯度弥散现象。sigmoid函数的输出恒为正值,不是以零为中心的,这会导致权值更新时只能朝一个方向更新,从而影响收敛速度。tanh 激活函数是sigmoid 函数的改进版,是以零为中心的对称函数,收敛速度快,不容易出现 loss 值晃动,但是无法解决梯度弥散的问题。2个函数的计算量都是指数级的,计算相对复杂。softsign 函数是 tanh 函数的改进版,为 S 型饱和函数,以零为中心,值域为(−1,1)

  • 3.ReLu -Rectified linear units(线性修正单元)
    Relu激活函数(The Rectified Linear Unit),用于隐层神经元输出。
    是一种人工神经网络中非常常用的激活函数(activation function),通常指代以斜坡函数及其变种为代表的非线性函数。
    公式如下:
    Here Insert Picture Description
tf.nn.relu() #relu 激活函数

优势:
相比于传统的神经网络激活函数,诸如逻辑函数(Logistic sigmoid)和tanh等双曲函数,线性整流函数有着以下几方面的优势:
1.仿生物学原理:相关大脑方面的研究表明生物神经元的信息编码通常是比较分散及稀疏的。通常情况下,大脑中在同一时间大概只有1%-4%的神经元处于活跃状态。使用线性修正以及正则化(regularization)可以对机器神经网络中神经元的活跃度(即输出为正值)进行调试;相比之下,逻辑函数在输入为0时达到,即已经是半饱和的稳定状态,不够符合实际生物学对模拟神经网络的期望。不过需要指出的是,一般情况下,在一个使用修正线性单元(即线性整流)的神经网络中大概有50%的神经元处于激活态。
2.更加有效率的梯度下降以及反向传播:避免了梯度爆炸和梯度消失问题
3.简化计算过程:没有了其他复杂激活函数中诸如指数函数的影响;同时活跃度的分散性使得神经网络整体计算成本下降。

  • 计算速度非常快,只需要判断输入是否大于0
  • 收敛速度远快于sigmoid和tanh

ReLU也有几个需要特别注意的问题:
Dead ReLU Problem,指的是某些神经元可能永远不会被激活,导致相应的参数永远不能被更新。有两个主要原因可能导致这种情况产生: (1) 非常不幸的参数初始化,这种情况比较少见 (2) learning rate太高导致在训练过程中参数更新太大,不幸使网络进入这种状态。解决方法是可以采用Xavier初始化方法,以及避免将learning rate设置太大或使用adagrad等自动调节learning rate的算法。

尽管存在这两个问题,ReLU目前仍是最常用的activation function,在搭建人工神经网络的时候推荐优先尝试!

五、梯度弥散和梯度爆炸(了解):

  • 靠近输出层的hidden layer 梯度大,参数更新快,所以很快就会收敛;而靠近输入层的hidden layer 梯度小,参数更新慢,几乎就和初始状态一样,随机分布。这种现象就是梯度弥散(vanishing gradient problem)。
  • 而在另一种情况中,前面layer的梯度通过训练变大,而后面layer的梯度指数级增大,这种现象又叫做梯度爆炸(exploding gradient problem)。

一般解决问题的方法(了解):

  • 为了解决梯度弥散的问题,我们介绍了两种方法。第一种方法是将随机初始化改为一个有关联的矩阵初始化。第二种方法是使用ReLU(Rectified Linear Units)代替sigmoid函数。ReLU的导数不是0就是1.因此,神经元的梯度将始终为1,而不会当梯度传播了一定时间之后变小。
  • 可以通过梯度截断,避免梯度爆炸(就是当梯度大于一定阈值的的时候,将它截断为一个较小的数。),还可以可以通过添加正则项,避免梯度爆炸。
Published 45 original articles · won praise 28 · views 10000 +

Guess you like

Origin blog.csdn.net/m0_43505377/article/details/103906599