Pytorch官方文档学习笔记 —— 3. Build Model

目录

1. 建立神经网络

2. 获取训练设备

3. 定义类

4. 模型层

4.1 nn.Flatten

4.2 nn.Linear

4.3 nn.ReLU

4.4 nn.Sequential

4.5 nn.Softmax

5. 模型参数


1. 建立神经网络

        torch.nn 提供了构建神经网络所需的所有构建块。PyTorch 中的每个模块都是 nn.Module 的子类。

        在接下来的部分中,我们将构建一个神经网络来对 FashionMNIST 数据集中的图像进行分类。

import os
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets, transforms

2. 获取训练设备

        我们通常希望能够在GPU等硬件加速器(如果可用)上训练模型。首先检查一下 torch.cuda 是否可用,否则就继续使用CPU。

device = "cuda" if torch.cuda.is_available() else "cpu"

3. 定义类

        我们通过子类化 nn.Module 来定义我们的神经网络,并在 __init__ 中初始化神经网络层。每个 nn.Module 子类都在 forward 方法中实现对输入数据的操作。

class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = nn.Sequential(
            nn.Linear(28*28, 512),
            nn.ReLU(),
            nn.Linear(512, 512),
            nn.ReLU(),
            nn.Linear(512, 10),
        )

    def forward(self, x):
        x = self.flatten(x)
        logits = self.linear_relu_stack(x)
        return logits

         我们创建一个 NeuralNetwork 实例,并将其移动到 device 上,并打印其结构。

model = NeuralNetwork().to(device)
print(model)

out: 
>> NeuralNetwork(
     (flatten): Flatten(start_dim=1, end_dim=-1)
     (linear_relu_stack): Sequential(
       (0): Linear(in_features=784, out_features=512, bias=True)
       (1): ReLU()
       (2): Linear(in_features=512, out_features=512, bias=True)
       (3): ReLU()
       (4): Linear(in_features=512, out_features=10, bias=True)
     )
   )

        要使用模型,我们将输入数据传递给它。这将执行模型的转发,以及一些后台操作。

        注意:请不要直接调用 model.forward()

        在输入上调用模型会返回一个 10 维张量,其中包含每个类的原始预测值。我们通过一个 nn.Softmax 模块的实例来获得预测概率。

X = torch.rand(1, 28, 28, device=device)
logits = model(X)
pred_probab = nn.Softmax(dim=1)(logits)
y_pred = pred_probab.argmax(1)
print(f"Predicted class: {y_pred}")

out: 
>> Predicted class: tensor([9], device='cuda:0')

4. 模型层

        让我们分解 FashionMNIST 模型中的层。为了方便说明,我们将抽取 3 张大小为 28x28 的图像的小批量样本,看看当我们通过网络传递它时会发生什么。

input_image = torch.rand(3,28,28)

4.1 nn.Flatten

        我们初始化 nn.Flatten 层以将每个 2D 28x28 图像转换为 784 个像素值的连续数组(保持小批量维度(dim=0))。

flatten = nn.Flatten()
flat_image = flatten(input_image)
print(flat_image.size())

out: 
>> torch.Size([3, 784])

4.2 nn.Linear

        linear 层是一个使用其存储的权重和偏置对输入进行线性转换的模块,即 y=wx+b

layer1 = nn.Linear(in_features=784, out_features=20)
hidden1 = layer1(flat_image)
print(hidden1.size())

>> torch.Size([3, 20])

4.3 nn.ReLU

        依靠非线性激活函数可以在模型的输入和输出之间创建复杂映射,在线性变换后应用以引入非线性,可以帮助神经网络学习各种复杂的现象,更多常见的激活函数及其介绍可见这里

hidden1 = nn.ReLU()(hidden1)

4.4 nn.Sequential

        nn.Sequential 是一个有序的模块容器。数据按照定义的顺序通过所有模块,您可以使用顺序容器来组合一个如同 seq_modules 的快速网络。

seq_modules = nn.Sequential(
    flatten,
    layer1,
    nn.ReLU(),
    nn.Linear(20, 10)
)
input_image = torch.rand(3,28,28)
logits = seq_modules(input_image)

4.5 nn.Softmax

        上述的神经网络的最后一层的线性层返回了取值在 [-infty, infty] 的未处理的数值 —— logits。将 logits 传给 nn.Softmax 模块后取值被缩放到 [0, 1] 内,以表示模型对每个类别的预测概率。dim 参数指示维度的数值总和必须为 1 。

softmax = nn.Softmax(dim=1)
pred_probab = softmax(logits)

5. 模型参数

        神经网络中的许多层都是参数化的,即具有在训练期间优化的相关权重和偏差。子类化 nn.Module 会自动跟踪模型对象中定义的所有字段,并使用模型的 parameters() 或 named_pa​​rameters() 方法使所有参数都可以访问。

        在此示例中,我们遍历每个参数,并打印其大小和其值的预览。

print(f"Model structure: {model}\n\n")

for name, param in model.named_parameters():
    print(f"Layer: {name} | Size: {param.size()} | Values : {param[:2]} \n")

Out: 

Model structure: NeuralNetwork(
  (flatten): Flatten(start_dim=1, end_dim=-1)
  (linear_relu_stack): Sequential(
    (0): Linear(in_features=784, out_features=512, bias=True)
    (1): ReLU()
    (2): Linear(in_features=512, out_features=512, bias=True)
    (3): ReLU()
    (4): Linear(in_features=512, out_features=10, bias=True)
  )
)


Layer: linear_relu_stack.0.weight | Size: torch.Size([512, 784]) | Values : tensor([[ 0.0033, -0.0081, -0.0354,  ..., -0.0335,  0.0070,  0.0030],
        [ 0.0106, -0.0064,  0.0300,  ...,  0.0071, -0.0062,  0.0169]],
       device='cuda:0', grad_fn=<SliceBackward0>)

Layer: linear_relu_stack.0.bias | Size: torch.Size([512]) | Values : tensor([-0.0193, -0.0153], device='cuda:0', grad_fn=<SliceBackward0>)

Layer: linear_relu_stack.2.weight | Size: torch.Size([512, 512]) | Values : tensor([[ 0.0408,  0.0078,  0.0300,  ...,  0.0058, -0.0142, -0.0226],
        [ 0.0319, -0.0063, -0.0093,  ..., -0.0096,  0.0352,  0.0178]],
       device='cuda:0', grad_fn=<SliceBackward0>)

Layer: linear_relu_stack.2.bias | Size: torch.Size([512]) | Values : tensor([0.0219, 0.0020], device='cuda:0', grad_fn=<SliceBackward0>)

Layer: linear_relu_stack.4.weight | Size: torch.Size([10, 512]) | Values : tensor([[ 0.0076,  0.0076,  0.0433,  ...,  0.0178,  0.0230,  0.0227],
        [-0.0396, -0.0042,  0.0342,  ..., -0.0364, -0.0184, -0.0329]],
       device='cuda:0', grad_fn=<SliceBackward0>)

Layer: linear_relu_stack.4.bias | Size: torch.Size([10]) | Values : tensor([-0.0380, -0.0044], device='cuda:0', grad_fn=<SliceBackward0>)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值