0% found this document useful (0 votes)
1 views129 pages

Module 3

The document outlines a session on Supervised Learning II, focusing on K-Nearest Neighbors (K-NN) and Logistic Regression. It details the K-NN algorithm, including its procedure for classification and regression, and discusses how to choose the value of K. Additionally, it explains logistic regression's role in classification, emphasizing the use of log-odds and the confusion matrix for performance evaluation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views129 pages

Module 3

The document outlines a session on Supervised Learning II, focusing on K-Nearest Neighbors (K-NN) and Logistic Regression. It details the K-NN algorithm, including its procedure for classification and regression, and discusses how to choose the value of K. Additionally, it explains logistic regression's role in classification, emphasizing the use of log-odds and the confusion matrix for performance evaluation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ML for Robotics

Duration: 120 min BCSE424L Dr. Harini S

Module 3: Supervised Learning – II

• Pattern Recognition and ML, Christopher Bishop, Chapter 4, 5, 7


• Machine Learning, Tom Mitchel, chapter 4 and 8
[Link]
[Link]
svm/?ref=lbp
Session Overview

• K-NN
• Logistic Regression
• Perceptrons
• Single layer & Multi-layer – Support
• Vector Machines
– Linear & Non-linear
• Error Bounds Fine tuning of algorithms for robotic
environment.
What isOverview
Session KNN?

• K-NN – K- Nearest Neighbours

• Simple and Powerful

• Regression and Classification

• When a new (unknown) data point appears:


• KNN finds the K closest data points (neighbors) in the training data
• Uses their labels or values to predict the output
Procedure
Session Overview
KNN Algorithm - Classification Model

Initialise the value of k i.e how many nearest neighbour data points should
consider?
To predict the class label for new observation, iterate through each training data.

1. Calculate the distance between the Query Instance and all the training samples
by using a defined distance measure equation such as Euclidean distance,
Manhattan distance, or Minkowski distance.
2. Sort the calculated distance in ascending order.
3. Get the top k rows from the sorted list of distance.
4. Return the most frequent class/ Majority class of these rows as known predicted
class
Procedure
Session Overview

• Choose K (no. of neighbors)

• Measure Euclidean Distance

• Rank K nearest neighbors and select


• Classification: Majority vote of neighbors

• Regression: Average of neighbors’ values


Simple Example
Session Overview
Calculation for Euclidean
Acid Tissue is good X1 X2 Y Euclidean Dist Distance Rank
Durability Strength or not
7 7 BAD Sqrt((7-3)2+(7-7)2) 4 3
X1 X2 Y
7 4 BAD Sqrt((7-3)2+(4-7)2 5 4
7 7 BAD
Sqrt((3-3)2+(4-7)2
7 4 BAD 3 4 GOOD 3 1
3 4 GOOD Sqrt((1-3)2+(4-7)2 3.60555127
1 4 GOOD 1 4 GOOD 5 2
3 7

X1 X2 Y Euclidean Distance Rank


3 4 GOOD 3 1 • 2 Good, 1 Bad → Majority is Good
1 4 GOOD 3.605551275 2
7 7 BAD 4 3
7 4 BAD 5 4
AnotherOverview
Session Example (K=3, for now consider random)
Heig Weig Euclidean Ran Hei Wei Euclidean Ran
Height Weight Class ht ht Class Distance k ght ght Class Distance k
Underwe 1.41421356
167 51 Underweight
167 51 ight 6.708203932 5 169 58 Normal 2 1
182 62 Normal
176 69 Normal 182 62 Normal 13 8 170 55 Normal 2 2
173 64 Normal 176 69 Normal 13.41640786 9 173 57 Normal 3 3
172 65 Normal Underw 4.12310562
173 64 Normal 7.615773106 6
174 56 Underweight 174 56 eight 6 4
169 58 Normal 172 65 Normal 8.246211251 7
Underwe Underw 6.70820393
173 57 Normal 167 51 eight 2 5
170 55 Normal 174 56 ight 4.123105626 4
7.61577310
170 57 169 58 Normal 1.414213562 1 173 64 Normal 6 6
173 57 Normal 3 3 8.24621125
170 55 Normal 2 2 172 65 Normal 1 7
170 57
182 62 Normal 13 8
13.4164078
176 69 Normal 6 9
Regression
Session with KNN
Overview
• Let k=2

House Size House Size House Size Distance


([Link]) Rent ([Link]) Rent Distance ([Link]) Rent (sorted)
500 8000 500 8000 250 700 12000 50
600 10000 600 10000 150 800 14000 50
700 12000 700 12000 50 900 16000 150
800 14000 800 14000 50 600 10000 150
900 16000 900 16000 150 500 8000 250
750 ? 750 ? 750 ?

• Taking average of top 2, (12000+14000)/2 → 13000


Regression
Session with KNN
Overview
• What if k=3 in the previous example?

House Size House Size House Size Distance


([Link]) Rent ([Link]) Rent Distance ([Link]) Rent (sorted)
500 8000 500 8000 250 700 12000 50
600 10000 600 10000 150 800 14000 50
700 12000 700 12000 50 900 16000 150
800 14000 800 14000 50 600 10000 150
900 16000 900 16000 150 500 8000 250
750 ? 750 ? 750 ?

• Taking average of top 3, (12000+14000+16000)/3 → 14000


• If you re-order 900 and 600, then the result will be 12000
• Both are correct but the results are different?
Questions
Session to be answered?
Overview
• Ordering makes results different → which one to choose?
• Both the answers are correct

• This is the known limitation of basic K-NN

• This can be handled with distance weighted KNN

• How to choose K?
How to Overview
Session Choose “k”?

• Sqrt (n), where n is the number of data points


• Odd value of k is selected to avoid confusion
Weight Fruit

150 Apple If k=1, Apple


155 Apple
If k=2, Apple and orange equal votes
If k=3, 2 Apple votes and 1 orange vote
160 Apple
This can go on for k =4, k=5,k=6
165 Orange

170 Orange
As per our logic, take k= 3 (rounding off to nearest odd)
175 Orange
Answer is Apple
162 ?
Other Distance
Session Metrics
Overview
Logistic Overview
Session Regression
• Name is somewhat misleading. Really a technique for classification,
not regression.
– “Regression” comes from fact that we fit a
linear model to the feature space.

• Involves a more probabilistic view of classification.

• In Logistic regression, instead of fitting a regression line, we fit an


"S" shaped logistic function, which predicts two maximum values (0
or 1). (Take the input features, pass it through sigmoid function, get
probability, probability in to class label using threshold)

• The independent variable should not have multi-collinearity.


Logistic Overview
Session Regression
Logistic regression is for predicting the categorical dependent variable
using a given set of independent variables.

Dependent Variable(y):
The response categorical variable holding
values like 0 or 1, yes or no etc. (Binary)

Independent Variable(x):
The predictor variable used to predict the
response variable.
Logistic Overview
Session Regression
Odds and
Session Log-Odds
Overview

• Logistic Regression depends on log-odds

• What is the meaning of “odds”?


• Odds measure how likely an event is to happen compared to it not happening.

• For an event with probability p= (P (Y=1))


odds = p / (1-p)
Eg: 80% chance of an event happening, odds = 0.8/0.2 = 4
Event is 4 times more likely to “happen” than to “not happen”

Eg 2: 50% chance of an event = 0.5/0.5 =1 (equally likely outcome)


Odds and
Session Log-Odds
Overview

• What is “log - odds”?


• Log-odds is just the natural logarithm of the odds

• For an event with probability p= (P (Y=1))


log- odds = ln (p / (1-p) )

• Why log-odds in logistic regression?


• ln(p/(1-p)) can take values between –infinity to +infinity
• In odds – probability can only be between 0 and 1 and odds will be
positive (not symmetric)
• In logistic regression, we try to map the output non-linearly
Logistic Overview
Session Regression - Equation
Logistic Overview
Session Regression - Example

Admission CGPA

0 4.2
0 5.1
0 5.5
1 8.2
1 9.0
1 9.1
Estimated
Session Regression Equation
Overview
AnotherOverview
Session Example

Example:
Consider the following training examples:
Marks scored: X = [81 42 61 59 78 49]
Grade (Pass/Fail): Y = [Pass Fail Pass Fail Pass Fail]
Assume we want to model the probability of Y of the form
which is parameterized by (β0, β1).
i) Which of the following parameters would you use to model p(x).
(a) (-119, 2) (b) (-120, 2) (c) (-121, 2)
(ii) With the chosen parameters, what should be the minimum mark to ensure
the student gets a ‘Pass’ grade with 95% probability?
ExampleOverview
Session (1-feature X)

Sample Calculation
for p(x) for
ExampleOverview
Session
ExampleOverview
Session
ExampleOverview
Session

▪ Among three, the maximum likelihood value is for β0 = -120 , β1 = 2.


▪ Therefore, we have to use these values to model p(x)

ii) With the chosen parameters, what should be the minimum mark to ensure
the student gets a ‘Pass’ grade with 95% probability?
Substituting p(x) = 0.95, 0 = -120 and 1 = 2, we will get
Hours of Pass(1)/Fail
AnotherOverview
Session Example Study (0)
29 0
Question: No. of hrs 32, pass class? Consider threshold 0.5 15 0
What if sample βi Values are not given? 33 1
• β0and β1 (coefficients) values are not given 38 1
39 1
• To find the coefficients, lets find the probability

• Low x → 15 to 29 – Class 0
• High x→ 33 to 39 → Class 1
• Rule of thumb estimate for coefficient is considering difference in 2 points
• Rough rule of thumb estimate for 1 feature
• Here x1➔ avg(high x values), x2 → avg (low x values)
• x1→avg(15,29), x2→avg(33,38,39), x1= 22, x2 = 36.67
• Y is 1 for x1 and 0 for x2 that is p1 =0, p2 = 1
• Instead of 1 and 0, we consider p1 = 0.01 and p2 = 0.99 respectively (since
they are probabilities and applying log is tricky)

• β1 = (ln(0.99/1-0.99))-(ln(0.01)/(1-0.01)) / (36.67-22)
=(4.595)–(-4.595) /14.67
= 9.190/14.67 ~ 0.625
…Continued…
Session Overview
Now computing β0
• β0 = (ln (p1/1-p1) – (β1 * x1)

= -4.595 -0.625* 22

= -4.595 – 13.75
=-18.345

• Z for x= 32,
-18.345+(0.625*32) = 1.655

• P = 1/ (1 +e-(1.655)) = 0.84

• Considering 0.5 as threshold for class 1, 0.84 → pass


• So 32 hrs → pass
…Continued
Session Overview
Considering β0 and β1 values calculated, substituting to find z and p

Hours of Z = β0 + β1 * x p= 1/ (1+ez) Threshold p= 1/ (1+ez)


Study (x) =0.5, 1.2

Class(y)
1

15 -8.22 0.00027 0
0.8
29 0.03 0.507 1
32 1.655 0.84 1 0.6

33 2.28 0.907 1 0.4

38 5.905 0.9973 1
0.2

39 6.53 0.9985 1
0
0 5 10 15 20 25 30 35 40 45

Hours of study
Performance
Session Evaluation
Overview

Example: To classify the given image is dog or not

Understanding TP, TN, FP and FN


Performance
Session Evaluation
Overview

Predicted No Predicted Yes

Actual No 1-TN 3-FP


Dog
Actual Dog 2-FN 4-TP
Performance
Session Evaluation – Confusion Matrix
Overview

• Confusion Matrix is a performance measurement for the machine learning


classification problems
• The output can be two or more classes.

• It is a table with combinations of predicted and actual values.

• Assume for the given problem out of 150 samples the classifier predicted “Yes” 100
times and “No” 50 times.
Performance
Session Evaluation – Confusion Matrix
Overview

• True Positive: We predicted positive and it’s true.

• True Negative: We predicted negative and it’s true.

• False Positive (Type 1 Error): We predicted positive and it’s false.

• False Negative (Type 2 Error): We predicted negative and it’s false.


Performance
Session Evaluation -- Accuracy
Overview

• It simply measures how often the classifier correctly predicts.

• It is the ratio of correct predictions to the total number of input Samples.


Performance
Session Evaluation -- Misclassification Rate
Overview
Performance
Session Evaluation -- True Negative Rate/ Specificity
Overview

• It explains how many of the actual negative cases we were able to predict correctly
with our model.

• It answers the question “How well a model correctly identifies negative cases?”
Performance
Session Evaluation -- True Positive Rate/ Sensitivity/ Recall
Overview

• It explains how many of the actual positive cases we were able to predict correctly
with our model.

=95%
Performance
Session Evaluation -- Precision
Overview

• High Precision = Few false alarms

Good Accuracy does not Guarantee Good Precision


Performance
Session Evaluation --Prevalence
Overview
Performance
Session Evaluation -- F1 Score
Overview

• F1 Score is the harmonic mean of precision and recall


Performance
Session Evaluation -- AUC-ROC Curve
Overview

• AUC-ROC stands for the Area Under the Receiver


Operating Characteristic Curve

• The ROC curve is drawn by calculating the true


positive rate (TPR) and false positive rate (FPR) at
every possible threshold (in practice, at selected
intervals), then graphing TPR over FPR.

• A perfect model, which at some threshold has a


TPR of 1.0 and a FPR of 0.0, can be represented
by either a point at (0, 1) if all other thresholds
are ignored, or by the figure shown
Performance
Session Evaluation -- AUC-ROC Curve
Overview

• The Area Under the Curve (AUC) is the measure of the ability of a classifier to
distinguish between classes and is used as a summary of the ROC curve.

• The higher the AUC, the better the performance of the model at distinguishing
between the positive and negative classes.

• When AUC = 1, then the classifier is able to perfectly distinguish between all
the Positive and the Negative class points correctly.

• If, however, the AUC had been 0, then the classifier would be predicting all
Negatives as Positives, and all Positives as Negatives.
Performance
Session Evaluation -- AUC-ROC Curve
Overview
• When 0.5<AUC<1, there is a high chance that
the classifier will be able to distinguish the
positive class values from the negative class
values.

• This is so because the classifier is able to


detect more numbers of True positives and
True negatives than False negatives and False
positives.

• When AUC=0.5, then the classifier is not able


to distinguish between Positive and Negative
class points. Meaning either the classifier is
predicting random class or constant class for
all the data points.
Performance
Session Evaluation – some Sample Curves
Overview

Excellent →
Classifier  Good
Classifier

 Poor Classifier

Random Classifier →
Perceptron
What are
Session Perceptrons?
Overview
• A perceptron is the simplest form of an artificial neural network and one of the earliest
machine learning algorithms.
How areOverview
Session Perceptron part of Machine Learning?
• Perceptron are learning systems because:
• They learn from data
• They adjust parameters (weights & bias)
• They improve performance iteratively
• They generalize to unseen data
• Supervised learning model, Discriminative classifier, Parametric
learner
• Algorithmic model that learns a decision boundary from data.
What isOverview
Session a decision boundary?
Characteristics
Session Overviewof Single Layer Perceptron

A Single Layer Perceptron is the simplest neural


network:
• 1 input layer
• 1 output neuron
• NO hidden layers
• Only one Dense layer
• Linear or activation-based decision making
(e.g., linear, sigmoid, step, ReLU)
Single Layer
Session Perceptron (SLP)
Overview
How SLPOverview
Session learns decision boundary from data?

SLP learns in an online manner:


• One training sample at a time
• With known label 𝑦∈{-1, +1}

When Learning Happens?


• only when the point is misclassified

The update rule happens When mistakes occurs


Misclassification
Session Overview

t = actual (target) output


y = predicted output
If t≠y the sample is misclassified.

x₁ x₂ Target (t) Predicted (y) Result

0 0 0 1 Misclassification

0 1 1 1 Correct

1 0 1 1 Correct

1 1 1 1 Correct
Types
Session of Mis
Overview Classification
Actual Predicted Type

0 1 False Positive (FP)

1 0 False Negative (FN)

• Increase weights when output should be 1 but predicted 0


• Decrease bias negativity to push outputs toward 1
Example
Session Overview
OR Gate Other linearly separable problems
Example : NAND Implementation using Perceptron
Learning
Session Rate
Overview
η (eta) is the learning rate of the perceptron.

It controls how much the weights change during each training step.
For a perceptron:

• η (eta) → learning rate


• t → target (true output)
• y → predicted output
• x → input
• w → weight

• Small η → slow learning, stable


• Large η → fast learning, may overshoot or diverge

Common Range : 0.001 to 0.1


Single
Session Layer
Overview Perceptron –for OR Gate

Input (z):

Activation (Step function):

Choose weights and bias


W1, w2 and b
Example
Session Overview
Epoch =1 , w1=0,w2=0,b=-0.2
If t≠y the sample(S) is misclassified. Here 2 samples (2 and 3) are mis classified

z = w₁x₁ + w₂x₂ + Updated


S x₁ x₂ t y t−y Remarks
b (w₁, w₂, b)
1 0 0 0 −0.2 0 0 (0, 0, −0.2) No change
Updated weights
and bias (use this for
2 0 1 1 −0.2 0 1 (0, 0.1, −0.1)
next input to
calculate Z)
Updated weights
and bias (use this for
3 1 0 1 −0.1 0 1 (0.1, 0.1, 0.0)
next input to
calculate Z)
4 1 1 1 0.2 1 0 (0.1, 0.1, 0.0) No Change

Updated (w₁, w₂, b) = (0.1, 0.1, 0.0)


Epoch =2 , w1=0.1,w2=0.1, b= 0.0
If t≠y the sample(S) is misclassified. Here 1 sample (1) is mis classified

Updated
S x₁ x₂ t z = w₁x₁ + w₂x₂ + b y t−y Remarks
(w₁, w₂, b)

Updated weights and bias


1 0 0 0 0.0 1 −1 (0.1, 0.1, −0.1) (use this for next input to
calculate z)

2 0 1 1 0.0 1 0 (0.1, 0.1, −0.1) No Change

3 1 0 1 0.0 1 0 (0.1, 0.1, −0.1) No Change

4 1 1 1 0.1 1 0 (0.1, 0.1, −0.1) No Change

Updated (w₁, w₂, b) = (0.1, 0.1, -0.1)


Epoch =3 , w1=0.1,w2=0.1,b=-0.1

If t≠y the sample(S) is misclassified. Here all samples are classified correctly

Updated
S x₁ x₂ t z = w₁x₁ + w₂x₂ + b y t−y Remarks
(w₁, w₂, b)

1 0 0 0 −0.1 0 0 (0.1, 0.1, -0.1) No change

2 0 1 1 0.0 1 0 (0.1, 0.1, -0.1) No change

3 1 0 1 0.0 1 0 (0.1, 0.1, -0.1) No change

4 1 1 1 0.1 1 0 (0.1, 0.1, -0.1) No Change

No Misclassifications – So Converged
XOR - Not linearly separable
• A single straight line cannot separate XOR outputs.
• Therefore:
• Single-Layer Perceptron (SLP) fails
• Multi-Layer Perceptron (MLP) works
• XOR proved that hidden layers are necessary, which led to modern neural networks.
To solve XOR, we need:
• At least one hidden layer
• Non-linear activation (ReLU, sigmoid, tanh)
• This creates a non-linear decision boundary.
Simple Example (Image Recognition)

For face recognition:


• Input Layer → Pixel values
• Hidden Layer 1 → Detects edges
• Hidden Layer 2 → Detects eyes, nose
• Hidden Layer 3 → Detects full face
• Output Layer → Person’s name
Multi-layer perceptron (MLP)
• A multilayer perceptron (MLP) is a feed-forward artificial neural
network that generates a set of outputs from a set of inputs.
• An MLP is characterized by several layers of input nodes connected as a
directed graph between the input nodes connected as a directed graph
between the input and output layers.
• MLP uses backpropagation for training the network.
• MLP is a deep learning method.
Back Propagation Training

• Back Propagation is the training or learning algorithm rather than the


network itself

• A Back Propagation network learns by example

• You give the algorithm examples of what you want the network to do and
it changes the network’s weights so that, when training is finished, it will
give you the required output for a particular input

• Back Propagation networks are ideal for simple Pattern Recognition and
Mapping Tasks
63
How Back Propagation Woks?

• The network is first initialized by setting up all its weights to


be small random numbers - say between -1 and +1
• Next, the input pattern is applied and the output is calculated
(this is called the forward pass)
• The calculation gives an output that is completely different to
what you want (the Target), since all the weights are random
• We then calculate the Error of each neuron : Target - Actual
Output (i.e., What you want - What you actually get)

64
How Back Propagation Works?

• This error is then used mathematically to change the weights


in such a way that the error will get smaller
• In other words, the Output of each neuron will get closer to
its Target (this part is called the reverse pass)
• The process is repeated again and again until the error is
minimal.

65
How Back Propagation Works?

66
MLP– Back Propagation Algorithm – 6 Step Cycle
Step 1: Initialize weights (wi), biases (bi) (if any) and learning rate Ƞ
Example: w1 : 0.5, w2 : 0.4 , b=0, x=1, y=1

Step 2: Forward Pass: First apply the inputs to the network and work out the output -
remember this initial output could be anything, as the initial weights were random
numbers.

• Calculate for Hidden Layer

• Calculate for Output Layer


67
MLP– Back Propagation Algorithm – 6 Step Cycle
• Step 3. Loss / Error Calculation (MSE)

• Loss will give measure of “how wrong we are” but not “how much the
output neuron is responsible for the error” this is quantified in step 4

• We want “How much the loss will change for change in weight?
(derivative of L with respect to w)”
68
MLP– Back Propagation Algorithm – 6 Step Cycle
• STEP 4: Compute Output Layer Gradient (Output Delta)
• Loss depends on weight through multiple steps

• Applying chain rule,

• First derivative of loss • Activation function • Linear part


(calculated in Step 3 ) derivative derivative

(sensitivity of loss to prediction)

Multiplying all three derivatives, 69


MLP– Back Propagation Algorithm
Step 4 – Continued…

• Here we will analyze the derivative of activation function f’(z)


• Activation function can be sigmoid, tanh, ReLU, etc.
• Below is the table with derivatives of some of the function
Tanh Sigmoid ReLU

70
MLP– Back Propagation Algorithm

• Step 5 – Just like Step 4 find the


delta for Hidden neuron

• STEP 6: Update Weights

71
Back Propagation Algorithm – Example

w1 H1 w5

w2
Output
w3 Layer
H2
w4 w6

72
Back Propagation Algorithm – Example

• Step 1: Already weights are given

• Step 2: Forward Pass

H1: H2: Output Layer:


Z1 = (0.35*0.1) + Z2 = (0.35*0.4) + Z3 =
(0.9*0.8) = 0.755 (0.6*0.9) = 0.68 (0.68*0.3)+(0.9*0.664) =
a1 = 𝛔(0.755)=0.68 a2 = 𝛔(0.68)=0.664 0.8016
Ŷ = 𝛔(0.8016)= 0.69

73
Back Propagation Algorithm – Example

• Step 3: Compute Loss

L = ½ (1-0.69)2 = 0.048

• Step 4: Output Delta

= -0.31 , f’(z) = 0.68 * (1-0.68) = 0.214

Output layer delta 𝝳out= -0.31*0.214 = - 0.066

74
Back Propagation Algorithm – Example

• Step 5 : Hidden Layer Deltas

H1: H2:
𝛔’(Z1) = 0.68 * (1-0.68) = 0.218 𝛔’(Z2) = (0.664)*(1-0.664) = 0.223

𝝳1 = 0.218 * 0.3 * (-0.66) = - 𝝳2 = 0.223 * (0.9)* (−0.066) = -


0.0043 0.0132

75
Back Propagation Algorithm – Example

• Step 6: Update Weights


Hidden → output Input --> Hidden
W5 =0.3 - 𝝳out * 0.1 * input W1 =0.1 – 0.1 * 𝝳1 * input
= 0.3 – (0.1)*(0.066)* 0.68 = 0.3045 =0.1 – 0.1 *0.044*0.35 = 0.10015

W6 =0.9 - 𝝳out * 0.1 * input W2 = 0.8 – 0.1 * 𝝳1 * input


=0.9 – (0.1)*0.066)*(0.664) = 0.9044 =0.8 – 0.1 *0.044*0.9 = 0.80039

W3 = 0.4 – 0.1 * 𝝳2 * input


=0.8 – 0.1 *-0.0132*0.35 = 0.4004

W4 = 0.6 – 0.1 * 𝝳2 * input


=0.8 – 0.6 *-0.0132*0.9 = 0.60119
76
Try it Yourself– Example 3

77
Train Network to recognize A, B, C, D

78
When to Stop Training?
• Ideally, stop it once the network can recognize all the letters successfully. But in Practice, let the error fall to a
lower value first

79
Four Fundamental Equations for Backpropagation
• In slide 9, equations given are for sigmoid function. Here they are generic, applicable to any activation
function

1. The error in the output layer:

• First component shows how fast the cost is changing and the second component shows how fast the
activation function is changing

• It can be rewritten as, , where the first component is a gradient descent matrix and
second is the activation function change

2. The error in other layers (hidden):

3. An equation for the rate of change of the cost with respect to any bias in the network:

4. An equation for the rate of change of the cost with respect to any weight in the network

80
Use of Validation Sets

81
Problems with Back Propagation

82
Stochastic Gradient Descent with BP

83
Vanishing Gradients, Exploding Gradients and Sigmoid Function
• Vanishing gradients are common when the Sigmoid or Tanh activation functions
are used in the hidden layer units
• When the inputs grow extremely small or extremely large, the sigmoid function
saturates at 0 and 1
• The derivatives are very near zero
• This function ranges/regions are called saturated regions
• As a result, if your input is located in one of the saturating regions, it has
essentially no gradient to propagate back through the network
• Exploding Gradient: Suppose that the initial weights supplied to the network
result in a high loss, then Gradients can collect during an update, resulting in
very big gradients. This will eventually result in huge modifications to network
weights, resulting in an unstable network. The parameters can occasionally
become so enormous that they overflow and produce NaN values

84
How to Know if Model is Suffering from Vanishing/ Exploding Gradient?
• Vanishing Gradient:
• The parameters of the higher layers vary dramatically, whereas the
parameters of the lower levels do not change significantly for
Vanishing (or not at all).
• During training, the model weights may become zero.
• The model learns slowly, and after a few cycles, the training may
become stagnant.

• Exploding Gradient:
• The model parameters are growing exponentially.
• During training, the model weights may become NaN.
• The model goes through an avalanche learning process

85
Tanh as Activation Function
• The Hyperbolic Tangent, also known as Tanh, is a similar shaped
nonlinear activation function that outputs value range from -1.0 and 1.0

• Tanh function was preferred over the Sigmoid activation function as


models that used it were easier to train and often had a better predictive
performance

86
Vanishing Gradient Problem

87
What is the solution to vanishing gradient?
• ReLu (Rectified Linear Unit)
• If the input value is positive, the ReLU function returns it; if it is
negative, it returns 0.

• The ReLU’s derivative is 1 for values larger than zero. This handles
vanishing gradient problem

88
What is the solution to vanishing gradient?
• ReLu (Rectified Linear Unit)
• If the input value is positive, the ReLU function returns it; if it is
negative, it returns 0.

• The ReLU’s derivative is 1 for values larger than zero. This handles
vanishing gradient problem

89
What is the solution to vanishing gradient?
• ReLu (Rectified Linear Unit)
• If the input value is positive, the ReLU function returns it; if it is
negative, it returns 0.

• The ReLU’s derivative is 1 for values larger than zero. This handles
vanishing gradient problem

90
Limitations of ReLU

1. Dying ReLU
• During training, some neurons effectively die, meaning they stop outputting anything other than 0
• In some cases, you may find that half of your network’s neurons are dead, especially if you used a
large learning rate
• A neuron dies when its weights get tweaked in such a way that the weighted sum of its inputs are
negative for all instances in the training set

• Solution:
• Variations of ReLU → Leaky ReLU, Parametric Leaky ReLU (PReLU), Exponential Linear Unit (ELU),
or Scaled Exponential Linear Unit (SELU)

91
Variations of ReLU

• Randomized Leaky Relu → α is • The parametric leaky ReLU (PReLU),


picked randomly in a given range where α is authorized to be learned
during training and is fixed to an during training.
average value during testing

• ELU (exponential linear unit) : • Scaled ELU (SELU) → is a scaled variant of the ELU activation function.
If we build a neural network composed exclusively of a stack of dense layers, and
if all hidden layers use the SELU activation function, then the network will self-
normalize: the output of each layer will tend to preserve a mean of 0, and the
standard deviation of 1 during training, which solves the vanishing/exploding
gradients problem. 92
Improving the way the network learn

• Cross-Entropy Cost Function→ better choice of cost function

• 4 Regularization methods → L1 and L2 Regularization, Drop out, Artificial Expansion of training


data

• Better initializing of weights techniques

• Set of heuristics for good hyper-parameters - tuning

93
Slow Learning in the network
• Example : Consider the single neuron below (take the input 1 to the output 0 )

Initial Weight : 0.6; initial bias: 0.9; Initial Weight : 2.0; initial bias: 2.0;
Learning rate:0.15 Learning rate:0.15
94
Cross-Entropy Cost Function

• If we can replace the cost function with a different cost function, learning slowdown can be
addressed
• What cost function to replace with? → Cross Entropy Cost Function

• Cross entropy loss is a metric used to measure how well a classification model in machine
learning performs

• Cross entropy loss measures the difference between the discovered probability distribution
of a machine learning classification model and the predicted distribution

95
Support Vector Machines(SVM)

• SVM is a supervised ML algorithm used for both classification(SVC) and regression(SVR).

• SVM chooses the extreme points/vectors that help in creating the hyperplane.

• These extreme cases are called support vectors, and hence algorithm is termed as Support
Vector Machine.

• The main objective of the SVM algorithm is to find the optimal hyperplane in an N-
dimensional space that can separate the data points in different classes in the feature space.

96
Support Vector Machines(SVM)

• The hyperplane tries that the margin between the closest points of different classes should
be as maximum as possible.

• The dimensions of the hyperplane depend on the features present in the dataset.

• If there are 2 features, the hyperplane will be a straight line.

• If there are 3 features, the hyperplane will be a 2-dimension plane.

• It can work on both linear and non-linear separable data

97
Support Vector Machines(SVM)

• The hyperplane tries that the margin between the closest points of different classes should
be as maximum as possible.

• The dimensions of the hyperplane depend on the features present in the dataset.

• If there are 2 features, the hyperplane will be a straight line.

• If there are 3 features, the hyperplane will be a 2-dimension plane.

• It can work on both linear and non-linear separable data

98
Support Vector Machines(SVM)

99
Types of SVM

• Linear SVM: Linear SVM is used for linearly separable data, which
means if a dataset can be classified into two classes by using a single
straight line, then such data is termed as linearly separable data, and
classifier is used called as Linear SVM classifier.

• Non-linear SVM: Non-linear SVM is used for non-linearly separated


data, which means if a dataset cannot be classified by using a straight
line, then such data is termed as non-linear data and the classifier used
is called as Non-linear SVM classifier.

100
Linearly Separable and Non- Linearly Separable Classes

101
Types of SVM
How to choose the best
The dataset has 2 features (x,y) to be
hyperplane?
classified as green and blue

102
How SVM chooses Hyper plane?

• Logic:
For points (𝑥𝑖,𝑦𝑖 ) with labels 𝑦i=±1:
1. The hyperplane :w x+b
T =0
2. All points must satisfy: yi(w x+b)>=1
T

3. Maximize margin → minimize ||w|| (because margin =2/||w||)


1
Optimization : min ||w|| such
2 that (w x+b)>=1
T
2

(wTx+b)>=1 103
Linear SVM Example
Positive labelled Class

Negative labelled Class

3 Support Vectors Identified

104
Linear SVM Example

105
Linear SVM Example

• What is the SVM weight vector?


The SVM weight vector is the vector w that defines the decision boundary
(hyperplane) separating the classes in a Support Vector Machine.

• This equation is obtained by substituting the SVM weight vector into


the support-vector margin condition

• This states that each support vector lies exactly on the SVM margin,
resulting in the expression

106
SVM support-vector condition

Original support-vector condition

We have Augmented support vectors


with bias-So bias removed explicitly-It is
embedded inside the dot product

107
SVM support-vector condition

For each support vector, the weighted sum of


dot products with all support vectors equals
its class label

108
Augmented Weight Vector

Distance interpretation

Distance of hyperplane from origin:


So the boundary is 2 units away from the origin

109
Points to remember
1
w= 0
(x,y) =(1,0) then the hyperplane is vertical
0
w= 1
(x,y) =(0,1) then the hyperplane is horizontal
1
w= 1
(x,y) =(1,1) then the hyperplane is 45-degree line

• b=-2 is the offset value.

110
Final Summary

• Decision boundary is vertical line x=2


• Left side - negative class
• Right side - positive class
• Margin touches the three support vectors.

111
Support Vectors & Hyperplane

112
How to use SVM Model to Classify Data?

Final classification
SVM rule:
f(x)>0 then positive class
f(x)<0 then negative class
Since: 1> 0
(3,2) belongs to the POSITIVE class

113
How to use SVM Model to Classify Data?

114
Non-Linear SVM

115
Projecting Data in higher dimension makes it Linearly Separable

116
Example 1
X Y

-2 +1(. )

-1 -1 (. )

1 -1 (. )

2 +1 ( )

117
Example 1
X Y φ(x) = x2
(New feature f(x) = x2-
space)
2.5 Step 1: Choose a polynomial kernel of degree 2 (Since
degree 1 is non-linear for the problem given)
-2 +1(. ) 4 1.5

Step 2: Choose Support Vectors


-1 -1 (. ) 1 -1.5
S1 = −1 , S2 = 1 , S3 = 2
Step 3: Augmented Support vectors S~= [K(xi,xj),1]
~ 1 ~ 1 ~ 4
S1 = , S2 = , S3 =
1 1 1
1 -1 (. ) 1 -1.5

Step 4: SVM equation


Dot product :[1,1].[1,1]=2, [4,1].[1,1]=5
2 +1 ( ) 4 1.5
α1​(−1)(2)+α2​(−1)(2)+α3​(1)(5)=−1 → for s1
α1​(−1)(2)+α2​(−1)(2)+α3​(1)(5)=−1 → for s2
α1​(−1)(5)+α2​(−1)(5)+α3​(1)(17)=1 → for s3
118
Example 1

Step 5 : Solve for α


α1​=α2​=0.2
𝛼3=0.12

Step 6: Decision Function

f(x)=0.2(−1)K(−1,x)+0.2(−1)K(1,x)+0.12(1)K(2,x)+b

This produces a quadratic boundary in original space.

Linear Separator now exists with f(x)


119
Example 2

Positive labelled Class

Negative labelled Class

This is a non –linear separable dataset


120
Example 2: Map 2D to 3D

Use 3D Mapping

Where

Now data is in 3D
In 3D, SVM finds a plane:
w1x1+w2x2+w3z+b=0
That plane separates the classes
linearly.
121
Example 2:How Does this Become 2D Again?
The plane equation becomes

The above equation is not linear


anymore.
It’s a non linear equation in 2D
Here we are able to plot only 2 points
so this mapping function is not a right
choice.

122
Example 2

A better mapping function needs to be used to map from input


space into some feature space

123
Example 2 : Non-Linear SVM - using the new mapping function
Positive labelled Class

2 10 6 6
2 6 6 10

Negative labelled Class

2 Support Vectors Identified

124
Example 2: Support vectors Identified

125
Example 2

126
Example 2

127
How to use SVM Model to Classify Data?

128
Another Example

129

You might also like