Pytorch最最适合研究生的入门教程,Q2 网络模型建立

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+ex1
  • 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+exexex
  • 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(ex1),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

batch,10
Linear in=10,out=30
Sigmoid
Linear in=30,out=100
batch,100
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

构造以下模型

batch,1,28,28
Linear in=784,out=1024
Linear in=784,out=1024
Linear in=784,out=2048
Sigmoid
Concat
ReLU
+
Linear in=2048,out=10
batch,10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纸墨青鸢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值