注意
点击此处下载完整示例代码
在 PyTorch 中定义神经网络¶
创建日期:2020年4月17日 | 最后更新:2024年2月6日 | 最后验证:2024年11月5日
深度学习使用人工神经网络(模型),这些计算系统由许多互连的单元层组成。通过数据在这些互连单元中的传递,神经网络能够学习如何近似计算,将输入转换为输出。在 PyTorch 中,可以使用 torch.nn
包构建神经网络。
引言¶
PyTorch 提供了设计优雅的模块和类,包括 torch.nn
,帮助您创建和训练神经网络。nn.Module
包含层,以及一个 forward(input)
方法,该方法返回 output
。
在本秘籍中,我们将使用 torch.nn
定义一个针对 MNIST 数据集的神经网络。
步骤¶
导入加载数据所需的所有必要库
定义并初始化神经网络
指定数据将如何通过模型传递
[可选] 将数据通过模型传递进行测试
1. 导入加载数据所需的所有必要库¶
在本秘籍中,我们将使用 torch
及其子模块 torch.nn
和 torch.nn.functional
。
import torch
import torch.nn as nn
import torch.nn.functional as F
2. 定义并初始化神经网络¶
我们的网络将识别图像。我们将使用 PyTorch 内置的一个过程,称为卷积。卷积将图像的每个元素与其局部邻居相加,并通过一个核或小矩阵进行加权,这有助于我们从输入图像中提取特定特征(如边缘检测、锐度、模糊度等)。
定义模型的 Net
类有两个要求。第一个是编写一个引用 nn.Module
的 __init__ 函数。在这个函数中,您定义神经网络中的全连接层。
使用卷积,我们将定义模型,使其接收 1 个输入图像通道,并输出与我们目标匹配的 10 个标签,代表数字 0 到 9。这个算法由您自行创建,我们将遵循一个标准的 MNIST 算法。
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
# First 2D convolutional layer, taking in 1 input channel (image),
# outputting 32 convolutional features, with a square kernel size of 3
self.conv1 = nn.Conv2d(1, 32, 3, 1)
# Second 2D convolutional layer, taking in the 32 input layers,
# outputting 64 convolutional features, with a square kernel size of 3
self.conv2 = nn.Conv2d(32, 64, 3, 1)
# Designed to ensure that adjacent pixels are either all 0s or all active
# with an input probability
self.dropout1 = nn.Dropout2d(0.25)
self.dropout2 = nn.Dropout2d(0.5)
# First fully connected layer
self.fc1 = nn.Linear(9216, 128)
# Second fully connected layer that outputs our 10 labels
self.fc2 = nn.Linear(128, 10)
my_nn = Net()
print(my_nn)
我们已经完成了神经网络的定义,现在我们必须定义数据将如何通过它传递。
3. 指定数据将如何通过模型传递¶
当您使用 PyTorch 构建模型时,只需定义 forward
函数,它将数据传递到计算图(即我们的神经网络)中。这将代表我们的前向传播算法。
您可以在 forward
函数中使用任何张量操作。
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout2d(0.25)
self.dropout2 = nn.Dropout2d(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, 10)
# x represents our data
def forward(self, x):
# Pass data through conv1
x = self.conv1(x)
# Use the rectified-linear activation function over x
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
# Run max pooling over x
x = F.max_pool2d(x, 2)
# Pass data through dropout1
x = self.dropout1(x)
# Flatten x with start_dim=1
x = torch.flatten(x, 1)
# Pass data through ``fc1``
x = self.fc1(x)
x = F.relu(x)
x = self.dropout2(x)
x = self.fc2(x)
# Apply softmax to x
output = F.log_softmax(x, dim=1)
return output
4. [可选] 将数据通过模型传递进行测试¶
为了确保我们获得期望的输出,让我们通过一些随机数据来测试我们的模型。
# Equates to one random 28x28 image
random_data = torch.rand((1, 1, 28, 28))
my_nn = Net()
result = my_nn(random_data)
print (result)
结果张量中的每个数字对应于与该随机张量相关联的标签的预测。
恭喜!您已成功在 PyTorch 中定义了一个神经网络。