pytorch - building a convolutional neural network

Build Convolutional Neural Networks

  • The input and layer in the convolutional network are somewhat different from the traditional neural network and need to be redesigned, and the training modules are basically the same
    import torch
    import torch.nn as nn
    import torch.optim as optim
    import torch.nn.functional as F
    from torchvision import datasets,transforms 
    import matplotlib.pyplot as plt
    import numpy as np
    %matplotlib inline

    First read the data

  • Build the training set and test set (validation set) separately
  • DataLoader to iteratively fetch data
    # 定义超参数 
    input_size = 28  #图像的总尺寸28*28
    num_classes = 10  #标签的种类数
    num_epochs = 3  #训练的总循环周期
    batch_size = 64  #一个撮(批次)的大小,64张图片
    
    # 训练集
    train_dataset = datasets.MNIST(root='./data',  
                                train=True,   
                                transform=transforms.ToTensor(),  
                                download=True) 
    
    # 测试集
    test_dataset = datasets.MNIST(root='./data', 
                               train=False, 
                               transform=transforms.ToTensor())
    
    # 构建batch数据
    train_loader = torch.utils.data.DataLoader(dataset=train_dataset, 
                                               batch_size=batch_size, 
                                               shuffle=True)
    test_loader = torch.utils.data.DataLoader(dataset=test_dataset, 
                                               batch_size=batch_size, 
                                               shuffle=True)

    Convolutional network module construction

  • General convolution layer, relu layer, pooling layer can be written as a package
  • Note that the final result of convolution is still a feature map, which needs to be converted into a vector to do classification or regression tasks
    class CNN(nn.Module):
        def __init__(self):
            super(CNN, self).__init__()
            self.conv1 = nn.Sequential(         # 输入大小 (1, 28, 28)
                nn.Conv2d(
                    in_channels=1,              # 灰度图
                    out_channels=16,            # 要得到几多少个特征图
                    kernel_size=5,              # 卷积核大小
                    stride=1,                   # 步长
                    padding=2,                  # 如果希望卷积后大小跟原来一样,需要设置padding=(kernel_size-1)/2 if stride=1
                ),                              # 输出的特征图为 (16, 28, 28)
                nn.ReLU(),                      # relu层
                nn.MaxPool2d(kernel_size=2),    # 进行池化操作(2x2 区域), 输出结果为: (16, 14, 14)
            )
            self.conv2 = nn.Sequential(         # 下一个套餐的输入 (16, 14, 14)
                nn.Conv2d(16, 32, 5, 1, 2),     # 输出 (32, 14, 14)
                nn.ReLU(),                      # relu层
                nn.Conv2d(32, 32, 5, 1, 2),
                nn.ReLU(),
                nn.MaxPool2d(2),                # 输出 (32, 7, 7)
            )
            
            self.conv3 = nn.Sequential(         # 下一个套餐的输入 (16, 14, 14)
                nn.Conv2d(32, 64, 5, 1, 2),     # 输出 (32, 14, 14)
                nn.ReLU(),             # 输出 (32, 7, 7)
            )
            
            self.out = nn.Linear(64 * 7 * 7, 10)   # 全连接层得到的结果
    
        def forward(self, x):
            x = self.conv1(x)
            x = self.conv2(x)
            x = self.conv3(x)
            x = x.view(x.size(0), -1)           # flatten操作,结果为:(batch_size, 32 * 7 * 7)
            output = self.out(x)
            return output

    Accuracy as an evaluation criterion

    def accuracy(predictions, labels):
        pred = torch.max(predictions.data, 1)[1] 
        rights = pred.eq(labels.data.view_as(pred)).sum() 
        return rights, len(labels) 

    Train the network model

    # 实例化
    net = CNN() 
    #损失函数
    criterion = nn.CrossEntropyLoss() 
    #优化器
    optimizer = optim.Adam(net.parameters(), lr=0.001) #定义优化器,普通的随机梯度下降算法
    
    #开始训练循环
    for epoch in range(num_epochs):
        #当前epoch的结果保存下来
        train_rights = [] 
        
        for batch_idx, (data, target) in enumerate(train_loader):  #针对容器中的每一个批进行循环
            net.train()                             
            output = net(data) 
            loss = criterion(output, target) 
            optimizer.zero_grad() 
            loss.backward() 
            optimizer.step() 
            right = accuracy(output, target) 
            train_rights.append(right) 
    
        
            if batch_idx % 100 == 0: 
                
                net.eval() 
                val_rights = [] 
                
                for (data, target) in test_loader:
                    output = net(data) 
                    right = accuracy(output, target) 
                    val_rights.append(right)
                    
                #准确率计算
                train_r = (sum([tup[0] for tup in train_rights]), sum([tup[1] for tup in train_rights]))
                val_r = (sum([tup[0] for tup in val_rights]), sum([tup[1] for tup in val_rights]))
    
                print('当前epoch: {} [{}/{} ({:.0f}%)]\t损失: {:.6f}\t训练集准确率: {:.2f}%\t测试集正确率: {:.2f}%'.format(
                    epoch, batch_idx * batch_size, len(train_loader.dataset),
                    100. * batch_idx / len(train_loader), 
                    loss.data, 
                    100. * train_r[0].numpy() / train_r[1], 
                    100. * val_r[0].numpy() / val_r[1]))
    Current epoch: 0 [0/60000 (0%)] Loss: 2.300918 Training set accuracy: 10.94% Test set accuracy: 10.10%
    Current epoch: 0 [6400/60000 (11%)] Loss: 0.204191 Training set accuracy: 78.06% Test set accuracy: 93.31%
    Current epoch: 0 [12800/60000 (21%)] Loss: 0.039503 Training set accuracy: 86.51% Test set accuracy: 96.69%
    Current epoch: 0 [19200/60000 (32%)] Loss: 0.057866 Training set accuracy: 89.93% Test set accuracy: 97.54%
    Current epoch: 0 [25600/60000 (43%)] Loss: 0.069566 Training set accuracy: 91.68% Test set accuracy: 97.68%
    Current epoch: 0 [32000/60000 (53%)] Loss: 0.228793 Training set accuracy: 92.85% Test set accuracy: 98.18%
    Current epoch: 0 [38400/60000 (64%)] Loss: 0.111003 Training set accuracy: 93.72% Test set accuracy: 98.16%
    Current epoch: 0 [44800/60000 (75%)] Loss: 0.110226 Training set accuracy: 94.28% Test set accuracy: 98.44%
    Current epoch: 0 [51200/60000 (85%)] Loss: 0.014538 Training set accuracy: 94.78% Test set accuracy: 98.60%
    Current epoch: 0 [57600/60000 (96%)] Loss: 0.051019 Training set accuracy: 95.14% Test set accuracy: 98.45%
    Current epoch: 1 [0/60000 (0%)] Loss: 0.036383 Training set accuracy: 98.44% Test set accuracy: 98.68%
    Current epoch: 1 [6400/60000 (11%)] Loss: 0.088116 Training set accuracy: 98.50% Test set accuracy: 98.37%
    Current epoch: 1 [12800/60000 (21%)] Loss: 0.120306 Training set accuracy: 98.59% Test set accuracy: 98.97%
    Current epoch: 1 [19200/60000 (32%)] Loss: 0.030676 Training set accuracy: 98.63% Test set accuracy: 98.83%
    Current epoch: 1 [25600/60000 (43%)] Loss: 0.068475 Training set accuracy: 98.59% Test set accuracy: 98.87%
    Current epoch: 1 [32000/60000 (53%)] Loss: 0.033244 Training set accuracy: 98.62% Test set accuracy: 99.03%
    Current epoch: 1 [38400/60000 (64%)] Loss: 0.024162 Training set accuracy: 98.67% Test set accuracy: 98.81%
    Current epoch: 1 [44800/60000 (75%)] Loss: 0.006713 Training set accuracy: 98.69% Test set accuracy: 98.17%
    Current epoch: 1 [51200/60000 (85%)] Loss: 0.009284 Training set accuracy: 98.69% Test set accuracy: 98.97%
    Current epoch: 1 [57600/60000 (96%)] Loss: 0.036536 Training set accuracy: 98.68% Test set accuracy: 98.97%
    Current epoch: 2 [0/60000 (0%)] Loss: 0.125235 Training set accuracy: 98.44% Test set accuracy: 98.73%
    Current epoch: 2 [6400/60000 (11%)] Loss: 0.028075 Training set accuracy: 99.13% Test set accuracy: 99.17%
    Current epoch: 2 [12800/60000 (21%)] Loss: 0.029663 Training set accuracy: 99.26% Test set accuracy: 98.39%
    Current epoch: 2 [19200/60000 (32%)] Loss: 0.073855 Training set accuracy: 99.20% Test set accuracy: 98.81%
    Current epoch: 2 [25600/60000 (43%)] Loss: 0.018130 Training set accuracy: 99.16% Test set accuracy: 99.09%
    Current epoch: 2 [32000/60000 (53%)] Loss: 0.006968 Training set accuracy: 99.15% Test set accuracy: 99.11%
    

Guess you like

Origin blog.csdn.net/qq_65838372/article/details/132725190