15-884/ machine learning 3: classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1...

33
15-884/ – Machine Learning 3: Classification J. Zico Kolter September 17, 2013 1

Upload: others

Post on 01-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

15-884/ – Machine Learning 3:Classification

J. Zico Kolter

September 17, 2013

1

Page 2: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

Classification

• Regression: predict continuous-valued outputs (yi ∈ R)

• Classification: predict discrete-valued outputs

– Common case, binary classification: yi ∈ {−1, 1}

2

Page 3: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

• Binary classification: predictions have yes/no answers

– Will the peak demand in be higher than 2GW tomorrow?

– Will a wind turbine operate at max capacity in the next hour?

– Will this electric line reach its maximum capacity?

– Is the the device plugged into this outlet a refrigerator?

• Even when predicting a numerical quantity, what we really careabout is often the answer to a yes/no question

3

Page 4: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

Understanding building energy consumption

Buildings (residential and commercial) account for 71% of electricity

consumption [Source: US EIA]

4

Page 5: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

• The task: automatically identify appliances from their(individual) power signals

• Feedback about building energy consumption allows users tomake more informed decisions

• Modified but similar techniques can be used to identifyappliances from just whole-building energy signals

5

Page 6: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

1000 2000 3000 4000 5000 6000 70000

50

100

150

200

250

Time (seconds)

Pow

er (

wat

ts)

Power signal for refrigerator 1

6

Page 7: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

1000 2000 3000 4000 5000 6000 70000

50

100

150

200

250

Time (seconds)

Pow

er (

wat

ts)

Power signal for refrigerator 2

7

Page 8: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

1000 2000 3000 4000 5000 6000 70000

50

100

150

200

250

Time (seconds)

Pow

er (

wat

ts)

Constructing inputs from power signal

8

Page 9: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

150 160 170 180 190 200 210

−1

−0.5

0

0.5

1

Power (watts)

Out

put

Classifying fridge 1 vs. fridge 2 using power as input

9

Page 10: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

150 160 170 180 190 200 210

500

1000

1500

2000

2500

Power (watts)

Dur

atio

n (s

econ

ds)

Fridge 1Fridge 2

Classifying fridge 1 vs. fridge 2 using power and duration as inputs

10

Page 11: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

150 160 170 180 190 200 210

500

1000

1500

2000

2500

Power (watts)

Dur

atio

n (s

econ

ds)

Fridge 1Fridge 2Classifier boundary

Classification boundary from a linear classifier (here, a supportvector machine)

11

Page 12: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

Formal problem setting

• Input: xi ∈ Rn, i = 1, . . . ,m

• Output: yi ∈ {−1,+1} (binary classification task)

• Feature mapping: φ : Rn → Rk

• Model Parameters: θ ∈ Rk

• Predicted output: yi ∈ R = θTφ(xi)

– Intuition: for y = +1 we want y > 0, for y = −1, y < 0

12

Page 13: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

Loss functions• Loss function: ` : R× {−1,+1} → R+

– Again, `(y, y) measures how “good” the prediction is

• 0-1 Loss

`(y, y) =

{0 if y = +1 and y ≥ 0, or if y = −1 and y ≤ 01 otherwise

=

{0 y · y ≥ 01 y · y < 0

≡ 1{y · y < 0}

−3 −2 −1 0 1 2 30

1

2

3

4

y × ypred

Loss

13

Page 14: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

• Unfortunately, hard to optimize 0-1 loss

• Many other loss functions are used in practice

Hinge loss: `(y, y) = max{1− y · y, 0}Squared hinge loss: `(y, y) = max{1− y · y, 0}2

Logistic loss: `(y, y) = log(1 + e−y·y)

Exponential loss: `(y, y) = e−y·y

14

Page 15: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

−3 −2 −1 0 1 2 30

0.5

1

1.5

2

2.5

3

3.5

4

y × ypred

Loss

0−1 LossHinge LossLogistic LossExponential Loss

Common loss functions for classification

15

Page 16: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

Some typical classification algorithms

• Logistic Regression: minimize logistic loss

minimizeθ

m∑i=1

log(1 + exp

(−yi · θTφ(xi)

))– Probabilistic interpretation: p(yi = +1|xi) = 1

1+exp(−θTφ(xi))

• Support vector machine (SVM): minimize (regularized) hingeloss

minimizeθ

m∑i=1

max{

0, 1− yi · θTφ(xi)}

+ λ‖θ‖22

– If you’ve seen SVMs before, you may have seen them describedgeometrically in terms of maximizing the margin of the linearclassifier; that formulation is equivalent to the above

16

Page 17: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

• YALMIP code for logistic regression

theta = sdpvar(n,1);

solvesdp([], sum(log(1+exp(-y.*(Phi*theta)))));

• YALMIP code for SVM

theta = sdpvar(n,1);

solvesdp([], sum(max(0,1-y.*(Phi*theta))) + ...

lambda*norm(theta)^2);

17

Page 18: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

150 160 170 180 190 200 210

500

1000

1500

2000

2500

Power (watts)

Dur

atio

n (s

econ

ds)

Fridge 1Fridge 2Classifier boundary

Classification boundary from a support vector machine

18

Page 19: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

Optimizing loss functions in classification

• YALMIP is great for rapid prototyping, and medium-sizeproblems

• For larger problems, we might prefer specialized solutionmethods, like we used for least-squares

• Logistic regression:

J(θ) =m∑i=1

log(1 + exp

(−yi · θTφ(xi)

))– Differentiable, but cannot analytically solve for ∇θJ(θ) = 0

19

Page 20: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

Newton’s method

• Newton’s method is a root-finding algorithm

– i.e., for some vector-input, vector output function f : Rn → Rn,it finds z ∈ Rn such that f(z) = 0

• 1D case: f : R→ R– Given some initial z, repeat z ← z − f(z)/f ′(z);

20

Page 21: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

• To extend to the multi-variate case, for a function f : Rn → Rmwe’ll define the Jacobian Dzf(z),

Dzf(z) ∈ Rm×n =

∂f1(z)∂z1

· · · ∂f1(z)∂zn

.... . .

...∂fm(z)∂z1

· · · ∂fm(z)∂zn

• Jacobian is like the gradient, but also defined for vector-valued

functions

– For scalar valued functions, Dzf(z) ∈ R1×n = (∇zf(z))T

• Multi-variate Newton’s method: for f : Rn → Rn

Repeat: z ← z − (Dzf(z))−1f(z)

21

Page 22: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

• Newton’s method applied to optimization: apply Newton’smethod to find z such that ∇zf(z) = 0

• The Hessian is a matrix of second derivatives of a real-valuedfunction f : Rn → R

∇2zf(z) ∈ Rn×n =

∂2f(z)∂z21

· · · ∂2f(z)∂z1∂zn

.... . .

...∂2f(z)∂zn∂z1

· · · ∂2f(z)∂z2n

= Dz(∇zf(z)) (Jacobian of the gradient)

• Newton’s method update:

Repeat: z ← z − (∇2zf(z))−1∇zf(z)

22

Page 23: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

• Logistic regression:

J(θ) =

m∑i=1

log(1 + exp(−yi · θTφ(xi)))

• Gradient and Hessian given by:

∇θJ(θ) = −ΦTZy

∇2θJ(θ) = ΦTZ(I − Z)Φ

where

Z ∈ Rm×mdiagonal, Zii =1

1 + exp(yi · θTφ(xi))

23

Page 24: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

• MATLAB code for logistic regression

function theta = logreg(Phi,y)

k = size(Phi,2);

theta = zeros(k,1);

g = 1;

while (norm(g) > 1e-10)

z = 1./(1 + exp(y.*(Phi*theta)));

g = -Phi'*(z.*y);

H = Phi'*diag(z.*(1-z))*Phi;

theta = theta - H \ g;

end

• YALMIP code: 20.9 seconds, logreg.m: 0.016 seconds(m = 300, n = 3)

• SVMs are a bit harder to optimize with custom routines; lots offree libraries available (libsvm, svm-light)

24

Page 25: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

150 160 170 180 190 200 210

500

1000

1500

2000

2500

Power (watts)

Dur

atio

n (s

econ

ds)

Newton’s method, iteration 1

25

Page 26: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

150 160 170 180 190 200 210

500

1000

1500

2000

2500

Power (watts)

Dur

atio

n (s

econ

ds)

Newton’s method, iteration 2

26

Page 27: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

150 160 170 180 190 200 210

500

1000

1500

2000

2500

Power (watts)

Dur

atio

n (s

econ

ds)

Newton’s method, iteration 3

27

Page 28: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

150 160 170 180 190 200 210

500

1000

1500

2000

2500

Power (watts)

Dur

atio

n (s

econ

ds)

Newton’s method, iteration 10

28

Page 29: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

2 4 6 8 1010

−10

10−5

100

105

Iteration

Nor

m o

f gra

dien

t

Progress of Newton’s method

29

Page 30: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

Non-linear classification

• Same idea as for linear regression: non-linear features, eitherexplicit or using kernels

120 140 160 180 200 220 240

500

1000

1500

2000

2500

Power (watts)

Dur

atio

n (s

econ

ds)

Other devicesRefrigerator

Classifying refrigerator vs. all other devices

30

Page 31: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

• Key component of kernels is still just the replacement

θ =

m∑j=1

αjφ(xj)

• YALMIP code for kernelized SVM:

K = (X*X' + 1).^d; % polynomial kernel

K = exp(-sqdist(X',X')/(2*sig^2)); % Gaussian kernel

alpha = sdpvar(m,1);

solvesdp([], lambda*alpha'*K*alpha + ...

sum(max(1 - y.*(K*alpha), 0)));

• Can derive Newton’s method for kernelized formulation as well

31

Page 32: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

120 140 160 180 200 220 240

500

1000

1500

2000

2500

Power (watts)

Dur

atio

n (s

econ

ds)

Kernelized SVM, Gaussian kernel

32

Page 33: 15-884/ Machine Learning 3: Classificationzkolter/course/15-884/ml_classification.pdf · i=1 log 1 + exp y i T˚(x i) {Probabilistic interpretation: p(y i= +1jx i) = 1 1+exp( T ˚(x

120 140 160 180 200 220 240

500

1000

1500

2000

2500

Power (watts)

Dur

atio

n (s

econ

ds)

Kernelized SVM, Gassian kernel (smaller bandwidth)

33