机器学习:神经网络中的激活函数
随着深度学习的兴起,神经网络也似乎成了所有计算机视觉任务的标配,大家除了研究各种各样的网络结构之外,还有研究优化方法的,以及激活函数的,这篇博客就对当前各种各样的激活函数做一个总结,分析其背后的性质。到目前为止,激活函数的形式有很多种了,早期的激活函数主要是 sigmoid 以及 tanh 函数,这两种函数都能将输入限制在很小的范围内,算是一种非线性函数,后来又出现了 RELU 以及各种基于 RE
随着深度学习的兴起,神经网络也似乎成了所有计算机视觉任务的标配,大家除了研究各种各样的网络结构之外,还有研究优化方法的,以及激活函数的,这篇博客就对当前各种各样的激活函数做一个总结,分析其背后的性质。
到目前为止,激活函数的形式有很多种了,早期的激活函数主要是 sigmoid 以及 tanh 函数,这两种函数都能将输入限制在很小的范围内,算是一种非线性函数,后来又出现了 RELU 以及各种基于 RELU 的变体。
Tanh 函数
tanh 是一种双曲函数,称为双曲正切,其表达式如下:
tanh(x)=ex−e−xex+e−x tanh(x) = \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}}tanh(x)=ex+e−xex−e−x
从上式可以看出,tanh 函数的取值范围是 [-1, 1],其导数为:
tanh′(x)=((ex−e−x)(ex+e−x)−1)′=(ex+e−x)(ex+e−x)−1−(ex−e−x)(ex+e−x)−2(ex−e−x)=1−(ex−e−xex+e−x)2=1−tanh2(x) \begin{aligned} tanh'(x) &= ((e^{x} - e^{-x})(e^{x} + e^{-x})^{-1})' \\ &= (e^{x} + e^{-x})(e^{x} + e^{-x})^{-1} - (e^{x} - e^{-x})(e^{x} + e^{-x})^{-2}(e^{x} - e^{-x}) \\ &= 1 - \left( \frac{e^{x} - e^{-x}}{e^{x} + e^{-x}} \right)^2 \\ &= 1 - tanh^2(x) \end{aligned} tanh′(x)=((ex−e−x)(ex+e−x)−1)′=(ex+e−x)(ex+e−x)−1−(ex−e−x)(ex+e−x)−2(ex−e−x)=1−(ex+e−xex−e−x)2=1−tanh2(x)
其函数曲线及导数曲线如下所示:
- tanh 函数曲线

Sigmoid 函数
sigmoid 函数也是非常常见的一种函数,其表达式如下:
sigmoid(x)=σ(x)=11+e−x sigmoid(x) = \sigma(x) = \frac{1}{1 + e^{-x}} sigmoid(x)=σ(x)=1+e−x1
sigmoid 函数的取值范围是 [0, 1],其导数为:
σ′(x)=1(1+e−x)2e−x=1+e−x−1(1+e−x)2=σ(x)−σ2x=σ(x)(1−σ(x)) \begin{aligned} \sigma'(x) &= \frac{1}{(1+e^{-x})^2} e^{-x} \\ &= \frac{1 + e^{-x} - 1}{(1+e^{-x})^2} \\ &= \sigma(x) - \sigma^2{x} \\ &= \sigma(x)(1 - \sigma(x)) \end{aligned} σ′(x)=(1+e−x)21e−x=(1+e−x)21+e−x−1=σ(x)−σ2x=σ(x)(1−σ(x))
其函数曲线及导数曲线如下:
- sigmoid 函数曲线

RELU 函数
relu 函数在如今的深度神经网络里面,应该是非常主流的一种函数了,上面介绍的两种激活函数,我们可以看到其导数的取值范围很小,在深度神经网络里,这种导数在链式传导的时候,有可能出现梯度消失的问题,所以为了解决这个问题,relu 这种函数获得了推广和关注,relu 函数的形式非常简单:
relu(x)=max(0,x) relu(x) = \max(0, x)relu(x)=max(0,x)
relu(x)={x if x>00 if x<0 relu(x) = \begin{cases} x & \text{ if } x > 0 \\ 0 & \text{ if } x < 0 \end{cases} relu(x)={x0 if x>0 if x<0
可以看出,就是把小于 0 的输出都给截断了,而大于 0 的输出都保留,其导数也很简单,不过 relu 函数的导数不连续,在 0 这个地方出现断裂:
relu′(x)={1 if x>00 if x<0 relu'(x) = \begin{cases} 1 & \text{ if } x > 0 \\ 0 & \text{ if } x < 0 \end{cases} relu′(x)={10 if x>0 if x<0
- relu 函数曲线

RELU6 函数
Relu6 属于 Relu 函数的一种变体,将大于 0 的输出在某个地方做了一个截断,从函数名上可以看出,这个截断就是在 6 这个地方,其函数表达式为:
Relu6(x)={0 if x<06 if x>6xotherwise Relu6(x) = \begin{cases} 0 & \text{ if } x < 0 \\ 6 & \text{ if } x > 6 \\ x & \text{otherwise} \end{cases} Relu6(x)=⎩⎪⎨⎪⎧06x if x<0 if x>6otherwise
从函数表达式可以看出,只有在 [0, 6] 之间的输入保持了线性关系,小于 0 和 大于 6 的输入都直接截断了,其导数形式为:
Relu6′(x)={0 if x<00 if x>61otherwise Relu6'(x) = \begin{cases} 0 & \text{ if } x < 0 \\ 0 & \text{ if } x > 6 \\ 1 & \text{otherwise} \end{cases} Relu6′(x)=⎩⎪⎨⎪⎧001 if x<0 if x>6otherwise
ELU 函数
ELU 函数属于 RELU 函数的变体,因为原始的 RELU 函数对小于 0 的输入都直接截断了,所以为了克服这个问题,提出了很多的变体,ELU 是其中的一种,其函数表达式如下:
elu(x)={x if x>0α∗(ex−1) if x<0 elu(x) = \begin{cases} x & \text{ if } x > 0 \\ \alpha * (e^{x} - 1) & \text{ if } x < 0 \end{cases} elu(x)={xα∗(ex−1) if x>0 if x<0
ELU 对小于 0 的输入没有直接截断,而是用一个指数函数来表示,一定程度保留了小于 0 的部分,相应地,其导数也分成两部分:
elu′(x)={1 if x>0α∗ex if x<0 elu'(x) = \begin{cases} 1 & \text{ if } x > 0 \\ \alpha * e^{x} & \text{ if } x < 0 \end{cases} elu′(x)={1α∗ex if x>0 if x<0
- elu 函数曲线

SELU 函数
SELU 函数的表达式如下:
Selu(x)=λ{x if x>0α∗ex−α if x<0 Selu(x) = \lambda \begin{cases} x & \text{ if } x > 0 \\ \alpha * e^{x} - \alpha & \text{ if } x < 0 \end{cases} Selu(x)=λ{xα∗ex−α if x>0 if x<0
上面的 α=1.6732632423543772848170429916717\alpha = 1.6732632423543772848170429916717α=1.6732632423543772848170429916717,
λ=1.0507009873554804934193349852946\lambda = 1.0507009873554804934193349852946λ=1.0507009873554804934193349852946,
Selu 的导数为:
Selu′(x)=λ{1 if x>0α∗ex if x≤0 Selu'(x) = \lambda \begin{cases} 1 & \text{ if } x > 0 \\ \alpha * e^{x} & \text{ if } x \leq 0 \end{cases} Selu′(x)=λ{1α∗ex if x>0 if x≤0
LeakyReLU 函数
LeakyReLU 函数也是 RELU 函数的变体,类似 ELU,其小于 0 的部分并没有截断,不过不同于 ELU 的是,LeakyReLU 没有用指数函数,而是简单的一个线性函数来表示:
LeakyReLU(x)={x if x>0α∗x if x<0 LeakyReLU(x) = \begin{cases} x & \text{ if } x > 0 \\ \alpha * x & \text{ if } x < 0 \end{cases} LeakyReLU(x)={xα∗x if x>0 if x<0
其导数形式也很简单:
LeakyReLU′(x)={1 if x>0α if x<0 LeakyReLU'(x) = \begin{cases} 1 & \text{ if } x > 0 \\ \alpha & \text{ if } x < 0 \end{cases} LeakyReLU′(x)={1α if x>0 if x<0
HardShrink 函数
HardShrink 类似一个对称函数,在大于一定阈值与小于一定阈值的输入保持不变,而在某个范围之间的为 0,其函数表达式如下:
HardShrink(x)={x if x>λ−x if x<−λ0otherwise HardShrink(x) = \begin{cases} x & \text{ if } x > \lambda \\ -x & \text{ if } x < -\lambda \\ 0 & \text{otherwise} \end{cases} HardShrink(x)=⎩⎪⎨⎪⎧x−x0 if x>λ if x<−λotherwise
λ\lambdaλ 一般取 0.5,其导数形式也很直接:
HardShrink(x)={1 if x>λ−1 if x<−λ0otherwise HardShrink(x) = \begin{cases} 1 & \text{ if } x > \lambda \\ -1 & \text{ if } x < -\lambda \\ 0 & \text{otherwise} \end{cases} HardShrink(x)=⎩⎪⎨⎪⎧1−10 if x>λ if x<−λotherwise
HardSigmoid 函数
HardSigmoid 函数类似 sigmoid 函数,取值范围也是 [0, 1] 之间,不过不是利用指数函数做非线性变换,而是一个线性函数来实现的,其函数形式如下所示:
HardSigmoid(x)={0 if x≤−31 if x≥3x/6+1/2otherwise HardSigmoid(x) = \begin{cases} 0 & \text{ if } x \leq -3 \\ 1 & \text{ if } x \geq 3 \\ x/6 + 1/2 & \text{otherwise} \end{cases} HardSigmoid(x)=⎩⎪⎨⎪⎧01x/6+1/2 if x≤−3 if x≥3otherwise
其导数形式也比较简单:
HardSigmoid′(x)={0 if x≤−30 if x≥31/6otherwise HardSigmoid'(x) = \begin{cases} 0 & \text{ if } x \leq -3 \\ 0 & \text{ if } x \geq 3 \\ 1/6 & \text{otherwise} \end{cases} HardSigmoid′(x)=⎩⎪⎨⎪⎧001/6 if x≤−3 if x≥3otherwise
Hardtanh
应该是基于 tanh 函数变化而来,tanh 的取值范围是 [-1, 1],hardtanh 的取值范围也是 [-1, 1],只不过在这个区间是一个线性函数的映射:
Hardtanh(x)={−1 if x≤−11 if x≥1xotherwise Hardtanh(x) = \begin{cases} -1 & \text{ if } x \leq -1 \\ 1 & \text{ if } x \geq 1 \\ x & \text{otherwise} \end{cases} Hardtanh(x)=⎩⎪⎨⎪⎧−11x if x≤−1 if x≥1otherwise
其导数形式为:
Hardtanh′(x)={0 if x≤−10 if x≥11otherwise Hardtanh'(x) = \begin{cases} 0 & \text{ if } x \leq -1 \\ 0 & \text{ if } x \geq 1 \\ 1 & \text{otherwise} \end{cases} Hardtanh′(x)=⎩⎪⎨⎪⎧001 if x≤−1 if x≥1otherwise
Hardswish
这个函数的形式如下:
Hardswish(x)={0 if x≤−3x if x≥3x⋅(x+3)/6otherwise Hardswish(x) = \begin{cases} 0 & \text{ if } x \leq -3 \\ x & \text{ if } x \geq 3 \\ x \cdot (x+3)/6 & \text{otherwise} \end{cases} Hardswish(x)=⎩⎪⎨⎪⎧0xx⋅(x+3)/6 if x≤−3 if x≥3otherwise
其导数形式如下:
Hardswish′(x)={0 if x≤−31 if x≥3x/6+1/2otherwise Hardswish'(x) = \begin{cases} 0 & \text{ if } x \leq -3 \\ 1 & \text{ if } x \geq 3 \\ x/6 + 1/2 & \text{otherwise} \end{cases} Hardswish′(x)=⎩⎪⎨⎪⎧01x/6+1/2 if x≤−3 if x≥3otherwise
更多推荐
所有评论(0)