# Making Linear Predictions in PyTorch

Last Updated on November 28, 2022

Linear regression is a statistical technique for estimating the relationship between two variables. A simple example of linear regression is to predict the height of someone based on the square root of the person’s weight (that’s what BMI is based on). To do this, we need to find the slope and intercept of the line. The slope is how much one variable changes with the change in other variable by one unit. The intercept is where our line crosses with the $y$-axis.

Let’s use the simple linear equation $y=wx+b$ as an example. The output variable is $y$, while the input variable is $x$. The slope and $y$-intercept of the equation are represented by the letters $w$ and $b$, hence referring them as the equation’s parameters. Knowing these parameters allows you to forecast the outcome $y$ for any given value of $x$.

Now that you have learnt some basics of the simple linear regression, let’s try to implement this useful algorithm in the PyTorch framework. Here, we’ll focus on a few points described as follows:

• What is Linear Regression and how it can be implemented in PyTorch.
• How to import linear class in PyTorch and use it for making predictions.
• How we can build custom module for a linear regression problem, or for more complex models in the future.

So let’s get started.

Making Linear Predictions in PyTorch.
Picture by Daryan Shamkhali. Some rights reserved.

## Overview

This tutorial is in three parts; they are

• Preparing Tensors
• Using Linear Class from PyTorch
• Building a Custom Linear Class

## Preparing Tensors

Note that in this tutorial we’ll be covering one-dimensional linear regression having only two parameters. We’ll create this linear expression:

$$y=3x+1$$

We’ll define the parameters $w$ and $b$ as tensors in PyTorch. We set the requires_grad parameter to True, indicating that our model has to learn these parameters:

In PyTorch prediction step is called forward step. So, we’ll write a function that allows us to make predictions for $y$ at any given value of $x$.

Now that we have defined the function for linear regression, let’s make a prediction at $x=2$.

This prints

Let’s also evaluate the equation with multiple inputs of $x$.

This prints

As you can see, the function for linear equation successfully predicted outcome for multiple values of $x$.

In summary, this is the complete code

## Using Linear Class from PyTorch

In order to solve real-world problems, you’ll have to build more complex models and, for that, PyTorch brings along a lot of useful packages including the linear class that allows us to make predictions. Here is how we can import linear class module from PyTorch. We’ll also randomly initialize the parameters.

Note that previously we defined the values of $w$ and $b$ but in practice they are randomly initialized before we start the machine learning algorithm.

Let’s create a linear object model and use the parameters() method to access the parameters ($w$ and $b$) of the model. The Linear class is initialized with the following parameters:

• in_features: reflects the size of each input sample
• out_features: reflects the size of each output sample

This prints

Likewise, you can use state_dict() method to get the dictionary containing the parameters.

This prints

Now we can repeat what we did before. Let’s make a prediction using a single value of $x$.

This gives

which corresponds to $0.5153\times 2 – 0.4414 = 0.5891$. Similarly, we’ll make predictions for multiple values of $x$.

This prints

Put everything together, the complete code is as follows

## Building a Custom Linear Class

PyTorch offers the possibility to build custom linear class. For later tutorials, we’ll be using this method for building more complex models. Let’s start by importing the nn module from PyTorch in order to build a custom linear class.

Custom modules in PyTorch are classes derived from nn.Module. We’ll build a class for simple linear regression and name it as Linear_Regression. This should make it a child class of the nn.Module. Consequently, all the methods and attributes will be inherited into this class. In the object constructor, we’ll declare the input and output parameters. Also, we create a super constructor to call linear class from the nn.Module. Lastly, in order to generate prediction from the input samples, we’ll define a forward function in the class.

Now, let’s create a simple linear regression model. It will simply be an equation of line in this case. For sanity check, let’s also print out the model parameters.

This prints

As we did in the earlier sessions of the tutorial, we’ll evaluate our custom linear regression model and try to make predictions for single and multiple values of $x$ as input.

This prints

which corresponds to $-0.1939*2+0.4694=0.0816$. As you can see, our model has been able to predict the outcome and the result is a tensor object. Similarly, let’s try to get predictions for multiple values of $x$.

This prints

So, the model also works well for multiple values of $x$.

Putting everything together, the following is the complete code

## Summary

In this tutorial we discussed how we can build neural networks from scratch, starting off with a simple linear regression model. We have explored multiple ways of implementing simple linear regression in PyTorch. In particular, we learned:

• What is Linear Regression and how it can be implemented in PyTorch.
• How to import linear class in PyTorch and use it for making predictions.
• How we can build custom module for a linear regression problem, or for more complex models in the future.