Math Relearn/ Track 4 · Mastery/ Module 09

Roadmap: ML → DL → agentic models

The last module contains no new mathematics. It does three jobs: measure how much of the course actually stuck, show you where every idea reappears inside deep learning and agents, and hand you the schedule that keeps it alive. Honest, concrete, no cheerleading.

On this page
    Track 4 · Mastery

    09 · Roadmap: ML → DL → agentic models

    ⏱ ~1 h📎 All🎯 Leave with a schedule and a repeatable way to keep the math alive.

    Modules 01–08 gave you the tools: notation, linear algebra, calculus, probability, statistics, information theory, optimization, and eight algorithms rebuilt from those parts. This module is the payoff and the launchpad — it proves what you actually own, and then shows the same parts doing the same jobs inside neural networks and agents.

    By the end you can

    • Re-answer the Module 00 diagnostic cold and diff it against your day-one answers — the only measurement that counts.
    • Tick a mastery checklist of falsifiable abilities from Modules 01–08, and name the module behind each item.
    • Point at a deep-learning object — a layer, an attention matrix, a loss, an optimizer — and name the course concept underneath it.
    • Write the attention scaling, the UCB rule and the ELBO from memory, and say what each term is for.
    • Describe an agent as a loop of Bayes updates, expectations, dot products and gradients.
    • Leave with a week-by-week schedule and a retention system that outlives the notebook.

    Why this module exists

    Two failure modes kill self-directed relearning. The first is stopping after the last module and assuming the knowledge stuck — it does not, not without measurement and a maintenance plan. The second is the opposite: believing that because deep learning now generates the gradients, the fundamentals were optional after all. Both are answered here, in that order.

    So: first we re-run the diagnostic from Module 00 and compare — honestly — against where you started. Then we walk the same six tool-kits forward into deep learning, transformers and generative models, so that nothing there reads as a new subject. Then we go one step further, to the destination you named when you started: agentic models. The point of that section is not to teach agents, it is to show you that you already own their mathematics.

    What you will not get here: new definitions, new proofs, or a disguised advanced module. Mastery at this stage is demonstrated by retrieval and derivation, not by more reading.

    Deep learning is this course at larger scale, and an agent is Bayes, expectations, dot products and gradients arranged in a loop. Nothing else is coming.
    Pass 1

    Remember — the diagnostic, again

    Answer cold, in writing, before you scroll past this banner

    The 12 questions, re-stated — same questions, new attempt

    These are the twelve cold questions from Module 00, restated compactly. Do not open any module, do not search anything. Ten minutes, short answers. If you have your original answers to hand, do not look at them yet — the comparison happens in Section 1, and grading yourself against an answer you are currently looking at measures nothing.

    1. In words: what does $f(x) = x^2$ mean? Compute $f(3)$ and $f(-2)$.
    2. Expand $(a+b)^2$ and $(a+b)^3$.
    3. What is a vector? Give a real example from an ML dataset.
    4. What does the dot product of two vectors measure?
    5. What is a matrix? Geometrically, what does matrix × vector do?
    6. What is a derivative, in one sentence?
    7. Gradient of $g(x,y) = x^2 + y^2$ at $(1,2)$? Which direction does it point?
    8. $P(A) = 0.3$, $P(B) = 0.4$, independent: what is $P(A \cap B)$?
    9. What does $\mathbb{E}[X]$ mean? Expected value of a fair die?
    10. Correlation vs causation — one sentence each, and why ML cares.
    11. Why do we maximise the log-likelihood instead of the likelihood?
    12. Name one ML algorithm and list the mathematical ideas holding it together.

    Six "a month later" cards — the whole course in small questions

    The twelve above test what survived from before. These six test what Modules 01–08 were supposed to install. Same rule: write your answer, then open the card.

    Shape check: $X \in \mathbb{R}^{n\times d}$, $w \in \mathbb{R}^d$. Shapes of $Xw$, $Xw - y$, and the MSE gradient?
    $Xw \in \mathbb{R}^n$, $Xw - y \in \mathbb{R}^n$, gradient $\in \mathbb{R}^d$. One prediction per sample ($n$ numbers); the residual vector has one entry per sample; the gradient must live where the parameters live, in $\mathbb{R}^d$. It is $\nabla_w L = \tfrac{2}{n} X^\top (Xw - y)$ — the $X^\top$ turns an $n$-vector of residuals into a $d$-vector of per-feature sensitivities. If your gradient's shape does not match your parameter's shape, you are wrong; there is no third option.
    Write $\sigma'(x)$ in terms of $\sigma(x)$ — and say where it bites.
    $\sigma'(x) = \sigma(x)\,(1 - \sigma(x))$. Maximum value $0.25$, at $x = 0$; and near $0.25$ is exactly where the sigmoid saturates ($\sigma \to 0$ or $1$). It bites in deep stacks: backprop multiplies many of these together, so the gradient reaching early layers is multiplied by at most $0.25$ per sigmoid — after 20 layers that is roughly $10^{-12}$. This is the whole argument for ReLU and residual connections.
    Bayes' rule, and a base-rate posterior you can do in your head.
    $P(\theta \mid D) = \dfrac{P(D \mid \theta)\,P(\theta)}{P(D)}$. Test: prevalence 1%, sensitivity 90%, false-positive rate 9%. In 10 000 people: 100 sick, 90 of them test positive; 9 900 healthy, 891 test positive anyway. So $P(\text{sick} \mid +) = 90 / (90 + 891) \approx 9\%$. A "90% accurate" test, in a rare-disease population, is right fewer than one time in ten. The prior does real work — always ask for the base rate.
    What is a p-value? Name one thing it is not.
    Probability of data at least this extreme, assuming the null hypothesis is true — $P(\text{data} \mid H_0)$. It is not the probability that the null is true, not the probability the result is a fluke, and not an effect size. Getting that direction right is the difference between doing statistics and doing numerology; Module 05 section 5 is the repair kit.
    Cross-entropy from surprise. KL asymmetry, and one place it matters.
    Surprisal of an outcome under $p$ is $-\log p(v)$; expected surprise is entropy $H(p) = -\sum_v p(v)\log p(v)$; using your model's $q$ instead of the truth scores outcomes at the wrong rate, giving cross-entropy $H(p,q) = -\sum_v p(v)\log q(v)$. And $\mathrm{KL}(p \| q) = \sum_v p(v)\log \frac{p(v)}{q(v)}$ is asymmetric: $\mathrm{KL}(p\|q) = 0$ only if $q$ covers everything $p$ uses, so a model that assigns tiny mass where the truth has mass is punished hard, and extra mass where the truth has none is punished gently. That asymmetry is why knowledge distillation, VAEs and contrastive losses each pick a direction deliberately.
    Name the mathematical backbone of four classics: PCA, k-means, Naive Bayes, decision tree.
    PCA: eigenvectors of the covariance matrix = directions of maximal variance (Module 02). k-means: alternating assignment by nearest-centroid (distance, Module 02) and update by the mean = the mean minimises squared error (Module 04's MLE under Gaussian noise). Naive Bayes: Bayes' rule plus conditional independence given the class (Module 04). Decision tree: greedy recursive splits maximising information gain = entropy reduction (Module 06), measured as a purity statistic (Module 05).
    Pass 2

    Understand — measure, map, and move up in scale

    Ten sections: the scorecard, the checklist, and where every idea goes next

    1 · The diagnostic revisited: measure the gain

    You have just answered the twelve questions above a second time. Take out your Module 00 answers — the ones with the date on them — and put the two attempts side by side. For each question, mark one of four states:

    1. Still wrong. Same misconception, same blank. That item maps to one module; put it on the repair list.
    2. Wrong, differently. The old mistake is gone and a new, subtler one took its place. This is progress — you have moved to a harder error. Treat it as half a win.
    3. Right, procedurally. You got the answer by grinding, and cannot say why it is true. Recognition, not ownership — schedule a re-derivation.
    4. Right, with a reason. You could have taught it. This is the only state that counts as done.

    The count that matters is state 4, and how it moved. This comparison — not any quiz on this site, not any score in a textbook — is the measurement of the relearning, for a plain reason: it is the only place your day-one baseline exists. Every other instrument here is a practice exercise; the diagnostic pair is a before/after on the same subject, cold and unprompted, which is the closest thing to a controlled experiment you can run on yourself.

    Three honest readings of the result. If states 3 and 4 dominate: the course did its job, and Section 8 is your maintenance plan. If states 1 and 2 dominate but the explanations now land on re-reading: you learned more than you can retrieve — the fix is not more content, it is more retrieval (Section 8 again). If question 12 is still blank: that is the real alarm, because question 12 is the course's whole thesis — every algorithm is an assembly of these tools — and it is the skill that transfers to deep learning and agents.

    Two ways to lie to yourself here

    Grading against the answer key instead of against you. The key in Module 00 was written by someone else; matching it is not progress, changing your own answers is. And recognising instead of producing. Reading question 7 and thinking "obviously, $(2,4)$" is not answering it — recognition is cheap and feels identical to recall until the exam, the bug report, or the whiteboard. Only a written, closed-book attempt counts. If you peeked: say so, and re-run the six cards in a week instead of pretending.

    2 · The mastery checklist

    Eight modules, one falsifiable ability each pair. Every item is phrased so that you either did it or you did not — no partial credit, no "sort of". Work through them aloud, one sitting, no notes. The module tag tells you exactly where the repair lives if an item fails.

    • M01 · Read any ML objective aloud: which symbols are data, which are parameters, what is being summed, and which direction is "good".
    • M02 · Given $X \in \mathbb{R}^{n \times d}$ and $w \in \mathbb{R}^d$, state the shape of every term in the gradient of the MSE — and the gradient itself.
    • M02 · Derive the normal equation $w = (X^\top X)^{-1} X^\top y$ in four lines, and say two situations where you must not use it.
    • M02 · Say what an eigenvector is for, and how PCA uses it — geometrically, not procedurally.
    • M03 · Explain why $\sigma'(x) = \sigma(x)(1-\sigma(x))$ and why that causes vanishing gradients in a deep stack.
    • M03 · Run a finite-difference gradient check on a loss you just wrote, and say what tolerance is acceptable.
    • M04 · State Bayes' rule and compute a base-rate posterior without a calculator — the 1%-prevalence test above is the standard item.
    • M04 · Say which loss MLE gives you under Gaussian noise and under Bernoulli noise — squared error and cross-entropy, and why the log turns one into the other.
    • M05 · Say what a p-value is and is not, and what a 95% confidence interval does and does not claim.
    • M05 · Write the bias–variance decomposition and name which knob moves which term.
    • M06 · Derive cross-entropy from surprise; state KL's asymmetry and one place the direction of the asymmetry matters in practice.
    • M07 · Write the gradient-descent, momentum and Adam updates from memory, including what $\beta_1$ and $\beta_2$ each smooth.
    • M07 · Explain why the learning rate cannot be regularised away, and what warmup and gradient clipping actually fix.
    • M08 · Name the mathematical backbone of eight classic algorithms: linear and logistic regression, k-means, Naive Bayes, decision trees, SVM, PCA, k-NN.
    • M08 · Say what an SVM maximises, and why the kernel trick is a dot product you never have to compute explicitly.
    The rule: do not continue past a failed item

    If you cannot perform an item on this checklist aloud, in one go, that module is not done. Go back to it — but only to the single section that owns the failed item, not the whole module, and not a different resource on the same topic. Finishing a page is not owning a skill, and re-reading a page you already "finished" rarely repairs the gap; re-doing its one exercise usually does. Progress that skips a failed item is debt with compounding interest, and deep learning is where it comes due.

    3 · The same math, bigger: deep learning

    Here is the central claim of this module, and it is a fact, not encouragement: deep learning introduces no new mathematics from this course. It uses the same linear algebra, the same chain rule, the same maximum-likelihood argument, the same cross-entropy, the same statistics — with bigger matrices, more composition, and automatic differentiation doing the mechanical derivative steps. Read the map first, then the three pieces that deserve prose.

    Course conceptMet inThe same object, inside a neural network
    Vectors & matricesM02A layer $h = \sigma(Wx + b)$: $W \in \mathbb{R}^{d_{\text{out}} \times d_{\text{in}}}$ is the whole layer; a network is affine maps composed with non-linearities
    Dot product, alignmentM02Attention scores $QK^\top$; embedding similarity; every retrieval in an agent
    Eigen, SVDM02Analysing what a trained weight matrix does; low-rank structure exploited by pruning and adapters (LoRA)
    Gradient, chain ruleM03Backpropagation: one backward sweep on the computation graph gives every partial derivative
    JacobianM03Reverse-mode automatic differentiation = a chain of vector–Jacobian products; Jacobian–vector products are the primitive
    Saturation, $\sigma'$M03, M08Vanishing and exploding gradients; why ReLU, residuals and normalisation exist
    Expectation & varianceM04Initialisation: choose weight variances so activations keep variance $\approx 1$ through depth (He/Xavier)
    Maximum likelihoodM04The training objective itself: maximum log-likelihood is minimum cross-entropy / squared error
    Bayes' ruleM04Amortised inference (VAE encoder), filtering in agents, and dropout's Bayesian reading (an analogy — see below)
    Standard error, samplingM05A mini-batch gradient is a noisy estimate of the full gradient; its noise is a regulariser, and it is why learning rate × batch size matter together
    Bias–varianceM05, M07Double descent and the interpolation threshold (see the statistics paragraph below)
    CalibrationM05Temperature scaling of logits; whether "0.9 confidence" means 90% frequency
    Entropy, cross-entropy, KLM06Every classification loss; perplexity; KL as a regulariser and as a distillation penalty
    Convexity & optimizersM07Non-convex landscapes, saddle points, SGD+momentum+Adam, warmup, schedules, clipping, learning-rate finding
    RegularizationM07Weight decay, dropout, early stopping, batch normalisation — same bias–variance trade, bigger knobs
    Distance & similarityM02, M08Nearest-neighbour retrieval over embeddings; contrastive losses shaped by the metric you choose

    Attention, shape by shape

    A layer is one affine map plus a non-linearity: $h = \sigma(Wx + b)$, and a network composes such maps. The one operation that made the current generation of models possible — attention — is nothing but matrix products and a softmax. Three token sequences of length $n$ become matrices of queries, keys and values:

    $$Q, K \in \mathbb{R}^{n \times d_k}, \quad V \in \mathbb{R}^{n \times d_v}, \qquad A = \mathrm{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right) V$$ each token's query is compared with every key by a dot product; the softmax turns the n scores into weights; the output is a weighted average of value vectors

    Read the shapes: $QK^\top$ is $n \times n$ — entry $(i,j)$ is token $i$'s query dotted with token $j$'s key, i.e. Module 02's alignment score. Row-normalising with a softmax makes every row a probability distribution over the $n$ keys. Multiplying by $V$ then averages value vectors, and each output row is an $d_v$-vector. Nothing here is beyond Module 02 plus Module 04's softmax.

    The $\sqrt{d_k}$ deserves its own sentence, because it is a variance argument (Module 05's tool). If the components of a query and a key are independent with unit variance, then their dot product $q \cdot k = \sum_{i=1}^{d_k} q_i k_i$ has variance $d_k$ — logits get wilder as the vectors get longer, the softmax saturates onto one key, and gradients die. Dividing by $\sqrt{d_k}$ divides variance by $d_k$, restoring unit variance at every width. Block 1 measures exactly this: with four random keys, the unscaled softmax at $d_k = 64$ has mean row-entropy $0.22$ nats against a maximum of $\ln 4 \approx 1.39$, while the scaled version sits at $1.11$ — and at $d_k = 256$ the unscaled version collapses further, to $0.11$, while the scaled one barely moves.

    Vanishing gradients: the chain rule, multiplied too many times

    Backpropagation is the chain rule over a computation graph — Module 03, section 7. To reach a parameter in layer 1 you multiply through every layer in between, and each sigmoid contributes a factor bounded by $0.25$: twenty such factors give roughly $10^{-12}$. The gradient is not "small", it is gone, indistinguishable from zero in float arithmetic. Exploding gradients are the mirror case, when the weight matrices have spectral radius above 1.

    Every architectural fix of the last decade is a patch on this one product. ReLU makes the derivative exactly 1 on the active side. Residual connections give the gradient a highway: with $h_{l+1} = h_l + F(h_l)$ the backward pass picks up an additive identity path, so the gradient is a sum of routes instead of a pure product — one route can never zero the whole thing. Initialisation keeps variance at 1 through depth, normalisation re-enforces it every layer, and clipping caps the damage when it still spikes. Four techniques, one equation.

    Note also what backprop hands you for a whole layer at once — the gradient of a layer family. For a linear layer $h = Wx + b$ with incoming error signal $\delta \in \mathbb{R}^{d_{\text{out}}}$ and layer input $x \in \mathbb{R}^{d_{\text{in}}}$, every weight gets its sensitivity from the same outer product, $\partial L / \partial W = \delta\, x^\top \in \mathbb{R}^{d_{\text{out}} \times d_{\text{in}}}$ — a matrix shaped exactly like $W$. A million parameters, one sweep, one matrix product. That is also why "the framework does the derivatives for you" never made the calculus optional: the sweep is mechanical, but whether those derivatives are meaningful — finite, non-saturating, pointing downhill — is not.

    Optimization at scale: same engine, harsher landscape

    Modules 03 and 07 gave you convexity, descent and the update rules; deep learning removes convexity and keeps everything else. The loss surface of a large network is non-convex, but the practical failure point is rarely a "local minimum" — it is a saddle point, where the gradient is near zero because some directions rise and others fall. Saddles are generic in high dimensions and rare in two, which is one honest reason textbook intuition misleads here. The update rules you wrote from memory still run the loop; the extra machinery manages noise and scale:

    $$v_{t+1} = \beta v_t + \eta \nabla L(\theta_t), \qquad \theta_{t+1} = \theta_t - v_{t+1}$$ momentum: accumulate a velocity that smooths noisy steps; Adam adds per-coordinate scaling by smoothed squared gradients, so each parameter gets its own effective step size

    Around those updates sit the tricks, each now legible. Warmup starts the learning rate tiny because Adam's per-coordinate statistics are meaningless in the first steps — a variance argument. Schedules decay the rate as the landscape flattens. Gradient clipping bounds the size of each step. Learning-rate finding sweeps $\eta$ once and reads off the largest stable value — the cheapest hyperparameter experiment there is. And batch normalisation is best read through Module 07's conditioning section: it re-centres and re-scales intermediate activations so the optimizer sees a less elongated loss surface, which is what makes aggressive learning rates survivable. Initialisation is the same distributional reasoning as Module 04: choose weight variances so activations keep variance $\approx 1$ at every depth, and the signal neither dies nor explodes.

    The ELBO: a lower bound you can actually optimise

    A latent-variable model defines $p(x) = \int p(x \mid z)\, p(z)\, dz$, an integral nobody can compute. Jensen's inequality (Module 06 — concavity of $\log$) turns that intractable quantity into a tractable lower bound, the evidence lower bound or ELBO, by sliding the expectation inside the log:

    $$\log p(x) \;\ge\; \mathbb{E}_{q_\phi(z \mid x)}\big[\log p_\theta(x \mid z)\big] - \mathrm{KL}\big(q_\phi(z \mid x)\,\|\,p(z)\big)$$ expected reconstruction log-likelihood, minus how far your approximate posterior sits from the prior — maximise this and you maximise a bound on the log-likelihood

    Two quantities trade off, and that trade is the entire design space of the model: reconstruction (the decoder must explain $x$ from $z$) against regularity (the encoder's posterior must stay close to the prior, so that drawing a fresh $z \sim p(z)$ produces something sensible). Push the first too hard and the latent space stops being usable; push the second and you get the posterior collapse failure mode, where the model ignores $z$ entirely. Everything else in a VAE is engineering.

    Information theory and statistics, scaled up

    Information theory becomes the vocabulary of losses. Every classification loss you will meet is cross-entropy, $H(p, q) = -\sum_v p(v)\log q(v)$ — at 2 classes or 100 000, same formula. Perplexity is exponentiated mean cross-entropy, quoted instead of nats because it reads as "the model is about as unsure as if it were choosing uniformly between $k$ equally likely tokens". KL shows up twice more: as a regulariser pulling a latent distribution toward a prior (the ELBO above, the VAE below), and as the distillation penalty — matching a teacher's soft outputs is $\mathrm{KL}(p_{\text{teacher}} \| q_{\text{student}})$, and the direction is chosen so the teacher's full distribution is honoured, not just its argmax.

    Statistics gets stranger, and more important, at scale. Generalisation is still the question, but deep networks routinely have more parameters than data points and still generalise — the classical bias–variance story predicted they should not. Double descent is the observed phenomenon: test error falls, spikes around the interpolation threshold (the point where the model first fits the training data exactly), then falls again as the model gets larger still, eventually below the first minimum. Be honest about the state of play: theory is catching up (implicit regularization, the role of noise and architecture) but lags the practice, so treat double descent as a measured fact with competing explanations, not settled doctrine. Two things did not change and matter more than ever: calibration — whether a stated 0.9 happens 90% of the time, measured by log-loss and repaired by temperature scaling — and evaluation discipline, because the larger the model, the more opportunities for leakage, contamination and test-set overfitting, and the more expensive a wrong "it works" claim becomes.

    One sentence of honesty about the Bayesian reading of dropout

    Randomly dropping units during training is analogous to sampling from a posterior over predictions, and the analogy is useful — it explains why averaging several dropout passes gives uncertainty estimates. It is not a derivation: the variational argument holds under assumptions that a real network violates. Keep the analogy, distrust the equivalence.

    4 · Transformers and language models, in your math

    A language model is a stack of the operations you already own. Walk it once, in shapes and objectives, and the architecture stops being exotic:

    1. Token embeddings — a vocabulary of size $|V|$ is a matrix $E \in \mathbb{R}^{|V| \times d}$; looking up token $i$ takes row $i$. A sequence of $n$ tokens is a matrix $X \in \mathbb{R}^{n \times d}$, one vector per token. Module 02: rows are data points.
    2. Positional encoding — embeddings alone are order-blind (a set, not a sequence), so each position $m$ gets a fixed vector of sines and cosines at geometrically spaced frequencies: bounded, unique per position, and arithmetic on positions shows up as linear operations on the vectors. Added to $X$, no training required.
    3. Self-attention — $A = \mathrm{softmax}(QK^\top / \sqrt{d_k}) V$, as in Section 3: every token gathers a weighted summary of every token.
    4. Residual stream — each block reads from and writes an additive update to one shared vector per token. Additive writes are why very deep stacks train at all: the gradient flows down the stream as a sum of paths (Section 3, again).
    5. Next-token objective — the model outputs $q(w_{t+1} \mid w_{\lt t})$, a distribution over the vocabulary; the data supplies a one-hot target $p$. The loss is cross-entropy $H(p, q) = -\log q(\text{actual next token})$ — Module 06's payoff, at $|V|$ classes, millions of times per step.

    The other quantity people quote for a language model is perplexity — just exponentiated mean cross-entropy, a change of units from nats back into "effective number of equally plausible next tokens":

    $$\mathrm{PPL} = \exp\Big(-\tfrac{1}{N}\sum_{t}\log q(w_t \mid w_{\lt t})\Big)$$ perplexity 20 means the model is as unsure as if it were picking uniformly between 20 tokens — Module 06's cross-entropy, wearing a friendlier unit

    Two knobs on the output distribution, both pure Module 06. Temperature divides the logits before the softmax, which is equivalent to raising the base distribution to $1/T$ and renormalising:

    $$p_T(v) = \frac{p(v)^{1/T}}{\sum_{u \in V} p(u)^{1/T}}$$ $T \to 0$ makes the model take its most likely token every time; $T \gt 1$ flattens the distribution — the entropy knob, turned by hand

    Block 3 measures it: on a ten-token distribution with overconfident logits, entropy goes from $0.046$ nats at $T = 0.5$ to $0.319$ at $T = 1$ to $0.909$ at $T = 2$, while the top probability falls from $0.992$ to $0.701$. Top-p (nucleus) sampling is the other knob: keep the smallest set of tokens whose cumulative probability reaches the threshold — 0.9 is a common choice — renormalise, and sample only from there. That is a support truncation — a hard edit of the distribution, not a smoothing — and it is where entropy meets the engineering constraint of never emitting the long tail of garbage.

    Context length is a memory-and-compute trade you can now price exactly: self-attention computes a dot product for every query–key pair, so attention work grows as $n^2$ in sequence length, and the cached key/value matrices grow linearly in $n \times$ layers $\times d$. Doubling the context quadruples the attention compute. Every architectural variant you will read about — sparse attention, sliding windows, state-space models — is an answer to that one line of arithmetic.

    Two softmaxes, one language model

    Attention applies a softmax over the keys — each output row is a distribution over positions in the context, and the rows are independent of each other. The output layer applies a different softmax over the vocabulary, giving the next-token distribution. Confusing "the model attends 30% to token 5" with "the model predicts token 5 with probability 30%" is a category error: those are two different normalisations at two different places. When you debug an attention pattern, ask which softmax you are actually looking at.

    5 · Generative models, in your math

    Three model families, three honest sketches. Each is one paragraph because each is one paragraph of mathematics from this course, applied.

    VAEs. An encoder $q_\phi(z \mid x)$ performs amortised inference: instead of fitting a separate posterior per input, one network maps any input to the parameters of a posterior over its latent code. A decoder $p_\theta(x \mid z)$ reconstructs. The loss is exactly the negative ELBO from Section 3: reconstruction minus $\mathrm{KL}(q_\phi(z \mid x) \| p(z))$. The KL term is what makes generation possible at all — it is what allows a brand-new $z \sim p(z)$ to decode to a plausible $x$. There is even a coding view of the same trade — the bits-back argument, optional reading — where the KL term reads as the bitrate the latent code costs, which is a neat reminder that information theory and likelihood are one subject.

    Diffusion models. A forward process corrupts data by adding small Gaussian noise over many steps until it is essentially pure noise; this is a Markov chain whose per-step distribution is known in closed form, so training pairs $(x_t, \text{noise})$ are free. The model learns to predict the noise at any step — equivalently, the score, $\nabla_x \log p_t(x)$, the gradient of the log-density with respect to the data (Module 03: a gradient is a direction of steepest change). Sampling is iterated denoising: start from noise, and repeatedly step in the direction that increases log-density — toward the data manifold. The reason these models are stable to train is that each step is a small regression problem, not a game.

    GANs. A generator and a discriminator in a minimax game:

    $$\min_G \max_D\; \mathbb{E}_{x \sim p_{\text{data}}}\big[\log D(x)\big] + \mathbb{E}_{z \sim p(z)}\big[\log(1 - D(G(z)))\big]$$ the discriminator is trained to tell real from generated; the generator is trained to make the discriminator fail — one loss, two optimisers, no closed form

    There is no likelihood to write down and no single objective descending — training instability and mode collapse are problems of game dynamics (Module 07's optimizers assume one player's loss; here two optimisers chase each other), which is why GAN training needed a decade of tricks and why diffusion largely displaced it.

    Where this course ends, honestly

    Three things you will meet beyond this notebook. Measure theory-lite: what a density over images even means, and the formal machinery that guarantees expectations exist. Stochastic processes: the diffusion forward pass is a Markov chain, and rigorously justifying its limits needs that language. Functional analysis and convex theory: the crowd that wants proofs about infinite-dimensional optimisation lives here. Do not oversell the need: you can build, use and debug VAEs, diffusion models and GANs with exactly the mathematics in Modules 01–08. The rigour buys you the right to invent and to read theory papers, not the right to run them.

    6 · Agentic models, in your math

    Your stated destination. An agent is a loop — perceive, think (an LLM call), act (a tool), observe — and the mathematics of that loop is decision-making under uncertainty. Nothing more, nothing less.

    Perceiveobservation $o_t$ → Believeupdate $b_t$ (Bayes) → Decide$\arg\max_a \mathbb{E}[R]$ → Acttool call → Observeresult, retry

    The belief state. The world has state you cannot see; you hold a distribution $b_t(s)$ over what it might be, and each observation revises it — Bayes' rule from Module 04, applied repeatedly, which is called filtering:

    $$b_{t+1}(s') \;\propto\; P(o_{t+1} \mid s') \sum_{s} P(s' \mid s, a_t)\, b_t(s)$$ predict forward through how actions change the world, then reweight by how likely the new observation is under each candidate state

    A partially observable Markov decision process (POMDP) is the formal frame for exactly this — hidden state, actions, observations, a belief over states — and the standard trick is that maintaining a correct belief turns the partially observable problem into an ordinary one over beliefs. An agent keeping a to-do list, a hypothesis about a bug, or a running summary of a document is maintaining $b_t$; the LLM's context window is its (lossy, uncompressed) belief state.

    Planning. A policy $\pi$ is a distribution over actions, and the value of a state is the expected reward under it:

    $$\mathbb{E}[R] = \sum_a \pi(a)\, Q(s,a)$$ the Q-function scores each action; the policy decides how much probability to put on each — greedy is a point mass on the argmax

    Exploration vs exploitation — the smallest complete example is the multi-armed bandit, and its best-known rule closes this notebook's loop. UCB picks the arm maximising

    $$\mathrm{UCB}_a = \hat\mu_a + c\,\sqrt{\frac{\ln t}{n_a}}$$ term one: the exploitation estimate, your best guess of arm a's reward; term two: the uncertainty bonus, which shrinks as you pull that arm more often

    Unpack the second term and look at what it is: $\hat\mu_a$'s uncertainty scales as $1/\sqrt{n_a}$ — the standard error from Module 05, appearing as a decision rule. The bonus pays for information: an arm you have never pulled has $n_a = 0$ and an infinite bonus, so it is tried; an arm you have sampled 500 times has almost no bonus left, so it must win on merit. Statistics does not merely describe your measurements here — it tells the agent which experiment to run next. Block 2 shows UCB beating $\varepsilon$-greedy on a ten-armed bandit with cumulative regret $75$ against $363$ over 2 000 steps.

    Tool use and structured output are constrained decoding: at each step the token distribution is masked down to tokens that keep the output syntactically valid (JSON keys, closing brackets), then renormalised — another softmax over a restricted support. Retries and verification loops are Monte Carlo search over strategies: sample $k$ candidate plans or answers, run a checker, keep what survives. Self-consistency is the cleanest case — sample $k$ independent chains, take the majority answer. That is a committee of estimators, and voting reduces the variance of the final decision (Module 05, ensemble logic) at $k$-times the cost.

    Memory and retrieval are linear algebra wearing a product name. A memory item is an embedding vector $e \in \mathbb{R}^d$; retrieval is a dot product between the query embedding and every stored one, ranked — nearest neighbours in $\mathbb{R}^d$ (Module 02, again). The similarity metric is a modelling choice with consequences: raw dot product also rewards long vectors, so systems normalise and use cosine when magnitude should not mean popularity. RAG is then just conditional generation, $p(\text{answer} \mid \text{question}, \text{retrieved context})$ — the retrieval step decides which conditioning information the model ever sees.

    Uncertainty and calibration are the last piece, and the one practitioners skip. An agent that knows when it does not know is exactly a calibrated model: its stated confidence matches observed frequency, which you measure with log-loss (Module 06 — a strictly proper scoring rule, it rewards honesty) and repair with temperature scaling (Section 4). A poorly calibrated model makes a bad agent for a specific, mechanical reason: overconfidence turns a coin-flip into a committed tool call, and the agent has no reason to abstain, ask, or check. Calibration is what converts a predictor into a colleague.

    Nothing in an agent is new math — it is Bayes, expectations, dot products and gradients, arranged in a loop.
    Debugging an agent, mathematically

    When an agent misbehaves, resist the prompt-engineering reflex and name the failing component in mathematical terms: is it a belief update (the posterior drifted — context pollution, misread observation), a value estimate (the Q of the chosen action was wrong — a bad plan scored well), a retrieval miss (the dot products ranked the wrong memories — a similarity or embedding problem), or a calibration failure (confident about a guess — a threshold or abstention policy is missing)? Each diagnosis names a different fix, and every one of them is a concept from Modules 02, 04, 05 or 06.

    7 · The 8-week plan

    Paced for a working engineer: 25–45 focused minutes on most weekdays, not five-hour Sundays. The companion ML notebook carries the practical half of each week. If a week slips, shrink the scope — never the reviews, because the reviews are the mechanism, not the reward.

    WeekThis notebookML notebookDeliverable (proof of work)
    W100 + 0100–03Diagnostic answered cold; original answers dated; notes system set up; environment healthy
    W20205PCA and an embedding similarity search, from scratch in NumPy
    W303 + 0705–06Gradient check passing; logistic regression trained by your own gradient descent
    W40408Base-rate problems without a calculator; Naive Bayes in three lines; MLE derived twice
    W50504, 12A/B test simulated and calibrated; one baseline evaluated and honestly defended
    W606 + 0807, 09Cross-entropy derived from surprise; the eight-algorithm backbone map written from memory
    W7Module 08 finished + one project19One end-to-end model, evaluated without fooling yourself
    W8This module + capstone20Checklist passed aloud; diagnostic diff written; retention system running

    The daily rhythm inside every week, in order: (1) 5 minutes of cold recall — the previous module's questions, written, before opening anything; (2) 25–45 minutes on today's section, with its code typed rather than copied; (3) the 1/3/7-day rule — re-answer a module's questions one day, then three, then seven after you finish it. The weekly test is the explain-aloud: pick one concept, set a five-minute timer, explain it out loud with no notes, ideally to a person or a recording. If you cannot, that concept goes back into this week's rotation — not into next month's reading list.

    The realistic failure mode

    Week 3 is where self-directed plans die: work gets busy, the review queue piles up, and the response is to skip reviews and "catch up on Sunday" — which trades the mechanism for the illusion of progress. The countermeasure is quantitative: reviews are capped at ten minutes a day no matter how far behind you are, and a missed section is dropped from scope rather than postponed. A plan you actually finish, at 70% scope, beats one you abandon at 95%.

    8 · How to keep it: retention as a system

    Everything so far decays without maintenance, and the maintenance is not re-reading. It is four mechanisms, all cheap, all boring, all evidence-backed.

    1. Spaced repetition. Build one card per checklist item, in the form: front = a question that names a context, back = the smallest complete answer. Examples: front "gradient of BCE with respect to the logit", back "$\sigma(z) - y$"; front "why $1/\sqrt{d_k}$ in attention", back "keeps logit variance at 1 so the softmax does not saturate"; front "what does a 95% CI claim", back "the procedure, not the parameter". Five to ten minutes a day, forever.
    2. Monthly re-derivation drills. Once a month, pick one algorithm — logistic regression, PCA, k-means, the normal equation, backprop through a two-layer net — and derive it from definitions on paper, no notes, 30 minutes max. Derivation is the skill that survives; the ability to re-derive means the structure is stored, not the symbol string.
    3. Project-based retention. Knowledge you use stays. The ladder: linear regression → logistic regression → a two-layer MLP in NumPy → one transformer attention block, each with shape assertions. By the end you will have written every operation in Section 3 yourself, once, slowly — which is exactly what makes the fast frameworks legible later.
    4. The teaching test. Every quarter, explain the course to a colleague, or write it up. Teaching exposes the difference between "I follow this" and "I can rebuild this", and it costs nothing but willingness.
    Why re-reading fails, and what replaces it

    Re-reading produces fluency: the text becomes familiar, familiarity feels like knowledge, and the trace that decays is never exercised — so it decays anyway. Retrieval practice does the opposite: the effort of pulling an answer out of your own head is the act that strengthens the memory, and a wrong answer located on time is more valuable than a right one recognised late. Space the retrieval (1/3/7 days), interleave topics rather than blocking them, and generate answers rather than recognise them. That is the whole method, and it is why every module in this notebook began with discomfort.

    9 · Resources, chosen not listed

    Nine items, each with a job. Notice what is not here: a syllabus to replace this notebook. These are supplements with specific purposes.

    ResourceUse this for
    3Blue1Brown — Essence of Linear Algebra / of CalculusThe geometric picture, watched after a failed retrieval attempt — repair, never first exposure
    Gilbert Strang — Introduction to Linear Algebra (MIT 18.06)Linear algebra done properly with the matrix intuition intact; eigenvectors and SVD when Module 02 is not enough
    Khan AcademyTargeted prerequisite repair only — the one topic you failed the checklist item on, patched, not a course re-run
    Bishop — Pattern Recognition and Machine LearningProbabilistic depth: MLE/MAP, mixtures, EM, graphical models, and the generative-models chapter
    Murphy — Probabilistic Machine Learning (vol. 1, then Advanced Topics)The modern reference you look things up in — not a book to read cover to cover
    Hastie, Tibshirani, Friedman — Elements of Statistical Learning (free PDF)Statistical learning: generalization, regularization, trees, boosting, ensembles — the theory-lite companion to Modules 05 and 08
    Goodfellow, Bengio, Courville — Deep LearningThe DL textbook: the mathematics of representations and optimization; reference style, dense on purpose
    Vaswani et al. 2017 — Attention Is All You NeedRead once you can parse the shapes unaided — 15 pages, and Section 4 is your reading key
    distill.pubVisual explanations — attention, embeddings, memorisation — when a diagram beats a paragraph
    Andrej Karpathy — Neural Networks: Zero to HeroBuilding a GPT from scratch, token by token — the implementation companion to Sections 3–5
    The resource-hopping trap

    Collecting resources feels like progress and is the most common way to stop making it. Every switch costs the encoding you had just built, and a second explanation of an idea you cannot yet produce from memory is usually avoidance dressed as study. The rule: one intuition source (3Blue1Brown), one rigorous text (Murphy or ESL), one implementation track (Karpathy and the companion ML notebook). Finish those three before acquiring a fourth — which, realistically, is never.

    10 · Closing: what "done" means, and what deliberately remains undone

    "Done" with this notebook does not mean knowing more mathematics. It means a smaller, testable list: you can cold-derive the checklist items; when a model misbehaves your first suspects are a shape, a gradient, or a probability assumption; you can read a formula in a paper and say what it is for before you understand its details; and you know which of your gaps matter for your goal and which are academic.

    What was deliberately left out, so that the boundary is explicit rather than discovered later: measure-theoretic probability (the foundations under everything in Modules 04–06; needed only if you read or write theory); rigorous convex analysis (Module 07 states the results you need and why; the proofs are a separate, academic pursuit); representation-learning theory (why over-parameterised networks generalise is an open research question — no course can honestly teach it); and hardware and systems knowledge — mixed precision, kernels, distributed training. That last one is the exception to "academic": it is practically essential for training anything large, but it is engineering, not mathematics, and it belongs in the companion ML notebook and in your day job, not here.

    For your stated destination — agentic models — be equally blunt about what is needed: everything in Modules 01–08, plus evaluation discipline (the companion notebook's modules 04, 12 and 19), plus systems literacy, plus a working knowledge of how language models behave. Not measure theory. Not functional analysis. The mathematics that decides whether your agents work is the mathematics in this notebook, applied carefully and maintained.

    Which leaves one action, and it is today's, not next Monday's: pick one algorithm and rebuild it from the math, on paper or in NumPy, in the next 45 minutes. Not a tutorial, not a video — the normal equation, or logistic regression's gradient, or one attention head. That single act of production is worth more than everything you have read this week, including this page.

    Pass 3

    Implement — three demonstrations and one ladder

    Every claim in Sections 3, 4 and 6 with a number attached

    Block 1 · The attention scaling, verified

    Random $Q$, $K$ matrices; compute the softmax of $QK^\top$ with and without the $1/\sqrt{d_k}$ scaling and measure the entropy of each attention row. Saturation means low entropy; the scaling should hold entropy near its maximum $\ln n$ at any width.

    import numpy as np
    rng = np.random.default_rng(0)
    
    def softmax(z, axis=-1):
        z = z - z.max(axis=axis, keepdims=True)   # stability shift; cancels in the ratio
        e = np.exp(z)
        return e / e.sum(axis=axis, keepdims=True)
    
    def mean_row_entropy(dk, scale, n=500):
        """Entropy of softmax(QK^T / scale) rows, averaged over random Q, K (nats)."""
        ent = []
        for _ in range(n):
            Q = rng.standard_normal((4, dk))      # 4 queries, dk dims each
            K = rng.standard_normal((4, dk))      # 4 keys
            p = softmax(Q @ K.T / scale)          # (4, 4) rows sum to 1
            ent.append(-(p * np.log(p + 1e-12)).sum(axis=1).mean())
        return np.mean(ent)
    
    print("max possible entropy (4 keys) =", round(np.log(4), 3))     # 1.386
    for dk in (64, 256):
        print(f"dk={dk}: unscaled {mean_row_entropy(dk, 1.0):.3f}"
              f"   scaled {mean_row_entropy(dk, np.sqrt(dk)):.3f}")
    # dk= 64: unscaled 0.224   scaled 1.113   -- scaling keeps the softmax usable
    # dk=256: unscaled 0.108   scaled 1.119   -- unscaled gets WORSE as d_k grows
    
    # Shapes of one attention head, end to end (n tokens, dk keys, dv values)
    Q = rng.standard_normal((5, 64)); K = rng.standard_normal((7, 64)); V = rng.standard_normal((7, 32))
    A = softmax(Q @ K.T / np.sqrt(64))
    print(Q.shape, K.shape, V.shape, A.shape, (A @ V).shape)  # (5,64) (7,64) (7,32) (5,7) (5,32)
    print("rows of A sum to 1:", np.allclose(A.sum(axis=1), 1.0))     # True

    Block 2 · The standard error as a decision rule

    A ten-armed bandit. Compare UCB (exploitation estimate + uncertainty bonus, $c = 1$) against $\varepsilon$-greedy, measuring cumulative regret — how much reward you forgo versus always pulling the best arm. Same arms, same noise, 200 independent runs.

    import numpy as np
    rng = np.random.default_rng(1)
    
    true_means = rng.normal(0, 1, 10)             # unknown to the agent
    
    def run(policy, T=2000, runs=200):
        regrets = []
        for _ in range(runs):
            q = np.zeros(10); n = np.zeros(10); total = 0.0
            for t in range(1, T + 1):
                if policy == "ucb":
                    # bonus = c * sqrt(ln t / n_a): shrinks like a standard error; untried arms get inf
                    bonus = np.where(n > 0, np.sqrt(np.log(t) / np.maximum(n, 1)), np.inf)
                    a = int(np.argmax(q + bonus))
                else:
                    a = rng.integers(10) if rng.random() < 0.1 else int(np.argmax(q))
                r = rng.normal(true_means[a], 1.0)   # noisy reward
                n[a] += 1; q[a] += (r - q[a]) / n[a]  # running mean = the exploitation term
                total += true_means.max() - r         # opportunity cost of this pull
            regrets.append(total)
        return np.mean(regrets)
    
    print("best arm:", true_means.argmax(), "mean:", round(true_means.max(), 2))
    print("UCB regret        :", round(run("ucb"), 1))       # 75.2
    print("eps-greedy (0.1)  :", round(run("eps"), 1))       # 363.3

    Block 3 · Temperature as an entropy knob

    Take an overconfident ten-way logit vector and watch entropy, top probability and the probability assigned to a specific less-likely token move as $T$ moves. This is the exact quantity a sampling parameter changes — nothing else in the model.

    import numpy as np
    rng = np.random.default_rng(2)
    
    logits = rng.normal(0, 4, 10)                  # overconfident: logits 4 std devs apart
    true_tok = int(np.argsort(logits)[-3])         # pretend the "right" token ranks 3rd
    
    def probs(T):
        z = logits / T; z -= z.max()
        e = np.exp(z); return e / e.sum()          # = p(v)^(1/T) renormalised
    
    for T in (0.5, 1.0, 2.0):
        p = probs(T)
        H = -(p * np.log(p)).sum()                 # entropy in nats; max = ln 10 = 2.303
        print(f"T={T}: entropy {H:.3f}, max prob {p.max():.3f}, "
              f"p(true token) {p[true_tok]:.4f}, its NLL {-np.log(p[true_tok]):.3f}")
    # T=0.5: entropy 0.046, max prob 0.992, NLL 10.814  -- nearly argmax decoding
    # T=2.0: entropy 0.909, max prob 0.701, NLL  3.057  -- distribution actually usable

    Block 4 · The retention deck as data

    Your recall deck is a data structure, and the 1/3/7-day rule is a scheduler over it. This prints the review dates for a module finished today, and the day's queue — the whole maintenance system, ten lines.

    import numpy as np
    
    # One card per checklist item: (front, back). Keep backs to one line.
    deck = [
        ("gradient of BCE w.r.t. the logit", "sigmoid(z) - y"),
        ("why 1/sqrt(d_k) in attention", "keeps logit variance at 1, softmax out of saturation"),
        ("what a p-value is", "P(data this extreme | H0) — never P(H0 | data)"),
        ("UCB's second term", "uncertainty bonus, shrinks like 1/sqrt(n_a)"),
    ]
    today = np.datetime64("2026-10-14")            # the day you finished a module
    for offset in (1, 3, 7):
        print(f"day +{offset}: {today + offset}")   # 2026-10-15, 2026-10-17, 2026-10-21
    print(f"{len(deck)} cards due today; budget 5-10 minutes, closed-book")
    Exercise · the first rung of the rebuild ladder
    • Verify one gradient you will use forever. The claim: binary cross-entropy with logits, differentiated with respect to the logit $z$, gives $\sigma(z) - y$. Prove it with the same finite-difference check you built in Module 03 — then you own the single most-run derivative in machine learning.
    • Then climb the ladder (Section 8, mechanism 3): linear regression by the normal equation → logistic regression by your own gradient descent → a two-layer MLP in NumPy → one attention head, shape assertions at every step. One rung per week is a realistic pace.
    • Oral gate: before ticking Mark done on this module, explain the UCB bonus term out loud to someone, in one minute, without notes.
    import numpy as np
    rng = np.random.default_rng(3)
    
    def bce_with_logits(z, y):
        # numerically stable BCE on logit z, label y in {0,1} -- no sigmoid-then-log blowups
        return np.maximum(z, 0) - z * y + np.log1p(np.exp(-np.abs(z)))
    
    def sigmoid(z): return 1 / (1 + np.exp(-z))
    
    z = rng.normal(0, 1, 5); y = rng.integers(0, 2, 5).astype(float)
    claim = sigmoid(z) - y                       # the gradient we claim dBCE/dz equals
    
    eps = 1e-6; I = np.eye(5)                    # central differences, one coordinate at a time
    check = np.array([(bce_with_logits(z + eps * I[i], y).sum() -
                       bce_with_logits(z - eps * I[i], y).sum()) / (2 * eps) for i in range(5)])
    print(np.round(claim, 6)); print(np.round(check, 6))
    print("max abs diff:", np.abs(claim - check).max())   # ~1e-10: the claim stands
    Checkpoint

    Prove it — then go build something

    Retention of the whole course, not new facts

    Multiple choice — instant feedback

    1. In $A = \mathrm{softmax}(QK^\top / \sqrt{d_k})\, V$, what object does $QK^\top$ produce?

    2. Raising the sampling temperature $T$ above 1 does what to a language model's next-token distribution?

    3. The UCB bonus term $c\sqrt{\ln t / n_a}$ represents…

    4. What does backpropagation compute?

    5. ELBO — what does it stand for, and what does it trade off?

    Open questions — answer aloud, no notes

    1. Explain to a colleague why the math you learned does not disappear when frameworks do the derivatives for you. Three sentences: what the framework computes, what it cannot decide for you, and a failure mode where the difference matters.
    2. Write the UCB rule from memory, and unpack each of its two terms in one sentence — including where the second term's shape comes from statistically.
    3. Take one transformer block and name, for each operation inside it, the module of this course it comes from.
    4. An agent calls a tool, gets a silent failure, and retries the same call three times, confidently. Which of the four suspects — belief update, value estimate, retrieval miss, calibration — fits, and what is the fix?
    5. Diff your diagnostic: which three of the twelve items improved least, and what is your plan for exactly those three?
    6. You have 30 focused minutes a day for the next eight weeks. Write the schedule — including reviews — and say what you will drop when a week goes wrong.

    Re-answer all six in 1 day, then 3 days, then 7 days — from memory, out loud. If question 1 or 2 comes back fuzzy, that is not a surprise; it is the item going onto the deck described in Section 8.

    Continue in your ML notebook

    The practical half of everything above lives in the companion notebook. ML module 10 · neural networks & deep learning is where Sections 3–4 become code: layers, backprop, training loops and a first transformer block. ML module 19 · capstone ladder & portfolio is where Section 7's week-7 project and Section 8's rebuild ladder are staged as a graded set of deliverables. And ML module 20 · 4-week plan, recall deck & final checklist is the maintenance system: the deck format, the schedule, and the final checklist that pairs with Section 2 above. This notebook gave you the reasons; those modules give you the reps.