Skip to content

author: Juma Shafara date: "2024-08-09" title: Clear Linear keywords: [Training Two Parameter, Mini-Batch Gradient Decent, Training Two Parameter Mini-Batch Gradient Decent] description: In this lab, we will review how to make a prediction for Linear Regression with Multiple Output.


Photo by DATAIDEA

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.


Class Linear

from torch import nn
import torch

Set the random seed:

torch.manual_seed(1)
<torch._C.Generator at 0x7fee040d21b0>

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

model=linear_regression(1,10)
model(torch.tensor([1.0]))
tensor([ 0.7926, -0.3920,  0.1714,  0.0797, -1.0143,  0.5097, -0.0608,  0.5047,
         1.0132,  0.1887], grad_fn=<AddBackward0>)

we can use the diagram to represent the model or object

No description has been provided for this image

we can see the parameters

list(model.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

x=torch.tensor([[1.0]])

we can make a prediction

yhat=model(x)
yhat
tensor([[ 0.7926, -0.3920,  0.1714,  0.0797, -1.0143,  0.5097, -0.0608,  0.5047,
          1.0132,  0.1887]], grad_fn=<AddmmBackward>)

each row in the following tensor represents a different sample

X=torch.tensor([[1.0],[1.0],[3.0]])

we can make a prediction using multiple samples

Yhat=model(X)
Yhat
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=<AddmmBackward>)

the following figure represents the operation, where the red and blue represents the different parameters, and the different shades of green represent different samples.

No description has been provided for this image

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.

What's on your mind? Put it in the comments!