Kernels + Support Vector Machines (SVMs) SVM Readings: Matt - - PowerPoint PPT Presentation

kernels support vector machines svms
SMART_READER_LITE
LIVE PREVIEW

Kernels + Support Vector Machines (SVMs) SVM Readings: Matt - - PowerPoint PPT Presentation

10-601 Introduction to Machine Learning Machine Learning Department School of Computer Science Carnegie Mellon University Kernels + Support Vector Machines (SVMs) SVM Readings: Matt Gormley Murphy


slide-1
SLIDE 1

Kernels ¡+ ¡ Support ¡Vector ¡ Machines ¡(SVMs)

1

10-­‑601 ¡Introduction ¡to ¡Machine ¡Learning

Matt ¡Gormley Lecture ¡12 February ¡27, ¡2016

Machine ¡Learning ¡Department School ¡of ¡Computer ¡Science Carnegie ¡Mellon ¡University SVM ¡Readings: Murphy ¡14.5 Bishop ¡7.1 HTF ¡12 ¡-­‑ 12.38 Mitchell ¡-­‑-­‑

slide-2
SLIDE 2

Reminders

  • Homework 4: ¡Perceptron / ¡Kernels / ¡SVM

– Release: ¡Wed, ¡Feb. ¡22 – Due: ¡Fri, ¡Mar. ¡03 ¡at ¡11:59pm

  • Midterm Exam (Evening Exam)

– Tue, ¡Mar. ¡07 ¡at ¡7:00pm ¡– 9:30pm – See Piazza ¡for details about location

  • Grading

2

9 ¡days for ¡HW4

slide-3
SLIDE 3

Outline

  • Kernels

– Kernel ¡Perceptron – Kernel ¡as ¡a ¡dot ¡product – Gram ¡matrix – Examples: ¡Polynomial, ¡RBF

  • Support ¡Vector ¡Machine ¡(SVM)

– Background: ¡Constrained ¡ Optimization, ¡Linearly ¡Separable, ¡ Margin – SVM ¡Primal ¡(Linearly ¡Separable ¡Case) – SVM ¡Primal ¡(Non-­‑linearly ¡Separable ¡ Case) – SVM ¡Dual

3

This ¡Lecture Last ¡Lecture

slide-4
SLIDE 4

KERNELS

4

slide-5
SLIDE 5

Kernels: ¡Motivation

Most ¡real-­‑world ¡problems ¡exhibit ¡data ¡that ¡is ¡ not ¡linearly ¡separable.

5

Q: ¡When ¡your ¡data ¡is ¡not ¡linearly ¡separable, ¡ how ¡can ¡you ¡still ¡use ¡a ¡linear ¡classifier? A: Preprocess ¡the ¡data ¡to ¡produce ¡nonlinear features

Example: ¡pixel ¡representation ¡for ¡Facial ¡Recognition:

slide-6
SLIDE 6

Kernels: ¡Motivation

  • Motivation ¡#1: ¡Inefficient ¡Features

– Non-­‑linearly ¡separable ¡data ¡requires ¡high ¡ dimensional ¡representation – Might ¡be ¡prohibitively ¡expensive ¡to ¡compute ¡or ¡ store

  • Motivation ¡#2: ¡Memory-­‑based ¡Methods

– k-­‑Nearest ¡Neighbors ¡(KNN) ¡for ¡facial ¡recognition ¡ allows ¡a ¡distance ¡metric between ¡images ¡-­‑-­‑ no ¡ need ¡to ¡worry ¡about ¡linearity ¡restriction ¡at ¡all

6

slide-7
SLIDE 7

Kernels

Whiteboard

– Kernel ¡Perceptron – Kernel ¡as ¡a ¡dot ¡product – Gram ¡matrix – Examples: ¡RBF ¡kernel, ¡string ¡kernel

7

slide-8
SLIDE 8

Kernel ¡Methods

  • Key ¡idea: ¡

1. Rewrite the ¡algorithm ¡so ¡that ¡we ¡only ¡work ¡with ¡dot ¡products xTz

  • f ¡feature ¡vectors

2. Replace the ¡dot ¡products ¡xTz with ¡a ¡kernel ¡function ¡k(x, ¡z)

  • The ¡kernel ¡k(x,z) ¡can ¡be ¡any legal ¡definition ¡of ¡a ¡dot ¡product: ¡

k(x, ¡z) ¡= ¡φ(x) Tφ(z) ¡for ¡any ¡function ¡φ: ¡X à RD So ¡we ¡only ¡compute ¡the ¡φ ¡dot ¡product ¡implicitly

  • This ¡“kernel ¡trick” can ¡be ¡applied ¡to ¡many ¡algorithms:

– classification: ¡perceptron, ¡SVM, ¡… – regression: ¡ridge ¡regression, ¡… – clustering: ¡k-­‑means, ¡…

8

slide-9
SLIDE 9

Kernel ¡Methods

9

Q: ¡These ¡are ¡just ¡non-­‑linear ¡features, ¡right? A: Yes, ¡but… Q: ¡Can’t ¡we ¡just ¡compute ¡the ¡feature ¡ transformation ¡φ explicitly? A: That ¡depends... Q: ¡So, ¡why ¡all ¡the ¡hype ¡about ¡the ¡kernel ¡trick? A: Because ¡the ¡explicit ¡features ¡might ¡either ¡ be ¡prohibitively ¡expensive ¡to ¡compute ¡or ¡ infinite ¡length ¡vectors

slide-10
SLIDE 10

Example: ¡Polynomial ¡Kernel

10

Slide ¡from ¡Nina ¡Balcan

For n=2, d=2, the kernel K x, z = x ⋅ z d corresponds to 𝑦1, 𝑦2 → Φ 𝑦 = (𝑦1

2, 𝑦2 2,

2𝑦1𝑦2) Φ

K x, z = x ⋅ z d 𝑦1, 𝑦2 → Φ 𝑦 = (𝑦1

2, 𝑦2 2,

2𝑦1𝑦2)

x2 x1

O O O O O O O O X X X X X X X X X X X X X X X X X X

Φ Original space K x, z = x ⋅ z d 𝑦1, 𝑦2 → Φ 𝑦 = (𝑦1

2, 𝑦2 2,

2𝑦1𝑦2)

z1 z3

O O O O O O O O O X X X X X X X X X X X X X X X X X X

Φ-space

ϕ: R2 → R3, x1, x2 → Φ x = (x1

2, x2 2,

2x1x2)

Φ

ϕ x ⋅ ϕ 𝑨 = x1

2, x2 2,

2x1x2 ⋅ (𝑨1

2, 𝑨2 2,

2𝑨1𝑨2) = x1𝑨1 + x2𝑨2 2 = x ⋅ 𝑨 2 = K(x, z) ϕ: R2 → R3 x1, x2 → Φ x = (x1

2, x2 2,

2x1x2)

Φ

ϕ x ⋅ ϕ 𝑨 = x1

2, x2 2,

2x1x2 ⋅ (𝑨1

2, 𝑨2 2,

2𝑨1𝑨2) = x1𝑨1 + x2𝑨2 2 = x ⋅ 𝑨 2 = K(x, z)

slide-11
SLIDE 11

Example: ¡Polynomial ¡Kernel

11

Slide ¡from ¡Nina ¡Balcan

Feature space can grow really large and really quickly….

Crucial to think of ϕ as implicit, not explicit!!!! – 𝑦1

𝑒, 𝑦1𝑦2 … 𝑦𝑒, 𝑦1 2𝑦2 … 𝑦𝑒−1

– Total number of such feature is 𝑒 + 𝑜 − 1 𝑒 = 𝑒 + 𝑜 − 1 ! 𝑒! 𝑜 − 1 !

– 𝑒 = 6, 𝑜 = 100, there are 1.6 billion terms

  • Polynomial kernel degreee 𝑒, 𝑙 𝑦, 𝑨 = 𝑦⊤𝑨 𝑒 = 𝜚 𝑦 ⋅ 𝜚 𝑨

𝑙 𝑦, 𝑨 = 𝑦⊤𝑨 𝑒 = 𝜚 𝑦 ⋅ 𝜚 𝑨

𝑃 𝑜 𝑑𝑝𝑛𝑞𝑣𝑢𝑏𝑢𝑗𝑝𝑜!

slide-12
SLIDE 12

Kernel ¡Examples

Side ¡Note: ¡The ¡feature ¡space ¡might ¡not ¡be ¡unique!

12

ϕ: R2 → R4, x1, x2 → Φ x = (x1

2, x2 2, x1x2, x2x1)

ϕ x ⋅ ϕ 𝑨 = (x1

2, x2 2, x1x2, x2x1) ⋅ (z1 2, z2 2, z1z2, z2z1)

= x ⋅ 𝑨 2 = K(x, z) ϕ: R2 → R3, x1, x2 → Φ x = (x1

2, x2 2,

2x1x2) ϕ x ⋅ ϕ 𝑨 = x1

2, x2 2,

2x1x2 ⋅ (𝑨1

2, 𝑨2 2,

2𝑨1𝑨2) = x1𝑨1 + x2𝑨2 2 = x ⋅ 𝑨 2 = K(x, z)

Slide ¡from ¡Nina ¡Balcan

Explicit ¡representation ¡#1: Explicit ¡representation ¡#2:

These ¡two ¡different ¡feature ¡representations ¡correspond ¡to ¡the ¡same ¡ kernel ¡function!

slide-13
SLIDE 13

Kernel ¡Examples

13

Name Kernel ¡Function (implicit dot ¡product) Feature ¡Space (explicit ¡dot ¡product) Linear Same ¡as ¡original ¡input ¡ space Polynomial ¡(v1) All ¡polynomials of degree ¡ d Polynomial (v2) All ¡polynomials up ¡to ¡ degree ¡d Gaussian Infinite ¡dimensional ¡space Hyperbolic Tangent ¡ (Sigmoid) ¡ Kernel (With SVM, ¡this ¡is ¡ equivalent ¡to ¡a ¡2-­‑layer ¡ neural ¡network)

slide-14
SLIDE 14

RBF ¡Kernel ¡Example

14

RBF ¡Kernel:

slide-15
SLIDE 15

RBF ¡Kernel ¡Example

15

RBF ¡Kernel:

slide-16
SLIDE 16

RBF ¡Kernel ¡Example

16

RBF ¡Kernel:

slide-17
SLIDE 17

RBF ¡Kernel ¡Example

17

RBF ¡Kernel:

slide-18
SLIDE 18

RBF ¡Kernel ¡Example

18

RBF ¡Kernel:

slide-19
SLIDE 19

RBF ¡Kernel ¡Example

19

RBF ¡Kernel:

slide-20
SLIDE 20

RBF ¡Kernel ¡Example

20

RBF ¡Kernel:

slide-21
SLIDE 21

RBF ¡Kernel ¡Example

21

RBF ¡Kernel:

slide-22
SLIDE 22

RBF ¡Kernel ¡Example

22

RBF ¡Kernel:

slide-23
SLIDE 23

RBF ¡Kernel ¡Example

23

RBF ¡Kernel:

slide-24
SLIDE 24

RBF ¡Kernel ¡Example

24

RBF ¡Kernel:

slide-25
SLIDE 25

RBF ¡Kernel ¡Example

25

RBF ¡Kernel:

slide-26
SLIDE 26

RBF ¡Kernel ¡Example

26

RBF ¡Kernel: KNN ¡vs. ¡SVM

slide-27
SLIDE 27

RBF ¡Kernel ¡Example

27

RBF ¡Kernel: KNN ¡vs. ¡SVM

slide-28
SLIDE 28

RBF ¡Kernel ¡Example

28

RBF ¡Kernel: KNN ¡vs. ¡SVM

slide-29
SLIDE 29

RBF ¡Kernel ¡Example

29

RBF ¡Kernel: KNN ¡vs. ¡SVM

slide-30
SLIDE 30

Example: ¡String ¡Kernel

Setup:

– Input ¡instances ¡x are ¡strings ¡of ¡characters ¡(e.g. ¡ x(3) = ¡[‘s’, ¡‘a’, ¡‘t’], ¡x(7) = ¡[‘c’, ¡‘a’, ¡‘t’] ¡ – Want ¡indicator ¡features ¡for ¡the ¡presence ¡/ ¡ absence ¡of ¡each ¡possible ¡substring ¡up ¡to ¡length ¡ K

Questions:

1. What ¡is ¡the ¡best ¡runtime of ¡a ¡single ¡Standard ¡ Perceptron update?

  • 2. What ¡is ¡the ¡best ¡runtime of ¡a ¡single ¡Kernel ¡

Perceptron update?

30

slide-31
SLIDE 31
  • If all computations involving instances are in terms
  • f inner products then:

Conceptually, work in a very high diml space and the alg’s performance depends only on linear separability in that extended space. Computationally, only need to modify the algo by replacing each x ⋅ z with a K x, z .

How to choose a kernel:

  • Use Cross-Validation to choose the parameters, e.g., 𝜏 for

Gaussian Kernel K x, 𝑨 = exp −

𝑦−𝑨

2

2 𝜏2

  • Learn a good kernel; e.g., [Lanckriet-Cristianini-Bartlett-El Ghaoui-

Jordan’04]

  • Kernels often encode domain knowledge (e.g., string kernels)

Kernels: ¡Discussion

31

Slide ¡from ¡Nina ¡Balcan

slide-32
SLIDE 32

SUPPORT ¡VECTOR ¡MACHINE ¡ (SVM)

32

slide-33
SLIDE 33

SVM: ¡Optimization ¡Background

Whiteboard

– Constrained ¡Optimization – Linear ¡programming – Quadratic ¡programming – Example: ¡2D ¡quadratic ¡function ¡with ¡linear ¡ constraints

33

slide-34
SLIDE 34

Quadratic ¡Program

34

slide-35
SLIDE 35

Quadratic ¡Program

35

slide-36
SLIDE 36

Quadratic ¡Program

36

slide-37
SLIDE 37

Quadratic ¡Program

37

slide-38
SLIDE 38

Quadratic ¡Program

38

slide-39
SLIDE 39

SVM

Whiteboard

– SVM ¡Primal ¡(Linearly ¡Separable ¡Case) – SVM ¡Primal ¡(Non-­‑linearly ¡Separable ¡Case)

39

slide-40
SLIDE 40

SVM ¡QP

40

slide-41
SLIDE 41

SVM ¡QP

41

slide-42
SLIDE 42

SVM ¡QP

42

slide-43
SLIDE 43

SVM ¡QP

43

slide-44
SLIDE 44

SVM ¡QP

44

slide-45
SLIDE 45

SVM ¡QP

45

slide-46
SLIDE 46

Support Vector Machines (SVMs)

Input: S={(x1, 𝑧1), …,(xm, 𝑧m)}; argminw,𝜊1,…,𝜊𝑛 𝑥

2 + 𝐷 𝜊𝑗 𝑗

s.t.:

  • For all i, 𝑧𝑗𝑥 ⋅ 𝑦𝑗 ≥ 1 − 𝜊𝑗

Which is equivalent to:

Find 𝜊𝑗 ≥ 0

Primal form

Input: S={(x1, y1), …,(xm, ym)}; argminα

1 2 yiyj αiαjxi ⋅ xj − αi i j i

s.t.:

  • For all i,

Find 0 ≤ αi ≤ Ci

Lagrangian Dual

yiαi = 0

i

Can be kernelized!!!

46

Slide ¡from ¡Nina ¡Balcan

slide-47
SLIDE 47

SVMs (Lagrangian Dual)

  • Final classifier is: w = αiyixi

i

  • The points xi for which αi ≠ 0

are called the “support vectors” Input: S={(x1, y1), …,(xm, ym)}; argminα

1 2 yiyj αiαjxi ⋅ xj − αi i j i

s.t.:

  • For all i,

Find 0 ≤ αi ≤ Ci

yiαi = 0

i

+ + + +

  • -
  • +
  • w

𝑥 ⋅ 𝑦 = −1 𝑥 ⋅ 𝑦 = 1

47

Slide ¡from ¡Nina ¡Balcan

slide-48
SLIDE 48

SVM ¡Takeaways

  • Maximizing ¡the ¡margin ¡of ¡a ¡linear ¡separator ¡

is ¡a ¡good ¡training ¡criteria

  • Support ¡Vector ¡Machines ¡(SVMs) ¡learn ¡a ¡

max-­‑margin ¡linear ¡classifier

  • The ¡SVM ¡optimization ¡problem ¡can ¡be ¡

solved ¡with ¡black-­‑box ¡Quadratic ¡ Programming ¡(QP) ¡solvers

  • Learned ¡decision ¡boundary ¡is ¡defined ¡by ¡its ¡

support ¡vectors

48