• Abhishek Singh

# Chapter 2- How to perform Linear regression in PyTorch

Linear regression is one of the most basic algorithms in machine learning. In the last tutorial, we discuss the basics of operations in PyTorch.

In the machine learning series, we see about the concepts of linear regression and it's a basic implementation using Sklearn. In this tutorial, we are going to perform a linear regression in the PyTorch framework.

## What is Linear Regression

Linear regression is a statistical model that inspects the linear relationship between two or more variables which are dependent variable and independent variables.

For example, The price of milk is directly proportional to the quantity of milk that we have bought. Here in this situation, we can see a positive correlation between the price of milk and the quantity of milk.

Let us take a different situation in which the price of milk is inversely proportional to the production of milk. It means when the production of milk is more then the price of milk will be less, similarly vice-versa also true.

From the given figure we can conclude that linear regression can either be a positive correlated or negative correlated.

## When to use linear regression

Linear regression is used when we want to predict the dependency of one variable on another variable. The variable that we want to predict is called the dependent variable or outcome variable.

The mathematical equation of linear regression is Y = mX + b

where

• Y is an outcome variable,

• X is the input variable which we are using to make the predictions,

• m is the slope which determines the effect of x on y,

• and b is the bias which means how much our prediction is differing from the actual output.

## Assumptions of Linear Regression

There are four assumptions that are associated with Linear regression.

### Independence of Errors

If the distribution of errors is arbitrary and not affected or correlated to the errors in the prior observations.

How to check: We can plot a scatterplot in between x and y to check these assumptions.

### The Outlier Condition

An outlier is a data point that is different from the normal population. If you do have outliers in your dataset, it is a good analysis to run a regression test.

How to check: To detect the outliers you can use the box-plot method or you can use a mathematical approach called Elliptic envelope( In this method we assumes that the data is normally distributed. If the value is +1 then it is not an outlier and if the value is -1 then it is an outlier).

### Homoscedasticity

In this type of situation in which error terms are the same across all values of independent variables.

How to check: We can plot a scatterplot of the normalize residuals against the fitted values. If the homoscedasticity exists then the plot will be in a funnel shape. We can also use the Weisberg test to detect this phenomenon.

### Normality of Error Distribution

It means we have to assume that errors have a normal distribution. And when the confidence interval becomes unstable, it leads to trouble in estimating coefficients based on the minimization of least squares.

How to check: We can make a QQ plot or we can perform statistical tests of normality such as the Kolmogorov-Smirnov test, Shapiro-Wilk test.

## Implementation of Linear Regression in PyTorch

Let us start coding!

Step 1- Import all the required libraries.

• torch- This module is used to import all the torch dependencies.

• torch.nn- We use the nn module to create the neural network.

• torch.nn.functional- This module contains some useful functions like activation functions convolution operations you can use.

• torch.optim- In this package, we have many optimization algorithms like stochastic gradient descent, Adam optimizer, LBFGS, and many more.

• numpy-  It is used to perform mathematical computation like mean, variance, array creation, and many more.

• matplotlib- It is used for visualization purposes.

• sklearn.datasets- we use sklearn.datasets class to make our toy dataset for our regression model.

Step 2- Now we create a toy dataset for our regression model.

In the first two lines, we define no of features is equal to 1 and no of samples is equal to 100. Now we use the make_regression() function that will create a dataset with a linear relationship between features and the targets and then plot it using the matplotlib library.

Step 3- Now we convert the numpy array into tensors by using torch.from_numpy() function.

Step 4- Now we define linear regression class

Now we define __init__ constructor in which we instantiate two nn.Linear module. Next, in the forward function, we accept a variable of input data and we must return a variable of output data. Now we call our linear regression model.

In the next line, we construct our loss function and an optimizer. And then we call to model.parameters() in the SGD constructor will contain the learnable parameters of the two nn.

Step 5- Next we train our linear regression model.

Now we compute predicted y by passing X to the model and then we compute the loss. Zero gradients perform the backward pass and update the weights.

Step 6- Now we evaluate our model and plot the result.

## Wrap up the session

Finally, we made it to the end of the tutorial.

In this tutorial, we have learned about what is linear regression, examples, when to use linear regression, assumptions of linear regression, and implementation of linear regression in PyTorch.

If you have any problem regarding implementation feel free to comment.

You can also join our telegram channel to get free cheatsheets, projects, ebooks, study material related to machine learning, deep learning, data science, natural language processing, python programming, r programming, big data, and many more.

257 views

See All