Consider a regression task.
We are given a training set {(x(i),y(i))}i=1n, where x(i)∈Rd and y(i)∈R.
In vanilla linear regression, for a given input x=[x1,x2,…,xd]⊤∈Rd, we model the output as a linear combination of the features of x, where each feature xi is weighted by a parameter θi:
f(x;θ)=θ1x1+θ2x2+…+θdxd=θ⊤x.
To quantify how well a model f(x;θ) fits the training data, we use mean squared error loss:
We then find a parameterization θ that minimizes loss L(θ).
This optimal parameterization θ can be computed in closed form using the normal equations, or found using gradient descent.
Feature maps
Rather than fitting a linear model over the d-dimensional inputs, we can first transform the inputs to a higher-dimensional space using a “feature map”, and then fit a linear model over these transformed inputs.
We can roughly think of a feature map as enriching the input – it extracts additional features of the input, which can then be used directly by the linear model for prediction.
Formally, a feature map is simply a function ϕ:Rd→Rk, where k is the dimensionality of the “feature space”.1
We now model the output as a linear combination of the features computed by the feature map:
f(x;θ)=θ1ϕ1(x)+θ2ϕ2(x)+…+θkϕk(x)=θ⊤ϕ(x).
Our aim then becomes finding a parameterization θ∈Rk such that the following loss is minimized:
Notice that each update to θ is a linear combination of the transformed training samples {ϕ(x(i))}.
If we initialize θ0=0, then after each update, θ will remain in the span of {ϕ(x(i))}.
Thus, we can express θ as:
θ=i=1∑nαiϕ(x(i)),
for some coefficients αi∈R.
What if θ0=0?
Even without initializing θ0=0, we can assume that there exists an optimal θ that lies in the span of {ϕ(x(i))}.
Let’s decompose θ into components parallel and orthogonal to the span of {ϕ(x(i))}:
θ=θ∥+θ⊥.
The component θ⊥ does not affect the predictions on the training data:
f(x(i);θ)=θ⊤ϕ(x(i))=(θ∥⊤+θ⊥⊤)ϕ(x(i))=θ∥⊤ϕ(x(i))+θ⊥⊤ϕ(x(i))=θ∥⊤ϕ(x(i))since θ⊥⊤ϕ(x(i))=0 for all i∈[n].
Thus, any component θ⊥ orthogonal to the span of {ϕ(x(i))} does not influence the loss:
L(θ)=L(θ∥).
Therefore, we can, without loss of generality, assume that an optimal θ has θ⊥=0 and express θ as:
θ=i=1∑nαiϕ(x(i)).
Substituting this back into the prediction function:
Examining this expression, we see that the prediction for a new input x is a weighted sum of inner products between ϕ(x) and each of the training samples ϕ(x(i)).
We’ve reduced the problem of finding an optimal θ∈Rk to finding optimal weight coefficients [α1,α2,…,αn]⊤=:α∈Rn.
Gradient descent update step for α
We can write the gradient descent update step for α in terms of inner products as well:
As derived above, the prediction function can be expressed as a weighted sum of inner products between the transformed training samples and a new input:
f(x;θ)=i=1∑nαi⟨ϕ(x(i)),ϕ(x)⟩.
Computing these inner products naively would require mapping each input x(i) and x into the high-dimensional feature space Rk, which can be computationally expensive or infeasible when k is very large or infinite.
Amazingly, for some particular feature maps, we can actually compute the inner products ⟨ϕ(x(i)),ϕ(x)⟩ without ever explicitly computing ϕ(x(i)) or ϕ(x).
We can define a kernel functionK:Rd×Rd→R that corresponds to the inner product in feature space:
K(x,z)=⟨ϕ(x),ϕ(z)⟩.
Now we can rewrite the prediction function in terms of this kernel function:
f(x;θ)=i=1∑nαiK(x(i),x).
In this form, we no longer need to explicitly compute or store ϕ(x).
Instead, we compute K(x(i),x) directly from x(i) and x, working only in the original input space Rd.
This kernel trick allows us to effectively operate in high-dimensional feature space, without ever needing to explicitly map inputs to that high-dimensional feature space.
Examples of kernel functions
Many commonly used kernels, such as the polynomial or Gaussian kernel, provide a way to compute the inner product ⟨ϕ(x),ϕ(z)⟩ directly in the input space (i.e. working in Rd).
These kernels are efficient to compute, whereas explicitly constructing the corresponding feature map ϕ would often be computationally expensive or impossible, in particular when ϕ maps to a very high- or infinite-dimensional space.
Example: the polynomial kernel
The polynomial kernel of degree p is defined as follows:
K(x,z)=(x⊤z+1)p.
Suppose we have x=[x1,x2]⊤∈R2, and we would like to consider all monomials up to degree p=2.
The corresponding feature map ϕ(x) would look something like ϕ(x)=[1,x1,x2,x12,x22,x1x2]⊤.
For this case, there’s a trick to compute the inner product in this transformed space without explicitly computing ϕ(x):
K(x,z)=(x⊤z+1)2.
We can check explicitly that this formula gives the same result as computing the inner product in the transformed space:
We can see that the simple kernel function K(x,z)=(x⊤z+1)2 is equivalent to computing the inner product for the feature map ϕ(x)=[1,2x1,2x2,x12,x22,2x1x2]⊤.
More generally, we can use the following formula to consider all monomials up to degree p:
K(x,z)=(x⊤z+1)p.
Note that the effective number of features in the feature space is (pd+p).2
Writing this out and fixing the degree p, we see that the effective dimensionality grows with Ω(dp):
By using the kernel trick for polynomials of degree p, we can effectively operate in a feature space of dimension Ω(dp), while working only in the original input space Rd.
Example: the Gaussian kernel
The Gaussian kernel, also known as the radial basis function (RBF) kernel, is defined as follows:
K(x,z)=exp(−2σ2∥x−z∥22),
where σ>0 is the “bandwidth” parameter, controlling the “sharpness” of the kernel.
Notice that the kernel computes the “similarity” between two points x and z based on their Euclidean distance.
If two points x and z are geometrically close, then ∥x−z∥22 is small, and K(x,z) is close to 1.
If two points are far apart, then ∥x−z∥22 is large, and K(x,z) is close to 0.
When used for prediction, the Gaussian kernel yields a sort of “local” prediction – points that are close to x will have a stronger influence on the prediction for x than points that are far away.
Also note that the feature map corresponding to the Gaussian kernel is infinite dimensional, and therefore cannot be computed exactly without the kernel trick.
Mercer’s theorem
A kernel function K is only valid if it corresponds to the inner product in some feature space.
It turns out that there is a simple condition that is necessary and sufficient for a function to be a valid kernel.
The condition is given by Mercer’s theorem.
A function K:Rd×Rd→R is a valid (Mercer) kernel if and only if for any finite set {x(1),…,x(n)}, the corresponding kernel matrix K is symmetric positive semi-definite, where the kernel matrix is defined as Kij=K(x(i),x(j)):
Now let’s consider the case where our model f(x;θ):Rd→R is a very simple neural network with a single hidden layer, and a single-dimensional output:
f(x;θ)=Woutσ(Winx+bin),
where Win∈Rh×d, Wout∈R1×h, bin∈Rh, and σ is an element-wise nonlinear activation function.
We use θ to denote a flattened vector of all parameters in the network.
Let’s say the network is initialized with parameters θ0, and it is then trained via gradient descent to minimize empirical loss L(θ).
Now, let’s assume that the weights of the network don’t shift too much during training, i.e. θ≈θ0.3
In this regime, where the weights don’t shift too much from their initial values, we can use a first-order Taylor expansion to approximate the prediction function:
f(x;θ)≈f(x;θ0)+(θ−θ0)⊤∇θf(x;θ0).
We can rearrange this expression to group terms that are constant after initialization:
f(x;θ)≈f(x;θ0)+(θ−θ0)⊤∇θf(x;θ0)=constant after initializationf(x;θ0)−θ0⊤∇θf(x;θ0)+linear function of θθ⊤∇θf(x;θ0).
Let’s write c(x;θ0):=f(x;θ0)−θ0⊤∇θf(x;θ0), and ϕ(x;θ):=∇θf(x;θ):
The θ⊤ϕ(x;θ0) term looks very familiar!
In this form, we can see clearly that the model is fitting a linear model in the feature space defined by ϕ(x;θ0)=∇θf(x;θ0).
The corresponding kernel function is known as the neural tangent kernel (NTK)[1]Neural Tangent Kernel: Convergence and generalization in neural networks[link] Arthur Jacot, Franck Gabriel, and Clement Hongler. Advances in Neural Information Processing Systems. 2018.:
K(x,z;θ)=⟨ϕ(x;θ),ϕ(z;θ)⟩=⟨∇θf(x;θ),∇θf(z;θ)⟩.
Intuitively, the neural tangent kernel measures the similarity between two inputs x and z by measuring the similarity of their gradients with respect to parameters θ.
Training dynamics via NTK
Now let’s consider how gradient descent updates the parameters θ during training.
In practice, gradient descent makes discrete updates to the parameters θ, in the direction of −∇θL(θ):
θ(t+1)=θ(t)−η∇θL(θ(t)).
Rearranging, we get the following:
ηθ(t+1)−θ(t)=−∇θL(θ(t)).
Taking the limit as the step size η→0, we can analyze how the parameters θ change with an infinitesimally-small step size.
This is known as the gradient flow, and we write it as dtdθ, thinking of it as the change in parameters with respect to continuous time during training:
dtdθ=−∇θL(θ).
Now let’s consider how the function outputf(x;θ) changes with respect to time t during training:
This expression describes how the model output f(x;θ) on some datapoint x changes, in terms of its “similarity” to each datapoint x(i) (as measured by the neural tangent kernel) and the model’s error (f(x(i);θ)−y(i)) on those datapoints.
To gain intuition, consider a simple example.
Let’s say, for a given θ(t), x is very “similar” to training example x(1) and very “dissimilar” to all other training examples:
K(x,x(1);θ(t))K(x,x(j);θ(t))≈1≈0for all j=1.
Let’s assume the model’s prediction on x(1) is too high. Concretely, say f(x(1);θ(t))−y(1)≈10.
Here, we can see concretely that the model will update towards decreasing its output on x.
Intuitively, this makes a lot of sense.
The model prediction was too high on x(1), and so the gradient update should push it down.
But since x is “close” to x(1) (in neural tangent kernel feature space), the adjustment will propagate to x as well, effectively lowering the prediction on x.
The neural tangent kernel K(x,z;θ) can thus be thought of as measuring how much a change in the output f(z;θ) impacts the output of f(x;θ).
References
References cited in the text are listed first, in order of citation; additional references follow, ordered alphabetically.
Neural Tangent Kernel: Convergence and generalization in neural networks[link] Arthur Jacot, Franck Gabriel, and Clement Hongler. Advances in Neural Information Processing Systems. 2018.
Understanding the Neural Tangent Kernel[link] Rajat Vadiraj Dwaraknath. 2019.
CS229 lecture notes, part V: Kernel methods[link] Andrew Ng and Tengyu Ma. 2019.
Some math behind Neural Tangent Kernel[link] Lilian Weng. 2022.
Footnotes
It is usually the case that k≫d: the dimensionality of the “feature space” is usually much larger than the dimensionality of the “input space”. It is in these cases when the “kernel trick” becomes particularly useful, as we will see later. ↩
This can be shown by applying the “stars and bars” theorem. The number of monomials of degree exactly p in d variables can be thought of as the number of ways to distribute p “stars” into d “bins” (one bin per input feature), and this corresponds to (pp+d−1). For the number of monomials of degree at mostp, we consider d+1 “bins” (one bin per input feature, plus one bin for unused degrees), and this corresponds to (pp+d). ↩
If you’re bothered by this assumption, you’re right to be bothered. This assumption that weights remain close to their initial values is known as the “lazy regime”. This assumption is valid in the limit as network width approaches ∞, and is a useful lens for analyzing networks theoretically. However, this assumption does not hold in practice for finite width networks. In practice, weights change more than a negligibly small amount, and networks empirically exhibit feature learning. ↩