kernels and kernelized perceptron - github...

13
Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos Guestrin and Luke Zettlemoyer

Upload: trinhtuong

Post on 24-Jul-2019

247 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

Kernels and Kernelized Perceptron

Instructor: Alan Ritter

Many Slides from Carlos Guestrin and Luke Zettlemoyer

Page 2: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

What'if'the'data'is'not'linearly'separable?'

Use features of features of features of features….

Feature space can get really large really quickly!

�(x) =

0

BBBBBBBBBB@

x1

. . .

x

n

x1x2

x1x3

. . .

e

x1

. . .

1

CCCCCCCCCCA

Page 3: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

NonIlinear'features:'1D'input'•  Datasets'that'are'linearly'separable'with'some'noise'work'

out'great:''

•  But'what'are'we'going'to'do'if'the'dataset'is'just'too'hard?''

•  How'about…'mapping'data'to'a'higherIdimensional'space:'

0

0

x2

x

x

x

Page 4: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

Feature'spaces'•  General'idea:'''map'to'higher'dimensional'space'

–  if'x'is'in'Rn,'then'φ(x)'is'in'Rm'for'm>n'–  Can'now'learn'feature'weights'w#in'Rm'and'predict:''

–  Linear'funcXon'in'the'higher'dimensional'space'will'be'nonIlinear'in'the'original'space'

x → φ(x)

y = sign(w · �(x))

Page 5: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

Higher'order'polynomials'

number of input dimensions

num

ber o

f mon

omia

l ter

ms

d=2

d=4

d=3

m – input features d – degree of polynomial

grows fast! d = 6, m = 100 about 1.6 billion terms

Page 6: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

Efficient'dotIproduct'of'polynomials'Polynomials of degree exactly d

d=1

⇥(x) =

⌥⌥⌥⌥⌥⌥⌥⌥⌥⌥⌥⇧

x(1)

. . .x(n)

x(1)x(2)

x(1)x(3)

. . .

ex(1)

. . .

�����������⌃

⇤L

⇤w= w �

j

�jyjxj

⇥(u).⇥(v) =

�u1u2

⇥.

�v1v2

⇥= u1v1 + u2v2 = u.v

7

d=2

For any d (we will skip proof): •  Cool! Taking a dot product and an exponential gives same

results as mapping into high dimensional space and then taking dot product

⇥(x) =

⌥⌥⌥⌥⌥⌥⌥⌥⌥⌥⌥⇧

x(1)

. . .x(n)

x(1)x(2)

x(1)x(3)

. . .

ex(1)

. . .

�����������⌃

⇤L

⇤w= w �

j

�jyjxj

⇥(u).⇥(v) =

�u1u2

⇥.

�v1v2

⇥= u1v1 + u2v2 = u.v

⇥(u).⇥(v) = (u.v)d

7

⇥(x) =

⌥⌥⌥⌥⌥⌥⌥⌥⌥⌥⌥⇧

x(1)

. . .x(n)

x(1)x(2)

x(1)x(3)

. . .

ex(1)

. . .

�����������⌃

⇤L

⇤w= w �

j

�jyjxj

⇥(u).⇥(v) =

�u1u2

⇥.

�v1v2

⇥= u1v1 + u2v2 = u.v

⇥(u).⇥(v) =

⌥⌥⇧

u21u1u2u2u1u22

��⌃ .

⌥⌥⇧

v21v1v2v2v1v22

��⌃ = u21v21 + 2u1v1u2v2 + u22v

22

⇥(u).⇥(v) = (u.v)d

7

⇥(x) =

⌥⌥⌥⌥⌥⌥⌥⌥⌥⌥⌥⇧

x(1)

. . .x(n)

x(1)x(2)

x(1)x(3)

. . .

ex(1)

. . .

�����������⌃

⇤L

⇤w= w �

j

�jyjxj

⇥(u).⇥(v) =

�u1u2

⇥.

�v1v2

⇥= u1v1 + u2v2 = u.v

⇥(u).⇥(v) =

⌥⌥⇧

u21u1u2u2u1u22

��⌃ .

⌥⌥⇧

v21v1v2v2v1v22

��⌃ = u21v21 + 2u1v1u2v2 + u22v

22

= (u1v1 + u2v2)2

⇥(u).⇥(v) = (u.v)d

7

⇥(x) =

⌥⌥⌥⌥⌥⌥⌥⌥⌥⌥⌥⇧

x(1)

. . .x(n)

x(1)x(2)

x(1)x(3)

. . .

ex(1)

. . .

�����������⌃

⇤L

⇤w= w �

j

�jyjxj

⇥(u).⇥(v) =

�u1u2

⇥.

�v1v2

⇥= u1v1 + u2v2 = u.v

⇥(u).⇥(v) =

⌥⌥⇧

u21u1u2u2u1u22

��⌃ .

⌥⌥⇧

v21v1v2v2v1v22

��⌃ = u21v21 + 2u1v1u2v2 + u22v

22

= (u1v1 + u2v2)2

= (u.v)2

⇥(u).⇥(v) = (u.v)d

7

K(u, v) =

Page 7: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

The'�Kernel'Trick�'•  A'kernel&func*on'defines'a'dot'product'in'some'feature'space.'

& & &K(u,v)='φ(u)!#φ(v)'•  Example:''

'2Idimensional'vectors'u=[u1&&&u2]'and'v=[v1&&&v2];''let'K(u,v)=(1'+'u!v)2,''Need'to'show'that'K(xi,xj)='φ(xi)#!φ(xj):'''K(u,v)=(1'+'u!v)2

,='1+'u12v12&+&2'u1v1&u2v2+&u22v22&+'2u1v1&+&2u2v2=&

&&&&&&&=&[1,'u12,&&√2'u1u2,&&&u22,&&√2u1,&&√2u2]#!#[1,''v12,&&√2v1v2,&&v22,&&√2v1,&&√2v2]'=''''''''='φ(u)#!φ(v),''''where'φ(x)'='#[1,''x12,&&√2'x1x2,&&&x22,&&&√2x1,&&√2x2]'

•  Thus,'a'kernel'funcXon&implicitly&maps'data'to'a'highIdimensional'space'

(without'the'need'to'compute'each'φ(x)'explicitly).'•  But,'it'isn’t'obvious'yet'how'we'will'incorporate'it'into'actual'learning'

algorithms…'

Page 8: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

“Kernel'trick”'for'The'Perceptron!'•  Never'compute'features'explicitly!!!'

–  Compute'dot'products'in'closed'form'K(u,v)'='Φ(u)'!'Φ(v)''•  Standard'Perceptron:' •  Kernelized'Perceptron:'

•  set'wi=0'for'each'feature'i'•  set'ai=0'for'each'example'i'•  For't=1..T,'i=1..n:'

–  ''–  if'y'≠'yi'

•  ''•  'ai'+='yi'

•  At'all'Xmes'during'learning:'

y = sign(w · �(xi))

w = w + y

i�(xi)

•  set'ai=0'for'each'example'i'•  For't=1..T,'i=1..n:'

–  ''

'–  if'y'≠'yi'

•  ai'+='yi''

'

y = sign((X

k

a

k�(xk)) · �(xi))

= sign(X

k

a

kK(xk

, x

i))

Exactly the same computations, but can use

K(u,v) to avoid enumerating the features!!!

w =X

k

a

k�(xk)

Page 9: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

x2

x1

x1# x2# y#1' 1' 1'I1' 1' I1'I1' I1' 1'1' I1' I1'

IniXal:'•  a'='[a1,'a2,'a3,'a4]'='[0,0,0,0]'t=1,i=1'•  ΣkakK(xk,x1)'='0x4+0x0+0x4+0x0'='0,'sign(0)=I1'•  a1'+='y1 "'a1+=1,'new'a='[1,0,0,0]'t=1,i=2'•  ΣkakK(xk,x2)'='1x0+0x4+0x0+0x4'='0,'sign(0)=I1't=1,i=3'•  ΣkakK(xk,x3)'='1x4+0x0+0x4+0x0'='4,'sign(4)=1't=1,i=4'•  ΣkakK(xk,x4)'='1x0+0x4+0x0+0x4'='0,'sign(0)=I1't=2,i=1'•  ΣkakK(xk,x1)'='1x4+0x0+0x4+0x0'='4,'sign(4)=1'…''''Converged!!!'•  y=Σk'ak'K(xk,x)''''''''='1×K(x1,x)+0×K(x2,x)+0×K(x3,x)+0×K(x4,x)''''''''='K(x1,x)''''''''='K([1,1],x)'''(because'x1=[1,1])''''''''='(x1+x2)2'''''''''(because''K(u,v)'='(u!v)2)'''

•  set'ai=0'for'each'example'i'•  For't=1..T,'i=1..n:'

–  ''–  if'y'≠'yi'

•  ai'+='yi''

'

y = sign(X

k

a

kK(xk

, x

i))

K(u,v)'='(u!v)2'e.g.,''K(x1,x2)''''''='K([1,1],[I1,1])'''''='(1xI1+1x1)2'''''''='0'''

K# x1# x2# x3# x4#

x1' 4' 0' 4' 0'

x2' 0' 4' 0' 4'

x3' 4' 0' 4' 0'

x4' 0' 4' 0' 4'

Page 10: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

Common'kernels'•  Polynomials'of'degree'exactly'd&

•  Polynomials'of'degree'up'to'd&

•  Gaussian'kernels'

•  Sigmoid'''•  And'many'others:'very'acXve'area'of'research!'

Page 11: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

Overfipng?'•  Huge'feature'space'with'kernels,'what'about'overfipng???'– Oqen'robust'to'overfipng,'e.g.'if'you'don’t'make'too'many'Perceptron'updates'

– SVMs'(which'we'will'see'next)'will'have'a'clearer'story'for'avoiding'overfipng'

– But'everything'overfits'someXmes!!!'•  Can'control'by:'

–  Choosing'a'be@er'Kernel'–  Varying'parameters'of'the'Kernel'(width'of'Gaussian,'etc.)'

Page 12: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

Kernels'in'logisXc'regression'

•  Define'weights'in'terms'of'data'points:'

'•  Derive'gradient'descent'rule'on'αj,w0'•  Similar'tricks'for'all'linear'models:'SVMs,'etc'

w =X

j

↵j�(xj)

P (Y = 0|X = x,w, w0) =1

1 + exp(w0 +P

j ↵j�(xj) · �(x))

=1

1 + exp(w0 +P

j ↵jK(xj

,x))

P (Y = 0|X = x,w, w0) =1

1 + exp(w0 +w · x)

Page 13: Kernels and Kernelized Perceptron - GitHub Pagesaritter.github.io/courses/5523_slides/kernels.pdf · Kernels and Kernelized Perceptron Instructor: Alan Ritter Many Slides from Carlos

What'you'need'to'know'•  The'kernel'trick'•  Derive'polynomial'kernel'•  Common'kernels'•  Kernelized'perceptron'