最近正值秋招季,很多同学都在忙着复习深度学习相关的基础知识应对面试和笔试。这段时间,正好发现自己基础知识也比较薄弱,想系统性的复习和整理一下。基于这样一个出发点,最近准备开始一个名为【CV知识点扫盲】的专题文章,帮助自己和更多人复习计算机视觉中的基础知识,也希望能够对正在找工作的同学有帮助。
在神经网络中,一个节点的激活函数(Activation Function)定义了该节点在给定的输入变量或输入集合下的输出。wiki中以计算机芯片电路为例,标准的计算机芯片电路可以看作是根据输入得到开(1)或关(0)输出的数字电路激活函数。激活函数主要用于提升神经网络解决非线性问题的能力。激活函数各式各样,各有优缺点,目前常用的有 ReLU、sigmoid、tanh等。
当不用激活函数时,神经网络的权重和偏差只会进行线性变换。线性方程很简单,但是解决复杂问题的能力有限。没有激活函数的神经网络实质上就是一个线性回归模型。为了方便理解,以一个简单的例子来说明。考虑如下网络
在不用激活函数的情况下,该图可用如下公式表示
$\( output =w 7( input 1 * w 1+i n p u t 2 * w 2)+w 8(i n p u t 1 * w 3+i n p u t 2 * w 4)+w 9(i n p u t 1 * w 5+i n p u t 2 * w 6) \)\(</p><p>实质就是下面的线性方程:</p><p>\)\( output =left[begin{array}{c}w 1 * w 7+w 3 * w 8+w 5 * w 9 \ w 2 * w 7+w 4 * w 8+w 6 * w 9end{array} ight] *left[begin{array}{c} ext { input } 1 \ ext { input } 2end{array} ight] Longrightarrow Y=W X \)\(</p><p>若在隐藏层引入激活函数\)h(y)=max (y, 0)\(,那么原始式子就无法用简单线性方程表示了。</p><p>\)\( output =w 7 * max ( input 1 * w 1+i n p u t 2 * w 2,0)+w B * max ( input 1 * w 3+i n p u t 2 * w 4,0)+w 9 * max ( input 1 * w 5+i n p u t 2 * w 6,0) \)\(</p><p><strong>非线性(Nonlinear)</strong> 当激活函数是非线性的,那么一个两层神经网络也证明是一个通用近似函数通用近似理论。而恒等激活函数则无法满足这一特性,当多层网络的每一层都是恒等激活函数时,该网络实质等同于一个单层网络。</p><p><strong>连续可微(Continuously differentiable)</strong> 通常情况下,当激活函数连续可微,则可以用基于梯度的优化方法。(也有例外,如ReLU函数虽不是连续可微,使用梯度优化也存在一些问题,如ReLU存在由于梯度过大或学习率过大而导致某个神经元输出小于0,从而使得该神经元输出始终是0,并且无法对与其相连的神经元参数进行更新,相当于该神经元进入了“休眠”状态,但ReLU还是可以使用梯度优化的。)二值阶跃函数在0处不可微,并且在其他地方的导数是零,所以梯度优化方法不适用于该激活函数。</p><p><strong>单调(Monotonic) </strong>当激活函数为单调函数时,单层模型的误差曲面一定是凸面。即对应的误差函数是凸函数,求得的最小值一定是全局最小值。</p><p><strong>一阶导单调(Smooth functions with a monotonic derivative) </strong>通常情况下,这些函数表现更好。</p><p><strong>原点近似恒等函数(Approximates identity near the origin)</strong> 若激活函数有这一特性,神经网络在随机初始化较小的权重时学习更高效。若激活函数不具备这一特性,初始化权重时必须特别小心。</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52195" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: 一种输入和输出相等的激活函数,比较适合线性问题,如线性回归问题。但不适用于解决非线性问题。</p><p><strong>方程式</strong>:\)f(x)=x\(</p><p><strong>一阶导</strong>:\)f^{prime}(x)=1\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52196" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: step与神经元激活的含义最贴近,指当刺激超过阈值时才会激发。但是由于该函数的梯度始终为0,不能作为深度网络的激活函数</p><p><strong>方程式</strong>:\)f(x)=left{begin{array}{ll}0 & ext { for } x<0 \ 1 & ext { for } x geq 0end{array} ight.\(</p><p><strong>一阶导</strong>:\)f^{prime}(x)=left{begin{array}{ll}0 & ext { for } x eq 0 \ ? & ext { for } x=0end{array} ight.\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52197" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: 使用很广的一类激活函数,具有指数函数形状,在物理意义上最接近生物神经元。并且值域在(0,1)之间,可以作为概率表示。该函数也通常用于对输入的归一化,如Sigmoid交叉熵损失函数。Sigmoid激活函数具有梯度消失和饱和的问题,一般来说,sigmoid网络在5层之内就会产生梯度消失现象。  </p><p><strong>方程式</strong>:\)f(x)=sigma(x)=frac{1}{1+e^{-x}}\(</p><p><strong>一阶导</strong>:\)f^{prime}(x)=f(x)(1-f(x))\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52198" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: TanH与Sigmoid函数类似,在输入很大或很小时,输出几乎平滑,梯度很小,不利于权重更新,容易出现梯度消失和饱和的问题。不过TanH函数值域在(-1,1)之间,以0为中心反对称,且原点近似恒等,这些点是加分项。一般二分类问题中,隐藏层用tanh函数,输出层用sigmod函数。  </p><p><strong>方程式</strong>:\)f(x)= anh (x)=frac{left(e^{x}-e^{-x} ight)}{left(e^{x}+e^{-x} ight)}\(</p><p><strong>一阶导</strong>:\)f^{prime}(x)=1-f(x)^{2}\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52199" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: ArcTen从图形上看类似TanH函数,只是比TanH平缓,值域更大。从一阶导看出导数趋于零的速度比较慢,因此训练比较快。</p><p><strong>方程式</strong>:\)f(x)= an ^{-1}(x)\(</p><p><strong>一阶导</strong>:\)f^{prime}(x)=frac{1}{x^{2}+1}\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52200" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: Softsign从图形上看也类似TanH函数,以0为中心反对称,训练比较快。</p><p><strong>方程式</strong>:\)f(x)=frac{x}{1+|x|}\(</p><p><strong>一阶导</strong>:\)f^{prime}(x)=frac{1}{(1+|x|)^{2}}\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52201" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: 比较流行的激活函数,该函数保留了类似step那样的生物学神经元机制,即高于0才激活,不过因在0以下的导数都是0,可能会引起学习缓慢甚至神经元死亡的情况。</p><p><strong>方程式</strong>:\)f(x)=left{begin{array}{ll}0 & ext { for } x leq 0 \ x & ext { for } x>0end{array} ight.\(</p><p><strong>一阶导</strong>:\)f^{prime}(x)=left{begin{array}{ll}0 & ext { for } x leq 0 \ 1 & ext { for } x>0end{array} ight.\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52202" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: relu的一个变化,即在小于0部分不等于0,而是加一个很小的不为零的斜率,减少神经元死亡带来的影响。</p><p><strong>方程式</strong>:\)f(x)=left{begin{array}{ll}0.01 x & ext { for } x<0 \ x & ext { for } x geq 0end{array} ight.\(</p><p><strong>一阶导</strong>:\)f^{prime}(x)=left{begin{array}{ll}0.01 & ext { for } x<0 \ 1 & ext { for } x geq 0end{array} ight.\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52203" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: 也是ReLU的一个变化,与Leaky ReLU类似,只不过PReLU将小于零部分的斜率换成了可变参数α。这种变化使值域会依据α不同而不同。</p><p><strong>方程式</strong>:\)f(alpha, x)=left{begin{array}{ll}alpha x & ext { for } x<0 \ x & ext { for } x geqslant 0end{array} ight.\(</p><p><strong>一阶导</strong>:\)f^{prime}(alpha, x)=left{begin{array}{ll}alpha & ext { for } x<0 \ 1 & ext { for } x geq 0end{array} ight.\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52204" alt="" title="" loading="lazy"></p><p>Randomized leaky rectified linear unit(带泄露随机线性整流函数,RReLU)</p><p><strong>描述</strong>: 在PReLU基础上将α变成了随机数。</p><p><strong>方程式</strong>:\)f(alpha, x)=left{begin{array}{ll}alpha x & ext { for } x<0 \ x & ext { for } x geqslant 0end{array} ight.\(</p><p><strong>一阶导</strong>:\)f^{prime}(alpha, x)=left{begin{array}{ll}alpha & ext { for } x<0 \ 1 & ext { for } x geq 0end{array} ight.\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52205" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: ELU小于零的部分采用了负指数形式,相较于ReLU权重可以有负值,并且在输入取较小值时具有软饱和的特性,提升了对噪声的鲁棒性</p><p><strong>方程式</strong>:\)f(alpha, x)=left{begin{array}{ll}alphaleft(e^{x}-1 ight) & ext { for } x leq 0 \ x & ext { for } x>0end{array} ight.\(</p><p><strong>一阶导</strong>:\)f^{prime}(alpha, x)=left{begin{array}{ll}f(alpha, x)+alpha & ext { for } x leq 0 \ 1 & ext { for } x>0end{array} ight.\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52206" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: ELU的一种变化,引入超参λ和α,并给出了相应取值,这些取值在原论文中(Self-Normalizing Neural Networks)详细推导过程</p><p><strong>方程式</strong>:\)f(alpha, x)=lambdaleft{begin{array}{ll}alphaleft(e^{x}-1 ight) & ext { for } x<0 \ x & ext { for } x geq 0end{array} ight.$\(with quad lambda=1.0507 quad and quad alpha=1.67326\)
一阶导:\(f^{prime}(alpha, x)=lambdaleft{begin{array}{ll}alphaleft(e^{x} ight) & ext { for } x<0 \ 1 & ext { for } x geq 0end{array} ight.\)
图形:


描述: 是ReLU的平滑替代,函数在任何地方连续且值域非零,避免了死神经元。不过因不对称且不以零为中心,可以影响网络学习。由于导数必然小于1,所以也存在梯度消失问题。
方程式:
$\( f(x)=ln left(1+e^{x} ight) \)\(</p><p><strong>一阶导</strong>:</p><p>\)\( f^{prime}(x)=frac{1}{1+e^{-x}} \)\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52209" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: 可以理解为identity和ReLU之间的一种折中,不会出现死神经元的问题,不过存在梯度消失和梯度爆炸风险。</p><p><strong>方程式</strong>:</p><p>\)\( f(x)=frac{sqrt{x^{2}+1}-1}{2}+x \)\(</p><p><strong>一阶导</strong>:</p><p>\)\( f^{prime}(x)=frac{x}{2 sqrt{x^{2}+1}}+1 \)\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52210" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: Sinusoid作为激活函数,为神经网络引入了周期性,且该函数处处联系,以零点对称。</p><p><strong>方程式</strong>:</p><p>\)\( f(x)=sin (x) \)\(</p><p><strong>一阶导</strong>:</p><p>\)\( f^{prime}(x)=cos (x) \)\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52211" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: Sinc函数在信号处理中尤为重要,因为它表征了矩形函数的傅立叶变换。作为激活函数,它的优势在于处处可微和对称的特性,不过容易产生梯度消失的问题。</p><p><strong>方程式</strong>:</p><p>\)\( f(x)=left{begin{array}{ll}1 & ext { for } x=0 \ frac{sin (x)}{x} & ext { for } x eq 0end{array} ight. \)\(</p><p><strong>一阶导</strong>:</p><p>\)\( f^{prime}(x)=left{begin{array}{ll}0 & ext { for } x=0 \ frac{cos (x)}{x}-frac{sin (x)}{x^{2}} & ext { for } x eq 0end{array} ight. \)\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52212" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: 高斯激活函数不常用。</p><p><strong>方程式</strong>:</p><p>\)\( f(x)=e^{-x^{2}} \)\(</p><p><strong>一阶导</strong>:</p><p>\)\( f^{prime}(x)=-2 x e^{-x^{2}} \)\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52213" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: 是Sigmoid函数的分段线性近似,更容易计算,不过存在梯度消失和神经元死亡的问题<br><strong>方程式</strong>:</p><p>\)\( f(x)=left{begin{array}{ll}0 & ext { for } x<-2.5 \ 0.2 x+0.5 & ext { for }-2.5 geq x leq 2.5 \ 1 & ext { for } x>2.5end{array} ight. \)\(</p><p><strong>一阶导</strong>:</p><p>\)\( f^{prime}(x)=left{begin{array}{ll}0 & ext { for } x<-2.5 \ 0.2 & ext { for }-2.5 geq x leq 2.5 \ 0 & ext { for } x>2.5end{array} ight. \)\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52214" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: Tanh激活函数的分段线性近似。</p><p><strong>方程式</strong>:</p><p>\)\( f(x)=left{begin{array}{ll}-1 & ext { for } x<-1 \ x & ext { for }-1 geq x leq 1 \ 1 & ext { for } x>1end{array} ight. \)\(</p><p><strong>一阶导</strong>:</p><p>\)\( f^{prime}(x)=left{begin{array}{ll}0 & ext { for } x<-1 \ 1 & ext { for }-1 geq x leq 1 \ 0 & ext { for } x>1end{array} ight. \)\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52215" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: Tanh的缩放版本</p><p><strong>方程式</strong>:</p><p>\)\( f(x)=1.7519 anh left(frac{2}{3} x ight) \)\(</p><p><strong>一阶导</strong>:</p><p>\)\( begin{aligned} f^{prime}(x) &=1.7519 * frac{2}{3}left(1- anh ^{2}left(frac{2}{3} x ight) ight) \ &=1.7519 * frac{2}{3}-frac{2}{3 * 1.7519} f(x)^{2} end{aligned} \)\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52216" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: 是Tanh的一种替代方法,比Tanh形状更扁平,导数更小,下降更缓慢。</p><p><strong>方程式</strong>:</p><p>\)\( begin{aligned} f(x) &= anh (x / 2) \ &=frac{1-e^{-x}}{1+e^{-x}} end{aligned} \)\(</p><p><strong>一阶导</strong>:</p><p>\)\( begin{aligned} f^{prime}(x) &=0.5left(1- anh ^{2}(x / 2) ight) \ &=0.5left(1-f(x)^{2} ight) end{aligned} \)\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52217" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: 是Sigmoid的一种替代,相较于Sigmoid更饱和。</p><p><strong>方程式</strong>:</p><p>\)\( f(x)=1-e^{-e^{x}} \)\(</p><p><strong>一阶导</strong>:</p><p>\)\( f^{prime}(x)=e^{x}left(e^{-e^{x}} ight)=e^{x-e^{x}} \)\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52218" alt="" title="" loading="lazy"></p><p><strong>描述</strong>: 导数只有两个值。</p><p><strong>方程式</strong>:</p><p>\)\( f(x)=|x| \)\(</p><p><strong>一阶导</strong>:</p><p>\)\( f^{prime}(x)=left{begin{array}{ll}-1 & ext { for } x<0 \ 1 & ext { for } x>0 \ ? & ext { for } x=0end{array} ight. \)\(</p><p><strong>图形</strong>:</p><p><img referrerpolicy="no-referrer" src="https://segmentfault.com/img/remote/52219" alt="" title="" loading="lazy"></p><p>ReLU。ReLU的优点是收敛速度快、不会出现梯度消失or爆炸的问题、计算复杂度低。</p><p>Bert、GPT、GPT2、RoBERTa、ALBERT都是用的Gelu。</p><p>\)\( operatorname{GELU}(x)=x P(X leq x)=x Phi(x) \)$
直观理解:x做为神经元的输入,P(X<=x)越大,x就越有可能被保留;否则越小,激活函数输出就趋近于0.
- 用于分类器时,二分类为Sigmoid,多分类为Softmax,这两类一般用于输出层;
- 对于长序列的问题,隐藏层中尽量避免使用Sigmoid和Tanh,会造成梯度消失的问题;
- Relu在Gelu出现之前在大多数情况下比较通用,但也只能在隐层中使用;
- 现在2022年了,隐藏层中主要的选择肯定优先是Gelu、Swish了。
优点:
- 从计算的角度上,Sigmoid和Tanh激活函数均需要计算指数,复杂度高,而ReLU输入一个数值即可得到激活值;
- ReLU函数被认为有生物上的解释性,比如单侧抑制、宽兴奋边界(即兴奋程度 也可以非常高)人脑中在同一时刻大概只有1 ∼ 4%的神经元处于活跃状态,所以单侧抑制提供了网络的稀疏表达能力,宽激活边界则能有效解决梯度消失等问题。
缺点:
- ReLU和Sigmoid一样,每次输出都会给后一层的神经网络引入偏置偏移, 会影响梯度下降的效率。
- ReLU神经元死亡的问题,不正常的一次参数更新,可能是使得激活项为0,以后的梯度更新也为0,神经元死亡。
已建立深度学习公众号——FightingCV,关注于最新论文解读、基础知识巩固、学术科研交流,欢迎大家关注!!!

推荐加入FightingCV交流群,每日会发送论文解析、算法和代码的干货分享,进行学术交流,加群请添加小助手wx:FightngCV666,备注:地区-学校(公司)-名称
面向小白的顶会论文核心代码库:https://github.com/xmu-xiaoma666/External-Attention-pytorch
面向科研小白的YOLO目标检测库:https://github.com/iscyy/yoloair
https://www.jianshu.com/p/466e54432bac
https://zhuanlan.zhihu.com/p/
https://blog.csdn.net/_/article/details/
本文由mdnice多平台发布
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/172799.html