现代卷积网络实战系列3:PyTorch从零构建AlexNet训练MNIST数据集

🌈🌈🌈现代卷积网络实战系列 总目录

本篇文章的代码运行界面均在Pycharm中进行
本篇文章配套的代码资源已经上传

1、MNIST数据集处理、加载、网络初始化、测试函数
2、训练函数、PyTorch构建LeNet网络
3、PyTorch从零构建AlexNet训练MNIST数据集
4、PyTorch从零构建VGGNet训练MNIST数据集
5、PyTorch从零构建GoogLeNet训练MNIST数据集
6、PyTorch从零构建ResNet训练MNIST数据集

5、AlexNet

AlexNet提出了一下5点改进:

  1. 使用了Dropout,防止过拟合
  2. 使用Relu作为激活函数,极大提高了特征提取效果
  3. 使用MaxPooling池化进行特征降维,极大提高了特征提取效果
  4. 首次使用GPU进行训练
  5. 使用了LRN局部响应归一化(对局部神经元的活动创建竞争机制,使得其中响应比较大的值变得相对更大,并抑制其他反馈较小的神经元,增强了模型的泛化能力)

6、AlexNet网络结构

AlexNet(
 (feature): Sequential(
  (0): Conv2d(1, 32, kernel_size=(5, 5), stride=(1, 1), padding=(1, 1))
  (1): ReLU(inplace=True)
  (2): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (3): ReLU(inplace=True)
  (4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  (5): Conv2d(64, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (6): ReLU(inplace=True)
  (7): Conv2d(96, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (8): ReLU(inplace=True)
  (9): Conv2d(64, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (10): ReLU(inplace=True)
  (11): MaxPool2d(kernel_size=2, stride=1, padding=0, dilation=1, ceil_mode=False)
  )
 (classifier): Sequential(
  (0): Dropout(p=0.5, inplace=False)
  (1): Linear(in_features=4608, out_features=2048, bias=True)
  (2): ReLU(inplace=True)
  (3): Dropout(p=0.5, inplace=False)
  (4): Linear(in_features=2048, out_features=1024, bias=True)
  (5): ReLU(inplace=True)
  (6): Linear(in_features=1024, out_features=10, bias=True)
  )
)

AlexNet是在LeNet基础上进行的改进,它基本定义了一个固定组件,就是**(卷积层+激活层),然后还有就是(卷积层+激活层+卷积层+激活层+池化)**,LeNet实际上是第一个卷积神经网络,AlexNet才是真正让深度学习、神经网络、CNN这几个关键词被大家熟知的。

所以AlexNet基本就分两个部分,一个是卷积,一个是分类。卷积就是**(卷积层+激活层+卷积层+激活层+池化)**,分类就是(Dropout+全连接层+激活层+Dropout+全连接层+激活层+全连接层)

7、PyTorch构建AlexNet

class AlexNet(nn.Module):
    def __init__(self, num=10):
        super(AlexNet, self).__init__()
        self.feature = nn.Sequential(
            nn.Conv2d(1, 32, kernel_size=5, stride=1, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(32, 64, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=2),
            nn.Conv2d(64, 96, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(96, 64, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(64, 32, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=2, stride=1),
        )
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(32 * 12 * 12, 2048),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(2048, 1024),
            nn.ReLU(inplace=True),
            nn.Linear(1024, num),
        )

    def forward(self, x):
        x = self.feature(x)
        x = x.view(-1, 32 * 12 * 12)
        x = self.classifier(x)
        return x

10个epoch训练过程的打印:

D:\conda\envs\pytorch\python.exe A:\0_MNIST\train.py

Reading data…
train_data: (60000, 28, 28) train_label (60000,)
test_data: (10000, 28, 28) test_label (10000,)

Initialize neural network
test loss: 2302.56
test accuracy: 10.1 %

epoch step: 1
training loss: 167.49
test loss: 46.66
test accuracy: 98.73 %

epoch step: 2
training loss: 59.43
test loss: 36.14
test accuracy: 98.95 %

epoch step: 3
training loss: 49.94
test loss: 24.93
test accuracy: 99.22 %

epoch step: 4
training loss: 38.7
test loss: 20.42
test accuracy: 99.45 %

epoch step: 5
training loss: 35.07
test loss: 26.18
test accuracy: 99.17 %

epoch step: 6
training loss: 30.65
test loss: 22.65
test accuracy: 99.34 %

epoch step: 7
training loss: 26.34
test loss: 20.5
test accuracy: 99.31 %

epoch step: 8
training loss: 26.24
test loss: 27.69
test accuracy: 99.11 %

epoch step: 9
training loss: 23.14
test loss: 22.55
test accuracy: 99.39 %

epoch step: 10
training loss: 20.22
test loss: 28.51
test accuracy: 99.24 %

Training finished

进程已结束,退出代码为 0

效果已经非常好了

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供一份基于PyTorch实现LeNet-5模型的代码,并使用FashionMNIST数据集进行训练。 首先,您需要安装PyTorch和torchvision库,以便使用LeNet-5模型和FashionMNIST数据集。您可以使用以下命令在终端中安装它们: ``` pip install torch pip install torchvision ``` 然后,您可以使用以下代码来定义LeNet-5模型: ```python import torch.nn as nn import torch.nn.functional as F class LeNet5(nn.Module): def __init__(self): super(LeNet5, self).__init__() self.conv1 = nn.Conv2d(1, 6, kernel_size=5, stride=1) self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2) self.conv2 = nn.Conv2d(6, 16, kernel_size=5, stride=1) self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2) self.fc1 = nn.Linear(16 * 4 * 4, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) def forward(self, x): x = self.pool1(F.relu(self.conv1(x))) x = self.pool2(F.relu(self.conv2(x))) x = x.view(-1, 16 * 4 * 4) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = self.fc3(x) return x ``` 这个LeNet-5模型定义了两个卷积层和三个全连接层。在forward()函数中,我们使用ReLU激活函数和max pooling操作来提取特征,并将特征传递到全连接层中进行分类。 接下来,您可以使用以下代码来加载FashionMNIST数据集并进行训练: ```python import torch import torch.nn as nn import torch.optim as optim import torchvision.transforms as transforms import torchvision.datasets as datasets # 定义一些超参数 batch_size = 64 learning_rate = 0.01 num_epochs = 10 # 加载FashionMNIST数据集 transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ]) train_dataset = datasets.FashionMNIST(root='./data', train=True, transform=transform, download=True) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True) # 实例化LeNet-5模型和损失函数 model = LeNet5() criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(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步打印一次日志 if (i + 1) % 100 == 0: print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch + 1, num_epochs, i + 1, len(train_loader), loss.item())) ``` 在这个训练循环中,我们首先使用SGD优化器和交叉熵损失函数实例化了LeNet-5模型。然后,我们将FashionMNIST数据集加载到train_loader中,并使用train_loader在每个epoch中进行训练。对于每个batch,我们首先执行前向传播,计算输出和损失,然后执行反向传播并更新模型参数。最后,我们在每个epoch的日志中记录损失值。 希望这个代码对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机器学习杨卓越

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值