手写数字识别问题代码
时间: 2024-12-29 11:22:30 浏览: 4
### 手写数字识别示例代码
对于手写数字识别的任务,采用PyTorch框架下的卷积神经网络(CNN)是一种常见且高效的方法。下面展示一段基于MNIST数据集的手写数字识别的简化版代码。
#### 设置计算设备并加载所需库
```python
import torch
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.optim as optim
```
#### 数据预处理与加载
为了确保输入到模型的数据格式正确无误[^1],需对原始图像执行标准化操作。
```python
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
train_dataset = datasets.MNIST(root='./data', train=True, download=True, transform=transform)
test_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)
train_loader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=1000, shuffle=False)
```
#### 定义CNN架构
构建一个简单的两层卷积神经网络来提取特征,并通过全连接层完成分类任务。
```python
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(64*7*7, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2(x), 2))
x = x.view(-1, 64*7*7)
x = F.relu(self.fc1(x))
output = self.fc2(x)
return output
model = Net()
```
#### 训练过程配置
选择合适的激活函数能够增强模型的表现力[^5];同时定义损失函数和优化策略用于指导权重更新方向。
```python
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model.to(device)
epochs = 10
for epoch in range(epochs):
model.train()
running_loss = 0.0
for data, target in train_loader:
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
outputs = model(data)
loss = criterion(outputs, target)
loss.backward()
optimizer.step()
running_loss += loss.item()
print('Finished Training')
```
#### 测试评估性能
最后,在独立测试集中验证训练好的模型效果如何。
```python
correct = 0
total = 0
with torch.no_grad():
for images, labels in test_loader:
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
print(f'Test Accuracy of the model on {total} test images: {accuracy:.2f}% ')
```
阅读全文