PyTorch is a Python library that facilitates the creation of deep learning models. PyTorch is very flexible and easy to use for deep learning tasks. The accessibility and friendliness of early adopters in the years that followed its first publication, it became one of the most important in-depth learning tools in a wide range of applications. As the title suggests, In this article, I will take you through Linear Regression with PyTorch. I will simply use the PyTorch package to build a Linear Regression Model using python.
What is a Linear Regression?
Before working on the task of Linear Regression with PyTorch you must know what is a Linear Regression if you are working on it for the very first time. Linear Regression is one of the easiest and one of the most popular Supervised Machine learning algorithms. It is a common statistical tool for modeling the relationship between some “explanatory” variables and some real-valued outcome. It predicts by simply computing a weighted sum of the input features, plus a constant called the bias term.
Before trying Linear Regression with PyTorch, of you want to try it with Scikit-learn, which is mostly used for linear regression, then you can go through this article.
Also, Read – Telegram Bot with Python.
Linear Regression with PyTorch
I hope you now know what is a linear regression algorithm and how to work on it by using a very standard package like Scikit-Learn. Now let’s see how we can use linear regression with PyTorch.
Consider a very basic linear equation, that is, y = 2x + 1. Here, x is an independent variable and y is a dependent variable. I will use this equation to create a dataset that will be used to form a linear regression model:
After we create the dataset, we can start with the code for Linear Regression. I’ll start by defining the architecture of our linear regression model:
We have defined a class for linear regression, which is the basic neural network module containing all the required functions. Our linear regression model contains only one simple linear function. Now I will proceed to instantiate the model:
Training Linear Regression with PyTorch
After completing all the initializations, we can now start training linear regression. Let’s see how we can do it:
Testing The Model
Now that our linear regression model is trained, I will test it. Since this is a very simple model, I will test it on the existing dataset and I will also plot the results to see the original outputs versus the expected outputs:
[[ 0.3630475] [ 2.4547746] [ 4.5465016] [ 6.6382284] [ 8.729956 ] [10.821683 ] [12.913409 ] [15.0051365] [17.096863 ] [19.18859 ] [21.280317 ]]

Also, Read – Scraping Twitter with Python.
It seems that our model correctly understood the linear relationship between our dependent and independent variables. I hope you liked this article on Linear Regression with PyTorch. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning.