MathLabs

Applied and computational mathematics

Mathematics of machine learning

The linear algebra, optimization and probability that power algorithms learning patterns from data.

IntuitionLearning as descending a landscape

Suppose you want to predict something — a house price, a spam label, the next word in a sentence — using a model with adjustable knobs θ\theta. Training the model means choosing θ\theta so that a chosen error measure L(θ)L(\theta), the loss, is as small as possible. If you picture LL as the height of a landscape over the space of all possible knob settings, learning becomes physical: start somewhere, and repeatedly step downhill.

A 3D undulating surface with alternating hills and valleys formed by sin x times cos y, illustrating a non-convex loss landscape with multiple local minima rather than one global bowl.
z=sin⁡xcos⁡yz = \sin x \cos y: a loss landscape with many hills and valleys. Unlike a single bowl, gradient descent starting near different points can slide into different valleys — different local minima.

Which shape you get depends on the model. A linear model trained with squared error has a loss that is exactly a paraboloid — a single bowl like the one used to illustrate convex optimization — so gradient descent always finds the best fit. A deep neural network, with many layers of nonlinear functions composed together, typically has a loss landscape closer to the bumpy surface above: full of local dips, flat plateaus, and saddle points.

UndergraduateEmpirical risk minimization

Definition: Loss function

A loss function ℓ(y^,y)\ell(\hat y, y) measures how bad it is to predict y^\hat y when the true value is yy: it is 00 (or small) when y^\hat y is close to yy, and grows as the prediction gets worse. For regression, a common choice is squared error ℓ(y^,y)=(y^−y)2\ell(\hat y, y) = (\hat y - y)^2; for classification, cross-entropy is standard.

Definition: Empirical risk

Given a training set of nn examples {(xi,yi)}i=1n\{(x_i, y_i)\}_{i=1}^n and a model fθf_\theta, the empirical risk R^(θ)\hat R(\theta) is the average loss over the training set. Training a model — including linear regression, logistic regression, and deep networks alike — means finding θ\theta that minimizes R^(θ)\hat R(\theta).

R^(θ)=1n∑i=1nℓ(fθ(xi),yi)\hat R(\theta) = \frac{1}{n}\sum_{i=1}^n \ell(f_\theta(x_i), y_i)

In reality, we care about performance on new data, not just the training set: the population (true) risk R(θ)R(\theta) averages the loss over the entire underlying distribution D\mathcal{D} that examples come from, not just the nn we happened to sample. Since D\mathcal{D} is unknown, R(θ)R(\theta) cannot be computed directly — R^(θ)\hat R(\theta) is only a proxy, and the gap R(θ)−R^(θ)R(\theta) - \hat R(\theta) is called the generalization gap.

R(θ)=E(x,y)∼D[ℓ(fθ(x),y)]R(\theta) = \mathbb{E}_{(x,y)\sim \mathcal{D}}[\ell(f_\theta(x), y)]

UndergraduateGradient descent and its convergence rate

Gradient descent minimizes R^(θ)\hat R(\theta) (written L(θ)L(\theta) below for brevity) by repeatedly moving in the direction that decreases LL fastest: the negative gradient. At each step, we compute ∇L(θk)\nabla L(\theta_k) and move a small distance controlled by the learning rate η>0\eta > 0.

θk+1=θk−η∇L(θk)\theta_{k+1} = \theta_k - \eta \nabla L(\theta_k)
Common training algorithms and their update rules
MethodUpdate rulePer-step costTypical use
Batch gradient descentθk+1=θk−η∇L(θk)\theta_{k+1} = \theta_k - \eta \nabla L(\theta_k)O(n)O(n) per stepSmall datasets, exact gradient
Stochastic gradient descentθk+1=θk−η∇ℓi(θk)\theta_{k+1} = \theta_k - \eta \nabla \ell_i(\theta_k)O(1)O(1) per stepHuge datasets, noisy but fast updates
Mini-batch SGDθk+1=θk−η1b∑i∈B∇ℓi(θk)\theta_{k+1} = \theta_k - \eta \dfrac{1}{b}\sum_{i \in B} \nabla \ell_i(\theta_k)O(b)O(b) per stepStandard for deep learning; balances speed and stability
Momentumvk+1=βvk+∇L(θk),θk+1=θk−ηvk+1v_{k+1} = \beta v_k + \nabla L(\theta_k), \quad \theta_{k+1} = \theta_k - \eta v_{k+1}O(n)O(n) or O(b)O(b) per stepAccelerates through narrow valleys and plateaus

Let LL be convex and LL-smooth (its gradient is Lipschitz with constant LL: ∥∇L(x)−∇L(y)∥≤L∥x−y∥\|\nabla L(x) - \nabla L(y)\| \le L\|x-y\|), and let θ⋆\theta^\star minimize LL. Gradient descent with step size η=1/L\eta = 1/L satisfies L(θK)−L(θ⋆)≤L∥θ0−θ⋆∥22KL(\theta_K) - L(\theta^\star) \le \dfrac{L\|\theta_0 - \theta^\star\|^2}{2K} after KK steps.

Why is it true?

Smoothness guarantees each gradient step decreases LL by an amount proportional to the squared gradient size, so L(θk)L(\theta_k) never goes up. Convexity lets us compare that per-step decrease to the still-remaining gap L(θk)−L(θ⋆)L(\theta_k) - L(\theta^\star). Adding up the guaranteed decreases over KK steps — a telescoping sum — shows the total decrease is bounded, and since the best point in a non-increasing sequence is at least as good as the average, the final gap must shrink like 1/K1/K.

Proof

By LL-smoothness, for any x,yx, y: L(y)≤L(x)+∇L(x)⊤(y−x)+L2∥y−x∥2L(y) \le L(x) + \nabla L(x)^\top (y-x) + \dfrac{L}{2}\|y-x\|^2. Setting y=θk+1=θk−1L∇L(θk)y = \theta_{k+1} = \theta_k - \dfrac{1}{L}\nabla L(\theta_k) and x=θkx = \theta_k gives the descent lemma: L(θk+1)≤L(θk)−12L∥∇L(θk)∥2L(\theta_{k+1}) \le L(\theta_k) - \dfrac{1}{2L}\|\nabla L(\theta_k)\|^2.

By convexity of LL: L(θk)≤L(θ⋆)+∇L(θk)⊤(θk−θ⋆)L(\theta_k) \le L(\theta^\star) + \nabla L(\theta_k)^\top(\theta_k - \theta^\star). Adding this to the descent lemma: L(θk+1)−L(θ⋆)≤∇L(θk)⊤(θk−θ⋆)−12L∥∇L(θk)∥2L(\theta_{k+1}) - L(\theta^\star) \le \nabla L(\theta_k)^\top(\theta_k - \theta^\star) - \dfrac{1}{2L}\|\nabla L(\theta_k)\|^2.

Complete the square on the right-hand side: ∇L(θk)⊤(θk−θ⋆)−12L∥∇L(θk)∥2=L2(∥θk−θ⋆∥2−∥θk−θ⋆−1L∇L(θk)∥2)=L2(∥θk−θ⋆∥2−∥θk+1−θ⋆∥2)\nabla L(\theta_k)^\top(\theta_k - \theta^\star) - \dfrac{1}{2L}\|\nabla L(\theta_k)\|^2 = \dfrac{L}{2}\left(\|\theta_k - \theta^\star\|^2 - \left\|\theta_k - \theta^\star - \dfrac{1}{L}\nabla L(\theta_k)\right\|^2\right) = \dfrac{L}{2}\left(\|\theta_k - \theta^\star\|^2 - \|\theta_{k+1} - \theta^\star\|^2\right), since θk+1−θ⋆=θk−θ⋆−1L∇L(θk)\theta_{k+1} - \theta^\star = \theta_k - \theta^\star - \dfrac{1}{L}\nabla L(\theta_k).

So L(θk+1)−L(θ⋆)≤L2(∥θk−θ⋆∥2−∥θk+1−θ⋆∥2)L(\theta_{k+1}) - L(\theta^\star) \le \dfrac{L}{2}\left(\|\theta_k - \theta^\star\|^2 - \|\theta_{k+1} - \theta^\star\|^2\right). Summing this for k=0,…,K−1k = 0, \dots, K-1, the right-hand side telescopes to L2(∥θ0−θ⋆∥2−∥θK−θ⋆∥2)≤L2∥θ0−θ⋆∥2\dfrac{L}{2}\left(\|\theta_0 - \theta^\star\|^2 - \|\theta_K - \theta^\star\|^2\right) \le \dfrac{L}{2}\|\theta_0 - \theta^\star\|^2.

The descent lemma also shows L(θk)L(\theta_k) is non-increasing, so L(θK)L(\theta_K) is at most the average of L(θ1),…,L(θK)L(\theta_1), \dots, L(\theta_K): L(θK)−L(θ⋆)≤1K∑k=1K(L(θk)−L(θ⋆))≤L∥θ0−θ⋆∥22KL(\theta_K) - L(\theta^\star) \le \dfrac{1}{K}\sum_{k=1}^K \left(L(\theta_k) - L(\theta^\star)\right) \le \dfrac{L\|\theta_0 - \theta^\star\|^2}{2K}, which is exactly the claimed bound.

UndergraduateBackpropagation: the chain rule at scale

A network with DD layers computes z(l)=W(l)a(l−1)+b(l)z^{(l)} = W^{(l)} a^{(l-1)} + b^{(l)}, then applies a nonlinearity a(l)=σ(z(l))a^{(l)} = \sigma(z^{(l)}), for l=1,…,Dl = 1, \dots, D, with a(0)a^{(0)} the input. Naively applying the chain rule to compute ∂L/∂W(l)\partial L / \partial W^{(l)} for every layer separately would repeat the same sub-computations over and over, costing time that grows quadratically with depth.

z(l)=W(l)a(l−1)+b(l),a(l)=σ(z(l))z^{(l)} = W^{(l)} a^{(l-1)} + b^{(l)}, \qquad a^{(l)} = \sigma(z^{(l)})

Backpropagation avoids the repeated work by computing one intermediate quantity per layer, the error term δ(l)=∂L/∂z(l)\delta^{(l)} = \partial L / \partial z^{(l)}, starting from the output layer and working backward. Each δ(l)\delta^{(l)} is built directly from δ(l+1)\delta^{(l+1)}, reusing it instead of recomputing it, and the weight gradient falls out immediately from δ(l)\delta^{(l)} and the layer's input a(l−1)a^{(l-1)}:

δ(l)=((W(l+1))⊤δ(l+1))⊙σ′(z(l)),∂L∂W(l)=δ(l)(a(l−1))⊤\delta^{(l)} = \left((W^{(l+1)})^\top \delta^{(l+1)}\right) \odot \sigma'(z^{(l)}), \qquad \frac{\partial L}{\partial W^{(l)}} = \delta^{(l)} (a^{(l-1)})^\top

Because each δ(l)\delta^{(l)} is computed once and reused by the layer before it, the total cost of one backward pass is proportional to DD, the same order as one forward pass — not D2D^2. This is what makes training networks with dozens or hundreds of layers computationally feasible.

UndergraduateDimensionality reduction: SVD and PCA

Real datasets often have redundant, correlated features. The singular value decomposition (SVD) factors any matrix AA (for example, nn centered data points as rows) into A=UΣV⊤A = U \Sigma V^\top, where UU and VV have orthonormal columns and Σ\Sigma is diagonal with non-negative entries σ1≥σ2≥⋯≥0\sigma_1 \ge \sigma_2 \ge \cdots \ge 0, the singular values.

A=UΣV⊤A = U \Sigma V^\top

Principal component analysis (PCA) uses the right singular vectors v1,v2,…v_1, v_2, \dots (columns of VV) as new coordinate axes: v1v_1 is the direction along which the (centered) data spreads out the most, v2v_2 the next most, and so on, each orthogonal to the previous ones. Keeping only the top kk directions and discarding the rest compresses the data while keeping as much of its variation as any kk-dimensional projection possibly can.

Let A∈Rm×nA \in \mathbb{R}^{m \times n} have singular values σ1≥σ2≥⋯≥σr>0\sigma_1 \ge \sigma_2 \ge \cdots \ge \sigma_r > 0, and for k<rk < r let Ak=∑i=1kσiuivi⊤A_k = \sum_{i=1}^k \sigma_i u_i v_i^\top keep only the top kk singular components. Then for every matrix BB with rank⁡(B)≤k\operatorname{rank}(B) \le k, ∥A−B∥2≥σk+1\|A - B\|_2 \ge \sigma_{k+1}, and this bound is attained: ∥A−Ak∥2=σk+1\|A - A_k\|_2 = \sigma_{k+1}. So AkA_k is a best rank-kk approximation of AA in the operator norm.

Why is it true?

The singular values measure how much AA stretches vectors along each orthogonal direction viv_i; keeping the largest ones and dropping the smallest throws away the directions AA stretches least. Any other rank-kk matrix BB must fail to reconstruct some direction among the top k+1k+1 singular directions (there are too many of them to fit in a kk-dimensional image), and that failure costs at least σk+1\sigma_{k+1}.

Proof

Achievability. Since U,VU, V have orthonormal columns, ∥A−Ak∥2=∥U(Σ−Σk)V⊤∥2=∥Σ−Σk∥2\|A - A_k\|_2 = \|U(\Sigma - \Sigma_k)V^\top\|_2 = \|\Sigma - \Sigma_k\|_2, where Σk\Sigma_k keeps the top kk singular values and zeroes the rest. Σ−Σk\Sigma - \Sigma_k is diagonal with entries 0,…,0,σk+1,…,σr0, \dots, 0, \sigma_{k+1}, \dots, \sigma_r, so its operator norm is its largest entry, σk+1\sigma_{k+1}.

Optimality. Let BB be any matrix with rank⁡(B)≤k\operatorname{rank}(B) \le k; its null space (kernel) has dimension at least n−kn - k. Let S=span⁡(v1,…,vk+1)S = \operatorname{span}(v_1, \dots, v_{k+1}), a (k+1)(k+1)-dimensional subspace. Since dim⁡(ker⁡B)+dim⁡(S)≥(n−k)+(k+1)=n+1>n\dim(\ker B) + \dim(S) \ge (n-k) + (k+1) = n+1 > n, the two subspaces must intersect in more than just the origin: there exists a unit vector z∈Sz \in S with Bz=0Bz = 0.

Write z=∑i=1k+1civiz = \sum_{i=1}^{k+1} c_i v_i with ∑i=1k+1ci2=1\sum_{i=1}^{k+1} c_i^2 = 1 (since zz has unit norm and the viv_i are orthonormal). Because Bz=0Bz = 0, ∥(A−B)z∥=∥Az∥=∥∑i=1k+1ciσiui∥=∑i=1k+1ci2σi2\|(A-B)z\| = \|Az\| = \left\|\sum_{i=1}^{k+1} c_i \sigma_i u_i\right\| = \sqrt{\sum_{i=1}^{k+1} c_i^2 \sigma_i^2}, using orthonormality of the uiu_i.

Since σi≥σk+1\sigma_i \ge \sigma_{k+1} for every i≤k+1i \le k+1, ∑i=1k+1ci2σi2≥σk+12∑i=1k+1ci2=σk+12\sum_{i=1}^{k+1} c_i^2 \sigma_i^2 \ge \sigma_{k+1}^2 \sum_{i=1}^{k+1} c_i^2 = \sigma_{k+1}^2. Hence ∥A−B∥2≥∥(A−B)z∥≥σk+1\|A-B\|_2 \ge \|(A-B)z\| \ge \sigma_{k+1} for every rank-≤k\le k matrix BB, which together with achievability proves the theorem.

The same AkA_k also minimizes the Frobenius-norm error among rank-≤k\le k matrices, with the discarded error equal to the discarded singular values: ∥A−Ak∥F=∑i=k+1rσi2\|A - A_k\|_F = \sqrt{\sum_{i=k+1}^r \sigma_i^2}. Since the total variance of the data equals ∑iσi2\sum_i \sigma_i^2, this is exactly the variance PCA leaves behind when it keeps only kk components — the smaller this quantity, the more faithfully kk dimensions summarize the original data.

∥A−Ak∥F=∑i=k+1rσi2\|A - A_k\|_F = \sqrt{\sum_{i=k+1}^r \sigma_i^2}

UndergraduateReal-World Applications and Worked Examples

These tools appear together in almost every practical machine learning system: gradient descent (and its variants) trains recommendation engines, image classifiers, and language models; backpropagation is what makes training deep networks for speech recognition and computer vision tractable; and PCA is used to compress genomic data, denoise sensor readings, and visualize high-dimensional financial or scientific datasets in two or three dimensions.

Example: One step of gradient descent for house-price prediction

A real-estate site models predicted price (in 100,000100{,}000) as y^=θx\hat y = \theta x, where xx is house size (in 1,0001{,}000 sq ft). Three training houses give (xi,yi)(x_i, y_i): (1,2)(1, 2), (2,3)(2, 3), (3,5)(3, 5). Using empirical squared-error risk R^(θ)=13∑i=13(θxi−yi)2\hat R(\theta) = \tfrac{1}{3}\sum_{i=1}^3 (\theta x_i - y_i)^2 and starting from θ0=1\theta_0 = 1, find θ1\theta_1 after one gradient descent step with η=0.05\eta = 0.05.

Solution

First differentiate: ∇R^(θ)=23∑i=13xi(θxi−yi)=23[(θ−2)+2(2θ−3)+3(3θ−5)]=23(14θ−23)\nabla \hat R(\theta) = \tfrac{2}{3}\sum_{i=1}^3 x_i(\theta x_i - y_i) = \tfrac{2}{3}\big[(\theta - 2) + 2(2\theta - 3) + 3(3\theta - 5)\big] = \tfrac{2}{3}(14\theta - 23).

Evaluate at θ0=1\theta_0 = 1: predictions are y^i=1,2,3\hat y_i = 1, 2, 3; residuals y^i−yi=−1,−1,−2\hat y_i - y_i = -1, -1, -2; so ∇R^(1)=23(14⋅1−23)=23(−9)=−6\nabla \hat R(1) = \tfrac{2}{3}(14 \cdot 1 - 23) = \tfrac{2}{3}(-9) = -6.

Apply the update rule: θ1=θ0−η∇R^(θ0)=1−0.05×(−6)=1+0.3=1.3\theta_1 = \theta_0 - \eta \nabla \hat R(\theta_0) = 1 - 0.05 \times (-6) = 1 + 0.3 = 1.3.

The gradient was negative, so gradient descent correctly pushed θ\theta upward: increasing the price-per-size slope reduces the squared error on this training set, moving θ\theta toward the true least-squares optimum (which turns out to be θ⋆=23/14≈1.64\theta^\star = 23/14 \approx 1.64, found by setting ∇R^(θ)=0\nabla \hat R(\theta) = 0).

Example: Finding the principal component of a covariance matrix

Two centered features have covariance matrix Σ=(4221)\Sigma = \begin{pmatrix} 4 & 2 \\ 2 & 1 \end{pmatrix}. Find the direction of the first principal component and the fraction of total variance it explains.

Solution

The principal directions are eigenvectors of Σ\Sigma. The characteristic equation is det⁡(Σ−λI)=(4−λ)(1−λ)−2⋅2=λ2−5λ=0\det(\Sigma - \lambda I) = (4-\lambda)(1-\lambda) - 2 \cdot 2 = \lambda^2 - 5\lambda = 0, giving eigenvalues λ1=5\lambda_1 = 5, λ2=0\lambda_2 = 0.

For λ1=5\lambda_1 = 5: solve (Σ−5I)v=0(\Sigma - 5I)v = 0, i.e. (−122−4)v=0\begin{pmatrix} -1 & 2 \\ 2 & -4 \end{pmatrix} v = 0, giving v∝(2,1)v \propto (2, 1). Normalizing, the first principal component direction is u1=15(2,1)u_1 = \tfrac{1}{\sqrt5}(2, 1).

Total variance is tr⁡(Σ)=4+1=5\operatorname{tr}(\Sigma) = 4 + 1 = 5, which equals λ1+λ2=5+0\lambda_1 + \lambda_2 = 5 + 0. The fraction of variance explained by the first component is λ1/(λ1+λ2)=5/5=100%\lambda_1/(\lambda_1+\lambda_2) = 5/5 = 100\%.

Since λ2=0\lambda_2 = 0, this dataset is exactly one-dimensional: every point lies exactly along the direction (2,1)(2,1), so projecting onto that single line (a rank-1 approximation) loses no information at all, matching the Eckart–Young formula ∥A−A1∥F=λ2=0\|A-A_1\|_F = \sqrt{\lambda_2} = 0.

The loss is L(θ)=(θ−4)2L(\theta) = (\theta - 4)^2. Starting from θ0=0\theta_0 = 0 with learning rate η=0.1\eta = 0.1, what is θ1\theta_1 after one gradient descent step?

Which formula correctly defines the empirical risk R^(θ)\hat R(\theta) for a training set of nn examples {(xi,yi)}\{(x_i,y_i)\} and loss function ℓ\ell?

A matrix has singular values σ1=6\sigma_1 = 6, σ2=3\sigma_2 = 3, σ3=2\sigma_3 = 2. What is the Frobenius-norm error ∥A−A1∥F\|A - A_1\|_F of the best rank-1 approximation A1A_1?

A self-driving car's vision network has 50 layers. Computing every weight's gradient with backpropagation costs about the same as one extra forward pass through the network, not 50 times more. Which property of backpropagation explains this?

References

  1. Ian Goodfellow, Yoshua Bengio, Aaron Courville (2016). Deep Learning
  2. Christopher M. Bishop (2006). Pattern Recognition and Machine Learning
  3. Chiyuan Zhang, Samy Bengio, Moritz Hardt, Benjamin Recht, Oriol Vinyals (2017). Understanding deep learning requires rethinking generalization · arXiv:1611.03530
  4. Arthur Jacot, Franck Gabriel, Clément Hongler (2018). Neural Tangent Kernel: Convergence and Generalization in Neural Networks · arXiv:1806.07572
  5. Mikhail Belkin, Daniel Hsu, Siyuan Ma, Soumik Mandal (2019). Reconciling modern machine learning practice and the classical bias-variance trade-off · arXiv:1812.11118