Introduction to Deep
Learning
For Image classification
Outline
• Training Linear classifiers
• SoftMax Classification
• Neural Networks
• convolutional neural network
• generative adversarial network
Machine Learning and Deep Learning
Libraries in Python
Scikit-learn
(Machine Learning : regression,
classification, …)
3. Machine
Learning and
Deep Learning Keras
(Deep Learning Neural Networks, …)
Deep Learning Libraries in Python
TensorFlow
(Deep Learning: Production and
Deployment)
3. Deep
Learning
Libraries
PyTorch
(Deep Learning: regression, classification,… )
Logistic Regression
𝑦# = 0 𝑦# = 1
𝜎 𝑤#x <0.5
𝜎 𝑤 # x >0.5
𝐱 !
𝒘! 𝐱 = 0
𝒘! 𝒙𝟏 = +
𝐱 "
𝒘! 𝒙𝟐 = -
Logistic Regression
𝐱 !
𝒘! 𝐱 = 0
𝐱 % = [4, −4]# 𝐱" = [2, 0]#
𝑅1
𝑧
w= [1, −1]!
𝑝 𝑦" = 1 𝐱" , 𝒘 = 𝜎 𝒘# 𝐱" 𝐱 "
2
= 𝜎 −1,1 𝐱 $ = [−3, 0]#
0
= 𝜎 2 = 0. 881 𝑅2
Logistic Regression
𝐱 !
𝒘! 𝐱 = 0
𝐱 % = [4, −4]# 𝐱" = [2, 0]#
𝑅1
𝑧 w= [1, −1]!
𝑝 𝑦$ = 1 𝐱 $ , 𝒘 = 𝜎 𝒘# 𝐱 $ 𝐱 "
= 𝜎 −3 = 0. 047 𝐱 $ = [−3, 0]#
𝑝 𝑦" = 0 𝐱 $ , 𝒘 = 1 − 𝜎 𝒘# 𝐱 $
𝑅2
= 0.953
Logistic Regression
𝐱 !
𝒘! 𝐱 = 0
𝑅1
𝐱" = [2, 0]#
Xn P(y=1|x) P(y=0|x)
𝐱 % = [4, −4]#
x1 0. 881 0.119
𝐱 "
x2 0.047 0.953
x3 0.997 0.009 𝐱 $ = [−3, 0]#
𝑅2
Hyperplane
𝐱 !
𝒘! 𝐱 = 0
𝐱 % = [4, −4]# 𝐱" = [2, 0]#
𝑅1
How to find w? Best w?
w= [1, −1]!
𝐱 "
Fitting? Training process.
𝐱 $ = [−3, 0]#
𝑅2
The Big Picture
𝑦+ = 𝑓(𝑥|𝑊)
Train 𝑊, 𝑏
Iris Dataset
Classifier Prediction
𝑦!
Unknown
𝑥
Sample
Training
Training: Cost Function
• Lets try to train a model to predict 𝑦 = 1
• Lets forget about the logistic function try a simple model: 𝑦3 = 𝑏
• We use a cost function to see how good our model is :
(𝑦 − 𝑏) $
𝐶(𝑏) = (1 − 𝑏) $
What's the best value for b ?
𝑏
Training
• Let say we started with a guess b=-2
C(b=-2)=9
b
𝑦3 = −2
C(b=1)=0
© 2015 BigDataUniversity.com
𝑦3 =𝑏 1 13
Training
𝑠𝑙𝑜𝑝𝑒 (𝑏)
Learning Rate 𝐶(𝑏! )
𝑑𝐶(𝑏)
𝑑𝐶(𝑏% ) 𝑑𝑏
𝑏$ = 𝑏% − 𝜂
𝑑𝑏
𝐶(𝑏" )
𝑏$ = 𝑏% + 𝑛𝑢𝑚𝑏𝑒𝑟
0
© 2015 BigDataUniversity.com 𝑏
14
Training
𝐶 −2 = 9
𝑏$ = −2 − 1.5𝑠𝑙𝑜𝑝𝑒(−2)
𝑏% = −2 + 3
𝑏% = 1 𝐶 1 =0
𝑦3 = 1
15
Training: Cost
• All we do for the logistic regression just add the logistic function
𝑐 𝑤, 𝑏 = (𝑦 − 𝑏) $
𝑐 𝑤, 𝑏 = (𝑦 − 𝜎(𝑤𝑥 + 𝑏)) $
𝑐 𝑤, 𝑏 = (𝑦% − 𝜎(𝑤𝑥% + 𝑏)) $ +. . + (𝑦& − 𝜎(𝑤𝑥& + 𝑏)) $
Training
𝒘'
𝑐(𝒘& )
𝒘%
𝑐(𝒘" ) 𝒘$
−𝛻𝑓(𝜽! )
𝑐(𝒘$ ) −𝛻𝑓(𝜽" )
𝜃"
Training: logistic
𝑑𝐶(𝑏")
𝑏! = 𝑏" − 𝜂
𝑑𝑏
𝑏! = −1 + 2
𝜎(𝑤" 𝑥 + 𝑏)
𝑏
18
Training
𝑑𝐶(𝑏")
𝑏! = 𝑏" − 𝜂
𝑑𝑏
𝑏! = −1 + 0
𝜎(𝑤" 𝑥 + 𝑏)
19
Training: Threshold
Parameter b 0.69
Likelihood 0.445
1 − 𝜎(𝐱 " + 𝑏) 1 − 𝜎(𝐱 " + 𝑏) 𝜎(𝐱 $ + 𝑏)
𝑝 𝑦! = 0|𝒙! + 𝒃 𝑥𝑝 𝑦" = 0|𝒙" + 𝒃 x.. 𝑥𝑝 𝑦$ = 1|𝒙$ + 𝒃 ≈
Training: Threshold
Parameter b 0.69 0. 0.339
Likelihood 0.445 0.46
𝑝 𝑦! = 0|𝒙! + 𝒃 𝑥𝑝 𝑦" = 0|𝒙" + 𝒃 x.. 𝑥𝑝 𝑦" = 1|𝒙$ + 𝒃 ≈ 0.3378
Training: Threshold
Parameter b 0.69 0. 0.339 0
Likelihood 0.445 0.46 0.47
𝑝 𝑦! = 0|𝒙! + 𝒃 𝑥𝑝 𝑦" = 0|𝒙" + 𝒃 x.. 𝑥𝑝 𝑦" = 1|𝒙$ + 𝒃 ≈ 0.3378
22
Logistic Regression
𝑝 𝑦! = 0|𝒙! + 𝒃 𝑝 𝑦! = 0|𝒙! + 𝒃
C
𝑝 𝑌 𝛉, 𝑿 = * 𝜎(𝒘𝐱 @ + 𝑏)D! (1 − 𝜎(𝒘𝑻𝐱 @ + 𝑏)D! )BFD!
@AB
𝑙𝑛(𝑝 𝑌 𝛉, 𝑿 )
−𝑙𝑛(𝑝 𝑌 𝛉, 𝑿 )
B
− 𝑙𝑛(𝑝 𝑌 𝛉, 𝑿 )
C
© 2015 BigDataUniversity.com 𝜃G =argmax
23 % (P 𝑌 𝜃 )
Cross Entropy
&
1
ℓ(𝛉) = − E 𝑦( ln(𝜎(𝒘𝐱 ( + 𝑏))+(1 − 𝑦( ) ln(1 − 𝜎(𝒘𝐱 ( + 𝑏))
𝑁
()%
def criterion(yhat,y):
out=-1*torch.mean(y*torch.log(yhat) +(1-y)*torch.log(1-yhat))
return out
𝑦=0 𝑦=1 𝑦=2
𝑥
𝑧" = 𝑤" 𝑥 + 𝑏"
𝑧$ = 𝑤$ 𝑥 + 𝑏$
𝑧# = 𝑤# 𝑥 + 𝑏#
𝑥
𝑧" > 𝑧# 𝑎𝑛𝑑 𝑧$
𝑧"
𝑧$
𝑧#
𝑥
𝑧# > 𝑧" 𝑎𝑛𝑑 𝑧$
𝑧"
𝑧$
𝑧#
𝑧$ > 𝑧" 𝑎𝑛𝑑 𝑧#
𝑧"
𝑧$
𝑧#
Review of Argmax
i 0 1 2 3 4 5 6 7 8 9
𝒛 = [1 3 2 4 3 2 0 10 9 7]
𝑦! = 𝑎𝑟𝑔𝑚𝑎𝑥! {𝑧! }
𝑦! = 7
𝑧N = −1.7𝑥 − 0.36
𝑧O = 1.9𝑥 − 0.34
𝑧B = 1.3𝑥 + 1.3
𝑧N = −1.7𝑥 − 0.36
𝑧" 𝑧# 𝑧$ 𝑧O = 1.9𝑥 − 0.34
i 0 1 2
2.33 1.15 -3.3
𝑧B = 1.3𝑥 + 1.3
𝑦! = 𝑎𝑟𝑔𝑚𝑎𝑥! {𝑧! }
𝑦! = 0
𝑧N = −1.7𝑥 − 0.36
𝑧" 𝑧# 𝑧$ 𝑧O = 1.9𝑥 − 0.34
i 0 1 2
-1.25 1.36 0.64
𝑧B = 1.3𝑥 + 1.3
𝑦! = 𝑎𝑟𝑔𝑚𝑎𝑥! {𝑧! }
𝑦! = 1
𝑧N = −1.7𝑥 − 0.36
𝑧" 𝑧# 𝑧$ 𝑧O = 1.9𝑥 − 0.34
i 0 1 2
-3.05 1.46 2.6
𝑧B = 1.3𝑥 + 1.3
𝑦! = 𝑎𝑟𝑔𝑚𝑎𝑥! {𝑧! }
𝑦! = 2
Targets :MNIST Dataset
𝑦=0 𝑦=1 𝑦=2 ⋯ 𝑦=9
⋯
1
1
2
3
2
:
:
: 𝐱= 784
:
This is 2D not 784 D
𝑦 = 𝑎𝑟𝑔𝑚𝑎𝑥% {𝒘𝒊𝒙 + b𝒊}
𝒘# i 𝒘 𝑧' = 𝒘' 𝒙
𝒘" 1 1 1
,
2 2
2 1 1
− ,
2 2
3 0, −1
𝒘$
1
𝑧#
&! "
P( y =0|x)=
2 & ! " '& ! # '& ! $
𝑥! −1
1
2
2 P(y = 0|x)
𝑧!
&! #
P(y = 1|x)=
P(y = 1|x)
& ! " '& ! # '& ! $
1
𝑥" 0 2
1 P(y = 2|x)
𝑧" P(y = 2|x)=
&! $
& ! " '& ! # '& ! $
class SoftMax (nn.Module):
def __init__(self,in_size,out_size):
super(SoftMax,self).__init__()
self.linear = nn.Linear(in_size, output_size)
def forward(self,x):
out = self.linear(x)
return out
© 2017 IBM Corporation
input_dim = 28 * 28
output_dim = 10
model = SoftMax(input_dim, output_dim)
© 2017 IBM Corporation
criterion = nn.CrossEntropyLoss()
Loss
y : torch.LongTensor
y : N not Nx1
© 2017 IBM Corporation
optimizer = optim.SGD(model.parameters(), lr = 0.01) model
n_epochs = 100
accuracy_list = []
optimizer
© 2017 IBM Corporation
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size = 100)
x,y
validation_loader = torch.utils.data.DataLoader(dataset=validation_dataset, batch_size = 5000)
x,y
© 2017 IBM Corporation
for epoch in range(n_epochs):
for x, y in train_loader:
optimizer.zero_grad()
z = model(x.view(-1, 28*28))
loss = criterion(z, y)
loss.backward()
optimizer.step()
correct = 0
for x_test, y_test in validation_loader:
z = model(x_test.view(-1, 28*28))
_,yhat = torch.max(z.data, 1) :
correct = correct + (yhat == y_test).sum().item()
accuracy = correct / N_test :
accuracy_list.append(accuracy)
© 2017 IBM Corporation
What Is a Neural Networks
• A neural network is a function that can be used to approximate most
functions using a set of parameters
Features and targets: Example
1
Building a Neural Networks
with a Linear Classifier
Features and targets: Example
Linear Classifiers vs Neural Networks
Neural Networks
𝑥 𝑧
𝑧
𝑥
Neural Networks
activation function
𝑧(𝑥) 𝑎(𝑧)
𝑥
activation 𝑎
𝑧
Neural Networks
Neural Networks
Neural Networks
activation function
𝑧 𝑎
𝑥
𝑎!! 𝑎"! 𝑧"
𝑎"" − 𝑎$"
𝑧! 𝑎!! 1
𝑧"
𝑧! 𝑎"! −1
© 2017 IBM Corporation
Neural Networks
hidden layer
output layer
artificial neuron
artificial neuron
!
𝑤!"
"
𝑏"" 𝑀
" $ $
𝑤"" 𝑤## 𝑏#
𝑥0 "
𝑤"$
$
"
𝑏"$ 𝑤$#
!
𝑤!"
𝑀
" $
𝑤"" 𝑤##
𝑥0 "
𝑤"$
$
𝑤$#
!
𝑤!"
𝑥0
𝑧!! = 𝑥 + 5
𝑧!!
𝑧"!
𝑧"! = 𝑥 − 5
𝑧"!
𝑧!!
𝑎!! = 𝜎(𝑥 + 5) activation
𝑎"!
𝑎"! = 𝜎(𝑥 − 5)
𝑎!!
PyTorch
import torch
import torch.nn as nn
from torch import sigmoid
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__()
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
𝑥
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__()
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
𝑥
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__() D_in=1
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
𝑥
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__() H=2
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
𝑥
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__() H=2
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
𝑥
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__() H=2
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
𝑥
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__()
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
model = Net(1, 2, 1)
𝑥
x=torch.tensor([0.0])
yhat=model(x)
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__()
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
model = Net(1, 2, 1)
𝑥
x=torch.tensor([0.0])
yhat=model(x)
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__()
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
model = Net(1, 2, 1)
𝑥
x=torch.tensor([0.0])
yhat=model(x)
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__()
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
model = Net(1, 2, 1)
𝑥
x=torch.tensor([0.0])
yhat=model(x)
© 2017 IBM Corporation
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__()
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
model = Net(1, 2, 1)
𝑥
x=torch.tensor([0.0])
yhat=model(x)
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__()
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
model = Net(1, 2, 1)
𝑥
x=torch.tensor([0.0])
yhat=model(x)
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__()
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
model = Net(1, 2, 1)
x=torch.tensor([0.0])
yhat=model(x)
class Net(nn.Module):
def __init__(self,D_in,H,D_out):
super(Net,self).__init__()
self.linear1=nn.Linear(D_in,H)
self.linear2=nn.Linear(H,D_out)
def forward(self,x):
x=sigmoid(self.linear1(x))
x=sigmoid(self.linear2(x))
return x
!
𝑤!! $
𝑥0 𝑤&#
$
: 𝑤!$ 𝑤!"
! 𝑤&#
:
𝑤%!
:
:
!
𝑥1 𝑤%"
$
𝑤&#
𝑤%$
𝝈(𝒛 ) © 2017 IBM Corporation
Neuron 0
!
𝑤!! O
𝑤BB
𝑥0 O
𝑤BO O
𝑤BB Neuron 1
:
:
𝑤!$ 𝑤!"
! O
𝑤OB
𝑤%! O
𝑤OS
O
𝑤OO
O
O
𝑤SB 𝑤SS
Neuron 2
! O
𝑥1
𝑤SO
𝑤%" O
𝑤OS
𝑤%&
© 2019 IBM Corporation
Neuron 0
!
𝑤!! O
𝑤BB
1
𝑥! = 10
O
𝑤BO O
𝑤BB
Neuron 1
:
:
𝑤!$ 𝑤!"
! O
𝑤OB
𝑤%! O
𝑤OO
3
O
𝑤OS O
𝑤SS
Neuron 2
! O
𝑤SO
𝑥T = 4
𝑤%" O
𝑤OS
9
𝑤%&
𝑦+ = 2
© 2019 IBM Corporation
𝑥B
:
:
:
:
:
:
: :
𝑥T
ss Net(nn.Module):
def __init__(self,D_in,H1,H2,D_out):
super(Net,self).__init__()
self.linear1=nn.Linear(D_in, H1)
self.linear2=nn.Linear(H1, H2) 𝑥B
self.linear3=nn.Linear(H2, D_out) :
def forward(self,x): :
: :
x=torch. relu(self.linear1(x)) 𝑥T
: :
x=torch. relu(self.linear2(x))
x=self.linear3(x)
return x
255 255 255 255 0
255 255 255 235 0
255 255 0 0 0
255 0 0 0 0
Column
s
Column
s
rows
2 3 .. 40
rows : : .. 50
5 34 . . 24
0
1
2
3
0 2 3 .. 40
:
:
:
: : .. 50
500 5 34 . . 24
500 0 1 2 .. .. .. 50
0
0 1 2 .. .. .. 50
0
Image from: Rafael C. Gonzalez, Richard E. Woods - Digital Image Processing (2008,
Prentice Hall)
9 3 . . 40
: : . . 50
2 3 . . 40
5 34 . . 24
: : . . 50
4 5 3 34 . . . . 4024
: : . . 50
5 34 . . 24
9 3 . . 40
0 : : . . 50
2 3 . . 40
1
5 34 . . 24 2
2 : : . . 50
3
: 0 4 5 3 34 . . . . 4024 1
:
:
: : . . 50
500 500 5 34 . . 24 0
0 1 2 .. .. .. 50
0 1 2 .. .. .. 50 0
0
CNN for Image Classification
Feature Learning fully connected
layer
Classification
Input Convolution + RELU Pooling Convolution + RELU Pooling
image
𝐗
kernel
𝑾
0 0 1 0 0
1 0 -1
0 0 1 0 0
2 0 -2
0 0 1 0 0
1 0 -1
0 0 1 0 0
0 0 1 0 0 kernel_size=3
𝒁
-4
0x1+0x0+1x-1 -1
0 1 0 0 1 -1 0 0
+ +
0x2+0x0+1x-2
0 2 0 0 1 -2 0 0
-2
+
0 1 0 0 1 -1 0 0 0x1+0x0+1x-1 +
-1
0 0 1 0 0
=
0 0 1 0 0
-4
𝒁
-4 0
0x1+1x0+0x-1 0
0 0 1 1 0 0 -1 0
+ +
0x2+1x0+0x-2 0
0 0 2 1 0 0 -2 0
+ +
0 0 1 1 0 0 -1 0 0x1+0x0+0x-1 0
0
0 0 1 0 0
0 0 1 0 0
𝒁
-4 0 4
0 0 1 1 0 0 0 -1
0 0 1 2 0 0 0 -2
0 0 1 1 0 0 0 -1
0 0 1 0 0
0 0 1 0 0
𝒁
-4 0 4
0 0 1 0 0 -4
0 1 0 0 1 -1 0 0
0 2 0 0 1 -2 0 0
0 1 0 0 1 -1 0 0
0 0 1 0 0
𝒁
-4 0 4
0 0 1 0 0 -4 0
0 0 1 1 0 0 -1 0
0 0 2 1 0 0 -2 0
0 0 1 1 0 0 -1 0
0 0 1 0 0
𝒁
-4 0 4
0 0 1 0 0 -4 0 4
0 0 1 1 0 0 0 -1
0 0 1 2 0 0 0 -2
0 0 1 1 0 0 0 -1
0 0 1 0 0
𝒁
-4 0 4
0 0 1 0 0 -4 0 4
0 0 1 0 0 -4
0 1 0 0 1 -1 0 0
0 2 0 0 1 -2 0 0
0 1 0 0 1 -1 0 0
𝒁
-4 0 4
0 0 1 0 0 -4 0 4
0 0 1 0 0 -4 0
0 0 1 1 0 0 -1 0
0 0 2 1 0 0 -2 0
0 0 1 1 0 0 -1 0
𝒁
-4 0 4
0 0 1 0 0 -4 0 4
0 0 1 0 0 -4 0 4
0 0 1 1 0 0 0 -1
Activation Map
0 0 1 2 0 0 0 -2
0 0 1 1 0 0 0 -1
Out channels
𝒁N = 𝑾N ∗ 𝐗B + 𝒃N
𝐗"
1 0 -1
0 0 1 0 0 1 0 -2
1 0 -1
0 0 1 0 0
0 0 1 0 0 𝒁B = 𝑾B ∗ 𝐗B + 𝒃B
1 2 -1
0 0 1 0 0
0 0 0
0 0 1 0 0 -1 -2 -1
𝒁O = 𝑾O ∗ 𝐗 + 𝒃O
image
1 1 1
1 1 1
1 1 1
Out channels
𝒁N = 𝑾N ∗ 𝐗 O + bN
𝐗$
1 0 -1
0 0 0 0 0 1 0 -2
1 0 -1
0 0 0 0 0
1 1 1 1 1 𝒁B = 𝑾B ∗ 𝐗 O + 𝑏B
1 2 -1
0 0 0 0 0
0 0 0
0 0 0 0 0 -1 -2 -1
𝒁O = 𝑾O ∗ 𝐗 O + bO
image1
1 1 1
1 1 1
1 1 1
Method similar to CNN https://developer.nvidia.com/blog/deep-learning-nutshell-core-concepts/
Convolution Convolution Convolution
+ RELU + RELU + RELU
CNN Architecture
Convolution
Layer Pooling Convolution
Layer Layer Pooling
Layer
: Output
: :
:
Input Image
Image by Alex Aklson hidden layer
Convolution Max Pooling
TORCH-VISION MODELS
CNN Architecture
Convolution
Layer Pooling Convolution
Layer Layer Pooling
Layer
: Output
ResNet18 : :
:
Input Image
Fully Connected
Convolution Max Pooling Layer
classes
1
: 0
ResNet18
:
,
𝐲=
:
:
:
0
115
import torch
import torchvision.models as models
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
import torch.nn as nn
torch.manual_seed(0)
Pretrained
model = models.resnet18(pretrained=True)
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
composed= transforms.Compose([transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize(mean, std)])
train_dataset=Dataset(transform=composed, train=True )
validation_data=Dataset(transform=composed)
for param in model.parameters():
param.requires_grad=False
model.fc=nn.Linear(512,7)
:
: :
:
train_loader=torch.utils.data.DataLoader(dataset=train_dataset, batch_size=15)
x,y
validation_loader=torch.utils.data.DataLoader(dataset=validation_dataset,batch_size=10)
x,y
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam([parameters for parameters in model.parameters() if parameters.requires_grad],lr=0.003)
N_EPOCHS = 20
loss_list = []
accuracy_list = []
correct = 0
n_test = len(validation_dataset)
for epoch in range(n_epochs):
loss_sublist = []
for x, y in train_loader:
model.train()
optimizer.zero_grad()
z = model(x)
loss = criterion(z, y)
loss_sublist.append(loss.data.item())
loss.backward()
optimizer.step()
loss_list.append(np.mean(loss_sublist))
correct = 0
for x_test, y_test in validation_loader:
model.eval()
z = model(x_test)
_, yhat = torch.max(z.data, 1)
correct += (yhat == y_test).sum().item()
accuracy = correct / n_test
accuracy_list.append(accuracy)
Generative Adversarial
Network
Generative Adversarial Networks (GAN’S)
• generate new data with the same statistics as the training set
https://thispersondoesnotexist.com/
GAN