pytorch中的 relu、sigmoid、tanh、softplus 函数

 四种基本激励函数是需要掌握的:

1.relu 

线性整流函数(Rectified Linear Unit, ReLU),又称修正线性单元, 是一种人工神经网络中常用的激活函数(activation function),通常指代以斜坡函数及其变种为代表的非线性函数。通常意义下,线性整流函数代指代数学中的斜坡函数,即

而在神经网络中,线性整流函数作为神经元的激活函数,定义了该神经元在线性变换  之后的的非线性输出结果。换言之,对于进入神经元的来自上一层神经网络的输入向量  ,使用线性整流激活函数的神经元会输出

至下一层神经元或作为整个神经网络的输出(取决现神经元在网络结构中所处位置)

2.sigmoid

Sigmoid函数是一个在生物学中常见的S型函数,也称为S型生长曲线。 [1]  在信息科学中,由于其单增以及反函数单增等性质,Sigmoid函数常被用作神经网络的阈值函数,将变量映射到0,1之间。

Sigmoid函数由下列公式定义

3.tanh函数

函数:y=tanh x;定义域:R,值域:(-1,1)。y=tanh x是一个奇函数,其函数图像为过原点并且穿越Ⅰ、Ⅲ象限的严格单调递增曲线,其图像被限制在两水平渐近线y=1和y=-1之间。

4.softplus函数

  https://blog.csdn.net/bqw18744018044/article/details/81193241

就是relu函数的平滑版本。


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)
x_np = x.data.numpy()   # numpy array for plotting

# 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
# y_softmax = torch.softmax(x, dim=0).data.numpy() softmax is a special kind of activation function, it is about probability

# plt to visualize these activation function
plt.figure(1, figsize=(8, 6))#横坐标与纵坐标
plt.subplot(221)
plt.plot(x_np, y_relu, c='red', label='relu')
plt.ylim((-1, 5))#设置纵坐标的范围
plt.legend(loc='best')

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

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

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

plt.show()

plt.grid()  # 生成网格

猜你喜欢

转载自blog.csdn.net/weixin_42528089/article/details/84865069
今日推荐