使用pytorch训练mnist数据集上的卷积神经网络
时间: 2023-04-27 15:06:02 浏览: 178
使用PyTorch训练MNIST数据集上的卷积神经网络可以按照以下步骤进行:
1. 导入必要的库和数据集
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.datasets as datasets
import torchvision.transforms as transforms
# 加载MNIST数据集
train_dataset = datasets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = datasets.MNIST(root='./data', train=False, transform=transforms.ToTensor(), download=True)
# 定义批次大小
batch_size = 64
# 创建数据加载器
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
```
2. 定义卷积神经网络模型
```python
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1)
self.relu1 = nn.ReLU()
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
self.relu2 = nn.ReLU()
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
self.fc1 = nn.Linear(64 * 7 * 7, 128)
self.relu3 = nn.ReLU()
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
out = self.conv1(x)
out = self.relu1(out)
out = self.conv2(out)
out = self.relu2(out)
out = self.pool(out)
out = out.view(out.size(), -1)
out = self.fc1(out)
out = self.relu3(out)
out = self.fc2(out)
return out
# 创建模型实例
model = ConvNet()
```
3. 定义损失函数和优化器
```python
# 定义损失函数
criterion = nn.CrossEntropyLoss()
# 定义优化器
optimizer = optim.Adam(model.parameters(), lr=.001)
```
4. 训练模型
```python
# 定义训练函数
def train(model, train_loader, criterion, optimizer, num_epochs):
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
# 前向传播
outputs = model(images)
loss = criterion(outputs, labels)
# 反向传播和优化
optimizer.zero_grad()
loss.backward()
optimizer.step()
# 每100个批次打印一次训练信息
if (i+1) % 100 == :
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, len(train_loader), loss.item()))
# 开始训练
num_epochs = 5
train(model, train_loader, criterion, optimizer, num_epochs)
```
5. 测试模型
```python
# 定义测试函数
def test(model, test_loader):
# 测试模式
model.eval()
# 定义变量
correct =
total =
# 不计算梯度
with torch.no_grad():
for images, labels in test_loader:
# 前向传播
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
# 统计正确率
total += labels.size()
correct += (predicted == labels).sum().item()
# 输出测试结果
print('Accuracy of the model on the 10000 test images: {:.2f} %'.format(100 * correct / total))
# 测试模型
test(model, test_loader)
```
以上就是使用PyTorch训练MNIST数据集上的卷积神经网络的完整代码。
阅读全文