Classification tasks for introductory learning of Pytorch code (1): Building a network framework

Table of contents

1. Introduction to network framework

2. Guide package

3. Define convolutional neural network

3.1 Code display

3.2 Define the purpose of the network

3.3 Pytorch builds network

4. Test network effects


1. Introduction to network framework

        Network understanding:

        Input the 32*32 size grayscale image (in the following code, enter the 32*32 size RGB color image) into the network; after the first convolution C1, it becomes 6 channels and 28*28 size A feature vector of; through one downsampling S2, it becomes a feature vector of 6 channels and 14*14 size, and its width and height are equivalent to a loss of 10%; after a second convolution C3, it becomes a 16-channel, 14*14 feature vector. A feature vector of size 10*10; through the second downsampling S4, it becomes a feature vector of 16 channels and size 5*5; the last three layers are fully connected and output.

        ​​ ​ ①Convolutious (convolution): involves the setting of input, output and many parameters, and needs to be initialized.

        ②Subsampling (downsampling): The network uses the maximum pooling downsampling method. The sum of the maximum pooling downsampling is Dimension 2*2 size.

       Max Pooling:Max Pooling, taking the maximum value in the window as the output.

        ③Full Connection: Initialization is required.

2. Guide package

import torch  # torch基础库
import torch.nn as nn  # torch神经网络库
import torch.nn.functional as F

3. Define convolutional neural network

3.1 Code display

class Net(nn.Module):
    # 初始化
    def __init__(self):
        super(Net,self).__init__()
        self.conv1 = nn.Conv2d(3,6,5)
        self.conv2 = nn.Conv2d(6,16,5)
        self.fc1 = nn.Linear(16*5*5,120)
        self.fc2 = nn.Linear(120,84)
        self.fc3 = nn.Linear(84,10)

    # 前向传播
    def forward(self,x):
        x = self.conv1(x)
        x = F.relu(x)
        x = F.max_pool2d(x,(2,2))
        x = F.max_pool2d(F.relu(self.conv2(x)),2)
        x = x.view(-1,x.size()[1:].numel())
        x = F.relu(self.fc1(x))  # 进入全连接层需要进行激活函数
        x = F.relu(self.fc2(x))
        x = self.fc3(x)  # 最后一层为输出层,要输出结果,不需要进行激活
        return x

3.2 Define the purpose of the network

        It is hoped that the network will have scientific parameters. Through the training of input data, the relevant parameters will be continuously updated and the gradient will drop to an appropriate value. After that, new pictures will be input for classification or prediction.

3.3 Pytorch builds network

        Pytorch usually uses classes for management when building a network, which can be named Net (the name can be changed). It is usually necessary to inherit the nn.Model class (Equivalent to using the methods defined by Model directly in Net). Building a network usually includes two functions:

        ①Initialization function (containing default parameters): A part that will be automatically executed when instantiating this class. This contains the content that needs to be initialized by the network.

 def __init__(self)

         A. super(Net,self).__init__(): In this function, multiple inheritance operations are usually required, which is equivalent to inheriting the classes in the Model class and all The methods of the class are inherited and used by Net;

        B. nn.Conv2d(3,6,5):2d convolution kernel function, involving only three parameters, the remaining parameters use default values; the first parameter is the number of input channels, the second parameter is the number of channels of the output feature vector, and the third parameter is the convolution kernel size (use the output formula to calculate W-F+1=28, W=32, F=5);

        Output = \frac{W-F+2P}{S} + 1: Where W refers to the width and height, F refers to the size of the requested ColorSize, P refers to Padding - like padding outside the picture, let it traverse, the default is 0; S refers to the step size, the convolution kernel traverses the picture Step size, default is 1;

        C. nn.Linear(16*5*5,120): The initialization of the fully connected layer involves two parameters (the dimensionality of the input feature and the dimensionality of the output feature) size), the fully connected layer needs to flatten the features, flatten each feature, and pull the previous feature vector into a straight line, which is sent to the fully connected layer;

        ②Forward propagation function: It is necessary to implement forward regression logic, which is equivalent to completing the logic of the entire network operation. x refers to the input, which is equivalent to the input in the above figure.

def forward(self,x)

        A. F.relu(x):relu activation function, after activation, the network has non-linear separation ability;

        B. tensor[batch,channel,H,W]: channel refers to the number of channels, such as the concepts of RBG three channels, H refers to height, and W refers to width. Batch refers to how many batches of such data;

        C. F.max_pool2d(x,(2,2)):Maximum pooling downsampling processes x;

        D. x.view(-1,x.size()[1:].numel()): is flattened and then given to the fully connected layer. The current input data ;The row information is automatically generated based on the batch information. -1 allows the program to automatically generate this row; why is it necessary to cut 1? For tensor information, the batch is cut off, and the multiplication of channel, H, and W is equal to 16*5*5; < /span>

       Note: Pytorch handles tensors (tensors are used by neural networks Main data structure) data.

4. Test network effects

        It is equivalent to printing the network initialization part, and can also be checked corresponding to the network structure.

net = Net()
print(net)

        Reference:Pytorch line-by-line code introduction_bilibili_bilibili

Guess you like

Origin blog.csdn.net/m0_53096519/article/details/134033250