作者:jhimlic1 编译:东岸因为@一点人工一点智能
入群邀请:7个专业方向交流群+1个资料需求群
01 什么是激活函数,为什么要使用它们?
在了解激活函数的类型之前,让我们先了解一下人工神经元的工作原理。
在人工神经网络中,我们有一个输入层,用户以某种格式输入数据,隐藏层执行隐藏计算并识别特征,输出是结果。因此,整个结构就像一个互相连接的神经元网络。
我们有人工神经元,这些神经元通过这些激活函数被激活。激活函数是一个执行计算的函数,提供一个可能作为下一个神经元输入的输出。理想的激活函数应该通过使用线性概念处理非线性关系,并且应该可微分,以减少错误并相应地调整权重。所有的激活函数都存在于torch.nn库中。
02 Pytorch激活函数的类型
让我们来看一下不同的Pytorch激活函数:
· ReLU激活函数
· Leaky ReLU激活函数
· Sigmoid激活函数
· Tanh激活函数
· Softmax激活函数
2.1 ReLU激活函数
ReLU代表修正线性激活函数。它是一个非线性函数,图形上ReLU具有以下转换行为:
ReLU是一种主流的激活函数,因为它是可微分且非线性的。如果输入是负数,则其导数变为零,导致神经元“死亡”,无法进行学习。让我们通过Python程序来说明如何使用ReLU。
import torch
import torch.nn as nn
# defining relu
r = nn.ReLU()
# Creating a Tensor with an array
input = torch.Tensor([1, -2, 3, -5])
# Passing the array to relu function
output = r(input)
print(output)
Output:
tensor([1., 0., 3., 0.])
2.2 Leaky ReLU激活函数
Leaky ReLU激活函数或LReLU是另一种类似于ReLU的激活函数,它解决了“死亡”神经元的问题,图形上Leaky ReLU具有以下转换行为:
这个函数非常有用,因为当输入为负数时,函数的导数不为零。因此,神经元的学习不会停止。让我们用Python程序来说明LReLU的使用。
# code
import torch
import torch.nn as nn
# defining Lrelu and the parameter 0.2 is passed to control the negative slope ; a=0.2
r = nn.LeakyReLU(0.2)
# Creating a Tensor with an array
input = torch.Tensor([1,-2,3,-5])
output = r(input)
print(output)
Output:
tensor([ 1.0000, -0.4000, 3.0000, -1.0000])
2.3 Sigmoid激活函数
Sigmoid函数是一种非线性且可微分的激活函数。它是一条S形曲线,不经过原点。它产生的输出值介于0和1之间。输出值通常被视为概率。它经常用于二分类。计算速度较慢,并且在图形上,Sigmoid具有以下转换行为:
Sigmoid激活函数存在“梯度消失”问题。梯度消失是一个重要问题,当大量输入被馈送到神经网络并且隐藏层数增加时,梯度或导数接近于零,从而导致神经网络的不准确性。
让我们通过一个Python程序来说明Sigmoid函数的使用。
import torch
import torch.nn as nn
# Calling the sigmoid function
sig = nn.Sigmoid()
# Defining tensor
input = torch.Tensor([1,-2,3,-5])
# Applying sigmoid to the tensor
output = sig(input)
print(output)
Output:
tensor([0.7311, 0.1192, 0.9526, 0.0067])
2.4 Tanh激活函数
Tanh函数是一种非线性且可微的函数,类似于Sigmoid函数,但输出值的范围是从-1到+1。它是一个S形曲线,通过原点,并且在图形上,Tanh函数具有以下的变换行为:
Tanh激活函数的问题在于它运算速度较慢且梯度消失问题仍然存在。让我们借助Python程序来说明Tanh函数的使用。
import torch
import torch.nn as nn
# Calling the Tanh function
t = nn.Tanh()
# Defining tensor
input = torch.Tensor([1,-2,3,-5])
# Applying Tanh to the tensor
output = t(input)
print(output)
Output:
tensor([0.7311, 0.1192, 0.9526, 0.0067])
2.5 Softmax激活函数
Softmax函数与其他激活函数不同,它被放置在最后以对输出进行归一化。我们可以将其他激活函数与Softmax结合使用,以产生概率形式的输出。它用于多类分类,并生成其总和为1的概率输出。输出的范围在0和1之间。Softmax具有以下转换行为:
让我们通过Python程序来进行说明:
import torch
import torch.nn as nn
# Calling the Softmax function with
# dimension = 0 as dimension starts
# from 0
sm = nn.Softmax(dim=0)
# Defining tensor
input = torch.Tensor([1,-2,3,-5])
# Applying function to the tensor
output = sm(input)
print(output)
Output:
tensor([0.7311, 0.1192, 0.9526, 0.0067])