如需完成第一次作业,请光速开抄 PyTorch 文档:Training a Classifier
第一个 PyTorch 程序#
对于编写第一个PyTorch程序,推荐以下资源:
计算机视觉、图像分类与 LeNet#
LeNet in PyTorch#
定义神经网络#
在PyTorch中,要定义一个神经网络,只需继承 nn.Module 类,在 __init__ 方法中定义各层,并在 forward 方法中定义前向传播逻辑。例如,LeNet可以这样实现:
import torch.nn as nnimport torch.nn.functional as F
class Net(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Conv2d(3, 6, 5) self.pool = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(6, 16, 5) self.fc1 = nn.Linear(16 * 5 * 5, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10)
def forward(self, x): x = self.pool(F.relu(self.conv1(x))) x = self.pool(F.relu(self.conv2(x))) x = torch.flatten(x, 1) # flatten all dimensions except batch x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x
net = Net()nn.Sequential 可以
class LeNet(nn.Module): def __init__(self): super().__init__() # Input: N x 3 x 32 x 32 (CIFAR-10) self.features = nn.Sequential( nn.Conv2d(3, 6, kernel_size=5, stride=1, padding=0), nn.ReLU(inplace=True), nn.AvgPool2d(kernel_size=2, stride=2), nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=0), nn.ReLU(inplace=True), nn.AvgPool2d(kernel_size=2, stride=2), ) self.classifier = nn.Sequential( nn.Flatten(), nn.Linear(16 * 5 * 5, 120), nn.ReLU(inplace=True), nn.Linear(120, 84), nn.ReLU(inplace=True), nn.Linear(84, 10), )
def forward(self, x): x = self.features(x) x = self.classifier(x) return x如你所见,你并不需要定义 backward 方法,这是因为 PyTorch 会通过自动微分(autograd)来处理反向传播。
加载数据#
使用 torchvision 库可以方便地加载和预处理图像数据集。
import torchimport torchvisionimport torchvision.transforms as transforms # 图像预处理下面的代码会自动下载 CIFAR-10 数据集,并进行预处理。这里的 transforms.Compose 用于将多个图像变换组合在一起。我们将图像转换为张量,并进行归一化处理。
NOTE
If running on Windows and you get a BrokenPipeError, try setting the num_worker of torch.utils.data.DataLoader() to 0.
transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
batch_size = 4
trainset = torchvision.datasets.CIFAR10( root="./data", train=True, download=True, transform=transform)trainloader = torch.utils.data.DataLoader( trainset, batch_size=batch_size, 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=batch_size, shuffle=False, num_workers=2)
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')损失函数和优化器#
定义损失函数和优化器。这里我们使用交叉熵损失函数(CrossEntropyLoss)和带有动量的随机梯度下降(SGD with momentum)。
import torch.optim as optim
criterion = nn.CrossEntropyLoss()optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)训练#
训练网络。我们将数据分成多个小批次(mini-batches),并在每个批次上进行前向传播、计算损失、反向传播和参数更新。
for epoch in range(2): # loop over the dataset multiple times
running_loss = 0.0 for i, data in enumerate(trainloader, 0): # get the inputs; data is a list of [inputs, labels] inputs, labels = data
# zero the parameter gradients optimizer.zero_grad()
# forward + backward + optimize outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step()
# print statistics running_loss += loss.item() if i % 2000 == 1999: # print every 2000 mini-batches print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}') running_loss = 0.0
print('Finished Training')要保存模型,可以使用 torch.save:
PATH = './cifar_net.pth'torch.save(net.state_dict(), PATH)要加载模型权重,可以使用 torch.load:
net = Net()net.load_state_dict(torch.load(PATH, weights_only=True))测试#
net = Net()net.load_state_dict(torch.load(PATH, weights_only=True))
outputs = net(images)
_, predicted = torch.max(outputs, 1)
print('Predicted: ', ' '.join(f'{classes[predicted[j]]:5s}' for j in range(4)))
correct = 0total = 0# since we're not training, we don't need to calculate the gradients for our outputswith torch.no_grad(): for data in testloader: images, labels = data # calculate outputs by running images through the network outputs = net(images) # the class with the highest energy is what we choose as prediction _, predicted = torch.max(outputs, 1) total += labels.size(0) correct += (predicted == labels).sum().item()
print(f'Accuracy of the network on the 10000 test images: {100 * correct // total} %')
# prepare to count predictions for each classcorrect_pred = {classname: 0 for classname in classes}total_pred = {classname: 0 for classname in classes}
# again no gradients neededwith torch.no_grad(): for data in testloader: images, labels = data outputs = net(images) _, predictions = torch.max(outputs, 1) # collect the correct predictions for each class for label, prediction in zip(labels, predictions): if label == prediction: correct_pred[classes[label]] += 1 total_pred[classes[label]] += 1
# print accuracy for each classfor classname, correct_count in correct_pred.items(): accuracy = 100 * float(correct_count) / total_pred[classname] print(f'Accuracy for class: {classname:5s} is {accuracy:.1f} %')