> For the complete documentation index, see [llms.txt](https://niyunsheng.gitbook.io/deeplearning/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://niyunsheng.gitbook.io/deeplearning/cv-ji-chu/activation.md).

# 激活函数

激活函数是非线性函数，如果不用激活函数，再深的网络也是线性模型。

## sigmoid

也叫s函数，其函数和导函数：

$$
\begin{aligned} &\operatorname{sigmod}(x)=\frac{1}{1+e^{-x}} \in(0,1) \ &\operatorname{sigmod}^{\prime}(x)=\operatorname{sigmod}(x) \*(1-\operatorname{sigmod}(x))=\frac{1}{1+e^{-x}} \* \frac{e^{-x}}{1+e^{-x}}=\frac{e^{-x}}{\left(1+e^{-x}\right)^{2}} \in(0,0.25) \end{aligned}
$$

* 输出范围在0\~1之间，均值为0.5，需要做数据偏移，不方便下一层的学习。
* 当x很小或很大时，存在导数很小的情况。另外，神经网络主要的训练方法是BP算法，BP算法的基础是导数的链式法则，也就是多个导数的乘积。而sigmoid的导数最大为0.25，多个小于等于0.25的数值相乘，其运算结果很小。随着神经网络层数的加深，梯度后向传播到浅层网络时，基本无法引起参数的扰动，也就是没有将loss的信息传递到浅层网络，这样网络就无法训练学习了。这就是所谓的梯度消失。

## tanh

$$
\begin{aligned} &\tanh (x)=\frac{1-e^{-2 x}}{1+e^{-2 x}} \in(-1,1) \ &\tanh ^{\prime}(x)=1-(\tanh (x))^{2}=\frac{4 e^{-2 x}}{\left(1+e^{-2 x}\right)^{2}} \in(0,1] \end{aligned}
$$

* 在神经网络的应用中，tanh通常要优于sigmod的，因为tanh的输出在-1\~1之间，均值为0，更方便下一层网络的学习。但有一个例外，如果做二分类，输出层可以使用sigmod，因为他可以算出属于某一类的概率
* Sigmod(x)和tanh(x)都有一个缺点：在深层网络的学习中容易出现梯度消失，造成学习无法进行。

## Relu

$$
\begin{aligned} &\operatorname{relu}(x)=\max (x, 0)=\left{\begin{array}{ll} x, & x \geq 0 \ 0, & x<0 \end{array} \in\[0,+\infty)\right. \ &\operatorname{relu}^{\prime}(x) & =\left{\begin{array}{ll} 1, & x \geq 0 \ 0, & x<0 \end{array} \in{0,1}\right. \end{aligned}
$$

* 有 Dead ReLU 问题，当x为负时导数等于零，但是在实践中没有问题，也可以使用leaky Relu。

## LeakyReLU

$$
\begin{aligned} &\text { LeakyReLU }(x)=\left{\begin{array}{l} x, \quad x \geq 0 \ a x, \quad x<0 \end{array} \in R\right. \ &\text { LeakyReL } U^{\prime}(x)=\left{\begin{array}{ll} 1, & x \geq 0 \ a, & x<0 \end{array} \in{a, 1}\right. \end{aligned}
$$

## Relu6

$$
\begin{aligned} & \text { relu } 6(x)=\min (\max (x, 0), 6) \in\[0,6] \ \text { relu } 6^{\prime}(x) &=\left{\begin{array}{ll} 1, & 0\<x<6 \ 0, & \text { 其他 } \end{array} \in{0,1}\right. \end{aligned}
$$

## ELu

$$
g(x)=\operatorname{ELU}(x)=\left{\begin{aligned} x, & x>0 \ \alpha\left(\mathrm{e}^{x}-1\right), & x \leqslant 0 \end{aligned}\right.
$$

* 没有 Dead ReLU 问题，输出的平均值接近 0，以 0 为中心；
* ELU 通过减少偏置偏移的影响，使正常梯度更接近于单位自然梯度，从而使均值向零加速学习；
* ELU 在较小的输入下会饱和至负值，从而减少前向传播的变异和信息。
* 一个小问题是它的计算强度更高。

## PRelu

$$
f\left(y\_{i}\right)= \begin{cases}y\_{i}, & \text { if } y\_{i}>0 \ a\_{i} y\_{i}, & \text { if } y\_{i} \leq 0\end{cases}
$$

* 如果 a\_i= 0，则 f 变为 ReLU
* 如果 a\_i> 0，则 f 变为 leaky ReLU
* 如果 a\_i 是可学习的参数，则 f 变为 PReLU
