23卷积神经网络LeNet
python
import torch
from torch import nn
import liliPytorch as lp
import matplotlib.pyplot as plt
# 定义一个卷积神经网络
net = nn.Sequential(
nn.Conv2d(1, 6, kernel_size=5, padding=2), # 卷积层1:输入通道数1,输出通道数6,卷积核大小5x5,填充2
nn.ReLU(), # 激活函数
nn.AvgPool2d(kernel_size=2, stride=2), # 平均池化层1:池化窗口大小2x2,步幅2
nn.Conv2d(6, 16, kernel_size=5), # 卷积层2:输入通道数6,输出通道数16,卷积核大小5x5
nn.ReLU(),
nn.AvgPool2d(kernel_size=2, stride=2), # 平均池化层2:池化窗口大小2x2,步幅2
nn.Flatten(), # 展平层:将多维输入展平为1维
nn.Linear(16 * 5 * 5, 120), # 全连接层1:输入节点数16*5*5,输出节点数120
nn.ReLU(),
nn.Linear(120, 84), # 全连接层2:输入节点数120,输出节点数84
nn.ReLU(),
nn.Linear(84, 10) # 全连接层3:输入节点数84,输出节点数10(对应10个分类)
)
# 通过在每一层打印输出的形状,我们可以检查模型
X = torch.rand(size=(1, 1, 28, 28), dtype=torch.float32) # 随机生成一个形状为(1,1,28,28)的张量,作为输入
for layer in net:
X = layer(X) # 将输入依次通过每一层
print(layer.__class__.__name__, 'output shape: \t', X.shape) # 打印每一层的输出形状
"""
Conv2d output shape: torch.Size([1, 6, 28, 28])
ReLU output shape: torch.Size([1, 6, 28, 28])
AvgPool2d output shape: torch.Size([1, 6, 14, 14])
Conv2d output shape: torch.Size([1, 16, 10, 10])
ReLU output shape: torch.Size([1, 16, 10, 10])
AvgPool2d output shape: torch.Size([1, 16, 5, 5])
Flatten output shape: torch.Size([1, 400])
Linear output shape: torch.Size([1, 120])
ReLU output shape: torch.Size([1, 120])
Linear output shape: torch.Size([1, 84])
ReLU output shape: torch.Size([1, 84])
Linear output shape: torch.Size([1, 10])
"""
# 模型训练
batch_size = 256
train_iter, test_iter = lp.loda_data_fashion_mnist(batch_size) # 加载Fashion-MNIST数据集
#分类精度
def accuracy(y_hat,y): #@save
"""计算预测正确的数量"""
#判断y_hat.shape是否为二维以上的矩阵
#并且列数大于1
if len(y_hat.shape) > 1 and y_hat.shape[1] > 1:
#axis = 1 表示按照每一行
#argmax(axis = 1)得到每行最大值的下标
y_hat = y_hat.argmax(axis = 1)
cmp = y_hat.type(y.dtype) == y
return float(cmp.type(y.dtype).sum())
def evaluate_accuracy_gpu(net, data_iter, device=None):
"""使用GPU计算模型在数据集上的精度"""
if isinstance(net, nn.Module):
net.eval() # 将模型设置为评估模式
metric = lp.Accumulator(2) # 正确预测数、预测总数
with torch.no_grad(): # 禁用梯度计算
for X, y in data_iter:
if isinstance(X, list):
X = [x.to(device) for x in X]
else:
X = X.to(device)
y = y.to(device)
metric.add(accuracy(net(X), y), y.numel()) # 累加正确预测数和样本总数
return metric[0] / metric[1] # 返回精度
def train_ch6(net, train_iter, test_iter, num_epochs, lr, device):
"""用GPU训练模型"""
def init_weights(m):
if type(m) == nn.Linear or type(m) == nn.Conv2d:
nn.init.xavier_uniform_(m.weight) # 初始化权重
net.apply(init_weights) # 对网络应用权重初始化
print('training on', device)
net.to(device) # 将模型加载到设备上
optimizer = torch.optim.SGD(net.parameters(), lr=lr) # 使用随机梯度下降优化器
loss = nn.CrossEntropyLoss() # 定义交叉熵损失函数
animator = lp.Animator(xlabel='epoch', xlim=[1, num_epochs],
legend=['train loss', 'train acc', 'test acc']) # 动画工具,绘制训练曲线
timer, num_batches = lp.Timer(), len(train_iter) # 计时器和批次数
for epoch in range(num_epochs):
metric = lp.Accumulator(3) # 训练损失之和,训练准确率之和,样本数
net.train() # 训练模式
for i, (X, y) in enumerate(train_iter):
timer.start()
optimizer.zero_grad() # 梯度清零
X, y = X.to(device), y.to(device) # 将数据加载到设备上
y_hat = net(X) # 前向传播
l = loss(y_hat, y) # 计算损失
l.backward() # 反向传播
optimizer.step() # 更新参数
with torch.no_grad(): # 禁用梯度计算
metric.add(l * X.shape[0], lp.accuracy(y_hat, y), X.shape[0]) # 累加损失、准确率和样本数
timer.stop()
train_l = metric[0] / metric[2] # 计算平均训练损失
train_acc = metric[1] / metric[2] # 计算平均训练准确率
if (i + 1) % (num_batches // 5) == 0 or i == num_batches - 1:
animator.add(epoch + (i + 1) / num_batches,
(train_l, train_acc, None)) # 更新动画
test_acc = evaluate_accuracy_gpu(net, test_iter, device) # 计算测试集上的准确率
animator.add(epoch + 1, (None, None, test_acc)) # 更新动画
print(f'loss {train_l:.3f}, train acc {train_acc:.3f}, '
f'test acc {test_acc:.3f}')
print(f'{metric[2] * num_epochs / timer.sum():.1f} examples/sec '
f'on {str(device)}')
lr, num_epochs = 0.5, 10
train_ch6(net, train_iter, test_iter, num_epochs, lr, lp.try_gpu()) # 训练模型
# d2l.plt.show() # 显示训练曲线
plt.show() # 显示训练曲线
# lr = 0.9,Sigmoid()
# loss 0.466, train acc 0.825, test acc 0.808
# lr = 0.1,Sigmoid()
# loss 1.277, train acc 0.551, test acc 0.568
# lr = 0.1,ReLU()
# loss 0.339, train acc 0.874, test acc 0.803
# lr = 0.5,ReLU()
# loss 0.302, train acc 0.887, test acc 0.857
# lr = 0.6,ReLU()
# loss 0.316, train acc 0.878, test acc 0.861
运行结果: