1D Regression
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, we will review how to make a prediction in several different ways by using PyTorch.
Estimated Time Needed: 15 min
Preparation
The following are the libraries we are going to use for this lab.Prediction
Let us create the following expressions: $b=-1,w=2$ $\hat{y}=-1+2x$ First, define the parameters:# Define w = 2 and b = -1 for y = wx + b
w = torch.tensor(2.0, requires_grad = True)
b = torch.tensor(-1.0, requires_grad = True)
forward(x, w, b)
makes the prediction: Let's make the following prediction at x = 1 $\hat{y}=-1+2x$ $\hat{y}=-1+2(1)$ tensor([2]) tensor([3.], grad_fn=# Predict y = 2x - 1 at x = 1
x = torch.tensor([[1.0]])
yhat = forward(x)
print("The prediction: ", yhat)

x
tensor first. Check the shape of x
. # Create x Tensor and check the shape of x tensor
x = torch.tensor([[1.0], [2.0]])
print("The shape of x: ", x.shape)
Practice
Make a prediction of the followingx
tensor using the w
and b
from above. # Practice: Make a prediction of y = 2x - 1 at x = [[1.0], [2.0], [3.0]]
x = torch.tensor([[1.0], [2.0], [3.0]])
yhat = forward(x)
print("The prediction: ", yhat)
Class Linear
The linear class can be used to make a prediction. We can also use the linear class to build more complex models. Let's import the module: Set the random seed because the parameters are randomly initialized:torch.nn.Module
model are contained in the model’s parameters accessed with lr.parameters()
: # Create Linear Regression Model, and print out the parameters
lr = Linear(in_features=1, out_features=1, bias=True)
print("Parameters w and b: ", list(lr.parameters()))
state_dict()
Returns a Python dictionary object corresponding to the layers of each parameter tensor. print("Python dictionary: ",lr.state_dict())
print("keys: ",lr.state_dict().keys())
print("values: ",lr.state_dict().values())
# Make the prediction at x = [[1.0]]
x = torch.tensor([[1.0]])
yhat = lr(x)
print("The prediction: ", yhat)

lr(x)
to predict the result. # Create the prediction using linear model
x = torch.tensor([[1.0], [2.0]])
yhat = lr(x)
print("The prediction: ", yhat)
Practice
Make a prediction of the followingx
tensor using the linear regression model lr
. # Practice: Use the linear regression model object lr to make the prediction.
x = torch.tensor([[1.0],[2.0],[3.0]])
yhat = lr(x)
print("The prediction: ", yhat)
Build Custom Modules
Now, let's build a custom module. We can make more complex models by using this method later on. First, import the following library. Now, let us define the class:# Customize Linear Regression Class
class LR(nn.Module):
# Constructor
def __init__(self, input_size, output_size):
# Inherit from parent
super(LR, self).__init__()
self.linear = nn.Linear(input_size, output_size)
# Prediction function
def forward(self, x):
out = self.linear(x)
return out
# Create the linear regression model. Print out the parameters.
lr = LR(1, 1)
print("The parameters: ", list(lr.parameters()))
print("Linear model: ", lr.linear)
# Try our customize linear regression model with single input
x = torch.tensor([[1.0]])
yhat = lr(x)
print("The prediction: ", yhat)
# Try our customize linear regression model with multiple input
x = torch.tensor([[1.0], [2.0]])
yhat = lr(x)
print("The prediction: ", yhat)
print("Python dictionary: ", lr.state_dict())
print("keys: ",lr.state_dict().keys())
print("values: ",lr.state_dict().values())
Practice
Create an objectlr1
from the class we created before and make a prediction by using the following tensor: # Practice: Use the LR class to create a model and make a prediction of the following tensor.
x = torch.tensor([[1.0], [2.0], [3.0]])
lr1 = LR(1, 1)
yhat = lr(x)
yhat
# print("The prediction: ", yhat)