PyTorch中常用的激活函数的方法示例
作者:Steven·简谈 时间:2022-11-02 01:17:44
神经网络只是由两个或多个线性网络层叠加,并不能学到新的东西,简单地堆叠网络层,不经过非线性激活函数激活,学到的仍然是线性关系。
但是加入激活函数可以学到非线性的关系,就具有更强的能力去进行特征提取。
构造数据
import torch
import torch.nn.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt
x = torch.linspace(-5, 5, 200) # 构造一段连续的数据
x = Variable(x) # 转换成张量
x_np = x.data.numpy()# 换成 numpy array, 出图时用
Relu
表达式:
代码:
y_relu = F.relu(x).data.numpy()
plt.plot(x_np, y_relu, c='red', label='relu')
plt.ylim((-1, 5))
plt.legend(loc='best')
plt.show()
形状如图:
Sigmoid
表达式:
代码:
y_sigmoid = F.sigmoid(x).data.numpy()
plt.plot(x_np, y_sigmoid, c='red', label='sigmoid')
plt.ylim((-0.2, 1.2))
plt.legend(loc='best')
plt.show()
形状如图:
Tanh
表达式:
代码:
y_tanh = F.tanh(x).data.numpy()
plt.plot(x_np, y_tanh, c='red', label='tanh')
plt.ylim((-1.2, 1.2))
plt.legend(loc='best')
plt.show()
形状如图:
Softplus
表达式:
代码:
y_softplus = F.softplus(x).data.numpy()
plt.plot(x_np, y_softplus, c='red', label='softplus')
plt.ylim((-0.2, 6))
plt.legend(loc='best')
plt.show()
形状如图:
来源:https://blog.csdn.net/weixin_44613063/article/details/90240900
标签:PyTorch,激活函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
鼠标经过超链时的提示图层源码
2010-03-17 20:51:00
python multiply()与dot使用示例讲解
2021-08-14 19:34:52
经验丰富程序员才知道的8种高级Python技巧
2022-12-27 03:05:57
![](https://img.aspxhome.com/file/2023/3/121053_0s.jpg)
python实现堆和索引堆的代码示例
2021-09-21 21:42:05
Python for Informatics 第11章 正则表达式(一)
2021-01-27 06:43:43
原生js仿浏览器滚动条效果
2024-04-10 16:09:33
![](https://img.aspxhome.com/file/2023/7/136787_0s.png)
基于vue实现微博三方登录流程解析
2024-05-03 15:08:42
![](https://img.aspxhome.com/file/2023/5/130225_0s.png)
Python实现提取Excel指定关键词的行数据
2022-09-01 10:15:59
![](https://img.aspxhome.com/file/2023/4/67834_0s.jpg)
Python进阶:生成器 懒人版本的迭代器详解
2021-12-21 16:08:05
linux mysql 报错:MYSQL:The server quit without updating PID file
2024-01-22 08:40:47
Linux下rpm方式安装mysql教程
2024-01-21 07:40:53
mysql清除log-bin日志的方法
2024-01-15 08:22:30
mysql drop database删除数据库命令实例讲解
2024-01-18 19:40:01
15款非常有用的前端开发CSS网格(grid system)生成器
2009-07-06 13:02:00
![](https://img.aspxhome.com/file/UploadPic/20097/6/grid-layout-generator-by-pagecolumn-14s.jpg)
Goland 关闭自动移除未使用的包操作
2024-05-21 10:23:23
![](https://img.aspxhome.com/file/2023/3/123983_0s.jpg)
Python爬虫基础之简单说一下scrapy的框架结构
2022-01-04 23:19:00
![](https://img.aspxhome.com/file/2023/8/87598_0s.png)
PDO::beginTransaction讲解
2023-06-06 00:57:46
详解pycharm的python包opencv(cv2)无代码提示问题的解决
2022-01-10 06:45:34
![](https://img.aspxhome.com/file/2023/5/103755_0s.png)
实例探究Python以并发方式编写高性能端口扫描器的方法
2022-01-12 14:38:52
Django中ORM的基本使用教程
2021-09-10 00:35:46
![](https://img.aspxhome.com/file/2023/3/127583_0s.png)