Implicit Lifting and the Kernel Trick

I disentangle the what I call the "lifting trick" from the kernel trick as a way of clarifying what the kernel trick is and does.

Implicit lifting

Imagine we have some data for a classification problem that is not linearly separable. A classic example is Figure 11a. We would like to use a linear classifier. How might we do this? One idea is to augment our data’s features so that we can “lift” it into a higher dimensional space in which our data are linearly separable (Figure 11b).

Figure 1: The "lifting trick". (a) A binary classification problem that is not linearly separable in R2\mathbb{R}^2. (b) A lifting of the data into R3\mathbb{R}^3 using a polynomial kernel, φ([x1    x2])=[x12    x22    2x1x2]\varphi([x_1 \;\; x_2]) = [x_1^2 \;\; x_2^2 \;\; \sqrt{2} x_1 x_2].

Let’s formalize this approach. Let our data be {(x1,y1),,(xN,yN)}\{(\mathbf{x}_1, y_1), \dots, (\mathbf{x}_N, y_N)\} where xnRD\mathbf{x}_n \in \mathbb{R}^D in general. Now consider D=2D = 2 and a single data point

x=[x1x2]. \mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \end{bmatrix}.

We might transform each data point with a function (the polynomial kernel for the curious),

φ(x)=[x12x222x1x2]. \varphi(\mathbf{x}) = \begin{bmatrix} x_1^2 \\ x_2^2 \\ \sqrt{2} x_1 x_2 \end{bmatrix}.

Since our new data, φ(x)\varphi(\mathbf{x}), is in R3\mathbb{R}^3, we might be able to find a hyperplane β\boldsymbol{\beta} in 3D to separate our observations,

βφ(x)=β0+β1x12+β2x22+β32x1x2=0. \boldsymbol{\beta}^{\top} \varphi(\mathbf{x}) = \beta_0 + \beta_1 x_1^2 + \beta_2 x_2^2 + \beta_3 \sqrt{2} x_1 x_2 = 0.

This idea, while cool, is not the kernel trick, but it deserves a name. Rather than calling it the pre-(kernel trick) trick, let’s just call it the lifting trick. Caveat: I am not aware of a name for this trick, but I find naming things useful. If you loudly call this “the lifting trick” at a machine-learning party, you might get confused looks.

In order to find this hyperplane, we need to run a classification algorithm on our data after it has been lifted into three-dimensional space. At this point, we could be done. We take RD\mathbb{R}^D, perform our lifting trick into RJ\mathbb{R}^J where D<JD < J, and then use a method like logistic regression to try to linearly classify it. However, this might be expensive for a “fancy” enough φ()\varphi(\cdot). For NN data points lifted into JJ dimensions, we need NJNJ operations just to preprocess the data. But we can avoid computing φ()\varphi(\cdot) entirely while still doing linear classification in this lifted space if we’re clever. This second trick is the kernel trick.

The kernel trick

Consider the loss function for a support vector machine (SVM):

L(w,α)=nαn12nNmNαnαmynym(xnxm) L(\mathbf{w}, \boldsymbol{\alpha}) = \sum_n \alpha_n - \frac{1}{2} \sum_n^N \sum_m^N \alpha_n \alpha_m y_n y_m (\mathbf{x}_n^{\top} \mathbf{x}_m)

w\mathbf{w} is a normed vector representing the linear decision boundary and α\boldsymbol{\alpha} is a vector of Lagrange multipliers. If this is new or confusing, please see these excellent lecture notes from Rob Schapire’s Princeton course on theoretical machine learning. Otherwise, you can elide the details if you like; the upshot is that SVMs require computing a dot product and that, as formulated, the SVM is linear. (Note that w\mathbf{w} is implicit in the equation above; see those lecture notes as needed.)

Now what if we had the data problem in Figure 11a? Could we use the lifting trick to make our SVM nonlinear? Sure. For the previously specified φ()\varphi(\cdot), we have

φ(xn)φ(xm)=[xn,12xn,222xn,1xn,2][xm,12xm,222xm,1xm,2]=xn,12xm,12+xn,22xm,22+2xn,1xn,2xm,1xm,2. \begin{aligned} \varphi(\mathbf{x}_n)^{\top} \varphi(\mathbf{x}_m) &= \begin{bmatrix} x_{n,1}^2 & x_{n,2}^2 & \sqrt{2} x_{n,1} x_{n,2} \end{bmatrix} \cdot \begin{bmatrix} x_{m,1}^2 \\ x_{m,2}^2 \\ \sqrt{2} x_{m,1} x_{m,2} \end{bmatrix} \\ &= x_{n,1}^2 x_{m,1}^2 + x_{n,2}^2 x_{m,2}^2 + 2 x_{n,1} x_{n,2} x_{m,1} x_{m,2}. \end{aligned}

We would then need to compute this for all our NN data points. As we discussed, the problem with this approach is scalability. However, consider the following derivation,

(xnxm)2=([xn,1xn,2][xm,1xm,2])2=(xn,1xm,1+xn,2xm,2)2=(xn,1xm,1)2+(xn,2xm,2)2+2(xn,1xm,1)(xn,2xm,2)=φ(xn)φ(xm). \begin{aligned} (\mathbf{x}_n^{\top} \mathbf{x}_m)^2 &= \Big( \begin{bmatrix} x_{n,1} & x_{n,2} \end{bmatrix} \cdot \begin{bmatrix} x_{m,1} \\ x_{m,2} \end{bmatrix} \Big)^2 \\ &= (x_{n,1} x_{m,1} + x_{n,2} x_{m,2})^2 \\ &= (x_{n,1} x_{m,1})^2 + (x_{n,2} x_{m,2})^2 + 2(x_{n,1} x_{m,1})(x_{n,2} x_{m,2}) \\ &= \varphi(\mathbf{x}_n)^{\top} \varphi(\mathbf{x}_m). \end{aligned}

What just happened? Rather than lifting our data into R3\mathbb{R}^3 and computing an inner product, we just computed an inner product in R2\mathbb{R}^2 and then squared the sum. While both derivations have a similar number of mathematical symbols, the actual number of operations is much smaller for the second approach. This is because a inner product in R2\mathbb{R}^2 is two multiplications and a sum. The square is just the square of a scalar, so 4 operations. The first approach squared three components of two vectors (6 operations), then performed an inner product (3 multiplications, 1 sum) for 9 operations.

This is the kernel trick: we can avoid expensive operations in high dimensions by finding an appropriate kernel function k(xn,xm)k(\mathbf{x}_n,\mathbf{x}_m) that is equivalent to the inner product in higher dimensional space. In our example above, k(xn,xm)=(xnxm)2k(\mathbf{x}_n, \mathbf{x}_m) = (\mathbf{x}_n^{\top} \mathbf{x}_m)^2. In other words, the kernel trick performs the lifting trick for cheap.

Mercer’s theorem

The mathematical basis for the kernel trick was discovered by James Mercer. Mercer proved that any positive definition function k(xn,xm)k(\mathbf{x}_n, \mathbf{x}_m) with xn,xmRD\mathbf{x}_n, \mathbf{x}_m \in \mathbb{R}^D defines an inner product of another vector space V\mathcal{V}. Thus, if you have a function φ()\varphi(\cdot) such that φ(xn),φ(xm)V\langle \varphi(\mathbf{x}_n), \varphi(\mathbf{x}_m) \rangle_{\mathcal{V}} is a valid inner product in V\mathcal{V}, you know a kernel function exists that can perform the lifting trick for cheap. Alternatively, if you have a positive definite kernel, you can deconstruct its implicit basis function φ()\varphi(\cdot).

This idea is formalized in Mercer’s Theorem (taken from Michael Jordan’s lecture notes){:target=”_blank”}:

Mercer’s Theorem: A symmetric function k(x,y)k(\mathbf{x}, \mathbf{y}) can be expressed as an inner product

>k(x,y)=φ(x),φ(y)> > k(\mathbf{x}, \mathbf{y}) = \langle \varphi(\mathbf{x}), \varphi(\mathbf{y}) \rangle >

for some φ()\varphi(\cdot) if and only if k(x,y)k(\mathbf{x}, \mathbf{y}) is positive semidefinite, i.e.

>k(x,y)g(x)g(y)dxdy0,g> > \int k(\mathbf{x}, \mathbf{y}) g(\mathbf{x}) g(\mathbf{y}) \text{d}\mathbf{x}\text{d}\mathbf{y} \geq 0, \qquad \forall g >

or, equivalently, if

>[>k(x1,x1)k(x1,x2)k(x1,xN)>>k(x2,x1)k(x2,x2)k(x2,xN)>>>>k(xN,x1)k(x2,x2)k(xN,xN)]> > \begin{bmatrix} > k(\mathbf{x}_1, \mathbf{x}_1) & k(\mathbf{x}_1, \mathbf{x}_2) & \dots & k(\mathbf{x}_1, \mathbf{x}_N) > \\ > k(\mathbf{x}_2, \mathbf{x}_1) & k(\mathbf{x}_2, \mathbf{x}_2) & \dots & k(\mathbf{x}_2, \mathbf{x}_N) > \\ > \vdots & \vdots & \ddots & \vdots > \\ > k(\mathbf{x}_N, \mathbf{x}_1) & k(\mathbf{x}_2, \mathbf{x}_2) & \dots & k(\mathbf{x}_N, \mathbf{x}_N) \end{bmatrix} >

is positive semidefinite for any collection {x1,,xN}\{\mathbf{x}_1, \dots, \mathbf{x}_N\}.

This theorem is if and only if, meaning we could explicitly construct a kernel function k(,)k(\cdot, \cdot) for a given φ()\varphi(\cdot) or we could take a kernel function and use it without having an explicit representation of φ()\varphi(\cdot).

If we assume everything is real-valued, then we can demonstrate this fact easily. Let K\mathbf{K} be the positive semidefinite Gram matrix above. Since it is real and symmetric, it has an eigendecomposition of the form

K=UΛU \mathbf{K} = \mathbf{U}^{\top} \boldsymbol{\Lambda} \mathbf{U}

where Λ=diag(λ1,,λN)\boldsymbol{\Lambda} = \text{diag}(\lambda_1, \dots, \lambda_N). Since K\mathbf{K} is positive definite, then λn0\lambda_n \geq 0 and the square root is real-valued. We can write an element of K\mathbf{K} as

Kij=[Λ1/2U:,i][Λ1/2U:,j]. \mathbf{K}_{ij} = \begin{bmatrix} \boldsymbol{\Lambda}^{1/2} & \mathbf{U}_{:, i} \end{bmatrix}\begin{bmatrix} \boldsymbol{\Lambda}^{1/2} \\ \mathbf{U}_{:, j} \end{bmatrix}.

Define φ(xi)=Λ1/2U:,i\varphi(\mathbf{x}_i) = \boldsymbol{\Lambda}^{1/2} \mathbf{U}_{:, i}. Therefore, if our kernel function is positive semidefinite—if it defines a Gram matrix that is positive semidefinite—then there exists a function φ:XV\varphi: \mathcal{X} \mapsto \mathcal{V} such that

k(x,y)=φ(x)φ(y) k(\mathbf{x}, \mathbf{y}) = \varphi(\mathbf{x})^{\top} \varphi(\mathbf{y})

where X\mathcal{X} is the space of samples.

Infinite-dimensional feature space

An interesting consequence of the kernel trick is that kernel methods, equipped with the appropriate kernel function, can be viewed as operating in infinite-dimensional feature space. As an example, consider the radial basis function (RBF) kernel,

kRBF(x,y)=exp(γxy2). k_{\texttt{RBF}}(\mathbf{x}, \mathbf{y}) = \exp\Big(-\gamma\lVert\mathbf{x}-\mathbf{y}\rVert^2\Big).

Let’s take it for granted that this is a valid positive semidefinite kernel. Let kpoly(r)k_{\texttt{poly(r)}} denote a polynomial kernel of degree rr, and let γ=1/2\gamma = 1/2. Then

kRBF(x,y)=exp(12xy2)=exp(12xy,xy)=exp(12[x,xyy,xy])=exp(12[x,xx,y[y,xy,y]])=exp(12[x,x+y,y2x,y])=exp(12x2)exp(12y2)exp(2x,y) \begin{aligned} k_{\texttt{RBF}}(\mathbf{x}, \mathbf{y}) &= \exp\Big(-\frac{1}{2} \lVert\mathbf{x}-\mathbf{y}\rVert^2\Big) \\ &= \exp\Big(-\frac{1}{2} \langle \mathbf{x}-\mathbf{y}, \mathbf{x}-\mathbf{y} \rangle \Big) \\ &\stackrel{\star}{=} \exp\Big(-\frac{1}{2} \Big[ \langle \mathbf{x}, \mathbf{x}-\mathbf{y} \rangle - \langle \mathbf{y}, \mathbf{x}-\mathbf{y} \rangle \Big] \Big) \\ &\stackrel{\star}{=} \exp\Big(-\frac{1}{2} \Big[ \langle \mathbf{x}, \mathbf{x} \rangle - \langle \mathbf{x}, \mathbf{y} \rangle - \big[ \langle \mathbf{y}, \mathbf{x} \rangle - \langle \mathbf{y}, \mathbf{y} \rangle \big] \rangle \Big] \Big) \\ &= \exp\Big(-\frac{1}{2} \Big[ \langle \mathbf{x}, \mathbf{x} \rangle + \langle \mathbf{y}, \mathbf{y} \rangle - 2 \langle \mathbf{x}, \mathbf{y} \rangle \Big] \Big) \\ &= \exp\Big(-\frac{1}{2} \rVert \mathbf{x} \lVert^2 \Big) \exp\Big(-\frac{1}{2} \rVert \mathbf{y} \lVert^2 \Big) \exp\Big(- 2 \langle \mathbf{x}, \mathbf{y} \rangle \Big) \end{aligned}

Above, the two steps labeled \star leverage the fact that

u+v,w=u,w+v,w \langle \mathbf{u} + \mathbf{v}, \mathbf{w} \rangle = \langle \mathbf{u}, \mathbf{w} \rangle + \langle \mathbf{v}, \mathbf{w} \rangle

in general for inner products (see here){:target=”_blank”}. Now let CC be a constant,

Cexp(12x2)exp(12y2). C \equiv \exp\Big(-\frac{1}{2} \rVert \mathbf{x} \lVert^2 \Big) \exp\Big(-\frac{1}{2} \rVert \mathbf{y} \lVert^2 \Big).

and note that the Taylor expansion of ef(x)e^{f(x)} is

ef(x)=r=0[f(x)]rr!. e^{f(x)} = \sum_{r=0}^{\infty} \frac{[f(x)]^r}{r!}.

We can write the RBF kernel as

kRBF(x,y)=Cexp(2x,y)=Cr=0x,yrr!=Crkpoly(r)(x,y)r!. \begin{aligned} k_{\texttt{RBF}}(\mathbf{x}, \mathbf{y}) &= C \exp\big(- 2 \langle \mathbf{x}, \mathbf{y} \rangle \big) \\ &= C \sum_{r=0}^{\infty} \frac{ \langle \mathbf{x}, \mathbf{y} \rangle^r}{r!} \\ &= C \sum_{r}^{\infty} \frac{k_{\texttt{poly(r)}}(\mathbf{x}, \mathbf{y})}{r!}. \end{aligned}

So the RBF kernel can be viewed as an infinite sum over polynomial kernels. As rr increases, each polynomial kernel lifts the data into higher dimensions, and the RBF kernel is an infinite sum over these kernels. (NB: kernel functions are linear operators.) Matthew Bernstein has a nice derivation more explicitly showing that φRBF:RDR\varphi_{\texttt{RBF}}: \mathbb{R}^D \mapsto \mathbb{R}^{\infty}, but I think the above logic captures the main point.

Why the distinction?

Why did I stress the distinction beween lifting and the kernel trick? Good research is about having a line of attack on a problem. A layperson might suggest good problems solve, but researchers find good solvable problems. This is the difference between saying, “We should cure cancer,” and the work done by an oncology researcher.

For similar reasons, I think it’s important to disentangle the lifting trick from the kernel trick. Without the mathematics of Mercer and others, we might have discovered the lifting trick but found it entirely useless in practice. With high probability, such currently useless solutions exist in the research wild today. It is mathematical relationship between kernel functions and lifting that is the eureka moment for the kernel trick.

   

Acknowledgements

I thank multiple readers for emailing me about various typos in this post.