“This is the ninth day of my participation in the First Challenge 2022. For details: First Challenge 2022”

preface

Now, we have a general idea of neural networks, and we introduced CNN neural networks, and we said that this convolutional neural network should be explained in two parts, one is convolution and one is a neural network. Convolution is a special way of data processing, and then after we deal with the data the results into the network, after we get the output, then the network is also a way of handling of data, a more abstract way of data processing, a us human is very difficult to directly obtain we get the actual way of expression, such as you go to the fitting of data, It’s basically hard to get the function that you fit, if you have a very complicated neural network, because we can have N linear layers, N nonlinear layers, N hidden nodes.

convolution

Now we come to the first part, which is convolution, and we have a convolution kernel, which is responsible for processing the data, and this convolution kernel is actually the parameter that needs to be optimized, and we can think of all of these convolution operations as parameters that need to be optimized. So let’s talk about the apis that we have here.

Conv2d convolution function

This is our convolution kernel.

There are a few parameters to note here. Check out our official website first.

These are the parameters

These parameters are very important when we build the CNN neural network. Let’s focus on a few parameters

padding

This parameter, we said that in front of the convolution after our image will become smaller, so sometimes, we can fill the original image, make it bigger, so the image size of the output after the convolution don’t send certain change, that is to say our padding to fill in the original image, padding_mode refers to, what are you going to fill in, For example, fill 0

Calculates the size of the output

A formula is provided to calculate the size of the output image.

We can calculate it. I’ll give you an example when we’re building the network.

MaxPool2d pooling

this

That’s the thing

Flatten a draw

It’s just amortizing our data.

The network structures,

Let’s go straight to the code now

import torchvision
from torch import nn
import torch
from torch.utils.data import DataLoader
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential, CrossEntropyLoss
from torchvision import transforms

trans = transforms.Compose([transforms.ToTensor()])
dataset = torchvision.datasets.CIFAR10(root="./dataset",train=False,transform=trans,download=True)

dataloader = DataLoader(dataset,batch_size=64)
class MyModule(nn.Module) :

    def __init__(self) :
        super().__init__()

        self.model = Sequential(
            Conv2d(3.32, kernel_size=(5.5), padding=2),
            MaxPool2d(2),
            Conv2d(32.32, (5.5), padding=2),
            MaxPool2d(2),
            Conv2d(32.64, (5.5), padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024.64),
            Linear(64.10))def forward(self,x) :
        x = self.model(x)

        return x


if __name__ == '__main__':
    mymodule = MyModule()
    loss = torch.nn.CrossEntropyLoss()
    optim = torch.optim.SGD(mymodule.parameters(),lr=0.01)

    for data in dataloader:
        imgs,targets = data
        outputs = mymodule(imgs)
        result_loss = loss(outputs,targets)
        optim.zero_grad()
        result_loss.backward()
        optim.step()
Copy the code

This is the CIFAR10 model code that we are going to build. Because the data is too large, SO I also took the training set first.

A network model

This is a network model of it, and we just need to build it according to this picture.

Compute the convolution kernel size

Now let’s pay attention

So we’re going to use the previous formula to calculate the parameters

Enter (32 + 2PADDing-1 (5-1) -1) /1) +1= 32-4 + 2padding

So the padding is equal to 2 and the rest of the parameters we’ll just use the default values, you don’t have to use them as long as you can guarantee your output.

So we end up with a model sheet like this.

class MyModule(nn.Module) :

    def __init__(self) :
        super().__init__()

        self.model = Sequential(
            Conv2d(3.32, kernel_size=(5.5), padding=2),
            MaxPool2d(2),
            Conv2d(32.32, (5.5), padding=2),
            MaxPool2d(2),
            Conv2d(32.64, (5.5), padding=2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024.64),
            Linear(64.10))def forward(self,x) :
        x = self.model(x)

        return x
Copy the code