MIT 6.790 — Lecture 7

Kernels, Evaluations, and Hyperparameter Learning

§ Kernels: Properties and Examples

Problem. Suppose our input space is X=R2\mathcal{X} = \mathbb{R}^2, and our kernel is k:R2×R2→Rk: \mathbb{R}^2 \times \mathbb{R}^2 \to \mathbb{R} via k(a,b):=(a⊤b)2k(a, b) := (a^{\top}b)^2. What feature map ϕ\phi is represented by this kernel, and what is the implied feature dimension?

Solution: Given inputs a=[a1a2]⊤a = \begin{bmatrix} a_1 & a_2 \end{bmatrix}^{\top} and b=[b1b2]⊤b = \begin{bmatrix} b_1 & b_2 \end{bmatrix}^{\top}, the kernel can be expressed as:

k(a,b)=a12b12+2a1a2b1b2+a22b22=[a122a1a2a22]⊤[b122b1b2b22].k(a, b) = a_1^2b_1^2 + 2a_1a_2b_1b_2 + a_2^2b_2^2 = \begin{bmatrix} a_1^2 \\ \sqrt{2} a_1a_2 \\ a_2^2 \end{bmatrix}^{\top} \begin{bmatrix} b_1^2 \\ \sqrt{2} b_1b_2 \\ b_2^2 \end{bmatrix}.

Having “decoupled” the kernel in this way, it is now clear that the feature map is ϕ(x):=[x122x1x2x22]⊤\phi(x) := \begin{bmatrix} x_1^2 & \sqrt{2}x_1x_2 & x_2^2 \end{bmatrix}^{\top}, and the implied feature dimension is 33. (More generally, the implied feature dimension is the “number of terms” in the kernel.)

To really emphasize how much cleaner kernels are to work with than features, here's a much larger example.

Problem. Suppose our input space is X=RD\mathcal{X} = \mathbb{R}^D, and our kernel is k:RD×RD→Rk: \mathbb{R}^D \times \mathbb{R}^D \to \mathbb{R} via k(a,b):=(a⊤b)Mk(a, b) := (a^{\top}b)^M. What is the implied feature dimension? Compare the difficulty of computing the matrices KK versus Φ\Phi (using the notation from Lecture 6).

Solution: The feature dimension is the “number of terms” in the expansion of k(a,b):=(a⊤b)Mk(a, b) := (a^{\top}b)^{M}. This is equivalent to the number of monomials of degree MM in DD variables, which (by Stars and Bars) is:

feature dimension=(M+D−1D−1).\text{feature dimension} = \binom{M + D - 1}{D - 1}.

Now for the computational difficulty comparisons.

Evidently, it is much more convenient to work with kernels than it is to work with features.

And it turns out that kernels do everything that features can do.

Theorem. (Mercer's Theorem) If, for every choice of inputs {x(n)}n=1N\{x^{(n)}\}_{n = 1}^N, the kernel matrix Ki,j:=k(x(i),x(j))K_{i, j} := k(x^{(i)}, x^{(j)}) is symmetric and positive semidefinite, and the kernel k ⁣:X×X→Rk\colon \mathcal{X} \times \mathcal{X} \to \mathbb{R} is continuous, then kk corresponds to some feature map ϕ\phi.

The proof is fairly nontrivial, so we omit it. The upshot is that we can now declare any kernel we'd like (so long as it satisfies the conditions of Mercer's theorem), and we need not have any concern for what features are involved.

Remark. If we can choose kernels arbitrarily, that raises the question: what makes one choice of kernel better than another?

Here's one possible answer, from the “feature” perspective.

Answer 1. The kernel ultimately describes a collection of PP features, and a ridge regression models the relationship between X\mathcal{X} and Y\mathcal{Y} via a linear combination of those features, with preference towards linear combinations with smaller coefficients.

Therefore, a good kernel should imply a collection of features that is capable of modeling the relationship between X\mathcal{X} and Y\mathcal{Y} using relatively small coefficients.

Here's another possible answer, from the “hypothesis” perspective.

Answer 2. Recall from Lecture 6 that, ultimately, our hypothesis h ⁣:X→Rh\colon \mathcal{X} \to \mathbb{R} looks like h(x)=Y⊤(λIN+K)−1k(x)h(x) = Y^{\top}(\lambda I_N + K)^{-1}k(x). Expressed component-wise, that looks like:

h(x)=[(λIN+K)−1Y]⊤k(x)=∑n=1Nαn⋅k(x(n),x)    where    α=(λIN+K)−1Y∈RN.h(x) = \left[(\lambda I_N + K)^{-1}Y\right]^{\top} k(x) = \sum_{n = 1}^N \alpha_n \cdot k(x^{(n)}, x) ~~~ \text{ where } ~~~ \alpha = (\lambda I_N + K)^{-1}Y \in \mathbb{R}^N.

In other words, h(x)h(x) is a weighted sum of the αn\alpha_n, with weights given by k(x(n),x)k(x^{(n)}, x). This brings out a sense in which k(a,b)k(a, b) reflects a similarity score between aa and bb. The more similar x(n)x^{(n)} is to xx, the more impact αn\alpha_n has on h(x)h(x).

The kernel can also be seen as a similarity score by recalling that k(a,b):=ϕ⊤(a)ϕ(b)k(a, b) := \phi^{\top}(a) \phi(b) is just the dot product of ϕ(a)\phi(a) and ϕ(b)\phi(b) in the feature space RP\mathbb{R}^P. Indeed, if we normalize the kernel so that k(x,x)≡1k(x, x) \equiv 1, then we have

∥ϕ(a)−ϕ(b)∥2=2−2k(a,b)\|\phi(a) - \phi(b)\|^2 = 2 - 2 k(a, b)

by the Law of Cosines. The similarity score k(a,b)k(a, b) captures the similarity of ϕ(a)\phi(a) and ϕ(b)\phi(b) as positions in the feature space.

Definition. One common choice of kernel is the Gaussian or RBF kernel, given by:

k(a,b):=exp⁡(−∥a−b∥22ℓ2)  for some ℓ>0.k(a, b) := \exp\left( -\frac{\|a - b\|^2}{2\ell^2} \right) ~ \text{ for some } \ell > 0.

Note that this kernel has an infinite-dimensional feature map. (All of the work done in the previous lecture works just as well in infinite-dimensional vector spaces, so there's nothing to be worried about.)

§ Recap: Two Approaches to Regression

Let's summarize all that we've discussed about regression so far.

Given some data {(x(n),y(n))}n=1N\{(x^{(n)}, y^{(n)})\}_{n = 1}^N, there are two approaches to learning a hypothesis h ⁣:X→Yh \colon \mathcal{X} \to \mathcal{Y}.

h(x):=θ⊤ϕ(x)  where  θ=Φ⊤(λIN+K)−1Y     ∥     h(x):=Y⊤(λIN+K)−1k(x).h(x) := \theta^{\top}\phi(x) ~ \text{ where } ~ \theta = \Phi^{\top}(\lambda I_N + K)^{-1}Y ~~~~~ \parallel ~~~~~ h(x) := Y^{\top}(\lambda I_N + K)^{-1}k(x).

These two approaches conceptually differ in the following way:

Besides their conceptual differences, however, these two approaches are the same: the hypotheses h(x)h(x) they generate are identical.

§ Learning Algorithms and Hyperparameters

There are two types of objects that appear in the previous section, and it's important to distinguish them clearly.

In the previous section, the two approaches described are both (the same) learning algorithm, whereas the results h(x)h(x) produced by these approaches are hypotheses (or decision rules).

Notably, the behavior of the hypothesis h(x)h(x), as presented by the first of the two approaches, depends on a parameter θ\theta. In contrast, the behavior of the learning algorithm Aλ\mathcal{A}_{\lambda} depends on a hyperparameter λ\lambda.

§ Evaluating Learning Algorithms

Recall that in Lecture 1 we defined the risk of a hypothesis hh to be:

risk(h):=E[L(Y,h(X))],\mathrm{risk}(h) := \mathbb{E}\left[L(Y, h(X))\right],

where L ⁣:Y×Y→RL \colon \mathcal{Y} \times \mathcal{Y} \to \mathbb{R} is a loss function of our choice. Note that risk is a property of a hypothesis, not of a learning algorithm—but the risk of a hypothesis can be controlled by carefully setting the hyperparameter of its learning algorithm.

Here's the focus of this section:

Question. How do we evaluate a learning algorithm Aλ\mathcal{A}_{\lambda} to determine the best values for its hyperparameters?

As with bootstrapping (discussed in Lecture 13 of 18.650), the idea is to empirically estimate the expected risk of the hypothesis produced, using our own training data as validation data. Stated more precisely:

  1. Randomly partition the data D\mathcal{D} into a training set DT\mathcal{D}_T and a validation set DV\mathcal{D}_V.

  2. Run the learning algorithm on the training set to obtain a hypothesis hDT,λ:=Aλ(DT)h_{\mathcal{D}_T, \lambda} := \mathcal{A}_{\lambda}(\mathcal{D}_T).

  3. Estimate the risk of this hypothesis to be the average loss across the validation data DV\mathcal{D}_V.

    risk^(hDT,λ)=1∣DV∣∑(x,y)∈DVL(y,hDT,λ(x)).\widehat{\mathrm{risk}}(h_{\mathcal{D}_T, \lambda}) = \frac{1}{|\mathcal{D}_V|} \sum_{(x, y) \in \mathcal{D}_V} L(y, h_{\mathcal{D}_T, \lambda}(x)).
  4. Repeat the previous three steps across many trials, and average the results from step 3 across all trials.

The Law of Large Numbers says that the risk estimates in step 3 will approach the true risk for large ∣DV∣|\mathcal{D}_V|, assuming all data is sampled i.i.d. from the same distribution. You can then quantify the (epistemic) uncertainty in the estimate of expected risk produced by step 4 by using basic statistics as well.

Remark. Pay attention to the difference between risk and expected risk.

Whereas the parameter θ\theta is evaluated by its risk and learned by the learning algorithm, the hyperparameter λ\lambda is evaluated by its expected risk and learned by us.