模型优化系列2:分类器ArcLoss使用Pytorch实现MNIST分类图示

ArcLoss实现

以MNIST数据集为例

前言

尝试了很多版本,目前没有找到一个适合CIFAR10数据集的网络模型0.0

V0

网络结构

self.hidden_layer = nn.Sequential(
    ConvLayer(1, 16, 3, 1, 0),
    nn.MaxPool2d(2),
    ConvLayer(16, 32, 3, 1, 0),
    ConvLayer(32, 64, 3, 1, 0),
    ConvLayer(64, 128, 3, 1, 0),
    ConvLayer(128, 256, 3, 1, 0),
)

self.fc = nn.Sequential(
    nn.Linear(256 * 5 * 5, 2)
)

参数

data_loader = DataLoader(dataset=mnist_data, shuffle=True, batch_size=256)
opt_net = torch.optim.Adam(net.parameters())
opt_arc = torch.optim.Adam(arc.parameters())

效果

image.png
训练到中途,数据不稳定
image.png
训练100次,无法进一步划分类别
image.png

结论

类别无法完全分开,训练到中途,数据图形爆炸

V1

增加网络深度

网络结构

self.hidden_layer = nn.Sequential(
    ConvLayer(1, 32, 5, 1, 2),
    ConvLayer(32, 64, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(64, 128, 5, 1, 2),
    ConvLayer(128, 256, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(256, 512, 5, 1, 2),
    ConvLayer(512, 512, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(512, 256, 5, 1, 2),
    ConvLayer(256, 128, 5, 1, 2),
    ConvLayer(128, 64, 5, 1, 2),
    nn.MaxPool2d(2, 2)
)

self.fc = nn.Sequential(
    nn.Linear(64, 2)
)

参数

data_loader = DataLoader(dataset=mnist_data, shuffle=True, batch_size=256)
opt_net = torch.optim.Adam(net.parameters())
opt_arc = torch.optim.Adam(arc.parameters())

效果

image.png
image.png

结论

持续十多轮,无法进一步减少损失,尝试更换优化器,实现降低损失

V1.1(最佳)

V1基础上调整优化器Adam–>SGD,其余条件不变

参数

data_loader = DataLoader(dataset=mnist_data, shuffle=True, batch_size=256)
opt_net = torch.optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
scheduler = torch.optim.lr_scheduler.StepLR(opt_net, 20, gamma=0.8)
opt_arc = torch.optim.SGD(arc.parameters(), lr=0.5)

效果

image.png
image.png

结论

分类效果明显,训练速度减慢,SGD优化器在合适的参数下,比Adam分类效果更好

V1.1.2(佳)

V1.1基础上减少网络宽度,其余条件不变

网络结构

self.hidden_layer = nn.Sequential(
    ConvLayer(1, 32, 5, 1, 2),
    ConvLayer(32, 32, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(32, 64, 5, 1, 2),
    ConvLayer(64, 64, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(64, 128, 5, 1, 2),
    ConvLayer(128, 128, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(128, 256, 5, 1, 2),
    ConvLayer(256, 128, 5, 1, 2),
    ConvLayer(128, 64, 5, 1, 2),
    nn.MaxPool2d(2, 2)
)

self.fc = nn.Sequential(
    nn.Linear(64, 2)
)

效果

image.png
image.png

结论

分类明显,宽度减少,收敛速度较慢

V1.1.3

V1.1.2基础上,网络全连接改为卷积,其余条件不变

网络结构

self.hidden_layer = nn.Sequential(
    ConvLayer(1, 32, 5, 1, 2),
    ConvLayer(32, 32, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(32, 64, 5, 1, 2),
    ConvLayer(64, 64, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(64, 128, 5, 1, 2),
    ConvLayer(128, 128, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(128, 256, 5, 1, 2),
    ConvLayer(256, 128, 5, 1, 2),
    ConvLayer(128, 64, 5, 1, 2),
    nn.MaxPool2d(2, 2)
)

self.fc = nn.Sequential(
    # nn.Linear(64, 2)
    nn.Conv2d(64, 2, 5, 1, 2)
)

效果

image.png
image.png

结论

存在重合类别,效果没有全连接好

V1.1.4

V1.1基础上,修改学习率

参数

data_loader = DataLoader(dataset=mnist_data, shuffle=True, batch_size=256)
opt_net = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
scheduler = torch.optim.lr_scheduler.StepLR(opt_net, 20, gamma=0.8)
opt_arc = torch.optim.SGD(arc.parameters(), lr=0.5)

效果

image.png
image.png

结论

类别存在重合

V1.2

V1.1基础上减少网络层数,其余条件不变

网络结构

self.hidden_layer = nn.Sequential(
    ConvLayer(1, 32, 3, 1, 1),
    ConvLayer(32, 64, 3, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(64, 128, 3, 1, 1),
    ConvLayer(128, 256, 3, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(256, 256, 3, 1, 1),
    ConvLayer(256, 128, 3, 1, 2),
    nn.MaxPool2d(2, 2)
)

self.fc = nn.Sequential(
    nn.Linear(128 * 5 * 5, 2)
)

效果

image.png

结论

训练速度较快,分类收敛速度不如较深层的网络结构,类别存在重合

V1.2.2

相比V1.2条件,更换网络宽度,减少参数

网络结构

self.hidden_layer = nn.Sequential(
    ConvLayer(1, 32, 5, 1, 2),
    ConvLayer(32, 32, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(32, 64, 5, 1, 2),
    ConvLayer(64, 64, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(64, 128, 5, 1, 2),
    ConvLayer(128, 128, 5, 1, 2),
    nn.MaxPool2d(2, 2)
)

self.fc = nn.Sequential(
    nn.Linear(128 * 3 * 3, 2)
)

效果

image.png
image.png

结论

类别存在重合

V1.2.3(佳)

V1.2.2基础上,修改网络学习率

参数

data_loader = DataLoader(dataset=mnist_data, shuffle=True, batch_size=256)
opt_net = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
scheduler = torch.optim.lr_scheduler.StepLR(opt_net, 20, gamma=0.8)
opt_arc = torch.optim.SGD(arc.parameters(), lr=0.5)

效果

image.png
image.png

结论

分类明显,收敛较快

V1.2.4

V1.2.3基础上,修改批次

参数

data_loader = DataLoader(dataset=mnist_data, shuffle=True, batch_size=512)
opt_net = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
scheduler = torch.optim.lr_scheduler.StepLR(opt_net, 20, gamma=0.8)
opt_arc = torch.optim.SGD(arc.parameters(), lr=0.5)

效果

image.png
image.png

结论

批次256–>512,类别存在重合

V2

相比V1条件,更换网络,加深深度,减少宽度,其余条件不变

网络结构

self.hidden_layer = nn.Sequential(
    ConvLayer(1, 32, 5, 1, 2),
    ConvLayer(32, 32, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(32, 32, 5, 1, 2),
    ConvLayer(32, 32, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(32, 32, 5, 1, 2),
    ConvLayer(32, 32, 5, 1, 2),
    ConvLayer(32, 32, 5, 1, 2),
    nn.MaxPool2d(2, 2),
    ConvLayer(32, 32, 5, 1, 2),
    ConvLayer(32, 32, 5, 1, 2),
    ConvLayer(32, 32, 5, 1, 2),
    nn.MaxPool2d(2, 2)
)

self.fc = nn.Sequential(
    nn.Linear(32, 2)
)

效果

image.png
image.png

结论

分类存在重合

V2.1

V2基础上,修改学习率

参数

data_loader = DataLoader(dataset=mnist_data, shuffle=True, batch_size=256)
opt_net = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
scheduler = torch.optim.lr_scheduler.StepLR(opt_net, 20, gamma=0.8)
opt_arc = torch.optim.SGD(arc.parameters(), lr=0.5)

效果

image.png
image.png

结论

类别存在重合

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MNIST是一个非常经典的手写数字识别数据集,使用PyTorch实现MNIST分类可以分为以下几个步骤: 1. 导入必要的库和数据集 ```python import torch import torch.nn as nn import torch.optim as optim import torchvision.datasets as datasets import torchvision.transforms as transforms train_data = datasets.MNIST(root='data', train=True, transform=transforms.ToTensor(), download=True) test_data = datasets.MNIST(root='data', train=False, transform=transforms.ToTensor(), download=True) train_loader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True) test_loader = torch.utils.data.DataLoader(test_data, batch_size=64, shuffle=False) ``` 2. 定义模型 我们可以使用一个简单的卷积神经网络来实现MNIST分类。这里我们定义了一个包含两个卷积层和两个全连接层的模型。 ```python class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 32, 3, padding=1) self.conv2 = nn.Conv2d(32, 64, 3, padding=1) self.fc1 = nn.Linear(64 * 7 * 7, 128) self.fc2 = nn.Linear(128, 10) def forward(self, x): x = nn.functional.relu(self.conv1(x)) x = nn.functional.max_pool2d(x, 2) x = nn.functional.relu(self.conv2(x)) x = nn.functional.max_pool2d(x, 2) x = x.view(-1, 64 * 7 * 7) x = nn.functional.relu(self.fc1(x)) x = self.fc2(x) return x model = Net() ``` 3. 定义损失函数和优化 ```python criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=0.001) ``` 4. 训练模型 ```python for epoch in range(10): for i, (images, labels) in enumerate(train_loader): optimizer.zero_grad() outputs = model(images) loss = criterion(outputs, labels) loss.backward() optimizer.step() if (i+1) % 100 == 0: print('Epoch [%d/%d], Step [%d/%d], Loss: %.4f' % (epoch+1, 10, i+1, len(train_loader), loss.item())) ``` 5. 测试模型 ```python 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() print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total)) ``` 这样就完成了使用PyTorch实现MNIST分类的过程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

饭碗、碗碗香

感谢壮士的慷概解囊!

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

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

打赏作者

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

抵扣说明:

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

余额充值