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 θ. Training the model means choosing θ so that a chosen error measure L(θ), the loss, is as small as possible. If you picture L 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=sinxcosy: 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) measures how bad it is to predict y^ when the true value is y: it is 0 (or small) when y^ is close to y, and grows as the prediction gets worse. For regression, a common choice is squared error ℓ(y^,y)=(y^−y)2; for classification, cross-entropy is standard.
Definition: Empirical risk
Given a training set of n examples {(xi,yi)}i=1n and a model fθ, the empirical riskR^(θ) is the average loss over the training set. Training a model — including linear regression, logistic regression, and deep networks alike — means finding θ that minimizes R^(θ).
R^(θ)=n1i=1∑nℓ(fθ(xi),yi)
In reality, we care about performance on new data, not just the training set: the population (true) riskR(θ) averages the loss over the entire underlying distribution D that examples come from, not just the n we happened to sample. Since D is unknown, R(θ) cannot be computed directly — R^(θ) is only a proxy, and the gap R(θ)−R^(θ) is called the generalization gap.
R(θ)=E(x,y)∼D[ℓ(fθ(x),y)]
UndergraduateGradient descent and its convergence rate
Gradient descent minimizes R^(θ) (written L(θ) below for brevity) by repeatedly moving in the direction that decreases L fastest: the negative gradient. At each step, we compute ∇L(θk) and move a small distance controlled by the learning rateη>0.
θk+1=θk−η∇L(θk)
Common training algorithms and their update rules
Method
Update rule
Per-step cost
Typical use
Batch gradient descent
θk+1=θk−η∇L(θk)
O(n) per step
Small datasets, exact gradient
Stochastic gradient descent
θk+1=θk−η∇ℓi(θk)
O(1) per step
Huge datasets, noisy but fast updates
Mini-batch SGD
θk+1=θk−ηb1∑i∈B∇ℓi(θk)
O(b) per step
Standard for deep learning; balances speed and stability
Let L be convex and L-smooth (its gradient is Lipschitz with constant L: ∥∇L(x)−∇L(y)∥≤L∥x−y∥), and let θ⋆ minimize L. Gradient descent with step size η=1/L satisfies L(θK)−L(θ⋆)≤2KL∥θ0−θ⋆∥2 after K steps.
Why is it true?
Smoothness guarantees each gradient step decreases L by an amount proportional to the squared gradient size, so L(θk) never goes up. Convexity lets us compare that per-step decrease to the still-remaining gap L(θk)−L(θ⋆). Adding up the guaranteed decreases over K 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/K.
Proof
By L-smoothness, for any x,y: L(y)≤L(x)+∇L(x)⊤(y−x)+2L∥y−x∥2. Setting y=θk+1=θk−L1∇L(θk) and x=θk gives the descent lemma: L(θk+1)≤L(θk)−2L1∥∇L(θk)∥2.
By convexity of L: L(θk)≤L(θ⋆)+∇L(θk)⊤(θk−θ⋆). Adding this to the descent lemma: L(θk+1)−L(θ⋆)≤∇L(θk)⊤(θk−θ⋆)−2L1∥∇L(θk)∥2.
Complete the square on the right-hand side: ∇L(θk)⊤(θk−θ⋆)−2L1∥∇L(θk)∥2=2L(∥θk−θ⋆∥2−θk−θ⋆−L1∇L(θk)2)=2L(∥θk−θ⋆∥2−∥θk+1−θ⋆∥2), since θk+1−θ⋆=θk−θ⋆−L1∇L(θk).
So L(θk+1)−L(θ⋆)≤2L(∥θk−θ⋆∥2−∥θk+1−θ⋆∥2). Summing this for k=0,…,K−1, the right-hand side telescopes to 2L(∥θ0−θ⋆∥2−∥θK−θ⋆∥2)≤2L∥θ0−θ⋆∥2.
The descent lemma also shows L(θk) is non-increasing, so L(θK) is at most the average of L(θ1),…,L(θK): L(θK)−L(θ⋆)≤K1∑k=1K(L(θk)−L(θ⋆))≤2KL∥θ0−θ⋆∥2, which is exactly the claimed bound.
UndergraduateBackpropagation: the chain rule at scale
A network with D layers computes z(l)=W(l)a(l−1)+b(l), then applies a nonlinearity a(l)=σ(z(l)), for l=1,…,D, with a(0) the input. Naively applying the chain rule to compute ∂L/∂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))
Backpropagation avoids the repeated work by computing one intermediate quantity per layer, the error term δ(l)=∂L/∂z(l), starting from the output layer and working backward. Each δ(l) is built directly from δ(l+1), reusing it instead of recomputing it, and the weight gradient falls out immediately from δ(l) and the layer's input a(l−1):
Because each δ(l) is computed once and reused by the layer before it, the total cost of one backward pass is proportional to D, the same order as one forward pass — not D2. 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 A (for example, n centered data points as rows) into A=UΣV⊤, where U and V have orthonormal columns and Σ is diagonal with non-negative entries σ1≥σ2≥⋯≥0, the singular values.
A=UΣV⊤
Principal component analysis (PCA) uses the right singular vectors v1,v2,… (columns of V) as new coordinate axes: v1 is the direction along which the (centered) data spreads out the most, v2 the next most, and so on, each orthogonal to the previous ones. Keeping only the top k directions and discarding the rest compresses the data while keeping as much of its variation as any k-dimensional projection possibly can.
Let A∈Rm×n have singular values σ1≥σ2≥⋯≥σr>0, and for k<r let Ak=∑i=1kσiuivi⊤ keep only the top k singular components. Then for every matrix B with rank(B)≤k, ∥A−B∥2≥σk+1, and this bound is attained: ∥A−Ak∥2=σk+1. So Ak is a best rank-k approximation of A in the operator norm.
Why is it true?
The singular values measure how much A stretches vectors along each orthogonal direction vi; keeping the largest ones and dropping the smallest throws away the directions A stretches least. Any other rank-k matrix B must fail to reconstruct some direction among the top k+1 singular directions (there are too many of them to fit in a k-dimensional image), and that failure costs at least σk+1.
Proof
Achievability. Since U,V have orthonormal columns, ∥A−Ak∥2=∥U(Σ−Σk)V⊤∥2=∥Σ−Σk∥2, where Σk keeps the top k singular values and zeroes the rest. Σ−Σk is diagonal with entries 0,…,0,σk+1,…,σr, so its operator norm is its largest entry, σk+1.
Optimality. Let B be any matrix with rank(B)≤k; its null space (kernel) has dimension at least n−k. Let S=span(v1,…,vk+1), a (k+1)-dimensional subspace. Since dim(kerB)+dim(S)≥(n−k)+(k+1)=n+1>n, the two subspaces must intersect in more than just the origin: there exists a unit vector z∈S with Bz=0.
Write z=∑i=1k+1civi with ∑i=1k+1ci2=1 (since z has unit norm and the vi are orthonormal). Because Bz=0, ∥(A−B)z∥=∥Az∥=∑i=1k+1ciσiui=∑i=1k+1ci2σi2, using orthonormality of the ui.
Since σi≥σk+1 for every i≤k+1, ∑i=1k+1ci2σi2≥σk+12∑i=1k+1ci2=σk+12. Hence ∥A−B∥2≥∥(A−B)z∥≥σk+1 for every rank-≤k matrix B, which together with achievability proves the theorem.
The same Ak also minimizes the Frobenius-norm error among rank-≤k matrices, with the discarded error equal to the discarded singular values: ∥A−Ak∥F=∑i=k+1rσi2. Since the total variance of the data equals ∑iσi2, this is exactly the variance PCA leaves behind when it keeps only k components — the smaller this quantity, the more faithfully k dimensions summarize the original data.
∥A−Ak∥F=i=k+1∑rσi2
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,000) as y^=θx, where x is house size (in 1,000 sq ft). Three training houses give (xi,yi): (1,2), (2,3), (3,5). Using empirical squared-error risk R^(θ)=31∑i=13(θxi−yi)2 and starting from θ0=1, find θ1 after one gradient descent step with η=0.05.
Solution
First differentiate: ∇R^(θ)=32∑i=13xi(θxi−yi)=32[(θ−2)+2(2θ−3)+3(3θ−5)]=32(14θ−23).
Evaluate at θ0=1: predictions are y^i=1,2,3; residuals y^i−yi=−1,−1,−2; so ∇R^(1)=32(14⋅1−23)=32(−9)=−6.
Apply the update rule: θ1=θ0−η∇R^(θ0)=1−0.05×(−6)=1+0.3=1.3.
The gradient was negative, so gradient descent correctly pushed θ upward: increasing the price-per-size slope reduces the squared error on this training set, moving θ toward the true least-squares optimum (which turns out to be θ⋆=23/14≈1.64, found by setting ∇R^(θ)=0).
Example: Finding the principal component of a covariance matrix
Two centered features have covariance matrix Σ=(4221). Find the direction of the first principal component and the fraction of total variance it explains.
Solution
The principal directions are eigenvectors of Σ. The characteristic equation is det(Σ−λI)=(4−λ)(1−λ)−2⋅2=λ2−5λ=0, giving eigenvalues λ1=5, λ2=0.
For λ1=5: solve (Σ−5I)v=0, i.e. (−122−4)v=0, giving v∝(2,1). Normalizing, the first principal component direction is u1=51(2,1).
Total variance is tr(Σ)=4+1=5, which equals λ1+λ2=5+0. The fraction of variance explained by the first component is λ1/(λ1+λ2)=5/5=100%.
Since λ2=0, this dataset is exactly one-dimensional: every point lies exactly along the direction (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.
The loss is L(θ)=(θ−4)2. Starting from θ0=0 with learning rate η=0.1, what is θ1 after one gradient descent step?
Which formula correctly defines the empirical risk R^(θ) for a training set of n examples {(xi,yi)} and loss function ℓ?
A matrix has singular values σ1=6, σ2=3, σ3=2. What is the Frobenius-norm error ∥A−A1∥F of the best rank-1 approximation A1?
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
Ian Goodfellow, Yoshua Bengio, Aaron Courville (2016). Deep Learning