Python and PyTorch in-depth implementation of linear regression model: an article to fully grasp the basic machine learning technology

1 Introduction

1.1 Overview of Linear Regression Model

file

Linear regression is a predictive analysis in statistics that is used to model the relationship between two or more variables. Linear regression uses a best-fit straight line (also known as a regression line) to establish an intuitive relationship between independent (input) variables and dependent variables (output). Simple linear regression is a linear relationship between input variables and output variables while multiple linear regression is a linear relationship between multiple input variables and output variables.

1.2 Introduction to Python and PyTorch

file

Python is a powerful programming language, especially suitable for processing and analyzing big data, and is widely used in various scientific computing. Python has many libraries that can easily implement various advanced functions, such as: NumPy, Pandas, Matplotlib, etc.

PyTorch is an open source Python machine learning library based on Torch. It was primarily developed by Facebook's AI research team to implement deep learning algorithms. PyTorch uses tensor as the basic data structure, which can be calculated on GPU or CPU. The feature of dynamically defining the calculation graph makes PyTorch more advantageous in writing and debugging models.

In the next sections, we will implement a linear regression model using Python and the PyTorch library.

2. Preparation of tools and libraries

Before starting to implement the linear regression model, we need to prepare related tools and libraries. We will use Python as the programming language and PyTorch as the main deep learning library.

2.1 Python environment configuration

First, we need to install Python. If Python is not installed on your computer, you can download it from Python's official website: https://www.python.org/downloads/
After the installation is complete, you can verify that Python is installed successfully by running the following command on the command line:

python --version

You should see the version number of Python. If Python has been successfully installed, we can start installing the necessary Python libraries. These libraries include: NumPy, Pandas, Matplotlib, and PyTorch.

2.2 Introduction to PyTorch Installation and Use

Next, we need to install the PyTorch library. The PyTorch installation process depends on your operating system and whether you have CUDA installed (if you plan to run PyTorch on a GPU, you will need CUDA). You can find a detailed installation guide on the official PyTorch website: https://pytorch.org/get-started/locally/

Run the following command on the command line, choose the appropriate command according to your environment:

# For CPU only
pip install torch==1.9.0+cpu torchvision==0.10.0+cpu torchaudio===0.9.0 -f https://download.pytorch.org/whl/torch_stable.html

# For CUDA 10.2
pip install torch==1.9.0+cu102 torchvision==0.10.0+cu102 torchaudio===0.9.0 -f https://download.pytorch.org/whl/torch_stable.html

Once the installation is complete, we can verify that PyTorch was successfully installed by running the following Python code:

import torch
print(torch.__version__)

3. Data preparation

3.1 Dataset overview

In this example, we'll use a fictional dataset that contains information on house sizes and prices. Our goal is to predict housing prices by area, which is a typical linear regression problem.

Suppose we have the following data:

Area (square meter) Price (10,000 yuan)
50 300
60 360
70 420

3.2 Data loading and preprocessing

Next, we need to load the data and preprocess it. This usually includes steps such as handling of missing values, data normalization, etc. In this example, we assume that all data are complete and no missing value handling is required. However, in order for the gradient descent algorithm to converge faster, we need to normalize the data.

import numpy as np

# 房屋面积
areas = np.array([50, 60, 70, ..., 120, 130, 140], dtype=float)

# 房价
prices = np.array([300, 360, 420, ..., 720, 780, 840], dtype=float)

# 数据规范化
areas = (areas - np.mean(areas)) / np.std(areas)
prices = (prices - np.mean(prices)) / np.std(prices)

The above code first defines the arrays of house area and price, and then normalizes the two arrays so that the values ​​of the two arrays fluctuate around 0 with a standard deviation of 1. The advantage of this treatment is that it can speed up the convergence of gradient descent.

4. Theoretical basis of linear regression

In this part, we will introduce the basic theoretical knowledge of linear regression, including the mathematical model of linear regression and the gradient descent method.

4.1 Linear regression model formula

The basic formula of the linear regression model is as follows:

y = wx + b

Among them, y is the target variable we want to predict, x is our feature variable, w and b are our model parameters, representing weight and bias respectively.

4.2 Loss Function and Gradient Descent

In order to train our model, we need a way to measure the difference between our model's prediction and actual value. This is the loss function (also called cost function). For linear regression models, we usually use mean squared error (MSE) as the loss function:

L = 1/N * ∑(y_pred - y_actual)^2

Among them, y_pred is the predicted value of the model, y_actual is the actual value, and N is the number of samples.

Our goal is to minimize the loss function by tuning the parameters w and b of the model. This process is called optimization. Gradient descent is a common optimization method. Its working principle is to calculate the gradient (derivative) of the loss function with respect to the parameters, and then adjust the parameters in the opposite direction of the gradient to descend on the loss function.

5. Using PyTorch to implement a linear regression model

With the previous theoretical foundation in place, we can now start using PyTorch to implement our linear regression model.

5.1 Define the model

First, we need to define our model. In PyTorch, we can torch.nn.Moduledefine our model by inheriting classes and implement forwardmethods to define forward propagation.

import torch
import torch.nn as nn

class LinearRegressionModel(nn.Module):
    def __init__(self):
        super(LinearRegressionModel, self).__init__()
        self.linear = nn.Linear(1, 1)  # 输入和输出的维度都是1

    def forward(self, x):
        out = self.linear(x)
        return out

5.2 Instantiate the model class

Then, we can create an instance of the model.

model = LinearRegressionModel()

5.3 Setting loss function and optimizer

Next, we define our loss function and optimizer. We use mean squared error as the loss function and stochastic gradient descent as the optimizer.

criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)

5.4 Training Model

Finally, we can start training our model.

# 转换为 PyTorch 张量
inputs = torch.from_numpy(areas)
targets = torch.from_numpy(prices)

# 转换为二维张量
inputs = inputs.view(-1,1)
targets = targets.view(-1,1)

# 进行 60 轮训练
for epoch in range(60):
    # 前向传播
    outputs = model(inputs)
    loss = criterion(outputs, targets)
    
    # 反向传播和优化
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    
    if (epoch+1) % 5 == 0:
        print ('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, 60, loss.item()))

The above code will complete the training process of the linear regression model, and the training results will be output on the console.

6. Model Evaluation and Prediction

After training, we need to evaluate the performance of the model and use the model to make predictions.

6.1 Model Evaluation

First, we can calculate the average loss of the model over all training data.

model.eval()  # 将模型设置为评估模式
with torch.no_grad():  # 不需要计算梯度
    predictions = model(inputs)
    loss = criterion(predictions, targets)
print('Final Loss:', loss.item())

Here, model.eval()the model is set to evaluation mode, so that operations such as dropout and batch normalization are not considered when calculating gradients. torch.no_grad()is to tell PyTorch that we don't need to compute gradients, because we don't need to do model optimization.

6.2 Model Prediction

Let's use the trained model to make predictions.

# 预测一个 100 平方米的房子的价格
area = torch.tensor([100.0])
area = (area - torch.mean(inputs)) / torch.std(inputs)  # 需要进行同样的数据规范化
price = model(area)
print('Predicted price:', price.item())

The above code uses the trained model to predict the price of a 100 square meter house. It should be noted that when we predict new data, we need to perform the same preprocessing operation on the new data as the training data.

So far, we have completed the entire content of the linear regression model, including the study of theoretical knowledge, model implementation and training using PyTorch, and model evaluation and prediction.

7. Summary

We have completed a complete linear regression model construction, training and prediction process. In this process, we learned the basic theoretical knowledge of the linear regression model, how to use PyTorch to implement the linear regression model, and how to evaluate and use the trained model.

7.1 Summary of key points

In this paper, we mainly did the following:

  1. The basic concepts and mathematical principles of the linear regression model are introduced.
  2. The training and prediction process of the linear regression model is implemented using Python and PyTorch.
  3. Shows how to evaluate the performance of the model.

Through this study, I hope you have a deeper understanding of the linear regression model and can use it flexibly in practical problems.

7.2 Outlook

Although the linear regression model is the most basic machine learning model, its ideas and methods are reflected in many complex models. For example, a neural network can be seen as an extension and deepening of a linear regression model. Therefore, understanding and mastering the linear regression model is very important for learning more complex machine learning models.

If it is helpful, please pay more attention to
the personal WeChat public account: [TechLead] Share the full-dimensional knowledge of AI and cloud service research and development, and talk about my unique insight into technology as a TechLead.
TeahLead KrisChang, 10+ years of experience in the Internet and artificial intelligence industry, 10+ years of experience in technology and business team management, Tongji Software Engineering Bachelor, Fudan Engineering Management Master, Alibaba Cloud Certified Cloud Service Senior Architect, AI Product Business with Hundreds of Millions of Revenue principal.

Guess you like

Origin blog.csdn.net/magicyangjay111/article/details/131981675