Multiple LR Prediction
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.
# Import the libraries and set the random seed
from torch import nn
import torch
torch.manual_seed(1)
<torch._C.Generator at 0x7f44041ee610>
Prediction
Set weight and bias.
# Set the weight and bias
w = torch.tensor([[2.0], [3.0]], requires_grad=True)
b = torch.tensor([[1.0]], requires_grad=True)
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>)
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:
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:
Build Custom Modules
Now, you'll build a custom module. You can make more complex models by using this method later.
# Create LinearRegression Class
class LinearRegression(nn.Module):
# Constructor
def __init__(self, input_size, output_size):
super(LinearRegression, self).__init__()
self.linear = nn.Linear(input_size, output_size)
# Prediction function
def forward(self, x):
yhat = self.linear(x)
return yhat
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:
Practice
Build a model or object of type linear_regression
. Using the linear_regression
object will predict the following tensor:
# Practice: Build a model to predict the follow tensor.
X = torch.tensor([[11.0, 12.0, 13, 14], [11, 12, 13, 14]])
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.