Dry goods|10 minutes to get started with PyTorch(2)~with source code

Getting started with PyTorch in 10 minutes (2)

The previous section introduced simple linear regression, a 10-minute quick start to PyTorch(1), how to use least squares to fit some discrete points in pytorch. In this section, we will start simple logistic regression and introduce image classification problems. The data used is the handwritten font data set MNIST.

1

logistic regression

Logistic regression is simply the same as linear regression, and the operation to be done is also y = w * x + b.
Logistic regression is simply to do a binary classification problem. Use the sigmoid function to turn all positive and negative numbers into numbers between 0-1, so that this number can be used to determine which category it belongs to. You can simply think of the probability Greater than 0.5 is the second category, and less than 0.5 is the first category.
Dry goods|10 minutes to get started with PyTorch(2)~with source code

This is the graph of sigmoid
Dry goods|10 minutes to get started with PyTorch(2)~with source code

And we need to do here is multi-classification problem, for each data, the dimension of our total output is classified, such as 10 classification, our output is a 10-dimensional vector, and then we use a different activation function, softmax
Dry goods|10 minutes to get started with PyTorch(2)~with source code
this It is the mechanism of the softmax function. In fact, the simple understanding is to determine the probability of each of these 10 numbers. Because these 10 numbers are positive and negative, they are all positive numbers through the exponential function, and then find Then, each of the 10 numbers is divided by this sum, so that the probability of each category is obtained.

data

First import a library dedicated to graphics processing in torch, torchvision. According to the official installation guide, torchvision will also be installed when you install pytorch.
What we need to use is torchvision.transforms and torchvision.datasets and torch.utils.data.DataLoader

First of all, DataLoader is the operation of importing images. There are some parameters in it, such as batch_size and shuffle. The default image type loaded in is the type of PIL.Image.open. If you don’t know PIL, it is simply a way to read images. Library

The operation in torchvision.transforms is to process the imported pictures. For example, you can randomly select the window frame size (50, 50), or flip it randomly, or go to the middle (50, 50) window frame size part, etc., But what must be used is transforms.ToTensor(), which can convert the picture type of PIL into tensor, so that pytorch can process it

There are many data types in torchvision.datasets. There are data processed by the official website. For example, the MNIST data set we want to use can be obtained through torchvision.datasets.MNIST(), and one that is often used is torchvision.datasets.ImageFolder (), this allows us to fetch pictures by folder, similar to flow_from_directory() in keras, you can go to the official document for details.
Dry goods|10 minutes to get started with PyTorch(2)~with source code

The above is our reading operation on the image data

model

I talked about the framework of model definition before, not much nonsense,
Dry goods|10 minutes to get started with PyTorch(2)~with source code
we need to pass in parameters to the model directly on the code , the first parameter is defined as the dimension of the data, and the second dimension is the number of our classification.

Then we can run the model on the GPU, how to do it?
First of all, you can judge whether you can run on the gpu

Dry goods|10 minutes to get started with PyTorch(2)~with source code
If it returns True, it means that there is gpu support.
Then you only need a simple command.

Dry goods|10 minutes to get started with PyTorch(2)~with source code

or

Dry goods|10 minutes to get started with PyTorch(2)~with source code

Both can
then need to define loss and optimizer

Dry goods|10 minutes to get started with PyTorch(2)~with source code

The loss we use here is cross entropy, which is a loss for processing classification problems. For optimizer, we still use stochastic gradient descent.

train

Then you can start training

Dry goods|10 minutes to get started with PyTorch(2)~with source code
Dry goods|10 minutes to get started with PyTorch(2)~with source code

Note that if we put the model on the gpu, correspondingly our Variable must also be put on the gpu, which is also very simple

Dry goods|10 minutes to get started with PyTorch(2)~with source code

Then you can test the model, the process is similar to training, but pay attention to change the model to test mode

Dry goods|10 minutes to get started with PyTorch(2)~with source code

This is the result of 100 epoch

Dry goods|10 minutes to get started with PyTorch(2)~with source code

How often will the specific results be printed? How to print can be designed in the for loop.

In this part, we explain how to use logistic regression to do a simple image classification problem, and know how to run the model on GPU. In the next section, we will introduce how to write a simple convolutional neural network. Students who don’t understand convolutional networks You can go to my column to see the previous introduction of convolutional networks.

The code of this article has been uploaded to github.
Welcome to check my Zhihu column. For deep alchemy,
welcome to visit my blog

Recommended reading articles:

10 minutes to get started quickly with PyTorch (1)
10 minutes to get started with pytorch (0)
hidden Markov model-forward algorithm

全是通俗易懂的硬货!只需置顶~欢迎关注交流~

Dry goods|10 minutes to get started with PyTorch(2)~with source code

Guess you like

Origin blog.51cto.com/15009309/2553589