在pytorch框架下,利用CNN实现手写数字识别的完整代码
时间: 2024-02-28 14:52:36 浏览: 68
基于PyTorch-手写数字识别-完整代码(含数据集+输出+完整注释)
好的,以下是一个基于PyTorch框架实现手写数字识别的完整代码:
```
import torch
import torch.nn.functional as F
from torchvision import datasets, transforms
# 定义超参数
batch_size = 64
learning_rate = 0.01
num_epochs = 10
# 加载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=False)
# 定义数据加载器
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)
# 定义CNN模型
class CNN(torch.nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2)
self.conv2 = torch.nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2)
self.fc1 = torch.nn.Linear(7*7*32, 256)
self.fc2 = torch.nn.Linear(256, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2)
x = x.view(-1, 7*7*32)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x, dim=1)
# 实例化模型和优化器
model = CNN()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
# 训练模型
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = images
labels = labels
outputs = model(images)
loss = F.nll_loss(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, len(train_loader), loss.item()))
# 测试模型
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images
labels = labels
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))
```
在这个代码中,我们首先加载MNIST数据集,然后定义了一个包含两个卷积层和两个全连接层的CNN模型。我们使用负对数似然损失函数和随机梯度下降优化器来训练模型,并在测试集上评估模型的准确率。
阅读全文