Last Updated on December 19, 2022

While in the previous few tutorials we worked with single output multilinear regression, here we’ll explore how we can use multilinear regression for multi-target predictions. Complex neural network architectures are essentially having each neuron unit to perform linear regression independently then pass on their result to another neuron. Therefore, knowing how such regression works is useful to understand how a neural network performs multi-target predictions.

The goal of this article is to provide a step-by-step guide for the implementation of multi-target predictions in PyTorch. We will do so by using the framework of a linear regression model that takes multiple features as input and produces multiple results.

We will start by importing the necessary packages for our model. We will then define our input data points and what we want to achieve with our model. Particularly, we’ll demonstrate:

- How to understand multilinear regression in multiple dimensions.
- How to make multi-target predictions with multilinear regression in PyTorch.
- How to build class linear using the ‘nn.Module’ in PyTorch.
- How to make multi-target predictions with a single input data sample.
- How to male multi-target predictions with multiple input data samples.

Note that we’ll not train our MLR model in this tutorial, we’ll only see how it makes simple predictions. In the subsequent tutorial of our PyTorch series, we’ll learn how this model can be trained on a dataset.

Let’s get started.

## Overview

This tutorial is in three parts; they are

- Create the Module
- Making Predictions with Sinple Input Samples
- Making Predictions with Multiple Input Samples

## Create the Module

We’ll build a custom linear class for our multilinear Regression model. We’ll define a linear class and make it a child class of the PyTorch package `nn.Module`

. This class inherits all the methods and attributes from the package, such as `nn.Linear`

.

1 2 3 4 5 6 7 8 9 10 11 |
import torch torch.manual_seed(42) # define the class for multilinear regression class MLR(torch.nn.Module): def __init__(self, input_dim, output_dim): super().__init__() self.linear = torch.nn.Linear(input_dim, output_dim) def forward(self,x): y_pred = self.linear(x) return y_pred |

Now, let’s create the model object and define the parameters accordingly. As we plan on making multi-target predictions, let’s first check how our model works for a single input sample. Later, we’ll make predictions for multiple input samples.

## Making Predictions with Single Input Samples

We’ll create our model object that takes a single input sample and makes five predictions.

1 2 3 |
... # building the model object model = MLR(1, 5) |

Now, lets define our input tensor `x`

for the model and make predictions.

1 2 3 4 5 |
... # define the single input sample 'x' and make predictions x = torch.tensor([[2.0]]) y_pred = model(x) print(y_pred) |

Here’s what the output looks like.

1 2 |
tensor([[ 1.7309, 1.1732, 0.1187, 2.7188, -1.1718]], grad_fn=<AddmmBackward0>) |

As you can see, our model made multiple predictions out of only a single input sample. Here is how we can list the model parameters.

1 2 |
... print(list(model.parameters())) |

and the output is like the following:

1 2 3 4 5 6 7 8 |
[Parameter containing: tensor([[ 0.7645], [ 0.8300], [-0.2343], [ 0.9186], [-0.2191]], requires_grad=True), Parameter containing: tensor([ 0.2018, -0.4869, 0.5873, 0.8815, -0.7336], requires_grad=True)] |

You may get a different result in numbers as those are randomized weights, but the shape of the weight tensors would match our design of taking one input and giving five output.

## Making Predictions with Multiple Input Samples

Similarly, let’s define a tensor `X`

for multiple input samples, where each row represents a data sample.

1 2 |
# define the multiple input tensor 'x' and make predictions X = torch.tensor([[2.0],[4.0],[6.0]]) |

We can make multi-target predictions with multiple input samples.

1 2 3 |
... Y_pred = model(X) print(Y_pred) |

As we have three samples of input, we should see three samples of output, like the following:

1 2 3 4 |
tensor([[ 1.7309, 1.1732, 0.1187, 2.7188, -1.1718], [ 3.2599, 2.8332, -0.3498, 4.5560, -1.6100], [ 4.7890, 4.4932, -0.8184, 6.3932, -2.0482]], grad_fn=<AddmmBackward0>) |

Putting everything together, the following is the complete code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import torch torch.manual_seed(42) # define the class for multilinear regression class MLR(torch.nn.Module): def __init__(self, input_dim, output_dim): super().__init__() self.linear = torch.nn.Linear(input_dim, output_dim) def forward(self,x): y_pred = self.linear(x) return y_pred # building the model object model = MLR(1, 5) # define the single input sample 'x' and make predictions x = torch.tensor([[2.0]]) y_pred = model(x) print(y_pred) print(list(model.parameters())) # define the multiple input tensor 'x' and make predictions X = torch.tensor([[2.0],[4.0],[6.0]]) Y_pred = model(X) print(Y_pred) |

## Summary

In this tutorial, you learned how you can make multi-target predictions with multilinear regression model. Particularly, you learned:

- How to understand multilinear regression in multiple dimensions.
- How to make multi-target predictions with multilinear regression in PyTorch.
- How to build class linear using the ‘nn.Module’ in PyTorch.
- How to make multi-target predictions with a single input data sample.
- How to male multi-target predictions with multiple input data samples.

## No comments yet.