Basics to Machine Learning · v1.10.27
2026-09-11 16:20:06
Two gaps: no justified loss, and no procedure.
What the previous unit fixed
What this unit supplies

One distribution per output domain.
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\).
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:
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]\).
Steps (2)–(4) never vary. Only the distribution in (1) does: normal, Bernoulli, categorical.
\[Pr(y|\mu,\sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\left[-\frac{(y-\mu)^2}{2\sigma^2}\right]\]

\[ \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} \]
Least squares is a consequence, not a convention.

Homoscedastic: constant variance.

Heteroscedastic: variance varies with \(x\).
A second network output learns the variance: \(\sigma^2 = f_2[x,\phi]^2\).
\[\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]\]
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}\]

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.
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]\).
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]\]
For \(y \in \{1,\dots,K\}\), \(K\) parameters with \(Pr(y=k) = \lambda_k\):
The network produces a logit vector \(z \in \mathbb{R}^K\), one output per class, with no constraint on any of them.

Any vertical slice sums to one.
\[\operatorname{softmax}_k[z] = \frac{\exp[z_k]}{\sum_{k'=1}^{K}\exp[z_{k'}]}\]
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)\).
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.
Training so far minimised a likelihood. The same objective can be posed as a distance between distributions.
The data distribution, \(q\)
The model distribution, \(p\)
Learning becomes: move \(p\) until it is as close as possible to \(q\).
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 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.
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)\]
Initialise \(\phi\), then iterate two steps:
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
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.
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}\]
Training cannot fail here. This is the exception.
\[ \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} \]

Where descent lands is decided by where it started.
Local minimum
Saddle point
Near a saddle the surface is flat, so a small-gradient stopping rule stops there.
The destination of a descent run is determined entirely by its starting point.
A deterministic path from a fixed start has a fixed destination.
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}\]
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.

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.
A learning rate schedule: \(\alpha\) starts high, cut by a constant factor every \(N\) epochs.
\[ \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.
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.

\(\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.
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}}\]

Normalized gradients, \(\alpha = 0.05\).
\[ \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}}\]
\[\class{hg-key}{\phi_{t+1} \leftarrow \phi_t - \alpha\cdot\frac{\tilde{m}_{t+1}}{\sqrt{\tilde{v}_{t+1}}+\epsilon}}\]
Adam is less sensitive to the initial learning rate, so it needs no elaborate schedule.


SGD, with and without momentum.

Adam: momentum on both terms.
In practice both statistics come from minibatches, so the real path is noisier.
The training algorithm carries parameters of its own, chosen before training and fixed throughout:
The gradient updates learn \(\phi\). The hyperparameters are set, not learned.
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.
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.
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.
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} \]
\[ \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 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.

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.
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\).
The cost
When it does not fit
Memory, not arithmetic, caps the model size.
Frameworks derive the gradients from the model specification alone.
MNIST-1D — ten classes, \(I = 4000\) examples, \(D_i = 40\) dimensions.
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 — 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.

A stochastic optimizer adds more.
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]\).
\[ \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} \]
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.

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

MNIST-1D, 15% of labels randomized.
The classical curve is not the whole picture.
Capacity beyond the interpolation threshold is not wasted.
The peak
The second descent
Which smooth solution is chosen is not yet explained.
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.
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.
\[\hat{\phi} = \underset{\phi}{\operatorname{argmin}}\left[\sum_{i=1}^{I}\ell_i[x_i,y_i] + \lambda\cdot g[\phi]\right]\]
\[\hat{\phi} = \underset{\phi}{\operatorname{argmin}}\left[\sum_{i=1}^{I}\ell_i[x_i,y_i] + \lambda\sum_j \phi_j^2\right]\]
The penalty buys smoothness, and \(\lambda\) sets the price.

Stop where the validation error is lowest.
Explicit
Implicit, or by data
Capacity is not the enemy: past the interpolation threshold, test error falls again.
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}\]
Convnets: one small kernel, shared across the whole image. 🔍
Everything in this unit makes training work. One failure it does not fix:
The next unit confronts both. 💪