代码分段讲解
时间: 2023-07-02 11:15:59 浏览: 91
好的,以下是代码的分段讲解:
1. 导入 PyTorch 和其他必要的库
```python
import torch
import torch.nn as nn
```
2. 定义卷积神经网络模型
```python
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(64 * 8 * 8, 1024)
self.fc2 = nn.Linear(1024, 10)
def forward(self, x):
x = self.conv1(x)
x = nn.functional.relu(x)
x = self.pool(x)
x = self.conv2(x)
x = nn.functional.relu(x)
x = self.pool(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = nn.functional.relu(x)
x = self.fc2(x)
return x
```
这里定义了一个名为 `CNN` 的子类,继承自 `nn.Module`。在构造函数中定义了卷积层、池化层和全连接层等网络结构,`forward` 函数则定义了前向传播的过程。
3. 定义训练函数
```python
def train(model, train_loader, optimizer, criterion, device):
model.train()
train_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(train_loader):
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, targets)
loss.backward()
optimizer.step()
train_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
return train_loss/(batch_idx+1), 100.*correct/total
```
这里定义了一个名为 `train` 的函数,接受模型、数据加载器、优化器、损失函数和设备等参数。在函数内部进行模型的训练,计算训练损失和准确率,并返回训练损失和准确率。
4. 定义测试函数
```python
def test(model, test_loader, criterion, device):
model.eval()
test_loss = 0
correct = 0
total = 0
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(test_loader):
inputs, targets = inputs.to(device), targets.to(device)
outputs = model(inputs)
loss = criterion(outputs, targets)
test_loss += loss.item()
_, predicted = outputs.max(1)
total += targets.size(0)
correct += predicted.eq(targets).sum().item()
return test_loss/(batch_idx+1), 100.*correct/total
```
这里定义了一个名为 `test` 的函数,接受模型、数据加载器、损失函数和设备等参数。在函数内部进行模型的测试,计算测试损失和准确率,并返回测试损失和准确率。
5. 加载数据集
```python
train_loader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2)
test_loader = torch.utils.data.DataLoader(testset, batch_size=128, shuffle=False, num_workers=2)
```
这里使用 PyTorch 提供的 `DataLoader` 类加载 CIFAR10 数据集,并设置每个 batch 的大小、是否随机打乱数据和使用的线程数。
6. 定义超参数
```python
device = 'cuda' if torch.cuda.is_available() else 'cpu'
lr = 0.001
momentum = 0.9
epochs = 10
```
这里定义了设备类型、学习率、动量和训练轮数等超参数。
7. 初始化模型、优化器、损失函数
```python
model = CNN().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=momentum)
criterion = nn.CrossEntropyLoss()
```
这里初始化了模型、优化器和损失函数,使用前面定义的 `CNN` 类构造一个卷积神经网络模型。
8. 开始训练和测试
```python
for epoch in range(1, epochs+1):
train_loss, train_acc = train(model, train_loader, optimizer, criterion, device)
test_loss, test_acc = test(model, test_loader, criterion, device)
print('Epoch: {:03d}, Train Loss: {:.3f}, Train Acc: {:.3f}%, Test Loss: {:.3f}, Test Acc: {:.3f}%'.format(epoch, train_loss, train_acc, test_loss, test_acc))
```
这里使用一个 for 循环进行训练和测试,每轮训练后输出训练损失、训练准确率、测试损失和测试准确率等信息。
阅读全文