深度学习中常用的激活函数有sigmoid、tanh、ReLU、LeakyReLU、PReLU等。
Sigmoid函数公式为 f ( x ) = 1 1 + e − x f(x)=\frac{1}{1+e^{-x}} f(x)=1+e−x1,它的输出值在[0,1]之间,可以用来解决二元分类问题。它的主要特点是它是可导的,并且输出值可以被解释为概率。但是,如果输入值过大或过小,会导致梯度消失问题,对于较深的神经网络来说不太适用。
PyTorch的代码示例:
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(10, 5)
self.fc2 = nn.Linear(5, 1)
def forward(self, x):
x = F.sigmoid(self.fc1(x))
x = F.sigmoid(self.fc2(x))
return x
Tanh函数公式为 f ( x ) = e x − e − x e x + e − x f(x)=\frac{e^x-e^{-x}}{e^x+e^{-x}} f(x)=ex+e−xex−e−x,它的输出值在[-1,1]之间,可以用来解决回归问题。与sigmoid不同的是,它的输出是以0为中心的,因此幂次大的输入值仍然会导致梯度消失问题。
PyTorch的代码示例:
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(10, 5)
self.fc2 = nn.Linear(5, 1)
def forward(self, x):
x = F.tanh(self.fc1(x))
x = F.tanh(self.fc2(x))
return x
ReLU函数公式为 f ( x ) = m a x ( 0 , x ) f(x)=max(0, x) f(x)=max(0,x),它的输出值在[0,无穷)之间,可以用来解决分类和回归问题。它有以下优点:1)解决了梯度消失问题;2)计算速度快。
PyTorch的代码示例:
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(10, 5)
self.fc2 = nn.Linear(5, 1)
def forward(self, x):
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
return x
LeakyReLU函数公式为 f ( x ) = m a x ( 0.01 x , x ) f(x)=max(0.01x, x) f(x)=max(0.01x,x),它的输出值在(-无穷,无穷)之间,是ReLU的改进版。在输入值为负数时,它不是完全为0,而是有一个小的斜率,可以避免神经元死亡。
PyTorch的代码示例:
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(10, 5)
self.fc2 = nn.Linear(5, 1)
self.LeakyReLU = nn.LeakyReLU(0.01)
def forward(self, x):
x = self.LeakyReLU(self.fc1(x))
x = self.LeakyReLU(self.fc2(x))
return x
PReLU函数公式为:
f ( x ) = { x , if x > 0 α x , otherwise f(x) = \begin{cases} x, & \text{if $x > 0$}\\ \alpha x, & \text{otherwise} \end{cases} f(x)={x,αx,if x>0otherwise
其中 α \alpha α 是可学习的参数,它的输出值在(-无穷,无穷)之间,是LeakyReLU的改进版。与LeakyReLU不同的是, α \alpha α 不是固定的,而是可以根据训练数据自适应调节。
PyTorch的代码示例:
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(10, 5)
self.fc2 = nn.Linear(5, 1)
self.PReLU = nn.PReLU()
def forward(self, x):
x = self.PReLU(self.fc1(x))
x = self.PReLU(self.fc2(x))
return x
ELU函数的数学公式为 f ( x ) = { x , x > 0 α ( e x − 1 ) , x ≤ 0 f(x)=\begin{cases}x, & x>0\\\alpha(e^x-1), & x\leq0\end{cases} f(x)={x,α(ex−1),x>0x≤0,它是另一种解决ReLU“死亡”现象的函数,通过引入一个指数函数来平滑负数区间。
以下是使用PyTorch实现ELU函数的代码示例:
import torch.nn.functional as F
x = torch.randn(2, 3)
y = F.elu(x, alpha=1.0)
print(y)
深度学习中常用的激活函数有sigmoid、ReLU、LeakyReLU、ELU、SeLU等,其中gelu是近年来提出的一种新的激活函数。
GELU (Gaussian Error Linear Units)函数是一种近年来提出的新型激活函数,其原理是基于高斯误差函数的近似。其作用是在保持ReLU函数优点的同时,减少其缺点。将输入的值 x x x通过高斯分布的累积分布函数(CDF) F ( x ) F(x) F(x),来获得激活函数的输出值。其数学表达式如下:
g e l u ( x ) = x ⋅ Φ ( x ) , 其中 Φ ( x ) = 1 2 [ 1 + e r f ( x 2 ) ] \mathrm{gelu}(x)=x\cdot\Phi (x), \ \mathrm{其中}\Phi(x)=\frac{1}{2}[1+\mathrm{erf}(\frac{x}{\sqrt{2}})] gelu(x)=x⋅Φ(x), 其中Φ(x)=21[1+erf(2x)]
其中, Φ ( x ) \Phi(x) Φ(x)为高斯分布的累积分布函数。
GELU函数具有以下特点:
由于高斯分布的概率密度函数(PDF)在均值处最大,因此gelu在接近0的地方具有很好的非线性特性,同时也有一定的平滑性,能够一定程度上减少梯度消失问题,提高模型的泛化能力。
PyTorch代码示例:
import torch.nn as nn
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.fc = nn.Linear(10, 20)
self.act = nn.GELU()
def forward(self, x):
x = self.fc(x)
x = self.act(x)
return x
model = MyModel()
import torch
import torch.nn.functional as F
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = torch.nn.Linear(10, 20)
self.fc2 = torch.nn.Linear(20, 2)
def forward(self, x):
x = F.gelu(self.fc1(x))
x = F.gelu(self.fc2(x))
return x
在上述示例代码中,我们使用了PyTorch中的F.gelu函数,实现了GELU激活函数对网络中的每个神经元进行激活。