Docsity

Kernel methods CSS Notes, Lecture notes of Computer Science

Toggle sidebar
bg1
Chapter 5
Kernel methods
5.1 Feature maps
Recall that in our discussion about linear regression, we considered the prob-
lem of predicting the price of a house (denoted by y) from the living area of
the house (denoted by x), and we fit a linear function of xto the training
data. What if the price ycan be more accurately represented as a non-linear
function of x? In this case, we need a more expressive family of models than
linear models.
We start by considering fitting cubic functions y=θ3x3+θ2x2+θ1x+θ0.
It turns out that we can view the cubic function as a linear function over
the a different set of feature variables (defined below). Concretely, let the
function φ:R→R4be defined as
φ(x) =
1
x
x2
x3
∈R4.(5.1)
Let θ∈R4be the vector containing θ0, θ1, θ2, θ3as entries. Then we can
rewrite the cubic function in xas:
θ3x3+θ2x2+θ1x+θ0=θTφ(x)
Thus, a cubic function of the variable xcan be viewed as a linear function
over the variables φ(x). To distinguish between these two sets of variables,
in the context of kernel methods, we will call the “original” input value the
input attributes of a problem (in this case, x, the living area). When the
49
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Kernel methods CSS Notes and more Lecture notes Computer Science in PDF only on Docsity!

Chapter 5

Kernel methods

5.1 Feature maps

Recall that in our discussion about linear regression, we considered the prob-

lem of predicting the price of a house (denoted by y) from the living area of

the house (denoted by x), and we fit a linear function of x to the training

data. What if the price y can be more accurately represented as a non-linear

function of x? In this case, we need a more expressive family of models than

linear models.

We start by considering fitting cubic functions y = θ 3 x

3

  • θ 2 x

2

  • θ 1 x + θ 0.

It turns out that we can view the cubic function as a linear function over

the a different set of feature variables (defined below). Concretely, let the

function φ : R → R

4 be defined as

φ(x) =

x

x

2

x

3

∈ R

4

. (5.1)

Let θ ∈ R

4 be the vector containing θ 0 , θ 1 , θ 2 , θ 3 as entries. Then we can

rewrite the cubic function in x as:

θ 3 x

3

  • θ 2 x

2

  • θ 1 x + θ 0 = θ

T φ(x)

Thus, a cubic function of the variable x can be viewed as a linear function

over the variables φ(x). To distinguish between these two sets of variables,

in the context of kernel methods, we will call the “original” input value the

input attributes of a problem (in this case, x, the living area). When the

original input is mapped to some new set of quantities φ(x), we will call those

new quantities the features variables. (Unfortunately, different authors use

different terms to describe these two things in different contexts.) We will

call φ a feature map, which maps the attributes to the features.

5.2 LMS (least mean squares) with features

We will derive the gradient descent algorithm for fitting the model θ

T φ(x).

First recall that for ordinary least square problem where we were to fit θ

T x,

the batch gradient descent update is (see the first lecture note for its deriva-

tion):

θ := θ + α

n ∑

i=

y

(i) − hθ(x

(i) )

x

(i)

:= θ + α

∑^ n

i=

y

(i) − θ

T x

(i)

x

(i)

. (5.2)

Let φ : R

d → R

p be a feature map that maps attribute x (in R

d ) to the

features φ(x) in R

p

. (In the motivating example in the previous subsection,

we have d = 1 and p = 4.) Now our goal is to fit the function θ

T φ(x), with

θ being a vector in R

p instead of R

d

. We can replace all the occurrences of

x

(i) in the algorithm above by φ(x

(i) ) to obtain the new update:

θ := θ + α

n ∑

i=

y

(i) − θ

T φ(x

(i) )

φ(x

(i) ) (5.3)

Similarly, the corresponding stochastic gradient descent update rule is

θ := θ + α

y

(i) − θ

T φ(x

(i) )

φ(x

(i) ) (5.4)

5.3 LMS with the kernel trick

The gradient descent update, or stochastic gradient update above becomes

computationally expensive when the features φ(x) is high-dimensional. For

example, consider the direct extension of the feature map in equation (5.1)

to high-dimensional input x: suppose x ∈ R

d , and let φ(x) be the vector that

linear combination of φ(x

(1) ),... , φ(x

(n) ) because

θ := θ + α

n ∑

i=

y

(i) − θ

T φ(x

(i) )

φ(x

(i) )

∑^ n

i=

βiφ(x

(i) ) + α

∑^ n

i=

y

(i) − θ

T φ(x

(i) )

φ(x

(i) )

n ∑

i=

(βi + α

y

(i) − θ

T φ(x

(i) )

new βi

φ(x

(i) ) (5.7)

You may realize that our general strategy is to implicitly represent the p-

dimensional vector θ by a set of coefficients β 1 ,... , βn. Towards doing this,

we derive the update rule of the coefficients β 1 ,... , βn. Using the equation

above, we see that the new βi depends on the old one via

βi := βi + α

y

(i) − θ

T φ(x

(i) )

Here we still have the old θ on the RHS of the equation. Replacing θ by

θ =

n j=

βj φ(x

(j) ) gives

∀i ∈ { 1 ,... , n}, βi := βi + α

y

(i) −

n ∑

j=

βj φ(x

(j) )

T φ(x

(i) )

We often rewrite φ(x

(j) )

T φ(x

(i) ) as 〈φ(x

(j) ), φ(x

(i) )〉 to emphasize that it’s the

inner product of the two feature vectors. Viewing βi’s as the new representa-

tion of θ, we have successfully translated the batch gradient descent algorithm

into an algorithm that updates the value of β iteratively. It may appear that

at every iteration, we still need to compute the values of 〈φ(x

(j) ), φ(x

(i) )〉 for

all pairs of i, j, each of which may take roughly O(p) operation. However,

two important properties come to rescue:

  1. We can pre-compute the pairwise inner products 〈φ(x

(j) ), φ(x

(i) )〉 for all

pairs of i, j before the loop starts.

  1. For the feature map φ defined in (5.5) (or many other interesting fea-

ture maps), computing 〈φ(x

(j) ), φ(x

(i) )〉 can be efficient and does not

necessarily require computing φ(x

(i) ) explicitly. This is because:

〈φ(x), φ(z)〉 = 1 +

d ∑

i=

xizi +

i,j∈{ 1 ,...,d}

xixj zizj +

i,j,k∈{ 1 ,...,d}

xixj xkzizj zk

∑^ d

i=

xizi +

∑d

i=

xizi

∑d

i=

xizi

= 1 + 〈x, z〉 + 〈x, z〉

2

  • 〈x, z〉

3 (5.9)

Therefore, to compute 〈φ(x), φ(z)〉, we can first compute 〈x, z〉 with

O(d) time and then take another constant number of operations to com-

pute 1 + 〈x, z〉 + 〈x, z〉

2

  • 〈x, z〉

3 .

As you will see, the inner products between the features 〈φ(x), φ(z)〉 are

essential here. We define the Kernel corresponding to the feature map φ as

a function that maps X × X → R satisfying:

2

K(x, z) , 〈φ(x), φ(z)〉 (5.10)

To wrap up the discussion, we write the down the final algorithm as

follows:

  1. Compute all the values K(x

(i) , x

(j) ) , 〈φ(x

(i) ), φ(x

(j) )〉 using equa-

tion (5.9) for all i, j ∈ { 1 ,... , n}. Set β := 0.

  1. Loop:

∀i ∈ { 1 ,... , n}, βi := βi + α

y

(i) −

∑^ n

j=

βj K(x

(i) , x

(j) )

Or in vector notation, letting K be the n × n matrix with Kij =

K(x

(i) , x

(j) ), we have

β := β + α(~y − Kβ)

With the algorithm above, we can update the representation β of the

vector θ efficiently with O(n) time per update. Finally, we need to show that

2 Recall that X is the space of the input x. In our running example, X = R

d

We can also write this as

K(x, z) =

d ∑

i=

xizi

d ∑

j=

xj zj

∑^ d

i=

∑^ d

j=

xixj zizj

d ∑

i,j=

(xixj )(zizj )

Thus, we see that K(x, z) = 〈φ(x), φ(z)〉 is the kernel function that corre-

sponds to the feature mapping φ given (shown here for the case of d = 3)

by

φ(x) =

x 1 x 1

x 1 x 2

x 1 x 3

x 2 x 1

x 2 x 2

x 2 x 3

x 3 x 1

x 3 x 2

x 3 x 3

Revisiting the computational efficiency perspective of kernel, note that whereas

calculating the high-dimensional φ(x) requires O(d

2 ) time, finding K(x, z)

takes only O(d) time—linear in the dimension of the input attributes.

For another related example, also consider K(·, ·) defined by

K(x, z) = (x

T z + c)

2

∑^ d

i,j=

(xixj )(zizj ) +

∑^ d

i=

2 cxi)(

2 czi) + c

2 .

(Check this yourself.) This function K is a kernel function that corresponds

to the feature mapping (again shown for d = 3)

φ(x) =

x 1 x 1

x 1 x 2

x 1 x 3

x 2 x 1

x 2 x 2

x 2 x 3

x 3 x 1

x 3 x 2

x 3 x 3 √ 2 cx 1 √ 2 cx 2 √ 2 cx 3

c

and the parameter c controls the relative weighting between the xi (first

order) and the xixj (second order) terms.

More broadly, the kernel K(x, z) = (x

T z + c)

k corresponds to a feature

mapping to an

d+k k

feature space, corresponding of all monomials of the

form xi 1 xi 2... xik that are up to order k. However, despite working in this

O(d

k )-dimensional space, computing K(x, z) still takes only O(d) time, and

hence we never need to explicitly represent feature vectors in this very high

dimensional feature space.

Kernels as similarity metrics. Now, let’s talk about a slightly different

view of kernels. Intuitively, (and there are things wrong with this intuition,

but nevermind), if φ(x) and φ(z) are close together, then we might expect

K(x, z) = φ(x)

T φ(z) to be large. Conversely, if φ(x) and φ(z) are far apart—

say nearly orthogonal to each other—then K(x, z) = φ(x)

T φ(z) will be small.

So, we can think of K(x, z) as some measurement of how similar are φ(x)

and φ(z), or of how similar are x and z.

Given this intuition, suppose that for some learning problem that you’re

working on, you’ve come up with some function K(x, z) that you think might

be a reasonable measure of how similar x and z are. For instance, perhaps

you chose

K(x, z) = exp

||x − z||

2

2 σ

2

This is a reasonable measure of x and z’s similarity, and is close to 1 when

x and z are close, and near 0 when x and z are far apart. Does there exist

Sufficient conditions for valid kernels. More generally, the condition

above turns out to be not only a necessary, but also a sufficient, condition

for K to be a valid kernel (also called a Mercer kernel). The following result

is due to Mercer.

3

Theorem (Mercer). Let K : R

d × R

d 7 → R be given. Then for K

to be a valid (Mercer) kernel, it is necessary and sufficient that for any

{x

(1) ,... , x

(n) }, (n < ∞), the corresponding kernel matrix is symmetric pos-

itive semi-definite.

Given a function K, apart from trying to find a feature mapping φ that

corresponds to it, this theorem therefore gives another way of testing if it is

a valid kernel. You’ll also have a chance to play with these ideas more in

problem set 2.

In class, we also briefly talked about a couple of other examples of ker-

nels. For instance, consider the digit recognition problem, in which given

an image (16x16 pixels) of a handwritten digit (0-9), we have to figure out

which digit it was. Using either a simple polynomial kernel K(x, z) = (x

T z)

k

or the Gaussian kernel, SVMs were able to obtain extremely good perfor-

mance on this problem. This was particularly surprising since the input

attributes x were just 256-dimensional vectors of the image pixel intensity

values, and the system had no prior knowledge about vision, or even about

which pixels are adjacent to which other ones. Another example that we

briefly talked about in lecture was that if the objects x that we are trying

to classify are strings (say, x is a list of amino acids, which strung together

form a protein), then it seems hard to construct a reasonable, “small” set of

features for most learning algorithms, especially if different strings have dif-

ferent lengths. However, consider letting φ(x) be a feature vector that counts

the number of occurrences of each length-k substring in x. If we’re consid-

ering strings of English letters, then there are 26

k such strings. Hence, φ(x)

is a 26

k dimensional vector; even for moderate values of k, this is probably

too big for us to efficiently work with. (e.g., 26

4 ≈ 460000.) However, using

(dynamic programming-ish) string matching algorithms, it is possible to ef-

ficiently compute K(x, z) = φ(x)

T φ(z), so that we can now implicitly work

in this 26

k -dimensional feature space, but without ever explicitly computing

feature vectors in this space.

3 Many texts present Mercer’s theorem in a slightly more complicated form involving

L

2 functions, but when the input attributes take values in R

d , the version given here is

equivalent.

Application of kernel methods: We’ve seen the application of kernels

to linear regression. In the next part, we will introduce the support vector

machines to which kernels can be directly applied. dwell too much longer on

it here. In fact, the idea of kernels has significantly broader applicability than

linear regression and SVMs. Specifically, if you have any learning algorithm

that you can write in terms of only inner products 〈x, z〉 between input

attribute vectors, then by replacing this with K(x, z) where K is a kernel,

you can “magically” allow your algorithm to work efficiently in the high

dimensional feature space corresponding to K. For instance, this kernel trick

can be applied with the perceptron to derive a kernel perceptron algorithm.

Many of the algorithms that we’ll see later in this class will also be amenable

to this method, which has come to be known as the “kernel trick.”