PyTorch教程(5)激活函数

article/2025/9/27 4:45:47

问题

激活函数是什么?它们在实际项目中是如何工作的?如何使用PyTorch实现激活函数?

解答

激活函数是一个数学公式,它根据数学转换函数的类型将二进制、浮点或整数格式的向量转换为另一种格式。神经元存在于不同的层——输入层、隐藏层和输出层,它们通过一个称为激活函数的数学函数相互连接。激活函数有不同的变体,下面将对此进行解释。理解激活函数有助于准确地实现神经网络模型。

作用原理

神经网络模型中所有的激活函数可以大致分为线性函数和非线性函数。PyTorch的torch.nn模块创建任何类型的神经网络模型。让我们看一些使用PyTorch和torch.nn模块中的激活函数的示例。

PyTorch和TensorFlow之间的核心区别在于计算图的定义方式、两个框架执行计算的方式,以及我们在修改脚本和引入其他基于python的库时所拥有的灵活性。在TensorFlow中,我们需要在初始化模型之前定义变量和占位符。我们还需要跟踪以后需要的对象,为此我们需要一个占位符。在TensorFlow中,我们需要先定义模型,然后编译运行;然而,在PyTorch中,我们可以随心所欲地定义模型——不必在代码中保留占位符。这就是为什么PyTorch框架是动态的原因。

线性函数

线性函数是一种简单的函数,通常用于从映射层向输出层传递信息。我们在数据变化较小的地方使用线性函数。在深度学习模型中,实践者通常在最后一个隐藏层到输出层之间使用线性函数。在线性函数中,输出总是被限制在一个特定的范围内;因此,它被用于深度学习模型的最后一个隐藏层,或基于线性回归的任务,或在深度学习模型的任务是预测从输入数据集的结果。公式为:
在这里插入图片描述

双线性函数

双线性函数是一种用于传递信息的简单函数。它对输入数据应用双线性变换。
在这里插入图片描述

import numpy as np
import torch
import torch.nn as nnx1 = torch.randn(100, 10)
x2 = torch.randn(100, 30)
linear = nn.Linear(in_features=10, out_features=5, bias=True)
output_linear = linear(x1)
print("output size:", output_linear.size())
bilinear = nn.Bilinear(in1_features=10, in2_features=30, out_features=5, bias=True)
output_bilinear = bilinear(x1, x2)
print("output size:", output_bilinear.size())
# output size: torch.Size([100, 5])
# output size: torch.Size([100, 5])weight = bilinear.weight.data.cpu().numpy()
bias = bilinear.bias.data.cpu().numpy()
np_x1 = x1.data.cpu().numpy()
np_x2 = x2.data.cpu().numpy()
print(np_x1.shape,weight.shape,np_x2.shape,bias.shape)
# (100, 10) (5, 10, 30) (100, 30) (5,)
y = np.zeros((np_x1.shape[0],weight.shape[0]))
# y.shape (100,5)
for k in range(weight.shape[0]):buff = np.dot(np_x1, weight[k])print(buff.shape)# buff.shape (100, 30)buff = buff * np_x2buff = np.sum(buff,axis=1)print(buff.shape)# buff.shape (100,)y[:,k] = buff
y += bias
dif = y - output_bilinear.data.cpu().numpy()
print(np.mean(np.abs(dif.flatten())))
# 2.2480543702840806e-07

Sigmoid函数

在数据挖掘和分析中,专业人员经常使用sigmoid函数,因为它更容易解释和实现。它是一个非线性函数。当我们将权值从神经网络的输入层传递到隐含层时,我们希望我们的模型能够捕获数据中呈现的所有非线性;因此,建议在神经网络的隐层中使用sigmoid函数。非线性函数有助于泛化数据集。用非线性函数计算函数的梯度比较容易。

sigmoid函数是一种特殊的非线性激活函数。sigmoid函数的输出总是限制在0和1之内;因此,它主要用于执行基于分类的任务。sigmoid函数的局限性之一是它可能会陷入局部极小值。这样做的好处是,它提供了属于这类的可能性。下面是它的方程。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

import torchx = torch.randn(100, 10)
y = torch.randn(100,30)
sig=nn.Sigmoid()
output_sigx = sig(x)
output_sigy = sig(y)
print("Output size:", output_sigx.size())
print("Output size:", output_sigy.size())
print(x[0])
print(output_sigx[0])
# Output size: torch.Size([100, 10])
# Output size: torch.Size([100, 30])
# tensor([ 0.4357, -1.5673, -0.8468,  0.0629, -1.2619, -0.2609, -0.3565,  0.5630, 1.8491,  0.3339])
# tensor([0.6072, 0.1726, 0.3001, 0.5157, 0.2206, 0.4351, 0.4118, 0.6372, 0.8640, 0.5827])class Sigmoid(nn.Module):def __init__(self):super(Sigmoid, self).__init__()def forward(self, x):return 1./(1. + torch.exp(-x))class Sigmoid(nn.Module):def __init__(self):super(Sigmoid, self).__init__()def forward(self, x):return F.sigmoid(x)

双曲正切

双曲正切函数是变换函数的另一种变体。它用于将信息从映射层转换到隐藏层。它通常用于神经网络模型的隐藏层之间。tanh函数的取值范围是-1到+1。公式如下:

在这里插入图片描述在这里插入图片描述

import torchx = torch.randn(100, 10)
y = torch.randn(100,30)func = nn.Tanh()
output_x = func(x)
output_y = func(y)
print("Output size:", output_x.size())
print("Output size:", output_y.size())
# Output size: torch.Size([100, 10])
# Output size: torch.Size([100, 30])
print(x[0])
print(output_x[0])
# tensor([-0.2978, -1.0893, -0.3137, -0.5245, -0.1842,  1.6686, -0.7928, -2.2920, -0.8125, -1.9004])
# tensor([-0.2893, -0.7966, -0.3038, -0.4812, -0.1822,  0.9314, -0.6600, -0.9798,-0.6709, -0.9563])class Tanh(nn.Module):def __init__(self):super(Tanh, self).__init__()def forward(self, x):return (torch.exp(x)-torch.exp(-x))/(torch.exp(x)+torch.exp(-x))class Tanh(nn.Module):def __init__(self):super(Tanh, self).__init__()def forward(self, x):return F.tanh(x)

Log Sigmoid 函数

下面的公式解释了用于将输入层映射到隐藏层的log sigmoid传递函数。如果数据不是二进制的,并且它是一个浮点类型,有很多异常值(如输入特征中出现的大数值),那么我们应该使用log sigmoid传递函数。
在这里插入图片描述
在这里插入图片描述

import torch
x = torch.randn(100, 10)
y = torch.randn(100,30)func = nn.LogSigmoid()
output_x = func(x)
output_y = func(y)
print("Output size:", output_x.size())
print("Output size:", output_y.size())
# Output size: torch.Size([100, 10])
# Output size: torch.Size([100, 30])
print(x[0])
print(output_x[0])
# tensor([-0.6855,  1.1084,  1.0223,  0.8324,  1.0061, -1.4613,  0.7731,  0.0463,0.5831, -0.6826])
# tensor([-1.0935, -0.2852, -0.3073, -0.3612, -0.3116, -1.6699, -0.3795, -0.6702,-0.4435, -1.0916])

ReLU函数

ReLU是另一个激活函数。它用于将信息从输入层传送到输出层。ReLU主要用于卷积神经网络模型。这个激活函数的输出范围是从0到无穷。它主要用于神经网络模型中不同隐藏层之间。
在这里插入图片描述
在这里插入图片描述

import torch
import torch.nn as nn
x = torch.randn(100, 10)
y = torch.randn(100, 30)func = nn.ReLU()
output_x = func(x)
output_y = func(y)
print("Output size:", output_x.size())
print("Output size:", output_y.size())
# Output size: torch.Size([100, 10])
# Output size: torch.Size([100, 30])
print(x[0])
print(output_x[0])
# tensor([ 0.5594,  0.2387, -1.5372, -0.6874,  1.1714, -0.1458, -1.1784, -0.0802,-0.5905,  2.2499])
# tensor([0.5594, 0.2387, 0.0000, 0.0000, 1.1714, 0.0000, 0.0000, 0.0000, 0.0000,2.2499])def relu(inX):return np.maximum(0,inX)

在神经网络结构中,不同类型的传递函数是可以互换的。它们可以在输入到隐含层、隐含层到输出层等不同阶段使用,以提高模型的精度。

ReLU6函数

Relu在x>0的区域使用x进行线性激活,有可能造成激活后的值太大,影响模型的稳定性,为抵消ReLU激励函数的线性增长部分,可以使用Relu6函数。

ReLU激活函数和导函数分别为:
在这里插入图片描述
在这里插入图片描述

import torch
import torch.nn as nn
x = torch.randn(100, 10)* 10
y = torch.randn(100, 30) func = nn.ReLU6()
output_x = func(x)
output_y = func(y)
print("Output size:", output_x.size())
print("Output size:", output_y.size())
# Output size: torch.Size([100, 10])
# Output size: torch.Size([100, 30])
print(x[0])
print(output_x[0])
# tensor([  8.1576,  11.7024,  -3.9226,  -2.6406,  11.0565, -16.3319,   7.0528, 0.8866,   0.6748,  -1.0607])
# tensor([6.0000, 6.0000, 0.0000, 0.0000, 6.0000, 0.0000, 6.0000, 0.8866, 0.6748, 0.0000])

Leaky ReLU函数

在标准的神经网络模型中,死亡梯度问题是常见的。为了避免这个问题,应用了leaky ReLU。Leaky ReLU允许一个小的非零梯度。

LeakyReLU函数与倒数分别为:
在这里插入图片描述
在这里插入图片描述

import torch
import torch.nn as nn
x = torch.randn(100, 10)
y = torch.randn(100, 30)func = nn.LeakyReLU()
output_x = func(x)
output_y = func(y)
print("Output size:", output_x.size())
print("Output size:", output_y.size())
# Output size: torch.Size([100, 10])
# Output size: torch.Size([100, 30])
print(x[0])
print(output_x[0])
# tensor([ 1.0718, -0.3105, -0.2773,  1.8701,  0.2646, -0.2166,  1.1425,  1.1959,-2.7848, -0.2138])
# tensor([ 1.0718, -0.0031, -0.0028,  1.8701,  0.2646, -0.0022,  1.1425,  1.1959, -0.0278, -0.0021])

PRELU函数

在这里插入图片描述
这里a是一个可学习的参数。当不带参数调用时,nn.PReLU()在所有输入通道中使用单个参数a。如果使用nn.PReLU(nChannels)调用,则每个输入通道使用一个单独的a。n_channels表示第二维度数量。
在这里插入图片描述

import torch
import torch.nn as nn
x = torch.randn(100, 10)
# torch.nn.PReLU(num_parameters=1, init=0.25, device=None, dtype=None)
# num_parameters表示要学习的a的数量。尽管它接受一个int作为输入,但只有两个值是合法的:1,或者输入处的通道数量。默认值:1
# init:a的初始值,默认是0.25
func = nn.PReLU(10)
y = func(x)
print(x[0])
print(y[0])
# tensor([-0.5982, -0.5509, -0.7107, -0.4316, -0.5107,  0.4100, -1.3367,  1.5206, -2.4008, -1.1881])
# tensor([-0.1495, -0.1377, -0.1777, -0.1079, -0.1277,  0.4100, -0.3342,  1.5206, -0.6002, -0.2970], grad_fn=<SelectBackward>)

RReLU函数

在RReLU中,负值的斜率在训练中是随机的,在之后的测试中就变成了固定的了。RReLU的亮点在于,在训练环节中,a是从一个均匀的分布U(I,u)中随机抽取的数值。
在这里插入图片描述
RReLU中的a是一个在一个给定的范围内随机抽取的值,这个值在测试环节就会固定下来。
在这里插入图片描述

import torch
import torch.nn as nn
x = torch.randn(100, 10)
# torch.nn.RReLU(lower=0.125, upper=0.3333333333333333, inplace=False)
func = nn.RReLU(0.1, 0.3)
y = func(x)
print(x[0])
print(y[0])
# tensor([-0.4467,  0.3433,  0.0522,  0.5509,  1.7010, -0.1909,  0.6914, -0.6053, 0.5511, -1.9335])
# tensor([-0.0514,  0.3433,  0.0522,  0.5509,  1.7010, -0.0270,  0.6914, -0.1125, 0.5511, -0.5593])

ELU函数

在这里插入图片描述
α \alpha α:默认为1.0
在这里插入图片描述

其中α是一个可调整的参数,它控制着ELU负值部分在何时饱和。右侧线性部分使得ELU能够缓解梯度消失,而左侧软饱能够让ELU对输入变化或噪声更鲁棒。ELU的输出均值接近于零,所以收敛速度更快.
在这里插入图片描述

import torch
import torch.nn as nn
x = torch.randn(100, 10)
# torch.nn.SELU(inplace=False)
func = nn.ELU()
y = func(x)
print(x[0])
print(y[0])
# tensor([-0.6763,  0.6669, -2.0544, -1.8889, -0.3581,  0.0884, -1.3734,  0.9181,0.4205,  0.1281])
# tensor([-0.4915,  0.6669, -0.8718, -0.8488, -0.3010,  0.0884, -0.7467,  0.9181,0.4205,  0.1281])import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as pltclass ELU(nn.Module):def __init__(self):super().__init__()def forward(self,x):x = F.elu(x)return xclass ELU(nn.Module):def __init__(self, alpha=1.0):super(ELU, self).__init__()self.alpha=alphadef forward(self, x):temp1 = F.relu(x)temp2 = self.alpha * (-1*F.relu(1-torch.exp(x)))return temp1 + temp2
x = torch.linspace(-10, 10, 1000).unsqueeze(0)func = nn.ELU()
func2 = ELU()y1 = func(x)
y2 = func2(x)plt.plot(x.detach().numpy()[0],y1.detach().numpy()[0],label="PyTorch")
plt.plot(x.detach().numpy()[0],y2.detach().numpy()[0],label="myELU")
plt.legend()
plt.grid()
plt.show()

在这里插入图片描述

CELU函数

在这里插入图片描述
α \alpha α:默认为1.0
在这里插入图片描述

import torch
import torch.nn as nn
x = torch.randn(100, 10)
# torch.nn.SELU(inplace=False)
func = nn.CELU()
y = func(x)
print(x[0])
print(y[0])
# tensor([-1.1315, -0.3425,  0.7596, -1.8625, -0.8638,  0.2654,  0.9773, -0.5946, 0.1348, -0.7941])
# tensor([-0.6775, -0.2900,  0.7596, -0.8447, -0.5785,  0.2654,  0.9773, -0.4482, 0.1348, -0.5480])import torch
import torch.nn as nn
import torch.nn.functional as F
from matplotlib import pyplot as plt
class CELU(nn.Module):def __init__(self, alpha=1.0):super(CELU, self).__init__()self.alpha=alphadef forward(self, x):temp1 = F.relu(x)# temp2 = self.alpha * (F.elu(-1*F.relu(-1.*x/self.alpha)))temp2 = self.alpha * (-1*F.relu(1-torch.exp(x/self.alpha)))return temp1 + temp2x = torch.linspace(-10, 10, 1000).unsqueeze(0)func = nn.CELU()
func2 = CELU()y1 = func(x)
y2 = func2(x)plt.plot(x.detach().numpy()[0],y1.detach().numpy()[0],label="PyTorch")
plt.plot(x.detach().numpy()[0],y2.detach().numpy()[0],label="myCELU")
plt.legend()
plt.grid()
plt.show()

在这里插入图片描述

SELU函数

在这里插入图片描述
此处的scale就是下方的 λ \lambda λ
在这里插入图片描述

在这里插入图片描述
当使用kaiming_normal或kaiming_normal_进行初始化时,为了得到自归一化的神经网络,应该使用nonlinearity='linear’而不是nonlinearity=‘selu’。

经过该激活函数后使得样本分布自动归一化到0均值和单位方差(自归一化,保证训练过程中梯度不会爆炸或消失,效果比Batch Normalization 要好)

关键在于这个 λ \lambda λ是大于1的,在方差过小的的时候可以让它增大,同时防止了梯度消失。

import torch
import torch.nn as nn
import torch.nn.functional as F
from matplotlib import pyplot as plt
class SELU(nn.Module):def __init__(self):super(SELU, self).__init__()self.alpha = 1.6732632423543772848170429916717self.scale = 1.0507009873554804934193349852946def forward(self, x):temp1 = self.scale * F.relu(x)# temp2 = self.scale * self.alpha * (F.elu(-1*F.relu(-1*x)))temp2 = self.scale * self.alpha * (-1*F.relu(1-torch.exp(x)))return temp1 + temp2x = torch.linspace(-10, 10, 1000).unsqueeze(0)func2 = nn.SELU()
func = SELU()y1 = func(x)
y2 = func2(x)plt.plot(x.detach().numpy()[0],y1.detach().numpy()[0],label="mySELU")
plt.plot(x.detach().numpy()[0],y2.detach().numpy()[0],label="PyTorch")
plt.legend()
plt.grid()
plt.show()

在这里插入图片描述

SiLU函数(swish)

在这里插入图片描述 在这里插入图片描述
在这里插入图片描述

import torch
import torch.nn as nn
import torch.nn.functional as F
from matplotlib import pyplot as plt
class SiLU(nn.Module):def __init__(self):super(SiLU, self).__init__()def forward(self, x):return x * F.sigmoid(x)x = torch.linspace(-10, 10, 1000).unsqueeze(0)
# torch.nn.SELU(inplace=False)
func = nn.SiLU()
func2 = SiLU()y1 = func(x)
y2 = func2(x)plt.plot(x.detach().numpy()[0],y1.detach().numpy()[0],label="mySiLU")
plt.plot(x.detach().numpy()[0],y2.detach().numpy()[0],label="PyTorch")
plt.legend()
plt.grid()
plt.show()

在这里插入图片描述

Hardswish函数

在这里插入图片描述
数值稳定性好,计算速度快等优点。

import torch
import torch.nn as nn
import torch.nn.functional as F
from matplotlib import pyplot as plt'''Hard Swish Module'''
class HardSwish(nn.Module):def __init__(self, inplace=False):super(HardSwish, self).__init__()self.act = nn.ReLU6(inplace)'''forward'''def forward(self, x):return x * self.act(x + 3) / 6x = torch.arange(-50, 50).unsqueeze(0)
func = HardSwish()
output_x = func(x)
plt.plot(x.detach().numpy()[0],output_x.detach().numpy()[0],label="HardSwish")
plt.legend()
plt.grid()
plt.show()

在这里插入图片描述

HardSigmod函数

在这里插入图片描述

import torch
import torch.nn as nn'''Hard Sigmoid Module'''
class HardSigmoid(nn.Module):def __init__(self, bias=1.0, divisor=2.0, min_value=0.0, max_value=1.0):super(HardSigmoid, self).__init__()assert divisor != 0, 'divisor is not allowed to be equal to zero'self.bias = biasself.divisor = divisorself.min_value = min_valueself.max_value = max_value'''forward'''def forward(self, x):x = (x + self.bias) / self.divisorreturn x.clamp_(self.min_value, self.max_value)
x = torch.linspace(-5, 5, 100).unsqueeze(0)
func = HardSigmoid()
output_x = func(x)
plt.plot(x.detach().numpy()[0],output_x.detach().numpy()[0],label="HardSigmoid")
plt.legend()
plt.grid()
plt.show()

在这里插入图片描述

Softplus函数

SoftPlus是一个平滑的近似于ReLU函数,可以用来约束输出总是正的。
在这里插入图片描述
在这里插入图片描述

import numpy as np
x = np.linspace(-10, 10, 100)
y1 = np.log(1 + np.exp(x))
y2 = np.maximum(0, x)
plt.plot(x,y1,label="softplus")
plt.plot(x,y2,label="relu")
plt.legend()
plt.show()import torch
import torch.nn as nn
x = torch.randn(100, 10)
y = torch.randn(100, 30)
# torch.nn.Softplus(beta=1, threshold=20)
func = nn.Softplus()
output_x = func(x)
output_y = func(y)
print("Output size:", output_x.size())
print("Output size:", output_y.size())
# Output size: torch.Size([100, 10])
# Output size: torch.Size([100, 30])
print(x[0])
print(output_x[0])
# tensor([ 2.6387,  0.6161, -0.0684, -0.3626,  2.2955, -0.1481, -0.1116,  0.1727, 0.2522,  0.6943])
# tensor([2.7077, 1.0479, 0.6595, 0.5282, 2.3915, 0.6218, 0.6389, 0.7832, 0.8272, 1.0994])class Softplus(nn.Module):def __init__(self, beta=1):super(Softplus, self).__init__()self.beta = betaassert beta !=0, "beta should not be equal to 0"def forward(self, x):return 1./beta * torch.log(1 + torch.exp(beta * x))
class Softplus(nn.Module):def __init__(self, beta=1.):super(Softplus, self).__init__()self.beta = betaassert beta !=0, "beta should not be equal to 0"def forward(self, x):return F.softplus(x, beta)

Mish函数

在这里插入图片描述

# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
from matplotlib import pyplot as pltclass Mish(nn.Module):def __init__(self):super().__init__()print("Mish activation loaded...")def forward(self,x):x = x * (torch.tanh(F.softplus(x)))return xmish = Mish()
x = torch.linspace(-10,10,1000)
y1 = mish(x)
# func = nn.Mish()  # PyTorch1.9才有Mish激活函数
# y2 = func(x)plt.plot(x,y1,label="myMish")
# plt.plot(x,y2,label="PyTorch")
plt.legend()
plt.show()

在这里插入图片描述

GELU函数

高斯误差线性单元激活函数在最近的 Transformer 模型(谷歌的 BERT 和 OpenAI 的 GPT-2)中得到了应用。
激活函数:
在这里插入图片描述
在这里插入图片描述
导数:
在这里插入图片描述

在这里插入图片描述

import torch.nn as nn
import torch
import matplotlib.pyplot as plt
import numpy as npclass GELU(nn.Module):def __init__(self, beta=1):super(GELU, self).__init__()def forward(self, x):return F.gelu(x)class GELU(nn.Module):def __init__(self, beta=1):super(GELU, self).__init__()def forward(self, x):x_= np.sqrt(2./np.pi) * (x+0.044715 * x * x * x)return 0.5 * x * (1 + (torch.exp(x_) - torch.exp(-x_))/(torch.exp(x_) + torch.exp(-x_)))x = torch.linspace(-10, 10, 1000).unsqueeze(0)
# torch.nn.SELU(inplace=False)
func2 = nn.GELU()
func = GELU()y1 = func(x)
y2 = func2(x)plt.plot(x.detach().numpy()[0],y1.detach().numpy()[0],label="myGELU")
plt.plot(x.detach().numpy()[0],y2.detach().numpy()[0],label="PyTorch")
plt.legend()
plt.grid()
plt.show()

在这里插入图片描述

softmax函数

Softmax 函数对 n 维输入 Tensor 进行重新缩放,以便 n 维输出 Tensor 的值位于 [0,1] 范围内并且总和为 1。当输入张量是一个稀疏张量时,未指定的值被视为负无穷。
在这里插入图片描述

import torch
import torch.nn as nn
import torch.nn.functional as F
from matplotlib import pyplot as pltclass Softmax(nn.Module):def __init__(self, dim=1):super().__init__()self.dim= dimprint("Softmax activation loaded...")def forward(self,X):X_exp = X.exp()partion = X_exp.sum(dim=self.dim, keepdim=True)  # 沿着列方向求和,即对每一行求和return X_exp/partion  # 广播机制,partion被扩展成与X_exp同shape的,对应位置元素做除法func= Softmax()
x = torch.linspace(-20,20,1000).unsqueeze(0)
y1 = func(x)func2 = nn.Softmax(dim=1)
y2 = func2(x)plt.plot(x.detach().numpy()[0],y1.detach().numpy()[0],label="mySoftmax")
plt.plot(x.detach().numpy()[0],y2.detach().numpy()[0],label="PyTorch")
plt.legend()
plt.show()

在这里插入图片描述
这个模块不直接与NLLLoss一起工作,它期望在Softmax和NLLLoss之间添加计算log的操作。使用LogSoftmax代替(它更快并且有更好的数值属性)。

总结

本章讨论了各种激活函数以及在各种情况下激活函数的使用。选择最佳激活函数的方法是精度驱动的;在模型中,应该始终使用能给出最佳结果的激活函数。

参考资料

https://blog.csdn.net/qq_20909377/article/details/79133981
https://blog.csdn.net/weixin_42528089/article/details/84865069
https://www.cnblogs.com/wqbin/p/11099612.html
https://blog.csdn.net/tototuzuoquan/article/details/113791252
https://pytorch.org/
https://blog.csdn.net/qq_24819773/article/details/104439170


http://chatgpt.dhexx.cn/article/AVJNPFGP.shtml

相关文章

swish激活函数

激活函数提出论文&#xff1a; swish f(x)x⋅sigmoid(βx) β是个常数或可训练的参数.Swish 具备无上界有下界、平滑、非单调的特性。 Swish 在深层模型上的效果优于 ReLU。例如&#xff0c;仅仅使用 Swish 单元替换 ReLU 就能把 Mobile NASNetA 在 ImageNet 上的 top-1 分类…

几种常用的激活函数

1. 激活函数 如下图&#xff0c;在神经元中&#xff0c;输入的 inputs 通过加权&#xff0c;求和后&#xff0c;还被作用了一个函数&#xff0c;这个函数就是激活函数 Activation Function。 1.1 激活函数的作用&#xff1a; 如果不用激活函数&#xff0c;每一层输出都是上层…

激活函数作用

激活函数作用&#xff1a; 将权值结果转化成分类结果。常用于 逻辑回归(Logistic Regression)神经网络(Neural Network) 这两处&#xff0c;激活函数都用于计算一个线性函数的结果。 sigmoid函数 &#xff1a; 在机器学习中可用在分类问题上&#xff0c;如逻辑回归模型分类器…

tensorflow中常用的激活函数

激活函数(activation function)运行时激活神经网络中某一部分神经元&#xff0c;将激活神经元的信息输入到下一层神经网络中。神经网络之所以能处理非线性问题&#xff0c;这归功于激活函数的非线性表达能力。激活函数需要满足数据的输入和输出都是可微的&#xff0c;因为在进行…

激活函数简介

1、什么是激活函数&#xff1f; 单一神经元模型展示&#xff1a; 神经网络中的每个神经元节点接受上一层神经元的输出值作为本神经元的输入值&#xff0c;并将输入值传入下一层&#xff0c;输入层神经元节点会将属性值直接传递给下一层&#xff08;隐层或输出层&#xff09;。…

激活函数maxout

激活函数maxout 系列文章:maxout函数相对于其他的激活函数有很大的区别,可以看做是在神经网络中激活函数的地方加入一个激活函数层。 maxout可以看做是一个可学习的分段线性函数,因为可学习所以是需要参数的,而且参数是可以通过反向传播来学习的。因为参数量的增大,势必导…

激活函数总结

一、引入激活函数的目的 图1&#xff1a;带一层隐藏层的神经网络 先看一个只含一层隐藏层的神经网络&#xff0c;如图1所示。输入为 n 条样本X&#xff0c;隐藏层H的权重和偏置分别为W_h&#xff0c;b_o&#xff0c;输出层O的权重和偏置分别为W_o&#xff0c;b_o。输出层的计算…

机器学习(14)——激活函数

文章目录 1 定义2 激活函数的必要性3 常用的激活函数3.1 单位阶跃函数3.2 Logistic函数3.3 Tanh函数3.4 ReLU函数3.5 LeakyReLU函数3.6 Softmax函数 4 选择恰当的激活函数 1 定义 激活函数 (Activation functions) 对于人工神经网络模型去学习、理解非常复杂和非线性的函数来说…

GELU激活函数

最近看bert论文&#xff0c;发现用的是GELU激活函数&#xff0c;找出来看看 论文&#xff1a;GAUSSIAN ERROR LINEAR UNITS (GELUS)项目&#xff1a;https://github.com/hendrycks/GELUs ABSTRACT 本文提出了高斯误差线性单元(GELU)&#xff0c;一个高性能的神经网络激活函数…

神经网络中的激活函数

文章目录 引言什么是激活函数&#xff1f;为什么我们要在神经网络中使用激活函数&#xff1f;线性激活函数非线性激活函数1. Sigmoid&#xff08;逻辑激活函数&#xff09;2. Tanh&#xff08;双曲正切激活函数&#xff09;3. ReLU&#xff08;线性整流单元&#xff09;激活函数…

激活函数简述

1、激活函数的作用 1.不带激活函数的单层感知机是一个线性分类器&#xff0c;不能解决线性不可分的问题 2.合并后的多个感知器本质上还是一个线性分类器&#xff0c;还是解决不了非线性的问题 3.激活函数是用来加入非线性因素的&#xff0c;提高神经网络对模型的表达能力&a…

常用激活函数

文章目录 前言为什么需要激活函数什么样的函数可以做激活函数什么样的函数是好的激活函数常用激活函数sigmoidtanhReLULeaky ReLURandomized Leaky ReLUMaxout 参考文章 前言 今天这篇文章对一些常用激活函数做一下总结吧。在神经网络中激活函数还是很重要的&#xff0c;并且熟…

激活函数

深度学习中的激活函数导引 我爱机器学习(52ml.net) 2016年8月29日 0 作者&#xff1a;程程 链接&#xff1a;https://zhuanlan.zhihu.com/p/22142013 来源&#xff1a;知乎 著作权归作者所有&#xff0c;已联系作者获得转载许可。 深度学习大讲堂致力于推送人工智能&#xff0c…

详解激活函数

文章目录 0️⃣前言1️⃣Sigmoid2️⃣tanh3️⃣Relu4️⃣Leaky Relu5️⃣Softmax6️⃣总结 0️⃣前言 用了这么久的激活函数&#xff0c;抽空总结一下吧&#xff0c;不然总是忘记&#xff0c;这里介绍常用到的sigmoid&#xff0c;tanh&#xff0c;relu&#xff0c;leaky relu&…

常用激活函数(激励函数)理解与总结

引言 学习神经网络的时候我们总是听到激活函数这个词&#xff0c;而且很多资料都会提到常用的激活函数&#xff0c;比如Sigmoid函数、tanh函数、Relu函数。那么我们就来详细了解下激活函数方方面面的知识。本文的内容包括几个部分&#xff1a; 什么是激活函数&#xff1f;激活…

常用激活函数总结(深度学习)

前言   学习神经网络的时候我们总是听到激活函数这个词&#xff0c;而且很多资料都会提到常用的激活函数&#xff0c;比如Sigmoid函数、tanh函数、Relu函数。在经过一段时间学习后&#xff0c;决定记录个人学习笔记。 一、激活函数 1.激活函数定义&#xff1f;   在神经网…

【概念梳理】激活函数

一、引言 常用的激活函数如下&#xff1a; 1、Sigmoid函数 2、Tanh函数 3、ReLU函数 4、ELU函数 5、PReLU函数 6、Leaky ReLU函数 7、Maxout函数 8、Mish函数 二、激活函数的定义 多层神经网络中&#xff0c;上层节点的输出和下层节点的输入之间具有一个函数关系&#xff0c;…

一文搞懂激活函数

目录 1、什么是激活函数 2、激活函数的用途&#xff08;为什么需要激活函数&#xff09;&#xff1f; 3、常见的激活函数介绍 3.1 Sigmoid函数 3.2 tanh函数 3.3.RelU函数 3.4 Leaky ReLU函数 和 PReLU 函数 --- ReLU 变体的提出 3.5 ELU (Exponential Linear Units) 函…

激活函数(Relu,sigmoid,Tanh,softmax)详解

目录 1 激活函数的定义 2 激活函数在深度学习中的作用 3 选取合适的激活函数对于神经网络有什么样的重要意义 4 常用激活函数 4.1 Relu 激活函数 4.2 sigmoid 激活函数 4.3 Tanh激活函数 4.4 softmax 激活函数 1 激活函数的定义 激活函数&#xff08;Activation Funct…

激活函数(Activation Function)

目录 1 激活函数的概念和作用 1.1 激活函数的概念 1.2 激活函数的作用 1.3 通俗的理解一下激活函数(图文结合) 1.3.1 无激活函数的神经网络 1.3.2 带激活函数的神经网络 2 神经网络梯度消失与梯度爆炸 2.1 简介梯度消失与梯度爆炸 2.2 梯度不稳定问题 2.3 产生梯度消…