[Pytorch] Learning Record (5) Logistic(Sigmoid) Regression

This chapter will start with the classification problem in machine learning . Although the name is regression, it is for classification. The most basic way of thinking about classification problems: For example, to recognize handwritten numbers, the model will predict ten categories of input pictures and give 10 probabilities. Select the one with the highest probability as the prediction result, which is multi-classification.

Here we use a classic data set - minist, which can be downloaded from pytorch, the code is as follows:

import torchvision
train_set = torchvision.datasets.MNIST(root=’../dataset/mnist',train=FTrue,download=True)
test_set  = torchvision.datasets.MNIST(root='../dataset/mnist',train=False,download=True)

The CIFAR-10 data set is also provided in pytorch, which is a bunch of 32×32 small pictures, including 50,000 training sets and 10,000 test sets, with 10 categories.

 Since the prediction y=wx+b, y∈R, but the output probability needs to be [0, 1], so we need to map the prediction result to [0, 1] . Here we will use the logistics function \frac{1}{1+e^{-x}}, the function image is shown in Figure 1, located in [0, 1].

Figure 1 logistics function

 Use this function to map y_hat to the required interval, and logistics is also called sigmoid, and logistics is called sigmoid in the pytorch library. In deep learning papers, if you see σ(), you are activating with the sigmoid function. The only difference between it and linear regression is the addition of a σ bias . The difference in code is shown in Figure 2:

Figure 2 code difference

The formula needed for the loss function in the binary classification problem is:

loss=-(ylog\hat{y}+(1-y)log(1-\hat{y}))

This function is called BCE Loss, and it is used in the code as:

criterion = torch.nn.BCELoss(size_average = False)

There are only two changes in the entire code, and such a framework structure can write a large number of models.

The next section will deal with multidimensional feature input.

Guess you like

Origin blog.csdn.net/m0_55080712/article/details/122893586