# Import the libraries and set the random seed
from torch import nn
import torch
1) torch.manual_seed(
<torch._C.Generator at 0x70efcb47eef0>
Juma Shafara
August 8, 2024
August 21, 2024
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
In this lab, you will review how to make a prediction in several different ways by using PyTorch.
Estimated Time Needed: 15 min
Before we continue, I have a humble request, to be among the first to hear about future updates of the course materials, simply enter your email below, follow us on (formally Twitter), or subscribe to our YouTube channel.
Import the libraries and set the random seed.
# Import the libraries and set the random seed
from torch import nn
import torch
torch.manual_seed(1)
<torch._C.Generator at 0x70efcb47eef0>
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:
The result: tensor([[9.]], grad_fn=<AddBackward0>)
Each row of the following tensor represents a sample:
The result: tensor([[ 6.],
[ 9.],
[12.]], grad_fn=<AddBackward0>)
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:
The result: tensor([[-0.3969]], grad_fn=<AddmmBackward0>)
Predict with multiple samples X
:
The result: tensor([[-0.0848],
[-0.3969],
[-0.7090]], grad_fn=<AddmmBackward0>)
The function performs matrix multiplication as shown in this image:
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:
The parameters: [Parameter containing:
tensor([[ 0.3319, -0.6657]], requires_grad=True), Parameter containing:
tensor([0.4241], requires_grad=True)]
You can also see the parameters by using the state_dict()
method:
The parameters: OrderedDict({'linear.weight': tensor([[ 0.3319, -0.6657]]), 'linear.bias': tensor([0.4241])})
Now we input a 1x2 tensor, and we will get a 1x1 tensor.
The result: tensor([[-0.5754]], grad_fn=<AddmmBackward0>)
The shape of the output is shown in the following image:
Make a prediction for multiple samples:
The result: tensor([[ 0.0903],
[-0.5754],
[-1.2411]], grad_fn=<AddmmBackward0>)
The shape is shown in the following image:
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.
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.