Q2 网络模型建立
2.1 什么是全连接层
全连接层(Fully Connected Layer,简称FC层)是神经网络中的一种特殊层,其特点是该层的每个神经元都与前一层的所有神经元相连,即实现了一个完全连接的矩阵乘法操作
人话版:全连接层时最基本的参数层,特征张量通过n个参数层后转换为目标张量
以下为创建一个全连接层
import torch
layer = torch.nn.Linear(
in_features=10,
out_features=32,
bias=True
)
print(layer)
Linear(in_features=10, out_features=32, bias=True)
参数名 | 解释 | 默认值 |
---|---|---|
in_features | 输入特征向量的维度 | 必填 |
out_features | 输出向量的维度 | 必填 |
bias | 是否使用偏置向量 | true |
使用例程 | ||
Linear(in_features=10, out_features=32, bias=True) |
input = torch.rand(16, 10) # 一组16个10维度的向量
output = layer(input) # 进行推理计算
print(input.shape, output.shape)
torch.Size([16, 10]) torch.Size([16, 32])
2.2 什么是激活函数
激活函数是一种非线性函数,它被用来引入非线性特性到神经网络中。
在神经网络中,激活函数被应用在每个神经元的输出上,它将输入的加权和转换成输出值。
人话版:为网络引入非线性,至于效果如何且如何配置,全靠实验和讲故事
常见激活函数
- Sigmoid:将输入压缩到0和1之间,适合表示概率。
σ ( x ) = 1 1 + e − x \sigma(x) = \frac{1}{1 + e^{-x}} σ(x)=1+e−x1 - ReLU(Rectified Linear Unit):将负值置为0,正值保持不变,计算简单,且不易受到梯度消失的影响。
R e L U ( x ) = max ( 0 , x ) ReLU(x) = \max(0, x) ReLU(x)=max(0,x) - Leaky ReLU:对ReLU函数的改进,允许在负值上有一个小的斜率,解决了死亡神经元问题。
L e a k y R e L U ( x ) = max ( 0.01 x , x ) Leaky ReLU(x) = \max(0.01x, x) LeakyReLU(x)=max(0.01x,x) - Tanh(Hyperbolic Tangent):将输入压缩到-1和1之间。
t a n h ( x ) = e x − e − x e x + e − x tanh(x) = \frac{e^x - e^{-x}}{e^x + e^{-x}} tanh(x)=ex+e−xex−e−x - Softmax:常用于多分类问题,将一组值转换为概率分布。
s o f t m a x ( x i ) = e x i ∑ j e x j softmax(x_i) = \frac{e^{x_i}}{\sum_{j} e^{x_j}} softmax(xi)=∑jexjexi - ELU(Exponential Linear Unit):类似于ReLU,但在负值上使用指数衰减,减少了死亡神经元问题。
E L U ( x ) = max ( 0.01 ( e x − 1 ) , x ) ELU(x) = \max(0.01(e^x - 1), x) ELU(x)=max(0.01(ex−1),x)
使用示例
import torch
relu = torch.nn.ReLU()
sigmoid = torch.nn.Sigmoid()
leaky_relu = torch.nn.LeakyReLU()
tanh = torch.nn.Tanh()
softmax = torch.nn.Softmax(dim=1) # softmax常使用dim=1
elu = torch.nn.ELU()
data = torch.rand(16, 128) # 向量, batch为16, 特征数量为128
result = relu(data) # 其余使用一致
2.3 自定义模型
自定义模型在torch模型中最重要的部分,即使用各种模块组合成所需的模型
定义以下模型in_features简写为in;out_features简写为out
import torch
class CustomModel(torch.nn.Module): # 所有的模型都要继承torch.nn.Module
def __init__(self):
super().__init__() # 运行torch.nn.Module
self.features = torch.nn.Linear(10, 30) # 输入向量特征数为30
self.sigmoid = torch.nn.Sigmoid()
self.classifier = torch.nn.Linear(30, 100) # 输出向量特征数为10(即分类数量为10)
def forward(self, data): # 定义模型的前馈方法
data = self.features(data)
data = self.sigmoid(data)
data = self.classifier(data)
return data
model = CustomModel()
print(model)
input = torch.rand(128, 10)
output = model(input) # 当定义了forward方法后可使用该方式调用
print(output.shape)
CustomModel(
(features): Linear(in_features=10, out_features=30, bias=True)
(sigmoid): Sigmoid()
(classifier): Linear(in_features=30, out_features=10, bias=True)
)
torch.Size([128, 100])
可以发现input张量在经过模型转换后从10个特征转换为100个特征
work
构造以下模型