In [ ]:
from torch import nn
import torch
Set the random seed:
In [ ]:
torch.manual_seed(1)
In [ ]:
w=torch.tensor([[ 2.0],[3.0]],requires_grad=True)
b=torch.tensor([ [1.0]],requires_grad=True)
Define the parameters. torch.mm
uses matrix multiplication instead of scaler multiplication.
In [ ]:
def forward(x):
yhat=torch.mm(x,w)+b
return yhat
The function forward
implements the following equation:
Input a 1x2 tensor and get a 1x1 tensor:
In [ ]:
x=torch.tensor([[1.0,2.0]])
yhat=forward(x)
yhat
Each column of the following tensor represents a sample:
In [ ]:
X=torch.tensor([[1.0,1.0],[1.0,2.0],[1.0,3.0]])
When you input a tensor with three rows and two columns, the function performs matrix multiplication as shown in this image:
In [ ]:
yhat=forward(X)
yhat
You can use the linear class to make a prediction. You'll also use the linear class to build more complex models. Import the module:
In [ ]:
model=nn.Linear(2,1)
Make a prediction with one sample:
In [ ]:
yhat=model(x)
yhat
Predict with multiple samples:
In [ ]:
yhat=model(X)
yhat
Now, you'll build a custom module. You can make more complex models by using this method later.
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
Build a linear regression object. The input feature size is two.
In [ ]:
model=linear_regression(2,1)
This will input the following equation:
You can see the randomly initialized parameters by using the parameters()
method:
In [ ]:
list(model.parameters())
You can also see the parameters by using the state_dict()
method:
In [ ]:
model.state_dict()
Input a 1x2 tensor and get a 1x1 tensor:
In [ ]:
yhat=model(x)
yhat
The shape of the output is shown in the following image:
Make a prediction for multiple samples:
In [ ]:
yhat=model(X)
yhat
The shape is shown in the following image:
Build a model or object of type linear_regression
. Using the linear_regression
object will predict the following tensor:
In [ ]:
X=torch.tensor([[11.0,12.0,13,14],[11,12,13,14]])
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.