Linear Regression with Multiple Outputs

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

    
    
    In [ ]:
    from torch import nn
    import torch
    
    
    
    In [ ]:
    Set the random seed:
    
    
    
    In [ ]:
    torch.manual_seed(1)
    

    Set the random seed:

    
    
    In [ ]:
    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

    
    
    In [ ]:
    model=linear_regression(2,2)
    

    we can use the diagram to represent the model or object

    we can see the parameters

    
    
    In [ ]:
    list(model.parameters())
    

    we can create a tensor with two rows representing one sample of data

    
    
    In [ ]:
    x=torch.tensor([[1.0,3.0]])
    

    we can make a prediction

    
    
    In [ ]:
    yhat=model(x)
    yhat
    

    each row in the following tensor represents a different sample

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

    we can make a prediction using multiple samples

    
    
    In [ ]:
    Yhat=model(X)
    Yhat
    

    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 Authors:

    Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.

    Other contributors: Michelle Carey


    Copyright © 2018 cognitiveclass.ai. This notebook and its source code are released under the terms of the MIT License.

    
    
    In [ ]: