Pytorch激活函数最全汇总

news/2024/7/24 2:09:20 标签: 神经网络, 人工智能, 深度学习

为了更清晰地学习Pytorch中的激活函数,并对比它们之间的不同,这里对最新版本的Pytorch中的激活函数进行了汇总,主要介绍激活函数的公式、图像以及使用方法,具体细节可查看官方文档。


目录

1、ELU

2、Hardshrink

3、Hardsigmoid

4、Hardtanh

5、Hardswish

6、LeakyReLU

7、LogSigmoid

8、PReLU

9、ReLU

10、ReLU6

11、RReLU

12、SELU

13、CELU

14、GELU

15、Sigmoid

16、SiLU

17、Mish

18、Softplus

19、Softshrink

20、Softsign

21、Tanh

22、Tanhshrink

23、Threshold

24、GLU

25、Softmin

26、Softmax

27、LogSoftmax

28、其它


1、ELU

公式:

ELU(x)=\left\{\begin{matrix} x, & x>0\\ \alpha\ast \left ( exp\left ( x \right )-1 \right ), & x\leqslant 0 \end{matrix}\right.

图像:

 示例:

m = nn.ELU()
input = torch.randn(2)
output = m(input)

2、Hardshrink

公式:

HardShrink(x)=\left\{\begin{matrix} x, & x>\lambda \\ x, & x<-\lambda \\ 0, & otherwise \end{matrix}\right.

图像:

示例:

m = nn.Hardshrink()
input = torch.randn(2)
output = m(input)

3、Hardsigmoid

公式:

Hardsigmoid(x)=\left\{\begin{matrix} 0, & x\leq -3 \\ 1, & x\geq +3 \\ \frac{x}{6}+\frac{1}{2}, & otherwise \end{matrix}\right.

图像:

 示例:

m = nn.Hardsigmoid()
input = torch.randn(2)
output = m(input)

4、Hardtanh

公式:

Hardsigmoid(x)=\left\{\begin{matrix} max\_val, & x>max\_val \\ min\_val, & x<min\_val \\ x, & otherwise \end{matrix}\right.

图像:

 示例:

m = nn.Hardtanh(-2, 2)
input = torch.randn(2)
output = m(input)

5、Hardswish

公式:

Hardswish(x)=\left\{\begin{matrix} 0, & x\leq -3 \\ x, & x\geq +3 \\ \frac{x\cdot \left ( x+3 \right )}{6}, & otherwise \end{matrix}\right.

图像:

 示例:

m = nn.Hardwish()
input = torch.randn(2)
output = m(input)

6、LeakyReLU

公式:

LeakyReLU(x)=\left\{\begin{matrix} x, & x\geq0\\ negetive\_slope\times x, & otherwise \end{matrix}\right.

图像:

示例:

m = nn.LeakyReLU(0.1)
input = torch.randn(2)
output = m(input)

7、LogSigmoid

公式:

LogSigmoid\left ( x \right )=log\left ( \frac{1}{1+exp(-x))} \right )

图像:

 

示例;

m = nn.LogSigmoid()
input = torch.randn(2)
output = m(input)

8、PReLU

公式:

PReLU(x)=\left\{\begin{matrix} x, & x\geq0 \\ ax, & otherwise \end{matrix}\right.

其中,a是可学习的参数。

图像:

 示例:

m = nn.PReLU()
input = torch.randn(2)
output = m(input)

9、ReLU

公式:

ReLU(x)=\left\{\begin{matrix} x, & x\geq0 \\ x, & otherwise \end{matrix}\right.

图像:

 示例:

m = nn.ReLU()
input = torch.randn(2)
output = m(input)

10、ReLU6

公式:

ReLU6=min(max(0,x),6)

图像:

 示例:

m = nn.ReLU6()
input = torch.randn(2)
output = m(input)

11、RReLU

公式:

RReLU(x)=\left\{\begin{matrix} x, & x\geq0 \\ ax, & otherwise \end{matrix}\right.

其中,a从均匀分布U(lower,upper)随机采样得到。

图像:

 示例:

m = nn.RReLU(0.1, 0.3)
input = torch.randn(2)
output = m(input)

12、SELU

公式:

SELU(x)=scale\ast (max(0,x)+min(0,\alpha\ast (exp(x)-1)))

其中,a=1.6732632423543772848170429916717,scale=1.0507009873554804934193349852946。

图像:

示例:

m = nn.SELU()
input = torch.randn(2)
output = m(input)

13、CELU

公式:

CELU\left (x \right )=max(0,x)+min(0,\alpha \ast (exp(x)-1))

图像:

 示例:

m = nn.CELU()
input = torch.randn(2)
output = m(input)

14、GELU

公式:

GELU(x)=0.5 \ast x \ast (1+Tanh(\sqrt{(2/\pi)} \ast (x+0.044715 \ast x^{3})))

图像:

 示例:

m = nn.GELU()
input = torch.randn(2)
output = m(input)

15、Sigmoid

公式:

Sigmoid(x)=\sigma (x)=\frac{1}{1+exp(-x)}

图像:

 示例:

m = nn. Sigmoid()
input = torch.randn(2)
output = m(input)

16、SiLU

公式:

SiLU(x)=x*\sigma (x)=x \ast \frac{1}{1+exp(-x)}

图像:

 示例:

m = nn.SiLU()
input = torch.randn(2)
output = m(input)

17、Mish

公式:

Mish(x)=x \ast Tanh(Softplus(x))

图像:

 示例:

m = nn.Mish()
input = torch.randn(2)
output = m(input)

18、Softplus

公式:

Softplus(x)=\frac{1}{\beta} \ast log(1+exp(\beta \ast x))

对于数值稳定性,当input \times \beta >threshold时,恢复到线性函数。

图像:

 示例:

m = nn.Softplus()
input = torch.randn(2)
output = m(input)

19、Softshrink

公式:

Softshrink(x)=\left\{\begin{matrix} x-\lambda, & x>\lambda\\ x+\lambda, & x<-\lambda\\ 0, & otherwise \end{matrix}\right.

图像:

 示例:

m = nn.Softshrink()
input = torch.randn(2)
output = m(input)

20、Softsign

公式:

SoftSign(x)=\frac{x}{1+\left | x \right |}

图像:

 示例:

m = nn.Softsign()
input = torch.randn(2)
output = m(input)

21、Tanh

公式:

Tanh(x)=tanh(x)=\frac{exp(x)-exp(-x)}{exp(x)+exp(-x)}

图像:

 示例:

m = nn.Tanh()
input = torch.randn(2)
output = m(input)

22、Tanhshrink

公式:

Tanhshrink(x)=x-tanh(x)

图像:

 示例:

m = nn.Tanhshrink()
input = torch.randn(2)
output = m(input)

23、Threshold

公式:

y=\left\{\begin{matrix} x, & x>threshold \\ value, & otherwise \end{matrix}\right.

示例:

m = nn.Threshold(0.1, 20)
input = torch.randn(2)
output = m(input)

24、GLU

公式:

GLU(a,b)=a\bigotimes \sigma(b)

其中,a是输入矩阵的前半部分,b是后半部分。

示例:

m = nn.GLU()
input = torch.randn(4, 2)
output = m(input)

25、Softmin

公式:

Softmin(x_i)=\frac{exp(-x_i)}{\sum_j exp(-x_j)}

示例:

m = nn.Softmin(dim=1)
input = torch.randn(2, 3)
output = m(input)

26、Softmax

公式:

Softmax(x_i)=\frac{exp(x_i)}{\sum_j exp(x_j)}

示例:

m = nn.Softmax(dim=1)
input = torch.randn(2, 3)
output = m(input)

27、LogSoftmax

公式:

LogSoftmax(x_i)=log\begin{pmatrix} \frac{exp(x_i)}{\sum_j exp(x_j)} \end{pmatrix}

示例:

m = nn.LogSoftmiax(dim=1)
input = torch.randn(2, 3)
output = m(input)

28、其它

还有MultiheadAttention、Softmax2d、AdaptiveLogSoftmaxWithLoss相对复杂一些没有添加,可去官网文档查看。


http://www.niftyadmin.cn/n/272626.html

相关文章

Android Signal 使用

一、官方 文档地址 示例代码 二、使用前说明 一定要注意使用的Signal 的版本号&#xff0c;如果客户端与服务端的版本不一致&#xff0c;可能会出现收不到消息的问题&#xff01;&#xff01;&#xff01; 三、使用 添加依赖 //signalr implementation com.microsoft.sign…

Vue3 Antd 父子嵌套子表格

Vue3 Antd 父子嵌套子表格 父子嵌套子表格 目标1&#xff1a;可以点击多个父节点表格&#xff0c;正确显示子表格数据 目标2&#xff1a;父表格数据刷新重载&#xff0c;解决子表格数据不刷新问题 官方示例代码&#xff0c;以及效果 https://www.antdv.com/components/tabl…

【Android -- 开源库】数据库 Realm 的基本使用

简介 Realm 是一个 MVCC &#xff08;多版本并发控制&#xff09;数据库&#xff0c;由Y Combinator公司在2014年7月发布一款支持运行在手机、平板和可穿戴设备上的嵌入式数据库&#xff0c;目标是取代 SQLite。Realm 本质上是一个嵌入式数据库&#xff0c;他并不是基于 SQLit…

边听歌边充电LDR6028+LDR9201既能充电又能OTG方案

随着type-c接口的普及&#xff0c;市面上的手机&#xff0c;平板&#xff0c;笔电逐渐都采用了type-c接口&#xff0c;设备为了不断的追求更轻薄的机身和防水要求慢慢的取消了一些影响手机外观完整性的接口&#xff0c;比如3.5mm耳机孔。 有线耳机用户一般会选择使用C口转3.5m…

二十三种设计模式第二篇--工厂模式

上篇我们了解了6条设计模式的准则&#xff0c;我相信如果你想了解设计模式&#xff0c;那么你迈出的第一步&#xff0c;我会将上一篇文档里边的6大准则进行一篇有关的代码展示&#xff0c;当然这是题外话了&#xff0c;本篇我们将重点围绕工厂模式进行讲解&#xff0c;天哪&…

OpenGL(一)——初识和搭建

目录 一、前言 二、概述 2.1 光学 2.2 三通道 2.3 上下文Context 2.4 渲染管线 2.5 着色器Shader 2.6 缓冲区和数组 三、安装 四、运行 五、库API 5.1 核心库GL 5.2 实用库GLUT 一、前言 渲染render是用软件从模型生成图像的过程&#xff0c;也表示编辑视频生成想达…

c# 数据保存为PDF(一) (spire pdf篇)

文章目录 前言了解 Spire使用Spire.PDF1 创建简单的PDF文档2 创建带有格式的PDF文档&#xff08;使用Draw&#xff09;头部信息页眉页脚测试数据完整的代码 3 创建带有格式的PDF文档&#xff08;使用Gird&#xff09;小结 先上一个效果图 前言 项目中需要将一些数据转存为PDF …

毕业小白的进阶之路 day1

摆烂的大学生活 学校里学到最多的也就是低阶算法以及python了&#xff0c;操作系统以及其他的就是学了忘压根不会应用。接触到最深的也就是Springboot了。学几年前淘汰的SSM框架真不知道是为了什么。最近临近毕业想要提升下自己&#xff0c;也就get到了Spring Cloud 框架。 S…