Objective
- How to make a prediction using multiple samples.
Table of Contents
In this lab, we will review how to make a prediction for Linear Regression with Multiple Output.
- Build Custom Modules
Estimated Time Needed: 15 min
Class Linear
Set the random seed:
<torch._C.Generator at 0x7f4538043150>
Set the random seed:
class linear_regression(nn.Module):
def __init__(self,input_size,output_size):
super(linear_regression,self).__init__()
self.linear=nn.Linear(input_size,output_size)
def forward(self,x):
yhat=self.linear(x)
return yhat
create a linear regression object, as our input and output will be two we set the parameters accordingly
tensor([ 0.7926, -0.3920, 0.1714, 0.0797, -1.0143, 0.5097, -0.0608, 0.5047,
1.0132, 0.1887], grad_fn=<ViewBackward0>)
we can use the diagram to represent the model or object
we can see the parameters
[Parameter containing:
tensor([[ 0.5153],
[-0.4414],
[-0.1939],
[ 0.4694],
[-0.9414],
[ 0.5997],
[-0.2057],
[ 0.5087],
[ 0.1390],
[-0.1224]], requires_grad=True),
Parameter containing:
tensor([ 0.2774, 0.0493, 0.3652, -0.3897, -0.0729, -0.0900, 0.1449, -0.0040,
0.8742, 0.3112], requires_grad=True)]
we can create a tensor with two rows representing one sample of data
we can make a prediction
tensor([[ 0.7926, -0.3920, 0.1714, 0.0797, -1.0143, 0.5097, -0.0608, 0.5047,
1.0132, 0.1887]], grad_fn=<AddmmBackward0>)
each row in the following tensor represents a different sample
we can make a prediction using multiple samples
tensor([[ 0.7926, -0.3920, 0.1714, 0.0797, -1.0143, 0.5097, -0.0608, 0.5047,
1.0132, 0.1887],
[ 0.7926, -0.3920, 0.1714, 0.0797, -1.0143, 0.5097, -0.0608, 0.5047,
1.0132, 0.1887],
[ 1.8232, -1.2748, -0.2164, 1.0184, -2.8972, 1.7091, -0.4722, 1.5222,
1.2912, -0.0561]], grad_fn=<AddmmBackward0>)
the following figure represents the operation, where the red and blue represents the different parameters, and the different shades of green represent different samples.
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.