
【激活函数总结】Pytorch中的激活函数详解: ReLU、Leaky ReLU、Sigmoid、Tanh 以及 Softmax
【激活函数总结】Pytorch中的激活函数详解: ReLU、Leaky ReLU、Sigmoid、Tanh 以及 Softmax
《博主简介》
小伙伴们好,我是阿旭。专注于人工智能、AIGC、python、计算机视觉相关分享研究。
👍感谢小伙伴们点赞、关注!
《------往期经典推荐------》
二、机器学习实战专栏【链接】,已更新31期,欢迎关注,持续更新中~~
三、深度学习【Pytorch】专栏【链接】
四、【Stable Diffusion绘画系列】专栏【链接】
五、YOLOv8改进专栏【链接】,持续更新中~~
六、YOLO性能对比专栏【链接】,持续更新中~
《------正文------》
引言
在深度学习和神经网络领域,激活函数扮演着至关重要的角色,它们为模型引入了非线性因素,使得网络能够学习和模拟复杂的数据模式。
本文将深入探讨 PyTorch 中的各种激活函数,这些函数是构建高效神经网络的关键组成部分。我们将从激活函数的基本概念出发,逐步介绍 ReLU、Leaky ReLU、Sigmoid、Tanh 以及 Softmax 等常见激活函数的特点和使用方法,并通过 Python 代码示例来演示它们在实际应用中的效果。
跟随本文的脚步,你将更加了解如何选择合适的激活函数来优化你的神经网络模型。
什么是激活函数?
激活函数
是 Pytorch 的构建块。在了解激活函数的类型之前,让我们先了解人脑中神经元的工作原理。在人工神经网络中,我们有一个输入层,它是用户以某种格式输入的,一个隐藏层,它执行隐藏计算并识别特征,输出是结果。所以整个结构就像一个神经元相互连接的网络。所以我们有人工神经元,它们被这些激活函数激活。激活函数是一种执行计算以提供可作为下一个神经元输入的输出的函数。理想的激活函数应该使用线性概念处理非线性关系,并且应该是可微的,以减少错误并相应地调整权重。所有激活函数都存在于 torch.nn 库中。
Pytorch 激活函数的类型
让我们看看不同的 Pytorch 激活函数:
- ReLU 激活函数
- Leaky ReLU 激活函数
- Sigmoid 激活函数
- Tanh 激活函数
- Softmax 激活函数
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)
*输出:*
tensor([1., 0., 3., 0.])
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)xxxxxxxxxx # codeimport torchimport torch.nn as nn# defining Lrelu and the parameter 0.2 is passed to control the negative slope ; a=0.2r = nn.LeakyReLU(0.2)# Creating a Tensor with an array input = torch.Tensor([1,-2,3,-5])output = r(input) print(output)# 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)
*输出:*
tensor([ 1.0000, -0.4000, 3.0000, -1.0000])
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)
*输出:*
tensor([0.7311, 0.1192, 0.9526, 0.0067])
Tanh 激活函数
Tanh 函数是一种非线性可微函数,类似于 S 型函数,但输出值范围从 -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)
*输出:*
tensor([0.7311, 0.1192, 0.9526, 0.0067])
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)
*输出:*
tensor([0.7311, 0.1192, 0.9526, 0.0067])
好了,这篇文章就介绍到这里,喜欢的小伙伴感谢给点个赞和关注,更多精彩内容持续更新~~
关于本篇文章大家有任何建议或意见,欢迎在评论区留言交流!
更多推荐
所有评论(0)