【Pytorch】Pytorch文档学习4:BUILD THE NEURAL NETWORK

原文首段
Neural networks comprise of layers/modules that perform operations on data. The torch.nn namespace provides all the building blocks you need to build your own neural network. Every module in PyTorch subclasses the nn.Module. A neural network is a module itself that consists of other modules (layers). This nested structure allows for building and managing complex architectures easily.

In the following sections, we’ll build a neural network to classify images in the FashionMNIST dataset.

简单翻译
建立神经网络
神经网络由对数据执行操作的层和模块狗狗从。命名空间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

# 获取训练的设备
# 如果可以的话,我们想要能够训练我们的模型在硬件计算设备上,比如GPU或MPS。
# 我们通过检查torch.cuda或者torch.backends.mps是否可用,否则我们就使用CPU。

device = (
    "cuda"
    if torch.cuda.is_available()
    else "cpu"
)
print(f"Using {device} device")


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

class NeuralNetwork(nn.Module):
    def __init__(self):
        super().__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


# 我们创建了一个神经网络的例子,并将其移动到device上,最后打印出这个结构。

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

# 为了使用这个模型,我们使其通过输入的数据。其执行了模型的forward,并伴随着一些背景操作。
# 请不要直接调用model.forward()。

# 对输入调用该模型将返回一个二维的tensor,其中第0维包含了对每一个类别其10个原始的预测值的输出;
# 而第1维包含了每个输出的各个值。我们通过将其传递给nn.Softmax模块的一个实例来获得预测概率。

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

# 模型网络
# 让我们分解FashionMNIST模型的网络。为了清楚的描述其,我们将举一个很小的batch的例子,其中包含三张28 * 28大小
# 的图片,我们将看看当把其通过这个网络的时候发生了什么。

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

# nn.Flatten
# 我们初始化一个nn.Flatten网络去将每一个2D的图片转换为一个拥有784个值的连续的数组(minibatch的维度(第0维)是被保持的)

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

# Linear网络是一个对输入使用线性变换的模型,这个模型会使用其存储的weights和biases进行线性变换。

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

# nn.ReLU
# 非线性激活函数是创建模型输入和输出之间复杂映射的关键。
# 其被应用于线性转换之后,通过引入非线性性,从而来帮助神经网络去学习各种各样的现象。

print(f"Before ReLU: {hidden1}\n\n")
hidden1 = nn.ReLU()(hidden1)
print(f"After ReLU: {hidden1}")

# nn.Sequential
# nn.Sequential是一个模型的有序容器。数据以相同的顺序通过定义好的所有模型。
# 你能够使用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)

# nn.Softmax
# 神经网络的最后一层返回了一个概率分布函数:在[-inf, inf]上原始的值,这些值
# 将会通过nn.Softmax模型。概率分布会被缩放到[0, 1]范围之内,从而代表模型对
# 每个类别的预测。dim参数表明了那些维度的值应该综合为1。

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

# 模型的参数
# 在神经网络中的许多层都是被参数化的。
# 也就是说在训练的过程中优化了相关联的权重和偏置。
# nn.Module自动化的追踪了所有在你模型中定义的参数位置,
# 并且通过使用模型的parameters()或named_parameters方法访问所有的参数。
# 在这个例子中,我们迭代所有的参数,并且输出其大小以及先前的值。

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")



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值