欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Pytorch实现卷积神经网络CNN

程序员文章站 2022-07-06 17:56:24
...

本文使用Pytorch实现卷积神经网络CNN,仅为Pytorch新入坑者提供帮助,避免入门到放弃;
数据集为MNIST如文中代码所示;
最后,恭喜各位入坑,在调参的路上越走越远!

import torch
from torch.autograd import Variable
import torch.utils.data as Data
import torchvision  # 数据库模块
import torch.nn as nn
import matplotlib.pyplot as plt


LR = 0.001  # 学习率
BATCH_SIZE = 50  # 表示每次选取50个样本作训练
EPOCH = 1  # epoch表示整个数据集重复训练次数
DOWNLOAD_MNIST = False  # 是否下载MNIST

train_data = torchvision.datasets.MNIST(root='./mnist',
					train=True,  # 表示这是训练集
                                       	transform=torchvision.transforms.ToTensor(),  # 原始数据是array数组,转换为tensor,同时进行归一化
                                        download=DOWNLOAD_MNIST)

# 打印图片

Pytorch实现卷积神经网络CNN

print(train_data.data.size())  # 打印训练集的大小
print(train_data.targets.size())  # 打印训练集标签的大小
‘’使用imshow()函数加载训练集中的图片’‘’
plt.imshow(train_data.data[0].numpy(), cmap='gray')  
plt.title('%i' % train_data.targets[0])
plt.show()

# 训练
train_loader = Data.DataLoader(dataset=train_data, batch_size=BATCH_SIZE,
                              			 shuffle=True, num_workers=2)  # 批量化(包装)处理数据集

test_data = torchvision.datasets.MNIST(root='./mnist', train=False)

‘’‘#测试集shape from (2000, 28, 28) to (2000, 1, 28, 28), value in range(0,1)’‘’
test_x = torch.unsqueeze(test_data.data, dim=[:2000].type(torch.FloatTensor).cuda()/255.  

‘’‘#这里使用cuda()表示使用调用GPU加速跑测试集/训练集’‘’
test_y = test_data.targets[:2000].cuda()  

# 搭建CNN网络
if __name__ == '__main__':
    class CNN(torch.nn.Module):  # 定义一个神经网络层
        def __init__(self):  # 定义初始化
            super(CNN, self).__init__()  # 继承初始层
            self.conv1 = torch.nn.Sequential(
                nn.Conv2d(in_channels=1,
                          out_channels=16,  # 卷积核个数
                          kernel_size=5,  # 卷积核大小
                          stride=1,  # 卷积核移动步长
                          padding=2,  # 填充数目
                          ),  #-->卷积后的图片形状大小(28,28,16)
                nn.ReLU(),
                nn.MaxPool2d(kernel_size=2),  #-->池化后的图片形状大小(14,14,16)
            )
            self.conv2 = nn.Sequential(
                nn.Conv2d(16, 32, 5, 1, 2),  #-->第二次卷积后的图片形状大小(14,14,32)
                nn.ReLU(),
                nn.MaxPool2d(kernel_size=2)  #-->第二次池化后的图片形状大小(7,7,32)
                )
            self.out = nn.Linear(32*7*7, 10)  # 全连接层

        def forward(self, x):
            x = self.conv1(x)
            x = self.conv2(x)
            x = x.view(x.size(0), -1)  # 数据展平为多维后的图片形状大小(batch size,7,7,32)
            output = self.out(x)
            return output

    cnn = CNN()
    print(cnn)  # 打印CNN网络
    cnn.cuda()  # CNN模型使用GPU加速

    optimizer = torch.optim.Adam(cnn.parameters(), lr=LR, betas=(0.9, 0.99))  # Adam优化器
    loss_func = nn.CrossEntropyLoss()  # 损失函数

    # 训练模型
    for epoch in range(EPOCH):
        for step, (b_x, b_y) in enumerate(train_loader):
            output = cnn(b_x.cuda())  # CNN预测输出
            loss = loss_func(output, b_y.cuda())
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if step % 50 == 0:
                test_output = cnn(test_x.cuda())
                test_pred = torch.max(test_output, 1)[1].cuda().data
                test_accuracy = float((test_pred == test_y).sum().item()) / float(test_y.size(0))
                print('Epoch:', epoch, 'Train loss:', loss.data.cpu().numpy(), 'Test Accuracy:', test_accuracy)

    # 观察前20个数据误差
    test_output = cnn(test_x[:20])
    pred_y = torch.max(test_output, 1)[1].cuda().data
    print(pred_y.cpu().numpy(), 'prediction number')
    print(test_y[:20].cpu().numpy(), 'real number')

前20张测试集图片标签预测结果如下所示:

Epoch: 0 Train loss: 2.3024046 Test Accuracy: 0.1915
Epoch: 0 Train loss: 0.7746874 Test Accuracy: 0.832
Epoch: 0 Train loss: 0.23315571 Test Accuracy: 0.884
Epoch: 0 Train loss: 0.2868251 Test Accuracy: 0.9175
Epoch: 0 Train loss: 0.3143016 Test Accuracy: 0.9255
Epoch: 0 Train loss: 0.11868002 Test Accuracy: 0.938
Epoch: 0 Train loss: 0.10169793 Test Accuracy: 0.941
Epoch: 0 Train loss: 0.2048641 Test Accuracy: 0.9525
Epoch: 0 Train loss: 0.12582631 Test Accuracy: 0.9675
Epoch: 0 Train loss: 0.03774266 Test Accuracy: 0.9625
Epoch: 0 Train loss: 0.051590152 Test Accuracy: 0.967
Epoch: 0 Train loss: 0.007484264 Test Accuracy: 0.9675
Epoch: 0 Train loss: 0.15514685 Test Accuracy: 0.9705
Epoch: 0 Train loss: 0.03676407 Test Accuracy: 0.97
Epoch: 0 Train loss: 0.035673436 Test Accuracy: 0.967
Epoch: 0 Train loss: 0.06010488 Test Accuracy: 0.9715
Epoch: 0 Train loss: 0.13796066 Test Accuracy: 0.9765
Epoch: 0 Train loss: 0.09808495 Test Accuracy: 0.974
Epoch: 0 Train loss: 0.016342957 Test Accuracy: 0.977
Epoch: 0 Train loss: 0.11043023 Test Accuracy: 0.9745
Epoch: 0 Train loss: 0.038324982 Test Accuracy: 0.9795
Epoch: 0 Train loss: 0.11415327 Test Accuracy: 0.9805
Epoch: 0 Train loss: 0.15161397 Test Accuracy: 0.981
Epoch: 0 Train loss: 0.11598789 Test Accuracy: 0.97
[7 2 1 0 4 1 4 9 5 9 0 6 9 0 1 5 9 7 3 4] prediction number
[7 2 1 0 4 1 4 9 5 9 0 6 9 0 1 5 9 7 3 4] real number
相关标签: Pytorch系列