svm - cs.cmu.eduaarti/class/10315_fall19/recitations/recitation_6.pdf · 9...

30
SVM Review Siddharth Ancha Slides from Aarti’s Lectures

Upload: others

Post on 18-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

SVM ReviewSiddharth Ancha

Slides from Aarti’s Lectures

Page 2: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

At#Pittsburgh#G!20#summit#…

3

Page 3: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Linear'classifiers'– which%line%is%better?

4

Page 4: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Pick%the%one%with%the%largest%margin!

5

Page 5: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Parameterizing+the+decision+boundary

6

w.x+"b"="0

w.x +"b">"0 w.x +"b"<"0

w.x ="!j w(j) x(j)

Page 6: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Parameterizing the$decision$boundary

7

w.x+"b"="0

w.x +"b">"0 w.x +"b"<"0

Page 7: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Maximizing)the)margin

8

margin'='! ="2a/ǁwǁw.x+"b"="0

w.x +"b">"0 w.x +"b"<"0w.x +

+"b"="a

w.x !

+"b"="!a

!

Distance)of)closest)examples)from%the%line/hyperplane

Step%1:"w"is#perpendicular#to#lines#since#for#any#x1,"x2on#line##w.(x1 – x2)"="0

x1

x2

w.x+"b"="0

Page 8: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Maximizing)the)margin

9

w.x+"b"="0

w.x +"b">"0 w.x +"b"<"0w.x +

+"b"="a

w.x !

+"b"="!a

!

Step1:w"is#perpendicular#to#lines

Step%2:"Take"a"point"x! on#w.x +b#=#!a"and"move"to"point&x+ that$is$! away$on$line%w.x+b ="a"

x+ ="x! +"!w/ǁwǁw.x+ ="w.x! +"!w."w/ǁwǁ

a!b ="!a!b"+"!ǁwǁ2a#=#!ǁwǁ

margin'='! ="2a/ǁwǁ

Page 9: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Maximizing)the)margin

10

w.x+"b"="0

w.x +"b">"0 w.x +"b"<"0

margin'='! ="2a/ǁwǁ

Distance)of)closest)examples)from%the%line/hyperplane

w.x +

+"b"="a

w.x !

+"b"="!a

! Smaller'margin'! larger&ǁwǁ

Page 10: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Maximizing)the)margin

11

w.x+"b"="0

w.x +"b">"0 w.x +"b"<"0

max$$! ="2a/ǁwǁw,b

s.t."(w.xj+b)"yj ≥"a !j"

margin'='! ="2a/ǁwǁ

Note: ‘a’$is$arbitrary$(can$normalize$equations*by*a)

Distance)of)closest)examples)from%the%line/hyperplane

w.x +

+"b"="a

w.x !

+"b"="!a

!

Page 11: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Support'Vector'Machines

12

w.x +"b">"0 w.x +"b"<"0

min$$w.ww,b

s.t."(w.xj+b)"yj ≥"1 !j"

Solve&efficiently&by&quadratic&programming)(QP)– Quadratic)objective, linear'

constraints– Well!studied'solution'

algorithms

w.x+"b"="0

w.x +

+"b"="1

w.x !

+"b"="!1

!

Page 12: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Support'Vectors

13

w.x +"b">"0 w.x +"b"<"0Linear'hyperplane defined&by&“support'vectors”

Moving'other'points a"little"doesn’t(effect(the(decision(boundary)

only%need%to%store%the%support'vectors'to'predict'labels&of&new&points

For$support$vectors(w.xj+b)"yj ="1

Page 13: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

What%if%data%is%still%not%linearly%separable?

16

min$$w.w +"C"Σξjw,b,{ξj}"

s.t."(w.xj+b)"yj ≥"1!ξj !jξj ≥"0 !j

j

Allow%“error”%in%classification

ξj" ! “slack”(variables(="(>1"if"xj misclassifed)

pay$linear$penalty$if$mistakeC""! tradeoff(parameter((chosen(by(

cross!validation)Still%QP%!

Soft margin approach

Page 14: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Soft!margin'SVM

17

(w.xj+b)"yj ≥"1!ξj !jξj ≥"0 !j

Soften'the'constraints:

Penalty(for(misclassifying:

C"ξj

How do we recover hardmargin SVM?

Set$C$=$∞w.x+"b"="1

w.x+"b"="!1

Page 15: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

18

w.x+"b"="1

w.x+"b"="!1

Notice'that

Slack&variables&– Hinge&loss

Page 16: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Slack&variables&– Hinge&loss

19

Hinge&loss

0!1"loss

0!1 1

min$$w.w +"C"Σξjw,b,{ξj}"

s.t."(w.xj+b)"yj ≥"1!ξj !jξj ≥"0 !j

j

Regularized+hinge+loss

min$$w.w +"C"Σ(1!(w.xj+b)yj)+w,b j

Page 17: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

SVM$vs.$Logistic$Regression

21

SVM : Hinge&loss

0!1"loss

0!1 1

Logistic(Regression :"Log$loss$ ("!ve log$conditional$likelihood)

Hinge&lossLog$loss

Page 18: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

SVM$– linearly(separable(case

• Convex'quadratic'program'– quadratic)objective,)linear)constraints

• But$expensive$to$solve$if$d$is$very$large• Often&solved&in&dual&form&(n!dim$problem)

2

w – weights on features (d-dim problem)

n"training"points (x1,"…,"xn)"d"features xj is#a#d!dimensional*vector*

• Primal'problem:

w.x+"b"="0

Page 19: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Constrained+Optimization

3

Constraint)inactive Constraint)active)and$tight

x⇤ = max(b, 0)

Page 20: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Constrained+Optimization+– Dual%Problem

4

Moving'the'constraint'to'objective'functionLagrangian:

Dual%problem:

! ="0"constraint"is"inactive! >"0""constraint"is"active

b"+ve

Primal'problem:

Page 21: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Connection(between(Primal(and(Dual

5

Dual%problem:%d*#=

Primal'problem:'p* =

=

Notice'that

=

Why?

Page 22: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Connection(between(Primal(and(Dual

7

Primal'problem:'p* ="

! Weak%duality:%The$dual$solution$d*$lower$bounds$the$primal$solution(p*(i.e.(d*(≤((p*

To#see#this,#recall#

For$every$feasible$x$(i.e.$x$≥$b)$and$feasible$α"(i.e.%α"≥"0)","notice""that

d(α)#=" ≤""x2 – !(x!b)#≤##x2

Since&this&holds&for&all&feasible&x,#in#particular#it#holds#for#x*#####achieving)the)min)of)p*,)hence))d(a) ≤"p*"for"all"feasible"α"≥"0.

Dual%problem:%d*#=

Page 23: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Connection(between(Primal(and(Dual

8

Primal'problem:'p* ="

! Weak%duality:%The$dual$solution$d*$lower$bounds$the$primal$solution(p*(i.e.(d*(≤((p*

Dual%problem:%d*#=

! Strong'duality:'d*#=#p*#holds#often#for#many#problems#of#interest'e.g.'if'the'primal'is'a'feasible'convex'objective'with'linear'constraints

Page 24: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

9

Connection(between(Primal(and(DualWhat does strong duality say about ↵⇤ (the ↵ that achieved optimal value ofdual) and x⇤ (the x that achieves optimal value of primal problem)?

Whenever strong duality holds, the following conditions (known as KKT con-ditions) are true for ↵⇤ and x⇤:

• 1. 5L(x⇤,↵⇤) = 0 i.e. Gradient of Lagrangian at x⇤ and ↵⇤ is zero.

• 2. x⇤ � b i.e. x⇤ is primal feasible

• 3. ↵⇤ � 0 i.e. ↵⇤ is dual feasible

• 4. ↵⇤(x⇤ � b) = 0 (called as complementary slackness)

We use the first one to relate x⇤ and ↵⇤. We use the last one (complimentaryslackness) to argue that ↵⇤ = 0 if constraint is inactive and ↵⇤ > 0 if constraintis active and tight.

What does strong duality say about ↵⇤ (the ↵ that achieved optimal value ofdual) and x⇤ (the x that achieves optimal value of primal problem)?

Whenever strong duality holds, the following conditions (known as KKT con-ditions) are true for ↵⇤ and x⇤:

• 1. 5L(x⇤,↵⇤) = 0 i.e. Gradient of Lagrangian at x⇤ and ↵⇤ is zero.

• 2. x⇤ � b i.e. x⇤ is primal feasible

• 3. ↵⇤ � 0 i.e. ↵⇤ is dual feasible

• 4. ↵⇤(x⇤ � b) = 0 (called as complementary slackness)

We use the first one to relate x⇤ and ↵⇤. We use the last one (complimentaryslackness) to argue that ↵⇤ = 0 if constraint is inactive and ↵⇤ > 0 if constraintis active and tight.

What does strong duality say about ↵⇤ (the ↵ that achieved optimal value ofdual) and x⇤ (the x that achieves optimal value of primal problem)?

Whenever strong duality holds, the following conditions (known as KKT con-ditions) are true for ↵⇤ and x⇤:

• 1. 5L(x⇤,↵⇤) = 0 i.e. Gradient of Lagrangian at x⇤ and ↵⇤ is zero.

• 2. x⇤ � b i.e. x⇤ is primal feasible

• 3. ↵⇤ � 0 i.e. ↵⇤ is dual feasible

• 4. ↵⇤(x⇤ � b) = 0 (called as complementary slackness)

We use the first one to relate x⇤ and ↵⇤. We use the last one (complimentaryslackness) to argue that ↵⇤ = 0 if constraint is inactive and ↵⇤ > 0 if constraintis active and tight.

Page 25: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

10

Solving(the(dual

Find%the%dual: Optimization*over%x%is%unconstrained.

Solve:"Now"need$to$maximize$L(x*,α)$over$α$≥$0$Solve&unconstrained&problem&to&get&α’ and$then$take$max(α’,0)

! ="0"constraint"is"inactive,"α">"0""constraint"is"active"and"tight

) ↵0 = 2b

Page 26: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Dual%SVM%– linearly(separable(case

• Primal'problem:

• Dual%problem (derivation):--

11

w – weights on features (d-dim problem)

! – weights on training pts (n-dim problem)

n"training"points,"d"features (x1,"…,"xn)"where"xi is#a#d!dimensional*vector'

Page 27: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Dual%SVM%– linearly(separable(case

• Dual%problem:%%

12

If#we#can#solve#for#!s"(dual"problem),"then%we%have%a%solution(for(w,b(primal(problem)(

Page 28: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Dual%SVM%– linearly(separable(case

Dual%problem%is%also%QPSolution(gives(!js

13

What%about%b?

Page 29: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Dual%SVM:%Sparsity of#dual#solution

14

w.x+"b"="0

Only%few%!js can$be$non!zero%:%where%constraint)is)active)and)tight

(w.xj +"b)yj ="1

Support'vectors –training'points'j'whose'!js are$non!zero

!j >"0

!j >"0

!j >"0

!j ="0

!j ="0

!j ="0

Page 30: svm - cs.cmu.eduaarti/Class/10315_Fall19/recitations/recitation_6.pdf · 9 Connection(between(Primal(and(Dual What does strong duality say about ↵⇤ (the ↵ that achieved optimal

Dual%SVM%– linearly(separable(case

Dual%problem%is%also%QPSolution(gives(!js

15

Use$support$vectors$with$!k>0#to#compute(b(since(constraint(is(tight((w.xk +"b)yk ="1