如何使用pytorch写一个数字识别CNN卷积神经网络模型
时间: 2024-10-23 07:08:56 浏览: 33
利用pytorch搭建卷积神经网络(CNN)训练简单手写数字数据集(MNIST)
在PyTorch中创建一个简单的数字识别CNN(Convolutional Neural Network)模型可以分为以下几个步骤:
1. **导入必要的库**:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
```
2. **数据预处理**:
```python
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) # MNIST数据集通常需要标准化像素值
train_dataset = datasets.MNIST(root='./data', train=True, transform=transform, download=True)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=64, shuffle=True)
```
3. **定义CNN模型**:
```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.relu = nn.ReLU()
self.pool = nn.MaxPool2d(kernel_size=2, stride=2) # 最大池化层
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(64 * 7 * 7, 128) # 全连接层,输入是经过卷积和pooling后的扁平化特征
self.fc2 = nn.Linear(128, 10) # 输出层,对应MNIST的10个类别
def forward(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.pool(x)
x = self.conv2(x)
x = self.relu(x)
x = x.view(-1, 64 * 7 * 7) # 扁平化
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
return x
model = ConvNet()
```
4. **配置损失函数、优化器和学习率调整策略**:
```python
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=3)
```
5. **训练模型**:
```python
for epoch in range(num_epochs):
for inputs, labels in train_loader:
optimizer.zero_grad() # 清零梯度
outputs = model(inputs) # 前向传播
loss = criterion(outputs, labels) # 计算损失
loss.backward() # 反向传播
optimizer.step() # 更新参数
scheduler.step(loss.item()) # 学习率调整
print(f"Epoch {epoch+1}/{num_epochs}, Loss: {loss.item():.4f}")
```
6. **评估模型**:
```python
# 添加测试集和评估代码
test_dataset = ... # 加载测试集
test_loader = ...
model.eval() # 转换到评估模式
correct = 0
total = 0
with torch.no_grad():
for images, labels in test_loader:
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: {accuracy:.2f}%")
```
阅读全文