PyTorch Tutorial
06. Logistic Regression
Lecturer : Hongpu Liu Lecture 6-1 PyTorch Tutorial @ SLAM Research Group
Revision - Linear Regression
𝑦
Linear Unit
𝑥 ∗ + 𝑦ො 𝑙𝑜𝑠𝑠(𝑦,
ො 𝑦) 𝑙𝑜𝑠𝑠
𝜔 𝑏
Affine Model Loss Function
𝑦ො = 𝑥 ∗ 𝜔 + 𝑏 𝑙𝑜𝑠𝑠 = (𝑦ො − 𝑦)2 = (𝑥 ∙ 𝜔 − 𝑦)2
Lecturer : Hongpu Liu Lecture 6-2 PyTorch Tutorial @ SLAM Research Group
Revision - Linear Regression
x (hours) y (points)
1 2
2 4
3 6
4 ?
Affine Model Loss Function
𝑦ො = 𝑥 ∗ 𝜔 + 𝑏 𝑙𝑜𝑠𝑠 = (𝑦ො − 𝑦)2 = (𝑥 ∙ 𝜔 − 𝑦)2
Lecturer : Hongpu Liu Lecture 6-3 PyTorch Tutorial @ SLAM Research Group
Classification – The MNIST Dataset
The database of handwritten digits
- Training set: 60,000 examples,
- Test set: 10,000 examples.
- Classes: 10
import torchvision
train_set = [Link](root='../dataset/mnist', train=True, download=True)
test_set = [Link](root='../dataset/mnist', train=False, download=True)
Lecturer : Hongpu Liu Lecture 6-4 PyTorch Tutorial @ SLAM Research Group
Classification – The CIFAR-10 dataset
- Training set: 50,000 examples,
- Test set: 10,000 examples.
- Classes: 10
import torchvision
train_set = [Link].CIFAR10(…)
test_set = [Link].CIFAR10(…)
Lecturer : Hongpu Liu Lecture 6-5 PyTorch Tutorial @ SLAM Research Group
Regression vs Classification
x (hours) y (points) x (hours) y (pass/fail)
1 2 1 0 (fail)
2 4 2 0 (fail)
3 6 3 1 (pass)
4 ? 4 ?
In classification, the output of model is the
probability of input belongs to the exact class.
Lecturer : Hongpu Liu Lecture 6-6 PyTorch Tutorial @ SLAM Research Group
How to map: ℝ → 0, 1
Logistic Function
1
𝜎(𝑥) =
1 + 𝑒 −𝑥
[Link]
Lecturer : Hongpu Liu Lecture 6-7 PyTorch Tutorial @ SLAM Research Group
Sigmoid functions
Lecturer : Hongpu Liu Lecture 6-8 PyTorch Tutorial @ SLAM Research Group
Logistic Regression Model
Affine Model Logistic Regression Model
𝑦ො = 𝑥 ∗ 𝜔 + 𝑏 𝑦ො = 𝜎(𝑥 ∗ 𝜔 + 𝑏)
Linear Unit Logistic Regression Unit
𝑥 ∗ + 𝑦ො 𝑥 ∗ + 𝜎 𝑦ො
𝜔 𝑏 𝜔 𝑏
Lecturer : Hongpu Liu Lecture 6-9 PyTorch Tutorial @ SLAM Research Group
Loss function for Binary Classification
Loss Function for Linear Regression
𝑙𝑜𝑠𝑠 = (𝑦ො − 𝑦)2 = (𝑥 ∙ 𝜔 − 𝑦)2
Loss Function for Binary Classification
𝑙𝑜𝑠𝑠 = −(𝑦 log 𝑦ො + (1 − 𝑦) log(1 − 𝑦))
ො
Lecturer : Hongpu Liu Lecture 6-10 PyTorch Tutorial @ SLAM Research Group
Mini-Batch Loss function for Binary Classification
Loss Function for Binary Classification 𝒚 ෝ
𝒚 BCE Loss
𝑙𝑜𝑠𝑠 = −(𝑦 log 𝑦ො + (1 − 𝑦) log(1 − 𝑦))
ො 1 0.2 1.6094
1 0.8 0.2231
0 0.3 0.3567
Mini-Batch Loss Function for Binary Classification
𝑁 0 0.7 1.2040
1
𝑙𝑜𝑠𝑠 = − 𝑦𝑛 log 𝑦ො𝑛 + (1 − 𝑦𝑛 ) log(1 − 𝑦ො𝑛 ) Mini-Batch Loss 0.8483
𝑁
𝑛=1
Lecturer : Hongpu Liu Lecture 6-11 PyTorch Tutorial @ SLAM Research Group
Implementation of Logistic Regression
Linear Unit Logistic Regression Unit
𝑥 ∗ + 𝑦ො 𝑥 ∗ + 𝜎 𝑦ො
𝜔 𝑏 𝜔 𝑏
import [Link] as F
class LinearModel([Link]): class LogisticRegressionModel([Link]):
def __init__(self): def __init__(self):
super(LinearModel, self).__init__() super(LogisticRegressionModel, self).__init__()
[Link] = [Link](1, 1) [Link] = [Link](1, 1)
def forward(self, x): def forward(self, x):
y_pred = [Link](x) y_pred = [Link]([Link](x))
return y_pred return y_pred
Lecturer : Hongpu Liu Lecture 6-12 PyTorch Tutorial @ SLAM Research Group
Implementation of Logistic Regression
Mini-Batch Loss Function for Binary Classification
𝑁
1
𝑙𝑜𝑠𝑠 = − 𝑦𝑛 log 𝑦ො𝑛 + (1 − 𝑦𝑛 ) log(1 − 𝑦ො𝑛 )
𝑁
𝑛=1
criterion = [Link](size_average=False)
Lecturer : Hongpu Liu Lecture 6-13 PyTorch Tutorial @ SLAM Research Group
Implementation of Logistic Regression
x_data = [Link]([[1.0], [2.0], [3.0]])
Prepare dataset
1
y_data = [Link]([[0], [0], [1]])
#-------------------------------------------------------#
class LogisticRegressionModel([Link]): we shall talk about this later
def __init__(self):
super(LogisticRegressionModel, self).__init__()
[Link] = [Link](1, 1)
def forward(self, x):
y_pred = [Link]([Link](x))
return y_pred
model = LogisticRegressionModel()
#-------------------------------------------------------#
criterion = [Link](size_average=False)
optimizer = [Link]([Link](), lr=0.01)
#-------------------------------------------------------#
for epoch in range(1000):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, [Link]())
optimizer.zero_grad()
[Link]()
[Link]()
Lecturer : Hongpu Liu Lecture 6-14 PyTorch Tutorial @ SLAM Research Group
Implementation of Logistic Regression
x_data = [Link]([[1.0], [2.0], [3.0]])
y_data = [Link]([[0], [0], [1]])
#-------------------------------------------------------#
class LogisticRegressionModel([Link]):
def __init__(self):
super(LogisticRegressionModel, self).__init__()
[Link] = [Link](1, 1)
Design model using Class
def forward(self, x):
y_pred = [Link]([Link](x))
2
inherit from [Link]
return y_pred
model = LogisticRegressionModel()
#-------------------------------------------------------#
criterion = [Link](size_average=False)
optimizer = [Link]([Link](), lr=0.01)
#-------------------------------------------------------#
for epoch in range(1000):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, [Link]())
optimizer.zero_grad()
[Link]()
[Link]()
Lecturer : Hongpu Liu Lecture 6-15 PyTorch Tutorial @ SLAM Research Group
Implementation of Logistic Regression
x_data = [Link]([[1.0], [2.0], [3.0]])
y_data = [Link]([[0], [0], [1]])
#-------------------------------------------------------#
class LogisticRegressionModel([Link]):
def __init__(self):
super(LogisticRegressionModel, self).__init__()
[Link] = [Link](1, 1)
def forward(self, x):
y_pred = [Link]([Link](x))
return y_pred
model = LogisticRegressionModel()
#-------------------------------------------------------#
criterion = [Link](size_average=False)
Construct loss and optimizer
3
optimizer = [Link]([Link](), lr=0.01)
#-------------------------------------------------------#
for epoch in range(1000): using PyTorch API
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, [Link]())
optimizer.zero_grad()
[Link]()
[Link]()
Lecturer : Hongpu Liu Lecture 6-16 PyTorch Tutorial @ SLAM Research Group
Implementation of Logistic Regression
x_data = [Link]([[1.0], [2.0], [3.0]])
y_data = [Link]([[0], [0], [1]])
#-------------------------------------------------------#
class LogisticRegressionModel([Link]):
def __init__(self):
super(LogisticRegressionModel, self).__init__()
[Link] = [Link](1, 1)
def forward(self, x):
y_pred = [Link]([Link](x))
return y_pred
model = LogisticRegressionModel()
#-------------------------------------------------------#
criterion = [Link](size_average=False)
optimizer = [Link]([Link](), lr=0.01)
#-------------------------------------------------------#
for epoch in range(1000):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, [Link]())
Training cycle
optimizer.zero_grad() 4
[Link]() forward, backward, update
[Link]()
Lecturer : Hongpu Liu Lecture 6-17 PyTorch Tutorial @ SLAM Research Group
Implementation of Logistic Regression
x_data = [Link]([[1.0], [2.0], [3.0]])
Prepare dataset
1
y_data = [Link]([[0], [0], [1]])
#-------------------------------------------------------#
class LogisticRegressionModel([Link]): we shall talk about this later
def __init__(self):
super(LogisticRegressionModel, self).__init__()
[Link] = [Link](1, 1)
Design model using Class
def forward(self, x): 2
y_pred = [Link]([Link](x)) inherit from [Link]
return y_pred
model = LogisticRegressionModel()
#-------------------------------------------------------#
criterion = [Link](size_average=False)
Construct loss and optimizer
3
optimizer = [Link]([Link](), lr=0.01)
#-------------------------------------------------------#
for epoch in range(1000): using PyTorch API
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, [Link]())
Training cycle
optimizer.zero_grad() 4
[Link]() forward, backward, update
[Link]()
Lecturer : Hongpu Liu Lecture 6-18 PyTorch Tutorial @ SLAM Research Group
Result of Logistic Regression
import numpy as np
import [Link] as plt
x = [Link](0, 10, 200)
x_t = [Link](x).view((200, 1))
y_t = model(x_t)
y = y_t.[Link]()
[Link](x, y)
[Link]([0, 10], [0.5, 0.5], c='r')
[Link]('Hours')
[Link]('Probability of Pass')
[Link]()
[Link]()
Lecturer : Hongpu Liu Lecture 6-19 PyTorch Tutorial @ SLAM Research Group
PyTorch Tutorial
06. Logistic Regression
Lecturer : Hongpu Liu Lecture 6-20 PyTorch Tutorial @ SLAM Research Group