In this lab, we will review how to make a prediction in several different ways by using PyTorch.
Estimated Time Needed: 15 min
The following are the libraries we are going to use for this lab.
In [ ]:
# These are the libraries will be used for this lab.
import torch
Let us create the following expressions:
First, define the parameters:
In [ ]:
# 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)
Then, define the function forward(x, w, b)
makes the prediction:
In [ ]:
# Function forward(x) for prediction
def forward(x):
yhat = w * x + b
return yhat
Let's make the following prediction at x = 1
In [ ]:
# Predict y = 2x - 1 at x = 1
x = torch.tensor([[1.0]])
yhat = forward(x)
print("The prediction: ", yhat)
Now, let us try to make the prediction for multiple inputs:
Let us construct the x
tensor first. Check the shape of x
.
In [ ]:
# Create x Tensor and check the shape of x tensor
x = torch.tensor([[1.0], [2.0]])
print("The shape of x: ", x.shape)
Now make the prediction:
In [ ]:
# Make the prediction of y = 2x - 1 at x = [1, 2]
yhat = forward(x)
print("The prediction: ", yhat)
The result is the same as what it is in the image above.
Make a prediction of the following x
tensor using the w
and b
from above.
In [ ]:
# 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]])
Double-click here for the solution.
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:
In [ ]:
# Import Class Linear
from torch.nn import Linear
Set the random seed because the parameters are randomly initialized:
In [ ]:
# Set random seed
torch.manual_seed(1)
Let us create the linear object by using the constructor. The parameters are randomly created. Let us print out to see what w and b are.
In [ ]:
# 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()))
This is equivalent to the following expression:
Now let us make a single prediction at x = [[1.0]].
In [ ]:
# Make the prediction at x = [[1.0]]
x = torch.tensor([[1.0]])
yhat = lr(x)
print("The prediction: ", yhat)
Similarly, you can make multiple predictions:
Use model lr(x)
to predict the result.
In [ ]:
# Create the prediction using linear model
x = torch.tensor([[1.0], [2.0]])
yhat = lr(x)
print("The prediction: ", yhat)
Make a prediction of the following x
tensor using the linear regression model lr
.
In [ ]:
# Practice: Use the linear regression model object lr to make the prediction.
x = torch.tensor([[1.0],[2.0],[3.0]])
Double-click here for the solution.
Now, let's build a custom module. We can make more complex models by using this method later on.
First, import the following library.
In [ ]:
# Library for this section
from torch import nn
Now, let us define the class:
In [ ]:
# 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 an object by using the constructor. Print out the parameters we get and the model.
In [ ]:
# Create the linear regression model. Print out the parameters.
lr = LR(1, 1)
print("The parameters: ", list(lr.parameters()))
print("Linear model: ", lr.linear)
Let us try to make a prediction of a single input sample.
In [ ]:
# Try our customize linear regression model with single input
x = torch.tensor([[1.0]])
yhat = lr(x)
print("The prediction: ", yhat)
Now, let us try another example with multiple samples.
In [ ]:
# Try our customize linear regression model with multiple input
x = torch.tensor([[1.0], [2.0]])
yhat = lr(x)
print("The prediction: ", yhat)
Create an object lr1
from the class we created before and make a prediction by using the following tensor:
In [ ]:
# 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]])
Double-click here for the solution.
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, Mavis Zhou
Copyright © 2018 cognitiveclass.ai. This notebook and its source code are released under the terms of the MIT License.