Multiple LR Prediction
title: Multiple Linear Regression author: Juma Shafara date: "2024-08-08" date-modified: "2024-08-21" keywords: [ Multiple Linear Regression,
How to make the prediction for multiple inputs, How to use linear class to build more complex models, How to build a custom module, ] description: In this lab, you will review how to make a prediction in several different ways by using PyTorch.

Objective
- How to make the prediction for multiple inputs.
- How to use linear class to build more complex models.
- How to build a custom module.
Table of Contents
In this lab, you will review how to make a prediction in several different ways by using PyTorch.
Estimated Time Needed: 15 min
Preparation
Import the libraries and set the random seed.
Prediction
Set weight and bias.
Define the parameters. torch.mm uses matrix multiplication instead of scaler multiplication.
The function forward implements the following equation:

If we input a 1x2 tensor, because we have a 2x1 tensor as w, we will get a 1x1 tensor:

Each row of the following tensor represents a sample:
Class Linear
We can use the linear class to make a prediction. You'll also use the linear class to build more complex models.
Let us create a model.
Make a prediction with the first sample:
Predict with multiple samples X:
The function performs matrix multiplication as shown in this image:

Build Custom Modules
Now, you'll build a custom module. You can make more complex models by using this method later.
Build a linear regression object. The input feature size is two.
This will input the following equation:

You can see the randomly initialized parameters by using the parameters() method:
You can also see the parameters by using the state_dict() method:
Now we input a 1x2 tensor, and we will get a 1x1 tensor.
The shape of the output is shown in the following image:

Make a prediction for multiple samples:
The shape is shown in the following image:

Practice
Build a model or object of type linear_regression. Using the linear_regression object will predict the following tensor:
Double-click here for the solution.
About the Author:
Hi, My name is Juma Shafara. Am a Data Scientist and Instructor at DATAIDEA. I have taught hundreds of peope Programming, Data Analysis and Machine Learning.
I also enjoy developing innovative algorithms and models that can drive insights and value.
I regularly share some content that I find useful throughout my learning/teaching journey to simplify concepts in Machine Learning, Mathematics, Programming, and related topics on my website jumashafara.dataidea.org.
Besides these technical stuff, I enjoy watching soccer, movies and reading mystery books.