Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning...

29
Fundamentals of Machine Learning Mohammad Emtiyaz Khan AIP (RIKEN), Tokyo http://icapeople.epfl.ch/mekhan/ [email protected] Jan 20, 2017 ©Mohammad Emtiyaz Khan 2017 1

Transcript of Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning...

Page 1: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Fundamentals ofMachine Learning

Mohammad Emtiyaz KhanAIP (RIKEN), Tokyo

http://icapeople.epfl.ch/mekhan/

[email protected]

Jan 20, 2017

©Mohammad Emtiyaz Khan 2017

1

Page 2: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Goals

Understand (some) fundamentals of Machine learning1.

Part I : Understand the basic set-up to analyzedata under a machine-learning framework.

1. Before Machine Learning.

2. ML Problem: Regression.

3. Model: Linear Regression.

4. Cost Function: MSE.

5. Algorithm: Gradient Descent.

Part II : Understand what can go wrong whenlearning from data and how to correct it.

6. Challenge: Overfitting.

7. Solutions: Regularization.

8. Bias-Variance Decomposition.

9. Recent Advances.

1Some figures are taken from Hastie, Tibshirani, and Friedman’s book onstatistical learning and also from Chris bishop’s Machine learning book

1

Page 3: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

1 Before Machine Learning

Acquiring Data

Data is the most important com-ponent of modern Machine Learn-ing. There are many importantsteps that can have a huge impacton the performance of a machine-learning system. To name a few:data collection, cleaning, validation,pre-processing, and storage.

Picture taken from “Doing data science”.

2

Page 4: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Defining an ML problem

Once we have some data, the nextstep is to re-define the real-worldproblem in the context of data, andthen to convert it to a machine-learning problem.

ML problems can be categorizedinto 3 main types: supervised, un-supervised, and reinforcement learn-ing. In practice, a successful end-to-end system might require a combi-nation of these problems.

3

Page 5: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

2 ML Problem: Regression

What is regression?

Regression is to relate input vari-ables to the output variable, to ei-ther predict outputs for new inputsand/or to understand the effect ofthe input on the output.

Dataset for regression

In regression, data consists of pairs(yn,xn), where yn is the n’th out-put and xn is a vector of D inputs.Number of pairs N is the data-sizeand D is the dimensionality.

Examples of regression

(a) Height is correlated with weight. Taken from

“Machine Learning for Hackers”

4

Page 6: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

(b) How does advertisement in TV, radio, and newspaper affect sales? Taken from the book ”An

Introduction to statistical learning”

Two goals of regression

In prediction, we wish to predictthe output for a new input vector,e.g. what is the weight of a personwho is 170 cm tall?

In interpretation, we wish to under-stand the effect of inputs on output,e.g. are taller people heavier too?

The regression function

For both the goals, we need to find afunction that approximates the out-put “well enough” given inputs.

yn ≈ f (xn), for all n

5

Page 7: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Additional Notes

Prediction vs Interpretation

Some questions to think about: are these prediction tasks or interpreta-

tion task?

1. What is the life-expectancy of a person who has been smoking for

10 years?

2. Does smoking cause cancer?

3. When the number of packs a smoker smokes per day doubles, their

predicted life span gets cut in half?

4. A massive scale earthquake will occur in California within next

30 years.

5. More than 300 bird species in north America could reduce their

habitat by half or more by 2080.

6

Page 8: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

3 Model: Linear Regression

What is it?

Linear regression is a model that as-sumes a linear relationship betweeninputs and the output.

Why learn about linear regression?

Plenty of reasons: simple, easy tounderstand, most widely used, eas-ily generalized to non-linear mod-els. Most importantly, you can learnalmost all fundamental concepts ofML with regression alone.

7

Page 9: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Simple linear regression

With only one input dimension, it issimple linear regression.

yn ≈ f (xn) := β0 + β1xn1

Here, β0 and β1 are parameters ofthe model.

Multiple linear regression

With multiple input dimension, it ismultiple linear regression.

yn ≈ f (xn)

:= β0 + β1xn1 + . . . + βDxnD

= xTnβ (1)

Learning/estimation/fitting

Given data, we would like to findβ = [β0, β1, . . . , βD]. This is calledlearning or estimating the parame-ters or fitting the model.

8

Page 10: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Additional Notes

p > n Problem

Consider the following simple situation: You have N = 1 and you want

to fit y1 ≈ β0 + β1x11, i.e. you want to find β0 and β1 given one pair

(y1, x11). Is it possible to find such a line?

This problem is related to something called p > n problem. In our nota-

tion, this will be called D > N problem, i.e. the number of parameters

exceeds number of data examples.

Similar issues will arise when we use gradient descent or least-squares to

fit a linear model. These problems are all solved by using regularization,

which we will learn later.

9

Page 11: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

4 Cost Function: MSE

MotivationConsider the following models.

1-parameter model: yn ≈ β0

2-parameter model: yn ≈ β0 + β1xn1

How can we estimate (or guess) val-ues of β given the data D?

What is a cost function?Cost functions (or utilities or en-ergy) are used to learn parametersthat explain the data well. They de-fine how costly our mistakes are.

Two desirable properties of cost functions

When y is real-valued, it is desirablethat the cost is symmetric around0, since both +ve and -ve errorsshould be penalized equally.

Also, our cost function should pe-nalize “large” mistakes and “very-large” mistakes almost equally.

10

Page 12: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Mean Square Error (MSE)

MSE is one of the most popular costfunction.

MSE(β) :=1

2N

N∑n=1

[yn − f (xn)]2

Does it have both the properties?

An exercise for MSECompute MSE for 1-param model:

L(β0) :=1

2N

N∑n=1

[yn − β0]2 (2)

Each row contains a yn and columnis β0. First, compute MSE for foryn = {1, 2, 3, 4} and draw MSE asa function of β0 (by adding the firstfour rows). Then add yn = 20 toit, and redraw MSE. What do youobserve and why?

yn\β0 1 2 3 4 5 6 7

1

2

3

4

MSE

20

MSE

11

Page 13: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Additional Notes

A question for cost functions

Is there an automatic way to define loss functions?

Nasty cost functions: Visualization

See Andrej Karpathy Tumblr post for many cost functionsgone “wrong” for neural network. http://lossfunctions.tumblr.com/.

12

Page 14: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

5 Algorithm: Gradient Descent

Learning/estimation/fitting

Given a cost function L(β), we wishto find β∗ that minimizes the cost:

minβL(β), subject to β ∈ RD+1

This is learning posed as an opti-mization problem. We will use analgorithm to solve the problem.

Grid searchGrid search is one of the simplestalgorithms where we compute costover a grid (of say M points) to findthe minimum. This is extremelysimple and works for any kind ofloss when we have very few param-eters and the loss is easy to compute.

For a large number of parameters,however, grid search has too many“for-loops”, resulting in exponentialcomputational complexity. Choos-ing a good range of values is anotherproblem.

Are there any other issues?

13

Page 15: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Follow the gradient

A gradient (at a point) is the slopeof the tangent (at that point). Itpoints to the direction of largestincrease of the function.

For 2-parameter model, MSE isshown below.(I used yT = [2,−1, 1.5] and xT = [−1, 1,−1]).

−10−5

05

10

−10−5

05

100

20

40

60

80

100

120

140

160

β0

β1

14

Page 16: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Batch gradient descent

To minimize the function, take astep in the (opposite) direction ofthe gradient

β(k+1) ← β(k) − α∂L(β(k))

∂β

where α > 0 is the step-size (orlearning rate).

Gradient descent for 1-parametermodel to minimize MSE:

β(k+1)0 = (1− α)β

(k)0 + αy

Where y =∑

n yn/N . When is thissequence guaranteed to converge?

Gradients for MSE

L(β) =1

2N

N∑n=1

(yn − xTnβ)2 (3)

then the gradient is given by,

∂L∂β

= − 1

N

N∑n=1

(yn − xTnβ)xn (4)

What is the computational complex-ity of batch gradient descent?

15

Page 17: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Stochastic gradient descent

When N is large, choose a randompair (xi, yi) in the training set andapproximate the gradient:

∂L∂β≈ − 1

N

[N(yi − xTi β)xi

](5)

Using the above “stochastic” gradi-ent, take a step:

β(k+1) = β(k) + α(k)(yi − xTi β(k))xi

What is the computational com-plexity?

For convergence, αk → 0 “appro-priately”. One such condition calledRobbins-Monroe condition suggeststo take αk such that:

∞∑k=1

α(k) =∞,∞∑k=1

(α(k))2 <∞

(6)

One way to obtain such sequenceis α(k) = 1/(1 + k)r where r ∈(0.5, 1).

16

Page 18: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

6 Challenge: Overfitting

MotivationLinear model can be easily modifiedto obtain more powerful non-linearmodel. We can use basis functionexpansion to get a non-linear regres-sion model, and then use a sequenceof these models to construct a deepmodel.

Consider simple linear regression.Given one-dimensional input xn, wecan generate a polynomial basis.

φ(xn) = [1, xn, x2n, x

3n, . . . , x

Mn ]

Then we fit a linear model using theoriginal and the generated features:

yn ≈ β0 +β1xn+β2x2n+ . . .+βMx

Mn

−1 0 1−1

−0.5

0

0.5

1

17

Page 19: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Overfitting and Underfitting

Overfitting is fitting the noise in ad-dition to the signal. Underfitting isnot fitting the signal well. In reality,it is very difficult to be able to tellthe signal from the noise.

Which is a better fit?

Try a real situation. Below, y-axis isthe frequency of an event and x-axisis the magnitude. It is clear that asmagnitude increases, frequency de-creases.

This example is taken from Nate Silver’s book.

18

Page 20: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Which model is a better fit? blue or red?

Another example: Which model isa better fit? black or red? Data isdenoted by circle.

19

Page 21: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Complex models overfit easily

Circles are data points, green line is the truth & red line isthe model fit. M is the maximum degree in the generatedpolynomial basis.

x

t

M = 0

0 1

−1

0

1

x

t

M = 1

0 1

−1

0

1

x

t

M = 3

0 1

−1

0

1

x

t

M = 9

0 1

−1

0

1

If you increase the amount of data, overfitting might reduce.

x

t

N = 15

0 1

−1

0

1

x

t

N = 100

0 1

−1

0

1

20

Page 22: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Occam’s razorOne solution is dictated by Occam’srazor which states that “Simplermodels are better – in absence ofcertainty.”

Sometimes, if you increase theamount of data, you might reduceoverfitting. But, when unsure,choose a simple model over a com-plicated one.

Additional Notes

Read about overfitting in the paper by Pedro Domingos (section 3 and 5

of “A few useful things to know about machine learning”). You can also

read Nate Silver’s book on “The signal and the noise” (the earthquake

example is taken from this book).

21

Page 23: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

7 Solutions: Regularization

What is regularization?

Through regularization, we can pe-nalize complex models and favorsimpler ones:

minβ

L(β) +λ

2N

M∑j=1

β2j

The second term is a regularizer(with λ > 0). The main point hereis that an input variable weighted bya small βj will have less influence onthe output.

Regularization Parameter

The parameter λ can be tuned toreduce overfitting. But, how do youchoose λ?

The generalization error

The generalization error of a learn-ing method is the expected predic-tion error for unseen data, i.e. mis-takes made on the data that we aregoing to see in the future. Thisquantifies how well the method gen-eralizes.

22

Page 24: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Simulating the future

Ideally, we should choose λ to mini-mize the mistakes that will be madein the future. Obviously, we do nothave the future data, but we can al-ways simulate the future using thedata in hand.

Splitting the data

For this purpose, we split the datainto train and validation sets, e.g.80% as training data and 20% asvalidation data. We pretend thatthe validation set is the future data.We fit our model on the training setand compute a prediction-error onthe validation set. This gives us anestimate of the generalization error(one instant of the future).

10−2

10−1

100

101

1.16

1.17

1.18

lambda

Te

st

RM

SE

10−3

10−2

10−1

100

101

1

1.05

1.1

lambda

Tra

in R

MSE

23

Page 25: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Cross-validationRandom splitting is not an efficientmethod.

K-fold cross-validation allows us todo this efficiently. We randomlypartition the data into K groups.We train on K − 1 groups and teston the remaining group. We re-peat this until we have tested on allK sets. We then average the results.

run 1

run 2

run 3

run 4

Cross-validation returns an estimateof the generalization error.

Additional Notes

Details on cross-validation are in Chapter 7 in the book by Hastie, Tib-

shirani, and Friedman (HTF). You can also read about bootstrap in

Section 7.11 in HTF book. This method is related to random splitting

and is a very popular method.

24

Page 26: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

8 Bias-Variance Decomposition

What is bias-variance?

One natural question is how doesthe test error vary wrt λ? Whenλ is high, the model underfits,while when λ is small, the modeloverfits. Therefore, a good value issomewhere in between.

Bias-variance decomposition ex-plains the shape of this curve.

Generalization error

Given training data Dtr of size N ,we would like to estimate the ex-pected error made in future predic-tion. This error is the generalizationerror. Below is a definition supposethat we have infinite test data Dte,

teErr(Dtr) := EDte[{y − f (x)}2]

Generalization error is different fromthe training error which measureshow well you fit the data.

trErr(Dtr) :=

N∑n=1

[{yn − f (xn)}2]

25

Page 27: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Errors vs model complexity

As we increase the model complex-ity, how do these errors vary? Theblue line shows training error for adataset with N = 50, while the redline shows the generalization errorfor that dataset.

Simple model have high train andgeneralization error since they havea high bias, while complex modelhave low train but high generaliza-tion error because they have highvariance.

Elements of Statistical Learning (2nd Ed.) c©Hastie, Tibshirani & Friedman 2009 Chap 7

0 5 10 15 20 25 30 35

0.0

0.2

0.4

0.6

0.8

1.0

1.2

Model Complexity (df)

Pred

ictio

n Er

ror

High Bias Low BiasHigh VarianceLow Variance

FIGURE 7.1. Behavior of test sample and trainingsample error as the model complexity is varied. Thelight blue curves show the training error err, while thelight red curves show the conditional test error ErrTfor 100 training sets of size 50 each, as the model com-plexity is increased. The solid curves show the expectedtest error Err and the expected training error E[err].

26

Page 28: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

Bias-variance decompo-sition

The shape of these curves can be ex-plained using bias-variance decom-position. The following four pointscan be explained by using the de-composition:

1. both bias and variance con-tribute to generalization error.

2. For bias, both model-bias andestimation-bias are important.When we increase model com-plexity, we increase general-ization error due to increasedvariance.

3. Regularization increases esti-mation bias while reducingvariance.

27

Page 29: Fundamentals of Machine Learning - GitHub Pages · 2021. 7. 26. · 1 Before Machine Learning Acquiring Data Data is the most important com-ponent of modern Machine Learn-ing. There

9 Recent Advances

Deep Learning & Over-fitting

Deep learning has shown a new (butold) way to combat overfitting. Formany applications, more data anddeep architecture combined withstochastic gradient-descent is ableto get us to a good minimum whichgeneralizes well.

Challenges

There are many challenges ahead.Learning from nasty, unreliabledata still remains a challenge (e.g.small sample size, redundant data,non-stationary data, sequentiallearning).

On the other hand, living beings -even young ones - are very good indealing with such data. How do theydo it, and how can we design MLmethods that can learn like them?

28