Artificial Intelligence and Machine Learning
Week 6 - Practical Aspects of Training Deep Neural Network
1
Practical Aspects of Training Deep Neural Network
2
Before Training
Three way hold out Method
3
Train Set: Used for updating weights through backpropagation.
Validation Set: Used to monitor the model during training and detect overfitting.
Test Set: Used only after training to evaluate generalization performance.
Bias vs Variance
Height
Weight
4
Let's say we have a bunch of weight data and the corresponding heights
Bias vs Variance
Height
Weight
5
Ideally, we’d like to create a mathematical model that describes the relationship between weight and height
Bias vs Variance
Height
Weight
6
First, let’s divide our data into training and testing data
Bias vs Variance
Height
Weight
7
Here are just the training sets
Bias vs Variance
Height
Weight
8
Firstly, lets use linear regression
Bias vs Variance
Height
Weight
9
But here’s the problem. No matter how we try to fit the line, our linear model never shows us the “true” relationship as it won’t curve.
Bias vs Variance
Height
Weight
10
But here’s the problem. No matter how we try to fit the line, our linear model never shows us the “true” relationship as it won’t curve.
Bias vs Variance
Height
Weight
11
But here’s the problem. No matter how we try to fit the line, our linear model never shows us the “true” relationship as it won’t curve.
Bias vs Variance
Height
Weight
12
Thus, the straight line will never show us the “true” relationship no matter how well we try to fit.
Bias vs Variance
Height
Weight
13
This inability for machine learning model to capture the true relationship is called bias. Our straight line model has high bias.
Bias vs Variance
Height
Weight
14
Let’s see another example. This time we fit a curvy line.
Bias vs Variance
Height
Weight
Height
Weight
15
So, when we take training set, clearly our curvy line has zero error and the linear model has high error.
Bias vs Variance
Height
Weight
16
But don’t forget we have test data too. And when you take only test data into account…
Bias vs Variance
Height
Weight
17
Linear regression model is showing a reasonable inaccuracy
Bias vs Variance
Height
Weight
18
But our curvy line is underperforming.
Bias vs Variance
Height
Weight
Height
Weight
19
So, when we take test set, clearly our curvy line has high error and the linear model has reasonable error like in the case of training.
Bias vs Variance
Height
Weight
Height
Weight
20
So, even though our curvy line did good job while training, the error was too high for test set.
Bias vs Variance
Height
Weight
Height
Weight
21
This difference in fits between test and training set for our model is called variance.
Bias vs Variance
Height
Weight
Height
Weight
22
And since our curvy line fits training data well and not testing data, this also means our model is overfitting.
Bias vs Variance
Height
Weight
23
In contrast, since our linear model cannot capture the true relationship even on the training data, this situation is called underfitting.
Bias vs Variance
Height
Weight
Height
Weight
24
The goal is to find a model that has…
Low variance
And low bias
Bias Variance Tradeoff
• Models with high bias tend to have low variance.
• Models with high variance tend to have low bias.
The idea is to find a “sweet spot”.
25
Bias
Variance
Underfitting zone
Overfitting zone
Generalization error
Model complexity
Practical workflow of Deep Learning
26
AI generated image
Dealing With the Overfitting
27
Training With the more Data
But Where to get more data?
28
Data Augmentation
Technique used in deep learning, especially in computer vision to artificially expand a dataset by creating modified versions of images!!
29
Copyright of the images used belongs to creator themselves.
Normalization
30
x
y
x
y
x
y
Mean subtraction
Normalize variance
Copyright of the images used belongs to creator themselves.
Normalization
31
https://ai.stackexchange.com/questions/6860/why-feature-scaling-for-skewed-contour
Copyright of the images used belongs to creator themselves.
Why?
Normalization
Why do we normalize the data in the first place?
y
32
Copyright of the images used belongs to creator themselves.
Batch Normalization
z[1]
z[2]
z[3]
33
Can we normalize the values of z[1] in order to train w[1],b[1] faster?
Given some values of Neural net z[1], z[2],...z[n]�
w[1],b[1]
Copyright of the images used belongs to creator themselves.
Batch Normalization
34
Early Stopping
iterations
error
train error
test error
Stop here
35
Regularization
Regularization refers to techniques that are used to calibrate machine learning models in order to minimize the adjusted loss function and prevent overfitting.
Regularization
36
Lasso Regularization (L1 Regularization)
Like Ridge, it adds a penalty to the loss (Loss function + λΣ|θ|), and it can shrink some weights exactly to zero
Without regularization (overfit)
With regularization (good fit)
37
Ridge Regularization (L2 Regularization)
Loss function + Regularized Term
Mathematically, we introduce a regularization parameter; which penalizes the size of the weights (coefficients).
Height
Weight
λ = 0 => No impact on the model
λ = minimal => acceptable model accuracy
λ = high => very high impact/underfitting
λ is chosen by cross-validation and hyperparameter tuning.
38
Ridge Regularization (L2 Regularization)
In other words, by starting with slightly worse fit, Ridge Regularization can provide better long term predictions.
By increasing bias slightly, the variance can drop significantly.
Height
Weight
39
Dropout
40
Copyright of the images used belongs to creator themselves.
While Training:
Apply a DropOut layer with p = 0.5
Vanishing and Exploding Gradients
41
Early-layer weights barely change.
The model learns very slowly.
Training stagnates at a very early stage just after a few iterations.
Copyright of the images used belongs to creator themselves.
Remember?
Vanishing and Exploding Gradients
42
The model weights may become NaN during training.
There is an exponential growth in the model parameter values.
Gradient descent diverges
Copyright of the images used belongs to creator themselves.
Choose Activation Function Wisely
43
Weight initialization techniques
44
x1
x2
x3
xn
z
w1
w2
w3
wn
z = x1w1 + x2w2 + … + xnwn
In order to regulate z (avoid vanishing or exploding), the larger n is, the smaller you want wi to be
Avoid zero initialization → all neurons get identical updates (symmetry).
Use random initialization → breaks symmetry, so neurons learn different features.
Issue with random initialization → Can cause vanishing/exploding
var(wi) = 1/n
For layer L
W[L] = np.random.randn(shape)*np.sqrt(1/n[L-1])
Weight initialization techniques
45
x1
x2
x3
xn
z
w1
w2
w3
wn
Some Popular Techniques
Xavier Initialization
Works well for sigmoid and TanH
Weight initialization techniques
46
x1
x2
x3
xn
z
w1
w2
w3
wn
Some Popular Techniques
He Initialization for ReLU and its Variants
Mini batch gradient descent
for t = 1 … 5000{
forward prop on X{t}
compute cost J{t}
backprop to compute gradient w.r.t J{t}
update w and b
}
1 Epoch
Mini batch gradient descent
X = [ x[1] x[2] … x[1000] x[1001] … x[m] ]
Here, the shape of X = (nx,m)
Y = [ y[1] y[2] … y[1000] y[1001] … y[m] ]
Here, the shape of Y = (1,m)
If you have 5 million data… let's create mini-batches of 1000 training examples
What is the shape of one block of X and Y?
…
…
x{1}
x{2}
x{3}
x{t}
y{1}
y{2}
y{3}
y{t}
Momentum
Copyright of the images used belongs to creator themselves.
RMSprop
2
2
W = W - ɑ dW/sqrt(vdw) , b = b - ɑ db/sqrt(vdb)
Adam
momentum
rms prop
Learning Rate Decay
The idea is to slowly decay the learning rate so that convergence occur.
lr = initial_lr / (1 + decay_rate × epoch_num)
Copyright of the images used belongs to creator themselves.
Hyperparameter Tuning
𝝰
𝛃1, 𝛃2, 𝝴 (0.9, 0.999, 10-8 respectively)
𝛃 (0.9 appx)
Number of layers
Number of hidden units
Learning rate decay
Mini-batch size
Some Popular CNN Architectures
54
Recap: CNN Architecture
55
An Architecture
56
A ConvNet architecture is in the simplest case a list of Layers
that transform the image volume into an output volume (holding the class scores).
Imagenet Dataset
57
contains over 14 million labelled images across ~20,000 categories (the ILSVRC subset uses 1,000 classes)
About ImageNet Competition
58
ImageNet Major Breakthrough.
59
AlexNet.
60
AlexNet Architecture Breakdown.
61
AlexNet Parameters.
62
VGG 16
63
VGG 16
64
VGG – 16 Architecture.
65
VGG – 16 Parameters.
66
GoogLeNet – Inception V1.
67
GoogLeNet – Inception V1.
68
GoogLeNet – Inception V1 - Architecture.
69
ResNet (Residual Networks)
70
Workings of ResNet.
71
Workings of ResNet.
72
ResNet Architecture.
73
ResNet – 50 Parameters.
74
Better Result in Practice.
Transfer Learning
75
Transfer Learning.
76
Transfer Learning-Process.
77
Transfer Learning – Process.
78
Transfer Learning – Process.
79
Freezing and Fine Tuning.
80
Where to find Pre-trained models?
81
Why it Works?
82
Closing Remarks.
83
Thank You!
84