pytorch白话入门笔记1.3-激励函数

激励函数图像显示:

代码:

import torch
import torch.nn.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt

#fake data
x = torch.linspace(-6,6,400)#x data(tensor),shape = (100,1)
x = Variable(x)
x_np = x.data.numpy()

y_relu = F.relu(x).data.numpy()
y_sigmoid = F.torch.sigmoid(x).data.numpy()
y_tanh = F.torch.tanh(x).data.numpy()
y_softplus = F.softplus(x).data.numpy()
#y_softmax = F.softmax(x)是激励函数,但不能用线图呈现出来

#画图
plt.figure(1,figsize=(10,8))
plt.subplot(221)
plt.plot(x_np,y_relu,c = 'red',label = 'relu')
plt.ylim(-1,7)
plt.legend(loc = 'best')


plt.subplot(222)
plt.plot(x_np,y_sigmoid,c = 'red',label = 'sigmoid')
plt.ylim(-0.2,1.6)
plt.legend(loc = 'best')

plt.subplot(223)
plt.plot(x_np,y_tanh,c = 'red',label = 'tanh')
plt.ylim(-1.2,1.6)
plt.legend(loc = 'best')

plt.subplot(224)
plt.plot(x_np,y_softplus,c = 'red',label = 'softplus')
plt.ylim(-0.5,6)
plt.legend(loc = 'best')

plt.savefig('./act_func.jpg')
plt.show()

运行结果:

原创文章 23 获赞 1 访问量 731

猜你喜欢

转载自blog.csdn.net/BSZJYAJ/article/details/105126991