Simple Algorithm
Machine Learning · Convergence · 26 min

Scaling Kernel Methods

Nyström approximation and random Fourier features.

Exact kernel methods hit a wall: an n×nn\times n matrix and an n3n^3 solve, which at a million examples means terabytes of memory and about 101710^{17} operations. Two ideas break the wall. Replace the Gram matrix by a low-rank approximation built from a few landmarks or a few random features, and replace exact factorizations by well-preconditioned iterations. The surprise is statistical: far less computation than n3n^3 loses nothing in accuracy.

What you’ll learn

  • Quantify the time and memory of exact kernel ridge regression, and explain why fast eigenvalue decay and the effective degrees of freedom make low-rank approximation possible.
  • Derive the Nyström approximation as a projection, and Nyström kernel ridge regression with its O(nm2)\order{nm^2} cost; state column-sampling guarantees.
  • Derive random Fourier features from Bochner's theorem and bound their approximation error.
  • Explain why about nlog⁡n\sqrt n\log n landmarks or features suffice statistically, and how preconditioned solvers (FALKON, EigenPro) and GPU iterations reach millions of points.

The cubic wall, and why it can be broken

nnGram matrix (8-byte entries)Cholesky, about n3/3n^3/3 operations
10410^40.8 GB3.3×10113.3\times10^{11}
10510^580 GB3.3×10143.3\times10^{14}
10610^68 TB3.3×10173.3\times10^{17}
Exact kernel ridge regression or Gaussian process regression. At 101310^{13} operations per second the last row is about nine hours of arithmetic, if 8 TB of memory could be found. Memory, not time, is usually the first wall.

The wall can be broken because the Gram matrix of a smooth kernel is nearly low-rank. Its eigenvalues s1≥s2≥…s_1\ge s_2\ge\dots decay fast (faster than any power of ii for the Gaussian kernel when the inputs lie in a bounded region), and kernel ridge regression barely uses the small ones: its fitted values are y^=K(K+λI)−1y\hat{\mathbf y}=K(K+\lambda I)^{-1}\mathbf y, which multiplies the ii-th eigendirection of y\mathbf y by si/(si+λ)s_i/(s_i+\lambda). The number of directions that survive is the effective degrees of freedom,

df(λ)=tr⁡[K(K+λI)−1]=∑i=1nsisi+λ,\mathrm{df}(\lambda)=\operatorname{tr}\big[K(K+\lambda I)^{-1}\big]=\sum_{i=1}^n\frac{s_i}{s_i+\lambda},

typically far smaller than nn. An approximation that keeps the top few hundred or thousand directions and discards the rest discards mostly what the penalty would have suppressed anyway.

Nyström: a few landmarks

DerivationThe Nyström approximation is a projection
  1. K ≈ K~=Knm Kmm+ KmnK\ \approx\ \tilde K=K_{nm}\,K_{mm}^{+}\,K_{mn}

    Pick m≪nm\ll n landmarks x~1,…,x~m\tilde{\mathbf x}_1,\dots,\tilde{\mathbf x}_m, for instance a uniform random subset of the training points. Let KnmK_{nm} hold the kernel values between all points and the landmarks, and KmmK_{mm} those among the landmarks. The Nyström approximation is

  2. ⟨Pφ(x),Pφ(x′)⟩=km(x)⊤Kmm+ km(x′)=k~(x,x′)\langle P\varphi(\mathbf x),P\varphi(\mathbf x')\rangle=\mathbf k_m(\mathbf x)^\top K_{mm}^{+}\,\mathbf k_m(\mathbf x')=\tilde k(\mathbf x,\mathbf x')

    Why this formula: let PP be the orthogonal projection of feature space onto span⁡{φ(x~1),…,φ(x~m)}\operatorname{span}\{\varphi(\tilde{\mathbf x}_1),\dots,\varphi(\tilde{\mathbf x}_m)\}. With km(x)=(k(x~j,x))j=1m\mathbf k_m(\mathbf x)=\big(k(\tilde{\mathbf x}_j,\mathbf x)\big)_{j=1}^m, the projected features have inner products

  3. K−K~=[⟨(I−P)φ(xi),(I−P)φ(xj)⟩]i,j⪰0K-\tilde K=\Big[\big\langle(I-P)\varphi(\mathbf x_i),(I-P)\varphi(\mathbf x_j)\big\rangle\Big]_{i,j}\succeq0

    So K~\tilde K is an honest Gram matrix (of projected features), exact on the landmark columns, and the error K−K~K-\tilde K is the Gram matrix of the residuals (I−P)φ(xi)(I-P)\varphi(\mathbf x_i), hence positive semidefinite: Nyström never overestimates. The name comes from Nyström's quadrature method for integral equations, which Williams & Seeger (2001) applied to kernel machines.

  4. (KmnKnm+λKmm)β=Kmny,f^(x)=∑j=1mβj k(x~j,x)\big(K_{mn}K_{nm}+\lambda K_{mm}\big)\boldsymbol\beta=K_{mn}\mathbf y,\qquad\hat f(\mathbf x)=\sum_{j=1}^m\beta_j\,k(\tilde{\mathbf x}_j,\mathbf x)

    Nyström kernel ridge regression. Restrict the function to the landmark sections, f=∑j=1mβjk(x~j,⋅)f=\sum_{j=1}^m\beta_jk(\tilde{\mathbf x}_j,\cdot), and minimize ∥y−Knmβ∥2+λβ⊤Kmmβ\norm{\mathbf y-K_{nm}\boldsymbol\beta}^2+\lambda\boldsymbol\beta^\top K_{mm}\boldsymbol\beta. Forming KmnKnmK_{mn}K_{nm} costs O(nm2)\order{nm^2}, the solve O(m3)\order{m^3}, the memory O(nm)\order{nm}:

Check your understanding

Why is the Nyström error K−KnmKmm+KmnK-K_{nm}K_{mm}^+K_{mn} always positive semidefinite?

  1. Because the landmarks are sampled at random
  2. Because it is the Gram matrix of the residual features (I−P)φ(xi)(I-P)\varphi(\mathbf x_i) left after projecting onto the landmarks' span
  3. Because Kmm+K_{mm}^+ is positive semidefinite
  4. It is not; Nyström errors can have either sign
Show the answer

B. Because it is the Gram matrix of the residual features (I−P)φ(xi)(I-P)\varphi(\mathbf x_i) left after projecting onto the landmarks' span

K~\tilde K is the Gram matrix of the projected features Pφ(xi)P\varphi(\mathbf x_i), and φ=Pφ+(I−P)φ\varphi=P\varphi+(I-P)\varphi with orthogonal parts, so K=K~+K=\tilde K+ (Gram matrix of residuals). Any Gram matrix is PSD. The approximation only ever loses energy, whichever landmarks are used.

Random Fourier features

DerivationFrom Bochner to features
  1. k(x,x′)=Eω∼p[cos⁡(ω⊤(x−x′))]k(\mathbf x,\mathbf x')=\E_{\boldsymbol\omega\sim p}\big[\cos\big(\boldsymbol\omega^\top(\mathbf x-\mathbf x')\big)\big]

    Bochner writes the kernel as an average over random frequencies:

  2. k(x,x′)=Eω,b[ 2cos⁡(ω⊤x+b) cos⁡(ω⊤x′+b) ]k(\mathbf x,\mathbf x')=\E_{\boldsymbol\omega,b}\big[\,2\cos(\boldsymbol\omega^\top\mathbf x+b)\,\cos(\boldsymbol\omega^\top\mathbf x'+b)\,\big]

    Split the cosine with a random phase. Since 2cos⁡(a+b)cos⁡(c+b)=cos⁡(a−c)+cos⁡(a+c+2b)2\cos(a+b)\cos(c+b)=\cos(a-c)+\cos(a+c+2b), and the second term averages to zero over b∼Unif[0,2π]b\sim\mathrm{Unif}[0,2\pi]:

  3. z(x)=2D (cos⁡(ω1⊤x+b1),…,cos⁡(ωD⊤x+bD)),E[z(x)⊤z(x′)]=k(x,x′)\mathbf z(\mathbf x)=\sqrt{\tfrac2D}\,\big(\cos(\boldsymbol\omega_1^\top\mathbf x+b_1),\dots,\cos(\boldsymbol\omega_D^\top\mathbf x+b_D)\big),\qquad\E\big[\mathbf z(\mathbf x)^\top\mathbf z(\mathbf x')\big]=k(\mathbf x,\mathbf x')

    Replace the expectation by an average over DD independent draws. The result is an explicit, data-independent feature map whose inner products are unbiased for the kernel: random Fourier features (Rahimi & Recht, 2007).

  4. Pr⁡[∣z(x)⊤z(x′)−k(x,x′)∣≥ε]≤2 e−Dε2/8\Pr\Big[\big|\mathbf z(\mathbf x)^\top\mathbf z(\mathbf x')-k(\mathbf x,\mathbf x')\big|\ge\varepsilon\Big]\le2\,e^{-D\varepsilon^2/8}

    Each term 2cos⁡(⋅)cos⁡(⋅)2\cos(\cdot)\cos(\cdot) lies in [−2,2][-2,2], so Hoeffding's inequality bounds the error at a fixed pair of points. Uniformly over a compact set M⊂Rd\mathcal M\subset\R^d, a union bound over a net plus a Lipschitz argument shows that D=O((d/ε2)log⁡(σpdiam⁡M/ε))D=\order{(d/\varepsilon^2)\log(\sigma_p\operatorname{diam}\mathcal M/\varepsilon)} features give error at most ε\varepsilon everywhere with constant probability, where σp2=E∥ω∥2\sigma_p^2=\E\norm{\boldsymbol\omega}^2. Rahimi & Recht (2007) proved this for paired features cos⁡(ωj⊤x)\cos(\boldsymbol\omega_j^\top\mathbf x), sin⁡(ωj⊤x)\sin(\boldsymbol\omega_j^\top\mathbf x), each pair contributing cos⁡(ωj⊤(x−x′))\cos(\boldsymbol\omega_j^\top(\mathbf x-\mathbf x')) to the inner product; Sutherland & Schneider (2015) proved a bound of the same order, with worse constants, for the random-phase features above, which also have the higher variance of the two for the Gaussian kernel at equal dimension.

Predict before you look

With D=1,000D=1{,}000 random Fourier features the kernel is approximated to about 0.030.03. Roughly how many features bring the typical error down to 0.010.01?

Show the answer

9000 features

The error of an average of DD independent terms shrinks like 1/D1/\sqrt D, so a threefold smaller error needs 32=93^2=9 times the features: about 9,000. Every extra digit costs a hundred times more features, which is why random features are used for learning, where an error well below the statistical error of order 1/n1/\sqrt n is wasted, rather than for evaluating kernels precisely.

Approximating the kernel to accuracy ε\varepsilon everywhere is the wrong target for learning. The regularized solution only uses the directions above the noise level, and the statistical error is already of order 1/n1/\sqrt n. Rudi & Rosasco (2017) showed that for kernel ridge regression with λ\lambda chosen for the O(1/n)\order{1/\sqrt n} rate, about nlog⁡n\sqrt n\log n random features suffice to keep that rate. For n=106n=10^6 that is about 10410^4 features and roughly 101410^{14} operations instead of 101810^{18}.

PropertyNyströmRandom Fourier features
Applies toany kernelshift-invariant kernels (Bochner); other random expansions exist for dot-product kernels
Basisdata-dependent: kernel sections at landmarksdata-independent: random cosines
Adapts to the spectrumyes; with a large eigengap its generalization bound can be far better (Yang et al., 2012)no; frequencies follow pp whatever the data
Training costO(nm2+m3)\order{nm^2+m^3}O(nDd+nD2+D3)\order{nDd+nD^2+D^3}, or one pass of SGD on z(x)\mathbf z(\mathbf x)
Streaming and distributedlandmarks must be chosen firstfeatures are fixed in advance, so data can stream
Both give an explicit mm- or DD-dimensional feature map, after which any linear method (least squares, logistic regression, an SVM solver) runs at linear cost in nn. A first look at random features, and at their double descent, is in Feature Maps & the Road to Kernels.

Iterative solvers and preconditioning

The other road keeps the exact problem and avoids the factorization. Conjugate gradients (CG) solves (K+λI)α=y(K+\lambda I)\boldsymbol\alpha=\mathbf y with one matrix–vector product per iteration: O(n2)\order{n^2} time, and O(n)\order{n} memory if the kernel is recomputed in blocks. Its speed depends on the condition number κ=(s1+λ)/(sn+λ)≈s1/λ\kappa=(s_1+\lambda)/(s_n+\lambda)\approx s_1/\lambda, which is large exactly when statistics asks for a small λ\lambda.

∥αt−α∥A≤2(κ−1κ+1)t∥α0−α∥A,A=K+λI,∥v∥A2=v⊤Av\norm{\boldsymbol\alpha_t-\boldsymbol\alpha}_{A}\le2\Big(\frac{\sqrt\kappa-1}{\sqrt\kappa+1}\Big)^t\norm{\boldsymbol\alpha_0-\boldsymbol\alpha}_{A},\qquad A=K+\lambda I,\quad\norm{\mathbf v}_A^2=\mathbf v^\top A\mathbf v
The classical CG bound: about κlog⁡(1/ε)\sqrt\kappa\log(1/\varepsilon) iterations for accuracy ε\varepsilon. A preconditioner that brings κ\kappa down to a constant leaves only a logarithmic number of matrix–vector products.
  • FALKON (Rudi, Carratino & Rosasco, 2017) combines both roads: Nyström kernel ridge regression with about n\sqrt n landmarks, solved by CG with a Nyström-based preconditioner that makes the system well conditioned, so that about log⁡n\log n iterations suffice. It reaches optimal statistical accuracy with essentially O(n)\order{n} memory and O(nn)\order{n\sqrt n} time.
  • EigenPro (Ma & Belkin, 2017) starts from an observation about gradient descent on kernel least squares: it converges quickly only along the top eigendirections of KK, so for smooth kernels a polynomial number of iterations reaches only a vanishingly small part of the function space. Its preconditioner flattens the top kk eigenvalues to the (k+1)(k+1)-th, P=I−∑i≤k(1−sk+1/si) eiei⊤P=I-\sum_{i\le k}(1-s_{k+1}/s_i)\,\mathbf e_i\mathbf e_i^\top, which permits far larger steps and suits mini-batches on GPUs.
  • Exact Gaussian processes on GPUs replace the Cholesky factorization by CG and Lanczos iterations built from matrix–matrix products; with these, exact GP regression has been run on more than a million points (Wang et al., 2019). Structured kernel interpolation (Wilson & Nickisch, 2015) goes further by interpolating onto a grid where KK has fast Kronecker or Toeplitz products.

Key takeaways

  • Exact kernel ridge regression needs O(n2)\order{n^2} memory and O(n3)\order{n^3} time, but the Gram matrix of a smooth kernel is nearly low-rank, and only about df(λ)=tr⁡[K(K+λI)−1]≪n\mathrm{df}(\lambda)=\operatorname{tr}[K(K+\lambda I)^{-1}]\ll n directions matter.
  • Nyström: K≈KnmKmm+KmnK\approx K_{nm}K_{mm}^+K_{mn} is the Gram matrix of features projected onto the landmarks' span, so its error is PSD. Nyström kernel ridge regression costs O(nm2+m3)\order{nm^2+m^3} time and O(nm)\order{nm} memory.
  • Random Fourier features: by Bochner's theorem a shift-invariant kernel is E[2cos⁡(ω⊤x+b)cos⁡(ω⊤x′+b)]\E[2\cos(\boldsymbol\omega^\top\mathbf x+b)\cos(\boldsymbol\omega^\top\mathbf x'+b)], so DD random cosines approximate it with error O(1/D)\order{1/\sqrt D}, independently of the data.
  • About nlog⁡n\sqrt n\log n landmarks or features preserve the worst-case n−1/2n^{-1/2} rate of exact kernel ridge regression, and leverage-score sampling needs only about df(λ)log⁡n\mathrm{df}(\lambda)\log n. FALKON attains optimal accuracy in O(nn)\order{n\sqrt n} time and O(n)\order{n} memory; EigenPro and GPU conjugate gradients scale exact solvers further.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Nyström kernel ridge regression with mm landmarks on nn training points costs, in time and memory,

  1. O(n3)\order{n^3} time and O(n2)\order{n^2} memory, as for the exact method
  2. O(m3)\order{m^3} time and O(m2)\order{m^2} memory
  3. O(nm2+m3)\order{nm^2+m^3} time and O(nm)\order{nm} memory
  4. O(n2m)\order{n^2m} time and O(n2)\order{n^2} memory
Show the answer

C. O(nm2+m3)\order{nm^2+m^3} time and O(nm)\order{nm} memory

It needs the n×mn\times m block KnmK_{nm} (O(nm)\order{nm} memory), the product KmnKnmK_{mn}K_{nm} (O(nm2)\order{nm^2} time) and one m×mm\times m solve (O(m3)\order{m^3}). The full Gram matrix is never formed.

Question 2

For which kernel are random Fourier features obtained by drawing the frequencies ω\boldsymbol\omega from a Gaussian distribution?

  1. The Laplacian kernel e−∥x−x′∥1e^{-\norm{\mathbf x-\mathbf x'}_1}
  2. The polynomial kernel (1+x⊤x′)2(1+\mathbf x^\top\mathbf x')^2
  3. Any positive-definite kernel
  4. The Gaussian RBF kernel e−∥x−x′∥2/(2ℓ2)e^{-\norm{\mathbf x-\mathbf x'}^2/(2\ell^2)}, with ω∼N(0,ℓ−2I)\boldsymbol\omega\sim\mathcal N(\mathbf 0,\ell^{-2}I)
Show the answer

D. The Gaussian RBF kernel e−∥x−x′∥2/(2ℓ2)e^{-\norm{\mathbf x-\mathbf x'}^2/(2\ell^2)}, with ω∼N(0,ℓ−2I)\boldsymbol\omega\sim\mathcal N(\mathbf 0,\ell^{-2}I)

By Bochner's theorem the sampling distribution is the kernel's normalized Fourier transform, and the Fourier transform of a Gaussian is a Gaussian, with the inverse width: a short length scale means high frequencies.

Question 3

A random-feature approximation of a kernel uses DD features. How does its error at a fixed pair of points typically shrink?

  1. Like 1/D1/\sqrt D: quadrupling DD halves the error
  2. Like 1/D1/D: doubling DD halves the error
  3. Exponentially in DD
  4. It does not shrink: random features have a fixed bias
Show the answer

A. Like 1/D1/\sqrt D: quadrupling DD halves the error

z(x)⊤z(x′)\mathbf z(\mathbf x)^\top\mathbf z(\mathbf x') is an average of DD independent, bounded, unbiased terms, so its standard deviation scales as 1/D1/\sqrt D and Hoeffding gives tails 2e−Dε2/82e^{-D\varepsilon^2/8}.

Question 4

Why can about nlog⁡n\sqrt n\log n landmarks or random features match the worst-case learning rate of exact kernel ridge regression on nn points?

  1. Because every Gram matrix has rank at most n\sqrt n
  2. Because the estimator's statistical error is already of order 1/n1/\sqrt n, and the regularized solution only uses the directions above λ\lambda, which a modest number of landmarks or features captures
  3. Because the Nyström approximation is exact for the Gaussian kernel
  4. Because the test error does not depend on the kernel
Show the answer

B. Because the estimator's statistical error is already of order 1/n1/\sqrt n, and the regularized solution only uses the directions above λ\lambda, which a modest number of landmarks or features captures

An approximation only needs to be good where it matters: on the top directions, about df(λ)\mathrm{df}(\lambda) of them, and up to an error smaller than the unavoidable n−1/2n^{-1/2}. That is what the analyses of Rudi, Camoriano & Rosasco (2015) and Rudi & Rosasco (2017) make precise.

Further reading

  • C. K. I. Williams & M. Seeger, "Using the Nyström method to speed up kernel machines," Advances in Neural Information Processing Systems 13, 682–688 (2001).
  • P. Drineas & M. W. Mahoney, "On the Nyström method for approximating a Gram matrix for improved kernel-based learning," Journal of Machine Learning Research 6, 2153–2175 (2005).
  • A. Rahimi & B. Recht, "Random features for large-scale kernel machines," Advances in Neural Information Processing Systems 20 (2007).
  • A. Rudi & L. Rosasco, "Generalization properties of learning with random features," Advances in Neural Information Processing Systems 30 (2017).
  • A. Rudi, L. Carratino & L. Rosasco, "FALKON: an optimal large scale kernel method," Advances in Neural Information Processing Systems 30 (2017).