Linear regression is a fundamental supervised learning technique used for predicting a continuous target variable based on one or more input features. In this tutorial, we'll learn how to implement linear regression using PyTorch deep learning framework. We'll cover the following topics:
- Introduction to linear regression
- Preparing data
- Building the linear regression model
- Training the model
- Evaluating the model
- Conclusion
- Source code listing
Let's get started.
Introduction to linear regression
Linear regression is a simple yet powerful statistical method used for modeling the relationship between two or more variables. It assumes a linear relationship between the input features and the target variable. The model aims to find the best-fitting line that minimizes the difference between the observed and predicted values.
Preparing data
We'll begin by loading the necessary libraries for this tutorial.
We'll use a synthetic regression dataset generated using the make_regression function from scikit-learn. The dataset contains 400 samples with 2 input features and a noise level of 20.
Then we split data into train and test parts. Here, we use 20 percent of data as test data.
The input data needs to be converted to PyTorch tensors. Below, we convert the training and test input data into tensors.
Building the linear regression model
We'll define a simple linear regression class using PyTorch's nn.Linear module. The model class consists of a single linear layer without any activation function. Inside the constructor, a linear transformation layer (nn.Linear) is created and assigned to the self.linear attribute of the object. The forward method defines the forward pass of the model. It takes an input tensor x as input and passes it through the linear layer (self.linear) to compute the output.
Next, we define model parameters and hyperparameters.
Model parameters:
- input_size: This represents the number of features in the input data.
- output_size: This specifies the dimensionality of the output, which is usually 1 for regression tasks since we are predicting a single target variable.
Hyperparameters:
- learning_rate: The learning rate determines the step size at which the model parameters are updated during training.
- num_epochs: The number of epochs refers to the number of times the entire dataset is passed forward and backward through the model during training.
Training the model
Next, we'll train the linear regression model on the training data. We'll define the loss function (mean squared error) and the optimizer (Stochastic Gradient Descent).
To train the model, the dataset is iterated for a fixed number of epochs. An epoch is one complete pass through the entire training dataset.
Inside the loop, we perform a forward pass through the model using the training data (X_train_ts). This generates predicted values (y_predicted) for the target variable. We then calculate the loss between the predicted values (y_predicted) and the actual target values (y_train_ts). The loss represents how far off the model's predictions are from the true values.
After computing the loss, we perform a backward pass through the model to compute gradients with respect to each parameter. This is done using the backward() method on the loss tensor. Then, we use the optimizer to update the model parameters based on these gradients, using the step() method.
We print the current value of the loss every 10 epochs to monitor the training progress and ensure that the loss is decreasing over time.
Evaluating the model
After training, we'll evaluate the model's performance on the test data. The torch.no_grad() is a context manager that ensures PyTorch operations do not track gradients inside this context manager. This is useful during model evaluation, where we do not need to compute gradients during inference. By disabling gradient tracking, we save memory and computational resources.
Then, we calculate the mean squared error and visualize the actual and predicted data on a graph.
The result is as follows.
Epoch [20/200], Loss: 6646.3257
Epoch [30/200], Loss: 4822.3564
Epoch [40/200], Loss: 3533.9141
Epoch [50/200], Loss: 2622.0962
Epoch [60/200], Loss: 1975.6768
Epoch [70/200], Loss: 1516.6350
Epoch [80/200], Loss: 1190.1326
Epoch [90/200], Loss: 957.5449
Epoch [100/200], Loss: 791.6177
Epoch [110/200], Loss: 673.0816
Epoch [120/200], Loss: 588.2905
Epoch [130/200], Loss: 527.5629
Epoch [140/200], Loss: 484.0189
Epoch [150/200], Loss: 452.7618
Epoch [160/200], Loss: 430.3016
Epoch [170/200], Loss: 414.1467
Epoch [180/200], Loss: 402.5165
Epoch [190/200], Loss: 394.1366
Epoch [200/200], Loss: 388.0937
MSE: 401.0717
Source code listing
No comments:
Post a Comment