Lecture 05 Linear Regression With PyTorch
Lecture 05 Linear Regression With PyTorch
Lecturer : Hongpu Liu Lecture 5-1 PyTorch Tutorial @ SLAM Research Group
Revision
𝑥 𝑦
𝜔 ∗ 𝑦ො − 𝑟 ^2 𝑙𝑜𝑠𝑠
Lecturer : Hongpu Liu Lecture 5-2 PyTorch Tutorial @ SLAM Research Group
Revision
w.grad.data.zero_()
Lecturer : Hongpu Liu Lecture 5-3 PyTorch Tutorial @ SLAM Research Group
PyTorch Fashion
Lecturer : Hongpu Liu Lecture 5-4 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 1. Prepare dataset
(1)
𝑦𝑝𝑟𝑒𝑑
𝑥 (1)
(2)
𝑦𝑝𝑟𝑒𝑑 = 𝜔 ∙ 𝑥 (2) + 𝑏
(3)
𝑦𝑝𝑟𝑒𝑑 𝑥 (3)
import torch
Lecturer : Hongpu Liu Lecture 5-5 PyTorch Tutorial @ SLAM Research Group
Revision: Gradient Descent Algorithm
Derivative Gradient
𝑁
𝜕𝑐𝑜𝑠𝑡(𝜔) 𝜕 1 𝜕𝑐𝑜𝑠𝑡
= 𝑥𝑛 ∙ 𝜔 − 𝑦𝑛 2
𝜕𝜔 𝜕𝜔 𝑁 𝜕𝜔
𝑛=1
𝑁
1 𝜕 Update
= 𝑥𝑛 ∙ 𝜔 − 𝑦𝑛 2
𝑁 𝜕𝜔 𝜕𝑐𝑜𝑠𝑡
𝑛=1 𝜔 =𝜔−𝛼
𝑁 𝜕𝜔
1 𝜕(𝑥𝑛 ∙ 𝜔 − 𝑦𝑛 )
= 2 ∙ (𝑥𝑛 ∙ 𝜔 − 𝑦𝑛 )
𝑁 𝜕𝜔 Update
𝑛=1
𝑁 𝑁
1 1
= 2 ∙ 𝑥𝑛 ∙ (𝑥𝑛 ∙ 𝜔 − 𝑦𝑛 ) 𝜔 = 𝜔 − 𝛼 2 ∙ 𝑥𝑛 ∙ (𝑥𝑛 ∙ 𝜔 − 𝑦𝑛 )
𝑁
𝑁 𝑛=1
𝑛=1
Lecturer : Hongpu Liu Lecture 5-6 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 2. Design Model
𝑦
Linear Unit
𝑥 ∗ + 𝑦ො 𝑙𝑜𝑠𝑠(𝑦,
ො 𝑦) 𝑙𝑜𝑠𝑠
𝜔 𝑏
Lecturer : Hongpu Liu Lecture 5-7 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 2. Design Model
model = LinearModel()
Lecturer : Hongpu Liu Lecture 5-8 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 2. Design Model
class LinearModel(torch.nn.Module):
def __init__(self):
super(LinearModel, self).__init__() Member methods __init__() and
self.linear = torch.nn.Linear(1, 1)
forward() have to be implemented.
def forward(self, x):
y_pred = self.linear(x)
return y_pred
model = LinearModel()
Lecturer : Hongpu Liu Lecture 5-9 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 2. Design Model
class LinearModel(torch.nn.Module):
def __init__(self):
super(LinearModel, self).__init__() Just do it. : )
self.linear = torch.nn.Linear(1, 1)
model = LinearModel()
Lecturer : Hongpu Liu Lecture 5-10 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 2. Design Model
𝜔 𝑏
Lecturer : Hongpu Liu Lecture 5-11 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 2. Design Model
Lecturer : Hongpu Liu Lecture 5-12 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 2. Design Model
(1)
𝑦𝑝𝑟𝑒𝑑
𝑥 (1)
(2)
Output 𝑦𝑝𝑟𝑒𝑑 = 𝜔 ∙ 𝑥 (2) + 𝑏
(3)
𝑦𝑝𝑟𝑒𝑑 𝑥 (3) Input
Lecturer : Hongpu Liu Lecture 5-13 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 2. Design Model
class LinearModel(torch.nn.Module):
def __init__(self): Class nn.Linear has implemented the
super(LinearModel, self).__init__()
self.linear = torch.nn.Linear(1, 1) magic method __call__(), which enable
def forward(self, x): the instance of the class can be called
y_pred = self.linear(x)
return y_pred just like a function. Normally the
model = LinearModel() forward() will be called.
Pythonic!!!
Lecturer : Hongpu Liu Lecture 5-14 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 2. Design Model
class LinearModel(torch.nn.Module):
def __init__(self):
super(LinearModel, self).__init__()
self.linear = torch.nn.Linear(1, 1)
Lecturer : Hongpu Liu Lecture 5-15 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 3. Construct Loss and Optimizer
criterion = torch.nn.MSELoss(size_average=False)
Lecturer : Hongpu Liu Lecture 5-16 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 3. Construct Loss and Optimizer
criterion = torch.nn.MSELoss(size_average=False)
Lecturer : Hongpu Liu Lecture 5-17 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 3. Construct Loss and Optimizer
criterion = torch.nn.MSELoss(size_average=False)
Lecturer : Hongpu Liu Lecture 5-18 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 4. Training Cycle
optimizer.zero_grad()
loss.backward()
optimizer.step()
Lecturer : Hongpu Liu Lecture 5-19 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 4. Training Cycle
optimizer.zero_grad()
loss.backward()
optimizer.step()
Lecturer : Hongpu Liu Lecture 5-20 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 4. Training Cycle
Lecturer : Hongpu Liu Lecture 5-21 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 4. Training Cycle
optimizer.zero_grad()
loss.backward() Backward: Autograd
optimizer.step()
Lecturer : Hongpu Liu Lecture 5-22 PyTorch Tutorial @ SLAM Research Group
Linear Regression – 4. Training Cycle
optimizer.zero_grad()
loss.backward()
optimizer.step() Update
Lecturer : Hongpu Liu Lecture 5-23 PyTorch Tutorial @ SLAM Research Group
Linear Regression – Test Model
# Test Model
x_test = torch.Tensor([[4.0]])
y_test = model(x_test)
print('y_pred = ', y_test.data)
Lecturer : Hongpu Liu Lecture 5-24 PyTorch Tutorial @ SLAM Research Group
Linear Regression
import torch
Prepare dataset
1
x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[2.0], [4.0], [6.0]])
x_test = torch.Tensor([[4.0]])
y_test = model(x_test) forward, backward, update
print('y_pred = ', y_test.data)
Lecturer : Hongpu Liu Lecture 5-25 PyTorch Tutorial @ SLAM Research Group
Exercise 5-1: Try Different Optimizer in Linear Regression
• torch.optim.Adagrad
• torch.optim.Adam
• torch.optim.Adamax
• torch.optim.ASGD
• torch.optim.LBFGS
• torch.optim.RMSprop
• torch.optim.Rprop
• torch.optim.SGD
Lecturer : Hongpu Liu Lecture 5-26 PyTorch Tutorial @ SLAM Research Group
Exercise 5-2: Read more example from official tutorial
https://pytorch.org/tutorials/beginner/pytorch_with_examples.html
Lecturer : Hongpu Liu Lecture 5-27 PyTorch Tutorial @ SLAM Research Group
PyTorch Tutorial
05. Linear Regression with PyTorch
Lecturer : Hongpu Liu Lecture 5-28 PyTorch Tutorial @ SLAM Research Group