哈工大机器学习实验五(卷积神经网络的实现)
只是简单的思路,不是全部实验流程
1. LeNet-5 流程示意图
2. 代码
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
# 定义LeNet-5模型
class LeNet5(nn.Module):
def __init__(self):
super(LeNet5, self).__init__()
# 第一个卷积层:输入通道1,输出通道6,卷积核大小5
self.conv1 = nn.Conv2d(1, 6, 5)
# 池化层:窗口大小2,步长2
self.pool = nn.MaxPool2d(2, 2)
# 第二个卷积层:输入通道6,输出通道16,卷积核大小5
self.conv2 = nn.Conv2d(6, 16, 5)
# 第一个全连接层:输入特征数(16*5*5),输出特征数120
self.fc1 = nn.Linear(16 * 5 * 5, 120)
# 第二个全连接层:输入特征数120,输出特征数84
self.fc2 = nn.Linear(120, 84)
# 第三个全连接层(输出层):输入特征数84,输出特征数10(数字类别数)
self.fc3 = nn.Linear(84, 10)
self.relu = nn.ReLU()
def forward(self, x):
x = self.conv1(x)
x = self.relu(x)
x = self.pool(x)
x = self.conv2(x)
x = self.pool(x)
x = x.view(-1, 16 * 5 * 5)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
output = F.log_softmax(x, dim=1)
return output
# 设置超参数
batch_size = 64
learning_rate = 0.001
num_epochs = 10
# 加载MNIST数据集
transform_train = transforms.Compose(
[
# 随机旋转图片
transforms.RandomHorizontalFlip(),
# 将图片尺寸resize到32x32
transforms.Resize((32, 32)),
# 将图片转化为Tensor格式
transforms.ToTensor(),
# 正则化(当模型出现过拟合的情况时,用来降低模型的复杂度)
transforms.Normalize((0.1307,), (0.3081,)),
]
)
transform_test = transforms.Compose(
[
# 将图片尺寸resize到32x32
transforms.Resize((32, 32)),
# 将图片转化为Tensor格式
transforms.ToTensor(),
# 正则化(当模型出现过拟合的情况时,用来降低模型的复杂度)
transforms.Normalize((0.1307,), (0.3081,)),
]
)
train_dataset = datasets.MNIST(
root="./data", train=True, download=True, transform=transform_train
)
test_dataset = datasets.MNIST(
root="./data", train=False, download=True, transform=transform_test
)
# 创建数据加载器
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
# 初始化模型、损失函数和优化器
model = LeNet5()
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
# 训练模型
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(
f"Epoch [{epoch + 1}/{num_epochs}], Step [{i + 1}/{len(train_loader)}], Loss: {loss.item():.4f}"
)
# 测试模型
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(f"Accuracy of the network on the 10000 test images: {100 * correct / total}%")
# 保存模型参数
torch.save(model.state_dict(), "lenet5_mnist.pth")