Training Models

Basics to Machine Learning · v1.10.27

2026-09-11 16:20:06

Where we are

Where this fits

  • The previous unit built shallow and deep networks, \(h_{k+1} = a[\beta_k + \Omega_k h_k]\)
  • It asserted the least-squares loss for real-valued targets
  • It described training as a search, and gave no algorithm for the search

Two gaps: no justified loss, and no procedure.

The supervised learning loop

What the previous unit fixed

  • A model \(f[x,\phi]\) — a family of functions
  • Its parameters \(\phi\) — which member
  • A loss \(L[\phi]\) — how badly it fits

What this unit supplies

  • Where \(L[\phi]\) comes from
  • The algorithm that minimises it
  • The gradients that algorithm consumes
  • The measurement that judges the result

Outline of key topics

  1. Losses derived from maximum likelihood
  2. Classification losses and cross-entropy
  3. Gradient descent and the shape of loss surfaces
  4. SGD, momentum and Adam
  5. Backpropagation and parameter initialization
  6. Test error, double descent, and regularization

Maximum likelihood and the negative log-likelihood

The model predicts a distribution

One distribution per output domain.

  • Until now \(f[x,\phi]\) returned a value \(y\)
  • Now it returns a conditional distribution \(Pr(y|x)\)
  • Real values, classes, counts, directions — each has its own family

Two models, not one

The probability model

\[Pr(y|\theta)\]

Chosen by hand, once, from the output domain. Fixed for the task.

The machine learning model

\[\theta = f[x,\phi]\]

Learned from data. Computes the parameters of the first.

The network never predicts \(y\). It predicts what governs \(y\).

The maximum likelihood criterion

Each input yields its own \(\theta_i = f[x_i,\phi]\). Maximise the combined probability:

\[\hat{\phi} = \underset{\phi}{\operatorname{argmax}} \left[ \prod_{i=1}^{I} Pr(y_i|f[x_i,\phi]) \right]\]

Two assumptions, together the i.i.d. assumption:

  • identically distributed — one distributional form everywhere
  • independent — so the likelihood factorises

From a product to a sum

Take the logarithm

\[\hat{\phi} = \underset{\phi}{\operatorname{argmax}} \left[ \sum_{i=1}^{I} \log\big[Pr(y_i|f[x_i,\phi])\big] \right]\]

A product of \(I\) small factors underflows finite precision.

Then negate

\[L[\phi] = -\sum_{i=1}^{I} \log\big[Pr(y_i|f[x_i,\phi])\big]\]

Fitting is framed as minimisation, so \(\hat{\phi} = \operatorname{argmin}_\phi\big[L[\phi]\big]\).

The recipe

  1. Choose a distribution \(Pr(y|\theta)\) over the domain of \(y\)
  2. Set the network to predict its parameters, \(\theta = f[x,\phi]\)
  3. Minimise the negative log-likelihood over the training pairs
  4. At test time return the distribution, or the value maximising it

Steps (2)–(4) never vary. Only the distribution in (1) does: normal, Bernoulli, categorical.

Univariate regression: the normal

\[Pr(y|\mu,\sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\left[-\frac{(y-\mu)^2}{2\sigma^2}\right]\]

  • Defined for \(y \in \mathbb{R}\)
  • \(\mu\) sets the peak, \(\sigma^2\) the width
  • Step (2) sets \(\mu = f[x,\phi]\)

Least squares falls out

\[ \begin{aligned} \hat{\phi} &= \underset{\phi}{\operatorname{argmin}}\left[-\sum_{i=1}^{I}\log\left[\frac{1}{\sqrt{2\pi\sigma^2}}\exp\left[-\frac{(y_i-f[x_i,\phi])^2}{2\sigma^2}\right]\right]\right]\\ &= \underset{\phi}{\operatorname{argmin}}\left[-\sum_{i=1}^{I}\left(\log\left[\frac{1}{\sqrt{2\pi\sigma^2}}\right] - \frac{(y_i-f[x_i,\phi])^2}{2\sigma^2}\right)\right]\\ &= \underset{\phi}{\operatorname{argmin}}\left[\sum_{i=1}^{I}(y_i-f[x_i,\phi])^2\right] \end{aligned} \]

  • The first term drops — no \(\phi\) in it
  • The \(2\sigma^2\) drops — a constant positive scale

Least squares is a consequence, not a convention.

Heteroscedastic regression

Homoscedastic: constant variance.

Heteroscedastic: variance varies with \(x\).

A second network output learns the variance: \(\sigma^2 = f_2[x,\phi]^2\).

The heteroscedastic loss

\[\hat{\phi} = \underset{\phi}{\operatorname{argmin}}\left[-\sum_{i=1}^{I}\left(\log\left[\frac{1}{\sqrt{2\pi f_2[x_i,\phi]^2}}\right] - \frac{(y_i - f_1[x_i,\phi])^2}{2f_2[x_i,\phi]^2}\right)\right]\]

  • \(f_1\) predicts the mean, \(f_2^2\) the variance
  • The squared error is weighted by the predicted precision

Classification and cross-entropy

Binary classification: the Bernoulli

For \(y \in \{0,1\}\), one parameter \(\lambda \in [0,1]\) — the probability that \(y = 1\):

\[Pr(y|\lambda) = (1-\lambda)^{1-y}\cdot\lambda^{y}\]

  • Step one of the recipe is all that changed
  • The network must produce a \(\lambda\) inside \([0,1]\)

Logit and probability

Zero maps to \(0.5\).

The network output

\(z = f[x,\phi] \in \mathbb{R}\) — the logit, unconstrained

The distribution parameter

\[\lambda = \operatorname{sig}[z] = \frac{1}{1+\exp[-z]}\]

The sigmoid is what maps an unconstrained output onto a valid parameter.

Binary cross-entropy

Both the label and the model are distributions over the domain \(y \in \{0,1\}\):

\[\ell_i = -\sum_{y \in \{0,1\}} q_i(y)\,\log\big[p_i(y)\big] = -\,\mathbb{E}_{y\sim q_i}\big[\log p_i(y)\big]\]

The data, \(q_i\)

\(q_i(1) = y_i\), \(q_i(0) = 1-y_i\)

The model, \(p_i\)

\(p_i(1) = \lambda_i\), \(p_i(0) = 1-\lambda_i\)

Expanding the two terms: \(-(1-y_i)\log[1-\lambda_i] - y_i\log[\lambda_i]\).

In terms of the logit

The network emits a logit, so \(\lambda_i = \operatorname{sig}[f[x_i,\phi]]\). Summing over the training set:

\[L[\phi] = \sum_{i=1}^{I} -(1-y_i)\log\big[1-\operatorname{sig}[f[x_i,\phi]]\big] - y_i \log\big[\operatorname{sig}[f[x_i,\phi]]\big]\]

  • At inference \(\operatorname{sig}[f[x,\phi]]\) is \(Pr(y=1)\)
  • A point estimate takes \(y=1\) when \(\lambda > 0.5\)

Multiclass: the categorical distribution

For \(y \in \{1,\dots,K\}\), \(K\) parameters with \(Pr(y=k) = \lambda_k\):

  • each \(\lambda_k \in [0,1]\)
  • collectively they sum to one

The network produces a logit vector \(z \in \mathbb{R}^K\), one output per class, with no constraint on any of them.

The softmax

Any vertical slice sums to one.

\[\operatorname{softmax}_k[z] = \frac{\exp[z_k]}{\sum_{k'=1}^{K}\exp[z_{k'}]}\]

  • the exponentials give positivity
  • the denominator gives the sum

Multiclass cross-entropy

The same shape, with the domain now \(y \in \{1,\dots,K\}\):

\[\ell_i = -\sum_{k=1}^{K} q_i(k)\,\log\big[p_i(k)\big] = -\,\mathbb{E}_{y\sim q_i}\big[\log p_i(y)\big]\]

The data, \(q_i\)

\(q_i(k) = \delta[k - y_i]\)

The Kronecker delta: \(1\) at \(k = y_i\), \(0\) elsewhere.

The model, \(p_i\)

\(p_i(k) = \operatorname{softmax}_k\big[f[x_i,\phi]\big]\)

The delta selects one term, leaving \(\ell_i = -\log p_i(y_i)\).

The loss in terms of the logits

Substituting the softmax into \(-\log Pr(y_i|\cdot)\) and expanding the logarithm:

\[ \begin{aligned} L[\phi] &= -\sum_{i=1}^{I}\log\left[\frac{\exp\big[f_{y_i}[x_i,\phi]\big]}{\sum_{k'=1}^{K}\exp\big[f_{k'}[x_i,\phi]\big]}\right]\\ &= -\sum_{i=1}^{I}\left(f_{y_i}[x_i,\phi] - \log\left[\sum_{k'=1}^{K}\exp\big[f_{k'}[x_i,\phi]\big]\right]\right) \end{aligned} \]

The correct class’s logit, against the log-sum-exp of all of them.

🎯 Another metric as a loss function: Kullback-Leibler divergence

Training so far minimised a likelihood. The same objective can be posed as a distance between distributions.

The data distribution, \(q\)

  • fixed by the training set
  • unknown in closed form, but sampled from

The model distribution, \(p\)

  • parameterised by \(\theta\)
  • free to move

Learning becomes: move \(p\) until it is as close as possible to \(q\).

Cross-entropy: the KL divergence

The divergence

\[D_{KL}[q\|p] = \mathbb{E}_{z\sim q}\left[\log\frac{q(z)}{p(z)}\right]\]

Expanding the ratio:

\[ \begin{aligned} &= \int q(z)\log[q(z)]\,dz\\ &\quad - \int q(z)\log[p(z)]\,dz \end{aligned} \]

What depends on \(\theta\)

  • The first carries no \(\theta\): it shifts the loss surface by a constant, leaving every gradient unchanged
  • The second is the cross-entropy — the only term training sees

Substituting the empirical distribution

The data give \(q\) as point masses at the observed outputs:

\[q(y) = \frac{1}{I}\sum_{i=1}^{I}\delta[y-y_i]\]

The cross-entropy term becomes a sum over the training set:

\[-\int q(z)\log[p(z)]\,dz = -\frac{1}{I}\sum_{i=1}^{I}\log\big[Pr(y_i|\theta)\big]\]

The factor \(1/I\) scales the surface, not the location of its minimum.

The same loss the recipe gave

Minimising the divergence

\[-\sum_{i=1}^{I}\log\big[Pr(y_i|\theta)\big]\]

Maximum likelihood

\[-\sum_{i=1}^{I}\log\big[Pr(y_i|f[x_i,\phi])\big]\]

With \(\theta\) the softmax of the \(K\) logits, both are the multiclass cross-entropy:

\[L[\phi] = -\sum_{i=1}^{I}\left(f_{y_i}[x_i,\phi] - \log\left[\sum_{k'=1}^{K}\exp\big[f_{k'}[x_i,\phi]\big]\right]\right)\]

Gradient descent and loss surfaces

The update rule

Initialise \(\phi\), then iterate two steps:

  1. Measure \(\dfrac{\partial L}{\partial\phi} = \left[\dfrac{\partial L}{\partial\phi_0},\dots,\dfrac{\partial L}{\partial\phi_N}\right]^T\) — the uphill direction

  2. Move against it: \(\phi \longleftarrow \phi - \alpha\cdot\dfrac{\partial L}{\partial\phi}\)

\(\alpha\) fixed is the learning rate; chosen per step by trying values is a line search.

The convex case: linear regression

For \(y = \phi_0 + \phi_1 x\) under least squares, each example contributes

\[\frac{\partial \ell_i}{\partial\phi} = \begin{bmatrix} 2(\phi_0 + \phi_1 x_i - y_i)\\ 2x_i(\phi_0+\phi_1 x_i - y_i)\end{bmatrix}\]

  • Convex — every chord lies above the surface
  • Descent from any start reaches the global minimum
  • Four line-search steps on \(I = 12\) pairs already fit closely

Training cannot fail here. This is the exception.

A non-convex surface: the Gabor model

\[ \begin{aligned} f[x,\phi] &= \sin[\phi_0 + 0.06\,\phi_1 x]\\ &\quad\cdot\exp\left(-\frac{(\phi_0+0.06\,\phi_1 x)^2}{32.0}\right) \end{aligned} \]

  • A sinusoid whose amplitude decays away from its centre
  • Two parameters, so the whole loss surface can be drawn
  • Trained on 28 pairs from \(\phi = [0.0, 16.6]^T\) plus noise

What two parameters already cost

  • Cyan — local minima, losses \(3.67\) to \(10.18\)
  • Gray — the global minimum, loss \(0.64\)
  • Blue cross — a saddle point

Where descent lands is decided by where it started.

Local minima and saddle points

Local minimum

  • Gradient zero
  • Loss increases in every direction
  • Not the overall minimum
  • No small step improves it

Saddle point

  • Gradient zero
  • Increases in some directions
  • Decreases in others
  • Descent escapes — but slowly

Near a saddle the surface is flat, so a small-gradient stopping rule stops there.

Higher dimensions make it worse

  • Two parameters can be searched exhaustively, or restarted from many positions
  • A network has millions of parameters, and neither is possible
  • Every additional dimension adds directions along which the surface can be flat

The destination of a descent run is determined entirely by its starting point.

Stochastic gradient descent

What the previous section assumed

  • Every parameter update used the whole training set
  • Each step computed \(\partial L/\partial\phi\) summed over all \(I\) examples
  • The trajectory was therefore deterministic

A deterministic path from a fixed start has a fixed destination.

Sampling a batch

At each iteration, draw a random subset \(\mathcal{B}_t\) and use only those examples:

\[\phi_{t+1} \longleftarrow \phi_t - \alpha\cdot\sum_{i\in\mathcal{B}_t}\frac{\partial \ell_i[\phi_t]}{\partial\phi}\]

  • Drawn without replacement; one pass through the data is an epoch
  • Batch of one, up to the whole set — the latter is full-batch descent

The noise is the point

  • The step is downhill on average, but need not be downhill at all for any batch
  • That makes moving temporarily uphill possible
  • And so jumping from one valley of the loss to another

An equivalent reading: SGD descends a different loss surface at each iteration, and a minimum for one batch is usually not one for the next.

Escaping the wrong valley

Left: gradient descent — point 1 reaches the global minimum, point 2 stalls at the local minimum 3. Right: SGD from the same point 2 crosses the ridge and arrives.

  • Line search reaches the global minimum only from the right valley
  • Started elsewhere, it descends to a local one
  • SGD escapes and still arrives

What follows from the noise

  • Each step still improves the fit to its own subset
  • Sampling without replacement weights every example equally
  • A subset is cheaper than the whole training set
  • Escapes local minima; less likely to stall at saddles
  • Evidence that it generalizes better in practice

SGD does not converge

  • Near the minimum every point is well described, so any batch gives a small gradient
  • But the parameters never quite stop moving

A learning rate schedule: \(\alpha\) starts high, cut by a constant factor every \(N\) epochs.

  • Early training explores, jumping between valleys
  • Later training fine-tunes, in smaller steps

Momentum and Adam

Momentum

\[ \begin{aligned} m_{t+1} &\leftarrow \beta\cdot m_t + (1-\beta)\sum_{i\in\mathcal{B}_t}\frac{\partial \ell_i[\phi_t]}{\partial\phi}\\ \class{hg-key}{\phi_{t+1}} &\class{hg-key}{{}\leftarrow \phi_t - \alpha\cdot m_{t+1}} \end{aligned} \]

The step is a weighted combination of this batch’s gradient and the direction moved last.

What momentum does

  • The recursion makes the step an infinite weighted sum of past gradients
  • Weights shrink as one moves back in time

Aligned gradients

Terms add, so the effective learning rate rises.

Oscillating gradients

Terms cancel, so the effective rate falls.

Smoother trajectory, less oscillation in valleys.

A fixed step size is misallocated

\(\alpha = 0.05\): stable, and very slow.

\(\alpha = 1.0\): fast, and unstable.

No single rate suits a surface steeper in one direction than another.

Normalizing the gradient

Measure the gradient and its pointwise square:

\[m_{t+1} \leftarrow \frac{\partial L[\phi_t]}{\partial\phi}, \qquad v_{t+1} \leftarrow \left(\frac{\partial L[\phi_t]}{\partial\phi}\right)^2\]

then divide one by the root of the other:

\[\class{hg-key}{\phi_{t+1} \leftarrow \phi_t - \alpha\cdot\frac{m_{t+1}}{\sqrt{v_{t+1}}+\epsilon}}\]

Why the normalizing factor

Normalized gradients, \(\alpha = 0.05\).

  • Only the sign survives in each coordinate
  • So each moves a fixed distance \(\alpha\) downhill
  • Good progress in both directions
  • But it bounces around the minimum

Adam

\[ \begin{aligned} m_{t+1} &\leftarrow \beta m_t + (1-\beta)\frac{\partial L}{\partial\phi}\\ v_{t+1} &\leftarrow \gamma v_t + (1-\gamma)\left(\frac{\partial L}{\partial\phi}\right)^2 \end{aligned} \]

Early estimates start from zero, so correct them:

\[\tilde{m}_{t+1} \leftarrow \frac{m_{t+1}}{1-\beta^{t+1}}, \qquad \tilde{v}_{t+1} \leftarrow \frac{v_{t+1}}{1-\gamma^{t+1}}\]

The correction terms

\[\class{hg-key}{\phi_{t+1} \leftarrow \phi_t - \alpha\cdot\frac{\tilde{m}_{t+1}}{\sqrt{\tilde{v}_{t+1}}+\epsilon}}\]

  • \(\beta, \gamma \in [0,1)\), so \(\beta^{t+1}\) and \(\gamma^{t+1}\) shrink each step
  • The denominators approach one, and the correction fades
  • It matters only for the first few iterations

The hyperparameters

  • \(\alpha\) — the learning rate
  • \(\beta\) — momentum on the gradient, typically \(0.9\)
  • \(\gamma\) — momentum on the squared gradient, typically \(0.99\)
  • \(\epsilon\) — a small constant preventing division by zero

Adam is less sensitive to the initial learning rate, so it needs no elaborate schedule.

Adam is the robust choice

  • Gradient magnitudes depend on a parameter’s depth in the network
  • Adam compensates, balancing changes across layers
  • \(\alpha = 0.05\), \(\beta = 0.9\), \(\gamma = 0.99\)

The trajectory, smoothed

SGD, with and without momentum.

Adam: momentum on both terms.

In practice both statistics come from minibatches, so the real path is noisier.

Hyperparameters of the training algorithm

What is not learned: the hyperparameters

The training algorithm carries parameters of its own, chosen before training and fixed throughout:

  • The optimiser, and its momentum coefficients \(\beta, \gamma\)
  • The learning rate \(\alpha\) and its schedule
  • The batch size

The gradient updates learn \(\phi\). The hyperparameters are set, not learned.

Choosing them

  • More art than science
  • Usual practice: train many models, keep the best — hyperparameter search
  • The protocol that makes that comparison honest comes later in this unit

Learning rate warm-up — adaptive rates rest on gradient statistics that are noisy at the start, so the rates are raised gradually over the first few thousand iterations.

Where this leaves the optimiser

  • Training finds \(\phi\) at the minimum of \(L[\phi]\)
  • Descent measures, then moves where the loss falls fastest
  • Non-convex surfaces hold local minima and saddles
  • SGD mitigates both, and is cheaper per step
  • Momentum converges faster; Adam normalizes per coordinate

Backpropagation

What the optimisers assumed

For a network with three hidden layers,

\[ \begin{aligned} h_1 &= a[\beta_0 + \Omega_0 x], & h_2 &= a[\beta_1 + \Omega_1 h_1],\\ h_3 &= a[\beta_2 + \Omega_2 h_2], & f[x,\phi] &= \beta_3 + \Omega_3 h_3 \end{aligned} \]

every update needs \(\partial\ell_i/\partial\beta_k\) and \(\partial\ell_i/\partial\Omega_k\) for every layer — at every iteration, for models of \(10^{12}\) parameters.

Two observations

Observation 1

A weight’s effect is scaled by the activation at its source.

So store every activation on the way forward — the forward pass.

Observation 2

A change ripples through every later layer.

The same quantities recur, so compute them once, in reverse — the backward pass.

The toy example

Eight scalar parameters, composing \(\sin\), \(\exp\) and \(\cos\):

\[ \begin{aligned} h &= \sin[\beta_0+\omega_0 x]\\ f[x,\phi] &= \beta_3 + \omega_3\cos\big[\beta_2 + \omega_2\exp[\beta_1+\omega_1 h]\big] \end{aligned} \]

  • Differentiating by hand is possible but error-prone
  • And wasteful: \(\partial\ell_i/\partial\omega_0\) repeats the same exponential three times

The forward pass

\[ \begin{aligned} f_0 &= \beta_0 + \omega_0 x_i, & h_1 &= \sin[f_0],\\ f_1 &= \beta_1 + \omega_1 h_1, & h_2 &= \exp[f_1],\\ f_2 &= \beta_2 + \omega_2 h_2, & h_3 &= \cos[f_2],\\ f_3 &= \beta_3 + \omega_3 h_3, & \ell_i &= (f_3 - y_i)^2 \end{aligned} \]

Every intermediate value is computed once and kept.

The parameter derivatives

The chain rule expresses each parameter derivative in terms of the derivative at its own pre-activation, \(\partial\ell_i/\partial f_k\):

\[\frac{\partial\ell_i}{\partial\beta_k} = \frac{\partial f_k}{\partial\beta_k}\frac{\partial\ell_i}{\partial f_k}, \qquad \frac{\partial\ell_i}{\partial\omega_k} = \frac{\partial f_k}{\partial\omega_k}\frac{\partial\ell_i}{\partial f_k}\]

Since \(f_k = \beta_k + \omega_k h_k\), the two local terms are immediate:

\[\frac{\partial f_k}{\partial\beta_k} = 1, \qquad \frac{\partial f_k}{\partial\omega_k} = h_k\]

The eight parameter derivatives reduce to the four \(\partial\ell_i/\partial f_k\), which the backward pass computes.

The backward pass

Start at the end, \(\partial\ell_i/\partial f_3 = 2(f_3-y_i)\), then chain backwards:

\[\frac{\partial\ell_i}{\partial f_2} = \frac{\partial h_3}{\partial f_2}\left(\frac{\partial f_3}{\partial h_3}\frac{\partial\ell_i}{\partial f_3}\right)\]

The bracketed term was computed at the previous step.

From scalars to layers

The toy example used scalar weights. In a real network each \(\omega_k\) becomes a matrix \(\Omega_k\) and each \(f_k\) a vector:

\[f_0 = \beta_0 + \Omega_0 x_i, \qquad h_k = a[f_{k-1}], \qquad f_k = \beta_k + \Omega_k h_k\]

The backward step carries the same structure, with a transpose where the scalar had none:

\[\frac{\partial\ell_i}{\partial\Omega_k} = \frac{\partial\ell_i}{\partial f_k}h_k^T, \qquad \frac{\partial\ell_i}{\partial f_{k-1}} = \mathbb{I}[f_{k-1}>0]\odot\left(\Omega_k^T\frac{\partial\ell_i}{\partial f_k}\right)\]

Forward multiplies by \(\Omega_k\); backward multiplies by \(\Omega_k^T\).

Cheap in time, expensive in memory

The cost

  • One matrix multiply per pass
  • \(\Omega_k\) forward, \(\Omega_k^T\) back
  • Every activation stored until the backward pass

When it does not fit

  • Checkpointing — keep some, recompute the rest
  • Distributed — split by batch, layer or operation

Memory, not arithmetic, caps the model size.

Algorithmic differentiation

Frameworks derive the gradients from the model specification alone.

  • Each component knows its own derivative
  • The framework knows the order of operations
  • Together: both passes, with nothing coded by hand
  • Batches run in parallel, the input a tensor

Sources of test error

Perfect training, poor test

MNIST-1D — ten classes, \(I = 4000\) examples, \(D_i = 40\) dimensions.

  • Two hidden layers of \(D = 100\), softmax over \(D_o = 10\)
  • SGD, batch 100, learning rate \(0.1\), 6000 steps
  • Training data classified perfectly by about 4000 steps
  • Test error on 1000 fresh examples: about 40%
  • Chance is 90%, so it learned — but it also memorised

The test loss rises

  • Test error falls, then flattens
  • Test loss falls for 1500 steps, then climbs

The model makes the same mistakes with growing confidence. The softmax drives pre-softmax activations to extremes, so the negative log-likelihood grows while the error rate does not.

Noise and bias

Noise — several valid \(y\) per \(x\).

Bias — the family cannot fit the truth.

Noise limits the test set, not the training set — the same \(x\) rarely recurs.

Variance

  • The training examples are limited
  • Noise cannot be told from signal
  • A different sample gives a different fit

A stochastic optimizer adds more.

Two averages

The truth

\[\mu[x] = \mathbb{E}_y\big[y[x]\big]\]

The mean of \(Pr(y|x)\), averaged over the noise.

The model

\[f_\mu[x] = \mathbb{E}_\mathcal{D}\Big[f\big[x,\phi[\mathcal{D}]\big]\Big]\]

The fitted function, averaged over training sets \(\mathcal{D}\).

Bias is the gap between them; variance is the spread about \(f_\mu[x]\).

The decomposition

\[ \begin{aligned} \mathbb{E}_\mathcal{D}\Big[\mathbb{E}_y\big[L[x]\big]\Big] &= \underbrace{\mathbb{E}_\mathcal{D}\Big[\big(f[x,\phi[\mathcal{D}]]-f_\mu[x]\big)^2\Big]}_{\text{variance}}\\ &+ \underbrace{\big(f_\mu[x]-\mu[x]\big)^2}_{\text{bias}} + \underbrace{\sigma^2}_{\text{noise}} \end{aligned} \]

  • The three combine additively for least-squares regression
  • The cross terms vanish, each containing a mean minus its own expectation

What to change

Variance

Reduced by more data — noise averages out and the input space is better sampled.

Bias

Reduced by more capacity — hidden units or layers the model can use.

Noise admits no remedy. It is the floor.

The bias-variance trade-off

  • Bias (orange) falls with capacity
  • Variance (cyan) rises
  • Their sum is least at capacity four

Ten regions fit fifteen points better than three — but the extra power models the noise.

Double descent and model selection

Double descent

MNIST-1D, 15% of labels randomized.

  • Test error peaks, then descends again
  • The second descent falls below the earlier best
  • Training error is already zero throughout

The classical curve is not the whole picture.

The three regimes

  1. Classical, under-parameterized — the bias-variance trade-off holds
  2. Critical — capacity just suffices to memorise; error peaks
  3. Modern, over-parameterized — error descends a second time

Capacity beyond the interpolation threshold is not wasted.

Why it happens

The peak

  • Just enough capacity to fit exactly
  • Contorts to reach every point
  • Erratic between them

The second descent

  • The training points are already fit
  • Capacity can only change what lies between them
  • More capacity, smoother interpolation

Which smooth solution is chosen is not yet explained.

Choosing hyperparameters honestly

  • Bias and variance are not measurable: neither the true function nor many datasets is available
  • No theory says how much capacity suffices

A third split, the validation set: train on training, select on validation, measure once on test.

The search itself is sampled, not solved — each sample is a full training run.

Regularization

Why regularize

The symptom

A generalization gap — training performance the test set does not match.

The lever

Among solutions with similar training loss, prefer the ones likely to be robust.

Strictly, an added term. Commonly, any strategy that closes the gap.

Controlling the weights

\[\hat{\phi} = \underset{\phi}{\operatorname{argmin}}\left[\sum_{i=1}^{I}\ell_i[x_i,y_i] + \lambda\cdot g[\phi]\right]\]

  • \(g[\phi]\) is larger where parameters are less preferred
  • \(\lambda\) sets the exchange rate between fitting and preference
  • The minima move, so training converges elsewhere

L2 regularization

\[\hat{\phi} = \underset{\phi}{\operatorname{argmin}}\left[\sum_{i=1}^{I}\ell_i[x_i,y_i] + \lambda\sum_j \phi_j^2\right]\]

  • Applied to weights but not biases, it is weight decay
  • Smaller weights make the output vary less, layer by layer
  • Too large a \(\lambda\) overpowers the likelihood

The penalty buys smoothness, and \(\lambda\) sets the price.

Early stopping

  • Coarse structure is learned first, the noise later
  • Weights start small and have no time to grow — like L2
  • Its one hyperparameter is free

Stop where the validation error is lowest.

The regularization toolbox

Explicit

  • L2 / weight decay — penalise large weights
  • Label smoothing — assume a fraction of labels are wrong
  • Noise — on inputs, weights or labels

Implicit, or by data

  • Early stopping
  • Dropout, ensembling, bagging
  • Augmentation, transfer, multi-task
  • SGD itself — it prefers batch agreement

Summary

🎯 What this unit established

  1. A network predicts a distribution, not a value
  2. Every loss in this unit is one criterion — maximum likelihood — applied to a different distribution
  3. Training succeeds on the geometry of the loss surface, not on the cleverness of the update rule
  4. Backpropagation gets every derivative in two passes

Capacity is not the enemy: past the interpolation threshold, test error falls again.

🚀 Where next: images

An image of \(224\times224\) pixels in colour is \(150{,}528\) numbers. One fully connected hidden layer of the same width:

\[\approx 2.3 \times 10^{10} \text{ weights}\]

  • Nowhere does the model learn that neighbouring pixels are related
  • Every pixel position is learned from scratch

Convnets: one small kernel, shared across the whole image. 🔍

🧱 And then: depth

Everything in this unit makes training work. One failure it does not fix:

  • Very deep plain networks reach a higher training loss than shallow ones
  • Not overfitting — they fail on the data they were trained on

The next unit confronts both. 💪