minist手写数字识别
时间: 2023-08-29 14:05:26 浏览: 169
MNIST是一个常用的手写数字识别数据集,其中包含了60,000张训练图片和10,000张测试图片。每张图片大小为28x28像素,灰度图像,数字的大小和位置不一定相同。以下是使用Python和PyTorch实现MNIST手写数字识别的基本步骤:
1. 加载MNIST数据集
```
import torch
import torchvision.datasets as dsets
import torchvision.transforms as transforms
# 定义数据预处理方式
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize(mean=[0.5], std=[0.5])])
# 加载训练集和测试集
train_dataset = dsets.MNIST(root='./data', train=True, transform=transform, download=True)
test_dataset = dsets.MNIST(root='./data', train=False, transform=transform, download=True)
# 定义数据加载器
batch_size = 128
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. 定义模型结构
```
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=5, padding=2)
self.pool1 = nn.MaxPool2d(kernel_size=2)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=5, padding=2)
self.pool2 = nn.MaxPool2d(kernel_size=2)
self.fc1 = nn.Linear(in_features=64*7*7, out_features=1024)
self.fc2 = nn.Linear(in_features=1024, out_features=10)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(p=0.5)
def forward(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.relu(x)
x = self.pool2(x)
x = x.view(x.size(0), -1)
x = self.fc1(x)
x = self.relu(x)
x = self.dropout(x)
x = self.fc2(x)
return x
```
3. 定义损失函数和优化器
```
import torch.optim as optim
model = Net()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
```
4. 训练模型
```
num_epochs = 10
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个batch打印一次训练信息
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Batch [{}/{}], Loss: {:.4f}'
.format(epoch+1, num_epochs, i+1, len(train_loader), loss.item()))
```
5. 测试模型
```
# 测试模型
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the 10000 test images: {} %'.format(100 * correct / total))
```
完整代码示例请参考:https://github.com/pytorch/examples/blob/master/mnist/main.py
阅读全文