脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - pytorch绘制曲线的方法

pytorch绘制曲线的方法

2021-09-16 00:16pipi_LovelyGirl Python

这篇文章主要为大家详细介绍了pytorch绘制曲线的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了pytorch绘制曲线的具体代码,供大家参考,具体内容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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()处理后的函数显示出来。

运行结果:

pytorch绘制曲线的方法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/pipi_lovelygirl/article/details/114381413

延伸 · 阅读

精彩推荐