使用PyTorch库构建深度学习模型


PyTorch是一个广泛使用的开源机器学习框架,由Facebook的AI研究团队开发。它以其灵活性和速度以及轻松构建复杂模型的能力而闻名。PyTorch基于Torch库(最初是用Lua开发的),并提供Python绑定。

PyTorch广泛应用于学术界和工业界,用于各种机器学习任务,例如计算机视觉、自然语言处理和语音识别。在本教程中,我们将学习如何使用PyTorch库构建深度学习模型。

入门

在深入使用Torch库之前,我们需要首先使用pip安装该库。但是,由于它并非内置,因此我们必须首先安装Torch库。这可以使用pip包管理器完成。

要安装Torch库,请打开您的终端并键入以下命令:

pip install torch

这将下载并安装Torch库及其依赖项。安装完成后,我们就可以开始使用Torch并利用其模块了!

在本教程中,我们将使用PyTorch构建一个用于图像分类的卷积神经网络(CNN)。卷积神经网络(CNN)是一种深度学习模型,广泛用于图像分类任务。在本教程中,我们将使用PyTorch构建一个CNN来对图像进行分类。

步骤1:导入所需的库

第一步是导入所需的库。我们将使用torch、torch.nn和torchvision库。

import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms

步骤2:加载和预处理数据集

我们将使用CIFAR-10数据集,这是一个广泛用于图像分类任务的数据集。该数据集包含60,000张32x32彩色图像,分为10个类别,每个类别有6,000张图像。

transform = transforms.Compose(
   [transforms.ToTensor(),
   transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
   download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
   shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False,
   download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
   shuffle=False, num_workers=2)

我们使用torchvision.transforms库来预处理图像。我们首先将图像转换为张量,然后对其进行归一化。然后,我们加载数据集并为训练集和测试集创建数据加载器。

步骤3:定义CNN模型

准备数据后,下一步是使用PyTorch定义CNN模型。在此步骤中,我们将定义CNN模型的结构。我们的模型将包含两个卷积层,然后是两个全连接层。

import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
   def __init__(self):
      super(Net, self).__init__()
      # input image channel, 3 for RGB images
      # output channel, 6 for 6 filters
      # kernel size = 5
      self.conv1 = nn.Conv2d(3, 6, 5)
      # input channel, 6 from previous layer
      # output channel, 16 for 16 filters
      # kernel size = 5
      self.conv2 = nn.Conv2d(6, 16, 5)
      # an affine operation: y = Wx + b
      # 16 * 5 * 5 is the size of the image after convolutional layers
      # 120 is the output size of the first fully connected layer
      self.fc1 = nn.Linear(16 * 5 * 5, 120)
      # 84 is the output size of the second fully connected layer
      self.fc2 = nn.Linear(120, 84)
      # 10 is the output size of the last fully connected layer
      self.fc3 = nn.Linear(84, 10)

   def forward(self, x):
      # max pooling over a (2, 2) window
      x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
      # if the size is a square you can only specify a single number
      x = F.max_pool2d(F.relu(self.conv2(x)), 2)
      # flatten the input for fully connected layers
      x = x.view(-1, self.num_flat_features(x))
      x = F.relu(self.fc1(x))
      x = F.relu(self.fc2(x))
      x = self.fc3(x)
      return x

   def num_flat_features(self, x):
      size = x.size()[1:]  # all dimensions except the batch dimension
      num_features = 1
      for s in size:
         num_features *= s
      return num_features

net = Net()

步骤4:训练模型

现在我们已经定义了CNN模型,是时候在我们自己的数据集上训练它了。为此,我们将使用PyTorch DataLoader类按批次加载数据,并将其馈送到模型进行训练。我们还将定义损失函数和优化器。

以下是训练模型的代码:

# Define the loss function and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)

# Train the model
num_epochs = 10
for epoch in range(num_epochs):
   running_loss = 0.0
   for i, data in enumerate(train_loader, 0):
      inputs, labels = data
      optimizer.zero_grad()

      # Forward pass
      outputs = model(inputs)
      loss = criterion(outputs, labels)

      # Backward and optimize
      loss.backward()
      optimizer.step()

      # Print statistics
      running_loss += loss.item()
      if i % 2000 == 1999:    # Print every 2000 mini-batches
         print('[%d, %5d] loss: %.3f' %
            (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0
print('Finished Training')

我们循环遍历数据集10个epoch,并使用训练数据训练模型。在每个epoch中,我们将运行损失重置为0,并循环遍历数据的批次。

对于每个批次,我们执行模型的前向传播,计算损失,执行反向传播,并使用优化器优化模型。最后,我们每2000个小批次打印一次训练损失。

步骤5:评估模型

现在我们已经训练了模型,是时候评估其在测试数据集上的性能了。我们将使用PyTorch DataLoader类按批次加载测试数据,并将其馈送到模型进行评估。

以下是评估模型的代码:

# Evaluate the model
correct = 0
total = 0
with torch.no_grad():
   for data in test_loader:
      images, labels = data
      outputs = model(images)
      _, predicted = torch.max(outputs.data, 1)
      total += labels.size(0)
      correct += (predicted == labels).sum().item()

print('Accuracy of the network on the 10000 test images: %d %%' % (
   100 * correct / total))

在此代码中,我们首先将**correct**和**total**变量初始化为0。然后,我们使用PyTorch DataLoader类循环遍历测试数据集,并将测试数据馈送到模型。我们使用**torch.max()**函数获取最高输出值的索引,该索引代表预测的类别。然后,我们将预测的类别与真实的类别进行比较,并相应地更新**correct**和**total**变量。

最后,我们打印模型在测试数据集上的准确率。

结论

总之,PyTorch是一个强大的深度学习包,它具有易于使用的界面,可用于创建和训练神经网络。在本教程中,我们介绍了使用PyTorch构建用于图像分类的卷积神经网络的基础知识。

PyTorch的灵活性和易用性使其成为对深度学习感兴趣的研究人员和从业人员的绝佳选择。该库的动态计算图和自动微分引擎使创建复杂模型并有效地对其进行优化变得简单。此外,PyTorch拥有一个庞大而活跃的社区,这意味着有很多资源可供学习,并在需要时获得帮助。

总的来说,对于任何对开始学习深度学习感兴趣的人来说,无论是新手还是经验丰富的从业者,PyTorch都是一个极好的选择。PyTorch凭借其简单的API和强大的功能,可以帮助您快速构建和训练各种应用的深度学习模型。

更新于:2023年8月31日

浏览量:117

启动您的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.