用PyTorch构建基于卷积神经网络的手写数字识别模型,包括卷积层、池化层、激活函数、损失函数等等,使用GPU进行训练,采用精确率、召回率、F1值评测模型表现。给出详细的代码注释。
时间: 2023-12-15 11:07:11 浏览: 95
基于PyTorch实现卷积神经网络的手写数字识别项目(源码+数据集+说明文档).rar
5星 · 资源好评率100%
好的,以下是基于PyTorch构建手写数字识别模型的代码。首先需要导入相关的包和库。我们需要使用torch和torchvision来构建模型和数据集,使用numpy来进行数据处理。
```python
import torch
import torchvision
import torchvision.transforms as transforms
import numpy as np
```
接下来,我们需要定义一些超参数,例如学习率、批次大小、训练轮数等等。
```python
learning_rate = 0.001
batch_size = 100
num_epochs = 10
```
然后,我们需要加载手写数字数据集MNIST,这个数据集已经被预处理成一个28x28的图像和一个标签。图像是灰度图像,所以只有一个通道。
```python
# 加载训练集和测试集
train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = torchvision.datasets.MNIST(root='./data', train=False, transform=transforms.ToTensor(), download=True)
# 加载数据集
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)
```
接下来,我们需要定义卷积神经网络的结构。这里我们使用两个卷积层和两个池化层,然后是一个全连接层和一个输出层。在每个卷积层和全连接层之后都使用ReLU激活函数。
```python
class ConvNet(torch.nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.layer1 = torch.nn.Sequential(
torch.nn.Conv2d(1, 32, kernel_size=5, stride=1, padding=2),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2))
self.layer2 = torch.nn.Sequential(
torch.nn.Conv2d(32, 64, kernel_size=5, stride=1, padding=2),
torch.nn.ReLU(),
torch.nn.MaxPool2d(kernel_size=2, stride=2))
self.fc1 = torch.nn.Linear(7*7*64, 1000)
self.fc2 = torch.nn.Linear(1000, 10)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.reshape(out.size(0), -1)
out = self.fc1(out)
out = self.fc2(out)
return out
model = ConvNet()
```
接下来,我们需要定义损失函数和优化器。这里我们使用交叉熵损失函数和Adam优化器。
```python
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
```
然后,我们可以开始训练模型。每个批次数据会被送入网络中进行前向传播、计算损失、反向传播和更新参数。训练过程中,我们会输出每个批次数据的损失和准确率。
```python
# 训练模型
total_step = len(train_loader)
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 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
# 测试模型
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('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))
```
最后,我们可以使用精确率、召回率和F1值来评估模型的性能。
```python
# 计算精确率、召回率和F1值
model.eval()
with torch.no_grad():
correct = 0
total = 0
true_positives = 0
false_positives = 0
false_negatives = 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()
true_positives += ((predicted == labels) & (predicted == 1)).sum().item()
false_positives += ((predicted != labels) & (predicted == 1)).sum().item()
false_negatives += ((predicted != labels) & (predicted == 0)).sum().item()
precision = true_positives / (true_positives + false_positives)
recall = true_positives / (true_positives + false_negatives)
f1_score = 2 * (precision * recall) / (precision + recall)
print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))
print('Precision of the model on the test images: {:.4f}'.format(precision))
print('Recall of the model on the test images: {:.4f}'.format(recall))
print('F1 Score of the model on the test images: {:.4f}'.format(f1_score))
```
这就是使用PyTorch构建基于卷积神经网络的手写数字识别模型的完整代码,希望对你有帮助。
阅读全文