pytorch绘制曲线的方法
作者:pipi_LovelyGirl 时间:2022-09-03 06:43:54
本文实例为大家分享了pytorch绘制曲线的具体代码,供大家参考,具体内容如下
import torch
import torch.nn.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt
# fake data
x = torch.linspace(-5, 5, 200) # x data (tensor), shape=(100, 1)
x = Variable(x) #创建 variable(变量),构造神经网络要使用Variable类型
x_np = x.data.numpy() # numpy array for plotting,用于绘图的numpy数组
# following are popular activation functions,以下是常用的激活函数
y_relu = torch.relu(x).data.numpy()
y_sigmoid = torch.sigmoid(x).data.numpy()
y_tanh = torch.tanh(x).data.numpy()
y_softplus = F.softplus(x).data.numpy() # there's no softplus in torch。torch没有softplus
# y_softmax = torch.softmax(x, dim=0).data.numpy() softmax is a special kind of activation function, it is about probability
#softmax是一种特殊的激活函数,它与概率有关
# plt to visualize these activation function
#将这些激活函数可视化
plt.figure(1, figsize=(8, 6)) # 横坐标与纵坐标
plt.subplot(221)
#plt.subplot()函数用于直接指定划分方式和位置进行绘图。
# 使用plt.subplot来创建小图. plt.subplot(221)表示将整个图像窗口分为2行2列, 当前位置为1.
plt.plot(x_np, y_relu, c='red', label='relu')
#plt.plot(x,y,format_string,**kwargs)
#x轴数据,y轴数据,format_string控制曲线的格式字串
#format_string 由颜色字符,风格字符,和标记字符
plt.ylim((-1, 5)) # 设置纵坐标的范围
plt.legend(loc='best')#plt.legend()函数的作用是给图像加图例。,就左上角relu那个
#图例是集中于地图一角或一侧的地图上各种符号和颜色所代表内容与指标的说明,有助于更好的认识地图
plt.subplot(222)# 使用plt.subplot来创建小图. plt.subplot(221)表示将整个图像窗口分为2行2列, 当前位置为2.
plt.plot(x_np, y_sigmoid, c='red', label='sigmoid')
#plt.plot(x,y,format_string,**kwargs)
#x轴数据,y轴数据,format_string控制曲线的格式字串
#format_string 由颜色字符,风格字符,和标记字符
plt.ylim((-0.2, 1.2)) # 设置纵坐标的范围
plt.legend(loc='best')#plt.legend()函数的作用是给图像加图例。,就左上角relu那个
#图例是集中于地图一角或一侧的地图上各种符号和颜色所代表内容与指标的说明,有助于更好的认识地图
plt.subplot(223)# 使用plt.subplot来创建小图. plt.subplot(221)表示将整个图像窗口分为2行2列, 当前位置为3.
plt.plot(x_np, y_tanh, c='red', label='tanh')
#plt.plot(x,y,format_string,**kwargs)
#x轴数据,y轴数据,format_string控制曲线的格式字串
#format_string 由颜色字符,风格字符,和标记字符
plt.ylim((-1.2, 1.2))# 设置纵坐标的范围
plt.legend(loc='best')#plt.legend()函数的作用是给图像加图例。,就左上角relu那个
#图例是集中于地图一角或一侧的地图上各种符号和颜色所代表内容与指标的说明,有助于更好的认识地图
plt.subplot(224)# 使用plt.subplot来创建小图. plt.subplot(221)表示将整个图像窗口分为2行2列, 当前位置为4.
plt.plot(x_np, y_softplus, c='red', label='softplus')
#plt.plot(x,y,format_string,**kwargs)
#x轴数据,y轴数据,format_string控制曲线的格式字串
#format_string 由颜色字符,风格字符,和标记字符
plt.ylim((-0.2, 6))# 设置纵坐标的范围
plt.legend(loc='best')#plt.legend()函数的作用是给图像加图例。,就左上角relu那个
#图例是集中于地图一角或一侧的地图上各种符号和颜色所代表内容与指标的说明,有助于更好的认识地图
plt.show()
#plt.show()则是将plt.imshow()处理后的函数显示出来。
运行结果:
来源:https://blog.csdn.net/pipi_lovelygirl/article/details/114381413
标签:pytorch,曲线
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp截取指定英汉混合字符串_支持中文
2011-04-19 10:39:00
网马解密大讲堂——网马解密初级篇
2009-09-16 14:45:00
Python3中常见配置文件写法汇总
2022-01-21 11:14:07
![](https://img.aspxhome.com/file/2023/0/110040_0s.png)
asp 去除最后一个逗号为空字符串的代码
2010-06-09 19:18:00
Python标准库defaultdict模块使用示例
2022-03-02 04:35:05
如何在Django配置文件里配置session链接
2022-09-08 18:17:33
用python3读取python2的pickle数据方式
2023-06-05 09:45:48
关注oblog 关注xml-rpc 自己开发客户端小记
2009-10-04 20:27:00
Python中url标签使用知识点总结
2022-05-12 11:13:09
![](https://img.aspxhome.com/file/2023/5/79465_0s.png)
Python自动化办公之Excel拆分与自动发邮件
2022-05-03 09:05:40
![](https://img.aspxhome.com/file/2023/4/93464_0s.png)
Django开发的简易留言板案例详解
2022-05-05 00:24:40
![](https://img.aspxhome.com/file/2023/5/85055_0s.png)
js表单提交显示进度条
2007-09-18 13:10:00
Python pyecharts绘制折线图详解
2021-05-22 17:42:49
![](https://img.aspxhome.com/file/2023/3/93623_0s.png)
微软证实最新的关键SQL Server漏洞
2008-12-23 13:31:00
python的多重继承的理解
2021-04-27 16:54:21
![](https://img.aspxhome.com/file/2023/7/101737_0s.jpg)
ASP基础教程之学习ASP中子程序的应用
2008-10-16 10:53:00
python实现五子棋程序
2022-05-26 23:24:31
![](https://img.aspxhome.com/file/2023/3/65973_0s.gif)
Python爬虫Requests库的使用详情
2023-05-22 20:15:52
![](https://img.aspxhome.com/file/2023/3/92173_0s.png)
Python中List.count()方法的使用教程
2023-08-12 11:56:15
Python时间的精准正则匹配方法分析
2022-12-10 12:59:28