0% found this document useful (0 votes)
16 views118 pages

AI/ML Training Program Overview

The AI/ML Professional Training Program, led by Prof. Dr. Ahmad Raza Shahid, covers essential topics in machine learning including supervised and unsupervised learning, data preprocessing techniques, and various regression and classification methods. The course emphasizes the importance of understanding machine learning concepts, such as overfitting and regularization, to build effective models. Participants will learn practical applications of machine learning in diverse fields, supported by hands-on examples and detailed methodologies.

Uploaded by

wadood42024
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views118 pages

AI/ML Training Program Overview

The AI/ML Professional Training Program, led by Prof. Dr. Ahmad Raza Shahid, covers essential topics in machine learning including supervised and unsupervised learning, data preprocessing techniques, and various regression and classification methods. The course emphasizes the importance of understanding machine learning concepts, such as overfitting and regularization, to build effective models. Participants will learn practical applications of machine learning in diverse fields, supported by hands-on examples and detailed methodologies.

Uploaded by

wadood42024
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

1

AI/ML Professional Training Program:


INNOQUEST Cohort-1

Instructor

Prof. Dr. Ahmad Raza Shahid

Department of Computer Science


FAST National University of Computer and Emerging Sciences

AI/ML Professional Training Program: INNOQUEST COHORT-1


Instructor: Prof. Ahmad Raza Shahid
Content
• Machine Learning Overview
• End-to-end pipeline
• Data preprocessing
• Feature scaling
• One-hot encoding
• Regression
• Linear regression
• Multi-regression
• Polynomial regression
• Over-fitting
• Regularization
• Evaluation metrics
• Classification
• Classification Examples
• Logistic regression
• Cross entropy
• Gradient descent
• Stochastic gradient descent
• Multinomial regression

AI/ML Professional Training Program: INNOQUEST COHORT-1


2
Instructor: Prof. Ahmad Raza Shahid
What is machine learning?

• Machine learning (ML) is a field of study in artificial


intelligence concerned with the development and study of statistical
algorithms that can learn from data and generalize to unseen data, and
thus perform tasks without explicit instructions.

• ML finds application in many fields, including natural language


processing, computer vision, speech recognition, email
filtering, agriculture, and medicine.

AI/ML Professional Training Program: INNOQUEST COHORT-1


3
Instructor: Prof. Ahmad Raza Shahid
Need for machine learning
• Explosion in the availability of data
• There must be a process that generates the data
• Though we do not know what the process is
• Consumer behavior:
• People do not buy things at random
• If they buy meat and vegetables
• They buy masalas for cooking as well
• There are certain patterns in the data

AI/ML Professional Training Program: INNOQUEST COHORT-1


4
Instructor: Prof. Ahmad Raza Shahid
Need for machine learning
• Knowing the actual underlying process is difficult
• Approximation can be done
• May not explain everything
• May explain part of the data
• Identifying the complete process may not be possible
• Certain patterns or regularities can be detected
• Assuming that the future, at least the near future, will not be much
different from the past, when the sample data was collected
• The future predictions can also be expected to be right

AI/ML Professional Training Program: INNOQUEST COHORT-1


5
Instructor: Prof. Ahmad Raza Shahid
Types of machine learning

AI/ML Professional Training Program: INNOQUEST COHORT-1


6
Instructor: Prof. Ahmad Raza Shahid
Supervised learning

AI/ML Professional Training Program: INNOQUEST COHORT-1


7
Instructor: Prof. Ahmad Raza Shahid
Types of supervised learning

• Classification (discrete labels)

• Regression (continuous values)

AI/ML Professional Training Program: INNOQUEST COHORT-1


8
Instructor: Prof. Ahmad Raza Shahid
Classification
Figure. illustrates the concept of a binary classification
task given 30 training examples; 15 training examples are
labeled as class A and 15 training examples are labeled as
class B. In this scenario, our dataset is two-dimensional,
which means that each example has two values associated
with it: x1 and x2. Now, we can use a supervised machine
learning algorithm to learn a rule—the decision boundary
represented as a dashed line—that can separate those two
classes and classify new data into each of those two
categories given its x1 and x2 values

AI/ML Professional Training Program: INNOQUEST COHORT-1


9
Instructor: Prof. Ahmad Raza Shahid
Regression

• Analyze the relationship between dependent (target) and independent


(input) variables.

• It predicts continuous, numerical values as outputs based on the input


features.

AI/ML Professional Training Program: INNOQUEST COHORT-1


10
Instructor: Prof. Ahmad Raza Shahid
Unsupervised Learning
• Clustering
Figure. illustrates how clustering can be applied to
organizing unlabeled data into three distinct
groups or clusters (A, B, and C, in arbitrary order)
based on the similarity of their features, x1 and x2

AI/ML Professional Training Program: INNOQUEST COHORT-1


11
Instructor: Prof. Ahmad Raza Shahid
Unsupervised learning

• Dimensionality Reduction

Figure 1.7 shows an example


where nonlinear dimensionality
reduction was applied to compress
a 3D Swiss roll onto a new 2D
feature subspace

AI/ML Professional Training Program: INNOQUEST COHORT-1


12
Instructor: Prof. Ahmad Raza Shahid
Reinforcement learning

AI/ML Professional Training Program: INNOQUEST COHORT-1


13
Instructor: Prof. Ahmad Raza Shahid
Basic terminologies

14
General end-to-end pipeline for machine learning
systems

AI/ML Professional Training Program: INNOQUEST COHORT-1


15
Instructor: Prof. Ahmad Raza Shahid
Detailed roadmap for building machine learning
systems

16
Data preprocessing
• Feature scaling

• Feature scaling is a preprocessing technique used in machine learning to


standardize the range of independent features (input data)

• Min-Max Scaling (Normalization)


• rescaling of the features to a range of [0, 1]

• Standardization (Z-Score Scaling)

• Robustscaler
• recommended if working with small datasets that contain many outliers
AI/ML Professional Training Program: INNOQUEST COHORT-1
17
Instructor: Prof. Ahmad Raza Shahid
Standardization

Figure 2.13 illustrates


possible gradient updates
with unscaled features (left)
and standardized features
(right), where the
concentric circles represent
the loss surface as a
function of two model
weights in a two-
dimensional classification
problem

AI/ML Professional Training Program: INNOQUEST COHORT-1


18
Instructor: Prof. Ahmad Raza Shahid
Standardization and Normalization

The table illustrates the difference between


the two commonly used feature scaling
techniques, standardization and
normalization, on a simple example dataset
consisting of numbers 0 to 5

AI/ML Professional Training Program: INNOQUEST COHORT-1


19
Instructor: Prof. Ahmad Raza Shahid
Data preprocessing
• One hot encoding
• Convert ordinal features into integers
• one-hot encoding convert the indices into vectors of zeros and ones.

AI/ML Professional Training Program: INNOQUEST COHORT-1


20
Instructor: Prof. Ahmad Raza Shahid
One hot encoding

AI/ML Professional Training Program: INNOQUEST COHORT-1


21
Instructor: Prof. Ahmad Raza Shahid
One hot encoding

AI/ML Professional Training Program: INNOQUEST COHORT-1


22
Instructor: Prof. Ahmad Raza Shahid
Data preprocessing
• Vectorization
• All inputs and targets in a neural network must be tensors of floating-
point data (or, in specific cases, tensors of integers).

• Whatever data you need to process—sound, images, text—you must


first turn into tensors, a step called data vectorization

AI/ML Professional Training Program: INNOQUEST COHORT-1


23
Instructor: Prof. Ahmad Raza Shahid
What is vectorization?
•Replacing iterative operations (e.g., loops) with vectorized implementations
using libraries.

•Improves computational efficiency and speeds up model training and inference.


Z = wx + b

non-vectorized vectorized
Z=0 z = [Link](w,x) + b
For I in range(1-x):
z += w[i] * x[i]
Z +=b
AI/ML Professional Training Program: INNOQUEST COHORT-1
24
Instructor: Prof. Ahmad Raza Shahid
Predicting continuous target variables with regression
analysis

AI/ML Professional Training Program: INNOQUEST COHORT-1


25
Instructor: Prof. Ahmad Raza Shahid
Regression examples
Income and Saving

AI/ML Professional Training Program: INNOQUEST COHORT-1


26
Instructor: Prof. Ahmad Raza Shahid
House sale price

AI/ML Professional Training Program: INNOQUEST COHORT-1


27
Instructor: Prof. Ahmad Raza Shahid
Health insurance

AI/ML Professional Training Program: INNOQUEST COHORT-1


28
Instructor: Prof. Ahmad Raza Shahid
Linear Regression

• goal of linear regression is to model the relationship between one or


multiple features and a continuous target variable.

• Simple linear regression (univariate): find the relationship between a


single feature and continuous-valued target

y = wx + b

AI/ML Professional Training Program: INNOQUEST COHORT-1


29
Instructor: Prof. Ahmad Raza Shahid
AI/ML Professional Training Program: INNOQUEST COHORT-1
30
Instructor: Prof. Ahmad Raza Shahid
Example
• Regression
• Prediction of the price of a used car
• Inputs:
• Brand
• Year
• Engine capacity
• Mileage
• Output
• Price of the car
• Where the output is a number is a regression problem.

AI/ML Professional Training Program: INNOQUEST COHORT-1


31
Instructor: Prof. Ahmad Raza Shahid
Example

• Regression
• Prediction of the price of a used car
• Let denote the car attributes
• Let be the price of the car
• Learns:

AI/ML Professional Training Program: INNOQUEST COHORT-1


32
Instructor: Prof. Ahmad Raza Shahid
Multiple linear regression

• find the relationship between a multiple features and continuous-


valued target

y= +…+ +b

AI/ML Professional Training Program: INNOQUEST COHORT-1


33
Instructor: Prof. Ahmad Raza Shahid
figure 9.2 shows how the two-
dimensional, fitted hyperplane of a
multiple linear regression model
with two features could look

AI/ML Professional Training Program: INNOQUEST COHORT-1


34
Instructor: Prof. Ahmad Raza Shahid
Polynomial regression

• the relationship between the independent variable x and the dependent


variable y is modeled as an nth degree polynomial in x. Polynomial
regression fits a nonlinear relationship between the value of x and the
corresponding conditional mean of y

+ …++b

AI/ML Professional Training Program: INNOQUEST COHORT-1


35
Instructor: Prof. Ahmad Raza Shahid
AI/ML Professional Training Program: INNOQUEST COHORT-1
36
Instructor: Prof. Ahmad Raza Shahid
In the resulting plot, you can see that
the polynomial fit captures the
relationship between the response and
explanatory variables much better
than the linear fit

AI/ML Professional Training Program: INNOQUEST COHORT-1


37
Instructor: Prof. Ahmad Raza Shahid
Overfitting and underfitting

• The fundamental issue in machine learning is the tension between


optimization and generalization.
• Optimization refers to the process of adjusting a model to get the best
performance possible on the training data (the learning in machine
learning), whereas generalization refers to how well the trained model
performs on data it has never seen before.
• The goal of the game is to get good generalization, of course, but you
don’t control generalization; you can only adjust the model based on
its training data

AI/ML Professional Training Program: INNOQUEST COHORT-1


38
Instructor: Prof. Ahmad Raza Shahid
Over-fitting

AI/ML Professional Training Program: INNOQUEST COHORT-1


39
Instructor: Prof. Ahmad Raza Shahid
AI/ML Professional Training Program: INNOQUEST COHORT-1
40
Instructor: Prof. Ahmad Raza Shahid
Over-fitting
• How to avoid overfitting?
• Regularization in machine learning
• Dropout in neural networks

AI/ML Professional Training Program: INNOQUEST COHORT-1


41
Instructor: Prof. Ahmad Raza Shahid
Regularization
• A solution for overfitting
• Add a regularization term R(θ) to the loss function (for now written as
maximizing log prob rather than minimizing loss)

• Idea: choose an R(θ) that penalizes large weights


• fitting the data well with lots of big weights not as good as fitting
the data a little less well, with small weights

AI/ML Professional Training Program: INNOQUEST COHORT-1


42
Instructor: Prof. Ahmad Raza Shahid
Regularization methods for regression

• The most popular approaches to regularized linear regression are:

• ridge regression (L2)

• least absolute shrinkage and selection operator (LASSO) (L1)

AI/ML Professional Training Program: INNOQUEST COHORT-1


43
Instructor: Prof. Ahmad Raza Shahid
Evaluation metrics for regression

• Mean squared error (MSE)


• Root mean squared error (RMSE)
• Mean absolute error (MAE)
• Mean absolute percentage error (MAPE)
• R Square

AI/ML Professional Training Program: INNOQUEST COHORT-1


44
Instructor: Prof. Ahmad Raza Shahid
Predicting discrete target variables with
classification analysis

AI/ML Professional Training Program: INNOQUEST COHORT-1


45
Instructor: Prof. Ahmad Raza Shahid
Articles classification

AI/ML Professional Training Program: INNOQUEST COHORT-1


46
Instructor: Prof. Ahmad Raza Shahid
Spam classification

AI/ML Professional Training Program: INNOQUEST COHORT-1


47
Instructor: Prof. Ahmad Raza Shahid
Image classification

AI/ML Professional Training Program: INNOQUEST COHORT-1


48
Instructor: Prof. Ahmad Raza Shahid
Image classification

AI/ML Professional Training Program: INNOQUEST COHORT-1


49
Instructor: Prof. Ahmad Raza Shahid
Medical image classification

AI/ML Professional Training Program: INNOQUEST COHORT-1


50
Instructor: Prof. Ahmad Raza Shahid
Iris dataset

AI/ML Professional Training Program: INNOQUEST COHORT-1


51
Instructor: Prof. Ahmad Raza Shahid
Logistic regression for classification

52
The two phases of logistic regression

• Training: we learn weights w and b using stochastic gradient


descent and cross-entropy loss.

• Test: Given a test example x we compute p(y|x) using learned


weights w and b, and return whichever label (y = 1 or y = 0) is
higher probability

AI/ML Professional Training Program: INNOQUEST COHORT-1


53
Instructor: Prof. Ahmad Raza Shahid
The problem: z isn't a probability, it's just a number!

• Solution: use a function of z that goes from 0 to 1

AI/ML Professional Training Program: INNOQUEST COHORT-1


54
Instructor: Prof. Ahmad Raza Shahid
The very useful sigmoid or logistic function

AI/ML Professional Training Program: INNOQUEST COHORT-1


55
Instructor: Prof. Ahmad Raza Shahid
Idea of logistic regression

• We’ll compute w∙x+b


• And then we’ll pass it through the sigmoid function:
σ(w∙x+b)
• And we'll just treat it as a probability

AI/ML Professional Training Program: INNOQUEST COHORT-1


56
Instructor: Prof. Ahmad Raza Shahid
Logistic regression model

AI/ML Professional Training Program: INNOQUEST COHORT-1


57
Instructor: Prof. Ahmad Raza Shahid
Making probabilities with sigmoid

AI/ML Professional Training Program: INNOQUEST COHORT-1


58
Instructor: Prof. Ahmad Raza Shahid
By the way:

Because

AI/ML Professional Training Program: INNOQUEST COHORT-1


59
Instructor: Prof. Ahmad Raza Shahid
Turning a probability into a classifier

0.5 here is called the decision boundary

AI/ML Professional Training Program: INNOQUEST COHORT-1


60
Instructor: Prof. Ahmad Raza Shahid
The probabilistic classifier

P(y=1)

wx + b 61
Turning a probability into a classifier

if w∙x+b > 0
if w∙x+b ≤ 0

AI/ML Professional Training Program: INNOQUEST COHORT-1


62
Instructor: Prof. Ahmad Raza Shahid
Logistic regression: a text example on
sentiment classification

63
Sentiment example: does y=1 or y=0?

It's hokey . There are virtually no surprises , and the writing is second-rate .
So why was it so enjoyable ? For one thing , the cast is great . Another nice
touch is the music . I was overcome with the urge to get off the couch and start
dancing . It sucked me in , and it'll do the same to you .

AI/ML Professional Training Program: INNOQUEST COHORT-1


64
Instructor: Prof. Ahmad Raza Shahid
AI/ML Professional Training Program: INNOQUEST COHORT-1 65
Instructor: Prof. Ahmad Raza Shahid
Classifying sentiment for input x

Suppose w =

b = 0.1 66
Classifying sentiment for input x

AI/ML Professional Training Program: INNOQUEST COHORT-1


67
Instructor: Prof. Ahmad Raza Shahid
Cross-Entropy loss

68
Where did the W’s come from?

• Supervised classification:
• We know the correct label y (either 0 or 1) for each x.
• But what the system produces is an estimate,
• We want to set w and b to minimize the distance between our estimate
(i)
and the true y(i).
• We need a distance estimator: a loss function or a cost function
• We need an optimization algorithm to update w and b to minimize
the loss.

AI/ML Professional Training Program: INNOQUEST COHORT-1


69
Instructor: Prof. Ahmad Raza Shahid
Learning components

• A loss function:
• cross-entropy loss

• An optimization algorithm:
• stochastic gradient descent

AI/ML Professional Training Program: INNOQUEST COHORT-1


70
Instructor: Prof. Ahmad Raza Shahid
The distance between and y

• We want to know how far is the classifier output:


• = σ(w∙x+b)

• from the true output:


• y [= either 0 or 1]

• We'll call this difference:


• L(,y) = how much differs from the true y
AI/ML Professional Training Program: INNOQUEST COHORT-1
71
Instructor: Prof. Ahmad Raza Shahid
Intuition of negative log likelihood loss
= cross-entropy loss

• A case of conditional maximum likelihood estimation

• We choose the parameters w,b that maximize the log probability of


the true y labels in the training data given the observations x

AI/ML Professional Training Program: INNOQUEST COHORT-1


72
Instructor: Prof. Ahmad Raza Shahid
Deriving cross-entropy loss for a single observation x

• Goal: maximize probability of the correct label p(y|x)


• Since there are only 2 discrete outcomes (0 or 1) we can express the
probability p(y|x) from our classifier (the thing we want to maximize) as

noting:
• if y=1, this simplifies to
• if y=0, this simplifies to 1-

AI/ML Professional Training Program: INNOQUEST COHORT-1


73
Instructor: Prof. Ahmad Raza Shahid
Deriving cross-entropy loss for a single observation x
Goal: maximize probability of the correct label p(y|x)
Maximize:

• Now take the log of both sides (mathematically handy)

Maximize:

• Whatever values maximize log p(y|x) will also maximize p(y|x)

AI/ML Professional Training Program: INNOQUEST COHORT-1


74
Instructor: Prof. Ahmad Raza Shahid
Deriving cross-entropy loss for a single observation x

Goal: maximize probability of the correct label p(y|x)


Maximize:

• Now flip sign to turn this into a loss: something to minimize


• Cross-entropy loss (because is formula for cross-entropy(y, ))
Minimize:
• Or, plugging in definition of

AI/ML Professional Training Program: INNOQUEST COHORT-1


75
Instructor: Prof. Ahmad Raza Shahid
Let's see if this works for our sentiment example

• We want loss to be:


• smaller if the model estimate is close to correct
• bigger if model is confused
•Let's first suppose the true label of this is y=1 (positive)

It's hokey . There are virtually no surprises , and the writing is second-rate .
So why was it so enjoyable ? For one thing , the cast is great . Another
nice touch is the music . I was overcome with the urge to get off the couch
and start dancing . It sucked me in , and it'll do the same to you .

AI/ML Professional Training Program: INNOQUEST COHORT-1


76
Instructor: Prof. Ahmad Raza Shahid
Let's see if this works for our sentiment example

• True value is y=1. How well is our model doing?

• Pretty well! What's the loss?

AI/ML Professional Training Program: INNOQUEST COHORT-1


77
Instructor: Prof. Ahmad Raza Shahid
Let's see if this works for our sentiment example

• Suppose true value instead was y=0.

• What's the loss?

AI/ML Professional Training Program: INNOQUEST COHORT-1


78
Instructor: Prof. Ahmad Raza Shahid
Let's see if this works for our sentiment example
• The loss when model was right (if true y=1)

• Is lower than the loss when model was wrong (if true y=0):

• Sure enough, loss was bigger when model was wrong!


AI/ML Professional Training Program: INNOQUEST COHORT-1
79
Instructor: Prof. Ahmad Raza Shahid
Stochastic gradient descent

80
Our goal: minimize the loss

• Let's make explicit that the loss function is parameterized by weights


𝛳=(w,b)
• And we’ll represent as f (x; θ ) to make the dependence on θ more
obvious
• We want the weights that minimize the loss, averaged over all examples:

AI/ML Professional Training Program: INNOQUEST COHORT-1


81
Instructor: Prof. Ahmad Raza Shahid
Intuition of gradient descent
• How do I get to the bottom of this river canyon?

Look around me 360∘


Find the direction of
steepest slope down
Go that way

AI/ML Professional Training Program: INNOQUEST COHORT-1


82
Instructor: Prof. Ahmad Raza Shahid
Our goal: minimize the loss
• For logistic regression, loss function is convex
• A convex function has just one minimum
• Gradient descent starting from any point is guaranteed to find the
minimum
• (Loss for neural networks is non-convex)

AI/ML Professional Training Program: INNOQUEST COHORT-1


83
Instructor: Prof. Ahmad Raza Shahid
Let's first visualize for a single scalar w
Q: Given current w, should we make it bigger or smaller?
A: Move w in the reverse direction from the slope of the function

AI/ML Professional Training Program: INNOQUEST COHORT-1


84
Instructor: Prof. Ahmad Raza Shahid
Let's first visualize for a single scalar w
Q: Given current w, should we make it bigger or smaller?
A: Move w in the reverse direction from the slope of the function

So we'll move positive

AI/ML Professional Training Program: INNOQUEST COHORT-1


85
Instructor: Prof. Ahmad Raza Shahid
Let's first visualize for a single scalar w

Q: Given current w, should we make it bigger or smaller?


A: Move w in the reverse direction from the slope of the function

So we'll move positive

AI/ML Professional Training Program: INNOQUEST COHORT-1


86
Instructor: Prof. Ahmad Raza Shahid
Gradients

• The gradient of a function of many variables is a vector pointing in


the direction of the greatest increase in a function.

• Gradient Descent: Find the gradient of the loss function at the current
point and move in the opposite direction.

AI/ML Professional Training Program: INNOQUEST COHORT-1


87
Instructor: Prof. Ahmad Raza Shahid
How much do we move in that direction ?

• The value of the gradient (slope in our example) weighted by a


learning rate η
• Higher learning rate means move w faster

AI/ML Professional Training Program: INNOQUEST COHORT-1


88
Instructor: Prof. Ahmad Raza Shahid
Now let's consider N dimensions

• We want to know where in the N-dimensional space (of the N


parameters that make up θ ) we should move.

• The gradient is just such a vector; it expresses the directional


components of the sharpest slope along each of the N dimensions.

AI/ML Professional Training Program: INNOQUEST COHORT-1


89
Instructor: Prof. Ahmad Raza Shahid
Imagine 2 dimensions, w and b

• Visualizing the gradient


vector at the red point
• It has two dimensions
shown in the x-y plane

AI/ML Professional Training Program: INNOQUEST COHORT-1


90
Instructor: Prof. Ahmad Raza Shahid
Real gradients

• Are much longer; lots and lots of weights


• For each dimension wi the gradient component i tells us the slope with
respect to that variable.
• “How much would a small change in wi influence the total loss
function L?”
• We express the slope as a partial derivative ∂ of the loss ∂wi
• The gradient is then defined as a vector of these partials.

AI/ML Professional Training Program: INNOQUEST COHORT-1


91
Instructor: Prof. Ahmad Raza Shahid
The gradient
We’ll represent as f (x; θ ) to make the dependence on θ more obvious:

The final equation for updating θ based on the gradient is thus

AI/ML Professional Training Program: INNOQUEST COHORT-1


92
Instructor: Prof. Ahmad Raza Shahid
What are these partial derivatives for logistic regression?

The loss function

The elegant derivative of this function (see textbook 5.8 for derivation)

AI/ML Professional Training Program: INNOQUEST COHORT-1


93
Instructor: Prof. Ahmad Raza Shahid
AI/ML Professional Training Program: INNOQUEST COHORT-1
94
Instructor: Prof. Ahmad Raza Shahid
Hyperparameters

• The learning rate η is a hyperparameter


• too high: the learner will take big steps and overshoot
• too low: the learner will take too long
• Hyperparameters:
• Briefly, a special kind of parameter for an ML model
• Instead of being learned by algorithm from supervision (like regular
parameters), they are chosen by algorithm designer.

AI/ML Professional Training Program: INNOQUEST COHORT-1


95
Instructor: Prof. Ahmad Raza Shahid
Stochastic gradient descent: an example
and more details

96
Working through an example

• One step of gradient descent


• A mini-sentiment example, where the true y=1 (positive)
• Two features:
x1 = 3 (count of positive lexicon words)
x2 = 2 (count of negative lexicon words)
Assume 3 parameters (2 weights and 1 bias) in Θ0 are zero:
w1 = w2 = b = 0
η = 0.1

AI/ML Professional Training Program: INNOQUEST COHORT-1


97
Instructor: Prof. Ahmad Raza Shahid
Example of gradient descent w1 = w2 = b = 0;
x1 = 3; x2 = 2
• Update step for update θ is:

• where

• Gradient vector has 3 dimensions:

AI/ML Professional Training Program: INNOQUEST COHORT-1


98
Instructor: Prof. Ahmad Raza Shahid
Example of gradient descent w1 = w2 = b = 0;
x1 = 3; x2 = 2
• Update step for update θ is:

• where
• Gradient vector has 3 dimensions:

AI/ML Professional Training Program: INNOQUEST COHORT-1


99
Instructor: Prof. Ahmad Raza Shahid
Example of gradient descent w1 = w2 = b = 0;
x1 = 3; x2 = 2
• Update step for update θ is:

• where
• Gradient vector has 3 dimensions:

AI/ML Professional Training Program: INNOQUEST COHORT-1


100
Instructor: Prof. Ahmad Raza Shahid
Example of gradient descent w1 = w2 = b = 0;
x1 = 3; x2 = 2
• Update step for update θ is:

• where
• Gradient vector has 3 dimensions:

AI/ML Professional Training Program: INNOQUEST COHORT-1


101
Instructor: Prof. Ahmad Raza Shahid
Example of gradient descent w1 = w2 = b = 0;
x1 = 3; x2 = 2
• Update step for update θ is:

• where
• Gradient vector has 3 dimensions:

AI/ML Professional Training Program: INNOQUEST COHORT-1


102
Instructor: Prof. Ahmad Raza Shahid
Example of gradient descent

Now that we have a gradient, we compute the new parameter vector θ1 by


moving θ0 in the opposite direction from the gradient:

η = 0.1;

AI/ML Professional Training Program: INNOQUEST COHORT-1


103
Instructor: Prof. Ahmad Raza Shahid
Example of gradient descent

Now that we have a gradient, we compute the new parameter vector θ1 by


moving θ0 in the opposite direction from the gradient:

η = 0.1;

AI/ML Professional Training Program: INNOQUEST COHORT-1


104
Instructor: Prof. Ahmad Raza Shahid
Example of gradient descent

Now that we have a gradient, we compute the new parameter vector θ1 by


moving θ0 in the opposite direction from the gradient:

η = 0.1;

AI/ML Professional Training Program: INNOQUEST COHORT-1


105
Instructor: Prof. Ahmad Raza Shahid
Example of gradient descent

Now that we have a gradient, we compute the new parameter vector θ1 by


moving θ0 in the opposite direction from the gradient:
η = 0.1;

Note that enough negative examples would eventually make w 2 negative


AI/ML Professional Training Program: INNOQUEST COHORT-1
106
Instructor: Prof. Ahmad Raza Shahid
Mini-batch training

• Stochastic gradient descent chooses a single random example at a


time.
• That can result in choppy movements
• More common to compute gradient over batches of training instances.
• Batch training: entire dataset
• Mini-batch training: m examples (512, or 1024)

AI/ML Professional Training Program: INNOQUEST COHORT-1


107
Instructor: Prof. Ahmad Raza Shahid
Multinomial logistic regression

108
Multinomial logistic regression
• Often we need more than 2 classes
• Text classification: Positive/negative/neutral
• Disease detection: covid /pneumonia/ normal
• Digit recognition: 0/1/2/3/4/5/6/7/8/9

• If >2 classes we use multinomial logistic regression


= Softmax regression

AI/ML Professional Training Program: INNOQUEST COHORT-1


109
Instructor: Prof. Ahmad Raza Shahid
Multinomial logistic regression

• The probability of everything must still sum to 1

P(positive|doc) + P(negative|doc) + P(neutral|doc) = 1

• Need a generalization of the sigmoid called the softmax


• Takes a vector z = [z1, z2, ..., zk] of k arbitrary values
• Outputs a probability distribution
• each value in the range [0,1]
• all the values summing to 1
AI/ML Professional Training Program: INNOQUEST COHORT-1
110
Instructor: Prof. Ahmad Raza Shahid
The softmax function

Turns a vector z = [z1, z2, ... , zk] of k arbitrary values into probabilities

AI/ML Professional Training Program: INNOQUEST COHORT-1


111
Instructor: Prof. Ahmad Raza Shahid
The softmax function
• Turns a vector z = [z1,z2,...,zk] of k arbitrary values into
probabilities

AI/ML Professional Training Program: INNOQUEST COHORT-1


112
Instructor: Prof. Ahmad Raza Shahid
Softmax in multinomial logistic regression

Input is still the dot product between weight vector w and input vector x
But now we’ll need separate weight vectors for each of the K classes.

AI/ML Professional Training Program: INNOQUEST COHORT-1


113
Instructor: Prof. Ahmad Raza Shahid
Features in binary versus multinomial logistic regression

• Binary: positive weight  y=1 neg weight  y=0

w5 = 3.0

• Multinominal: separate weights for each class:

AI/ML Professional Training Program: INNOQUEST COHORT-1


114
Instructor: Prof. Ahmad Raza Shahid
AI/ML Professional Training Program: INNOQUEST COHORT-1
115
Instructor: Prof. Ahmad Raza Shahid
Choosing a measure of success

• For balanced-classification problems, where every class is equally


likely, accuracy and area under the receiver operating characteristic
curve (ROC AUC) are common metrics.
• For class-imbalanced problems, you can use precision and recall.
• For ranking problems or multilabel classification, you can use mean
average precision.
• And it isn’t uncommon to have to define your own custom metric by
which to measure success

AI/ML Professional Training Program: INNOQUEST COHORT-1


116
Instructor: Prof. Ahmad Raza Shahid
Reference

• Machine Learning with PyTorch and Scikit-Learn (Sebastian Raschka,


Yuxi Liu, Vahid Mirjalili)
• Machine Learning (Tom M. Mitchell)
• Pattern Recognition and Machine Learning (Christopher M. Bishop)
• Stanford lectures (dan jurafsky)

AI/ML Professional Training Program: INNOQUEST COHORT-1


117
Instructor: Prof. Ahmad Raza Shahid
Thank you

118

You might also like