Mathematical Foundations of Generative AI

Quiz 2, decoded: the likelihood half of GenAI.

Where Quiz 1 was adversarial, Quiz 2 is variational. Across three past papers (2025 T2 · 2025 T3 · 2026 T1) every single question descends from one inequality — the ELBO — and its two great applications: the VAE (one latent) and the DDPM (a thousand latents). This guide walks that whole road and drills the exact question templates that keep coming back.

40–50Marks · GenAI section
~16Questions / paper
0Negative marking
6Must-know formulas
How to use this. Read top-to-bottom once for the narrative — each idea is the previous one pushed one step further. Then treat the ★ High-yield callouts and the example questions (click an option, or "Reveal answer") as your drill deck. Everything marked 2025 T2 / 2025 T3 / 2026 T1 is a real question from a past paper, with its official key. The cheat sheet and recall list are your final-hour pages.
00Strategy first

Exam intel — where the marks actually are

Three papers, one tightly-scoped bank. Several questions repeat verbatim across terms. Prioritise by frequency.

Topic frequency across the 3 papers

All 140 scored marks from 2025 T2 + 2025 T3 + 2026 T1, binned by theme. Longer bar = more marks = more of your revision time.

The topic bars sum to 137; the missing 3 marks are 2025 T2's Q136, whose text is truncated out of the released PDF. The PyTorch bar overlaps the others — those marks are counted twice, once under their topic and once here, because an implementation question is always about a VAE or a DDPM.

★ Six formulas cover the majority of the marks

If you memorise nothing else, memorise these cold:

  1. 1.GMM responsibility & M-step: \(\gamma_{nk}=\frac{\pi_k\mathcal N_k}{\sum_j \pi_j \mathcal N_j}\), \(\mu_k'=\frac{\sum_n \gamma_{nk}x_n}{\sum_n \gamma_{nk}}\)
  2. 2.The two-term ELBO: \(J=\mathbb E_q \log p_\theta(x|z) - D_{KL}(q\|p(z))\)
  3. 3.Diagonal-Gaussian KL: \(\tfrac12\sum_j(\sigma_j^2+\mu_j^2-1-\log\sigma_j^2)\)
  4. 4.Reparameterization: \(z=\mu+\sigma\odot\epsilon\), with \(\sigma=e^{\frac12\log\sigma^2}\)
  5. 5.DDPM forward marginal: \(x_t=\sqrt{\bar\alpha_t}\,x_0+\sqrt{1-\bar\alpha_t}\,\epsilon\), \(\bar\alpha_t=\prod_{s\le t}\alpha_s\)
  6. 6.DDPM posterior \(q(x_{t-1}|x_t,x_0)=\mathcal N(\mu_q,\sigma_q^2 I)\) and its KL \(=\frac{1}{2\sigma_q^2}\|\mu_\theta-\mu_q\|^2\)
⚠ Sign conventions — the single most common way to lose easy marks

The papers use both conventions, sometimes in the same paper, and they differ by a sign. Read which one the question states, and follow it.

  • Maximise (ELBO form): \(J_\theta(q_\phi)=\mathbb E_{q}\log p_\theta(x|z)-\beta D_{KL}\). Here "reconstruction mismatch = 5.2" means \(\mathbb E\log p = -5.2\), so \(J=-5.2-\beta(0.58)\). Answers come out negative.
  • Minimise (loss form): \(\mathcal L = \mathcal L_{\text{recon}} + \beta\,\mathcal L_{KL}\). Same numbers, answers come out positive.
  • Tell them apart by the verb: "the objective being minimized" → loss form; "compute the ELBO / \(J_\theta(q_\phi)\)" → maximise form.
01Mental model

The big picture: one inequality, told three ways

GMM, VAE and DDPM are not three topics. They are the same optimisation, with the latent variable made progressively harder to handle. Hold this and everything slots in.

Same starting point as Quiz 1: data \(D=\{x_1,\dots,x_n\}\) drawn i.i.d. from an unknown \(p_x\), and we want a model \(p_\theta\) we can sample from. Quiz 1 attacked this adversarially — pick a divergence, estimate it with a critic. Quiz 2 attacks it by maximum likelihood, and the whole difficulty is that the likelihood has a latent variable buried inside an integral.

The latent-variable model \(\displaystyle p_\theta(x)=\int_z p_\theta(x,z)\,dz\)   (or \(\sum_z\) if \(z\) is discrete) Introduce an unobserved \(z\) per data point. Discrete \(z\) → clustering (GMM, K-means). Continuous \(z\in\mathbb R^k\), \(k\ll d\) → autoencoder / feature vector.

That integral is what makes \(\log p_\theta(x)\) intractable. The entire course response is: don't maximise the log-likelihood, maximise a lower bound on it — the ELBO. Then the three models differ only in what the latent is:

ModelLatentIs \(q(z|x)\) known?How the ELBO is optimised
GMMOne discrete \(z\in\{1..M\}\)Yes — the exact posterior is computableEM: set \(q=p_\theta(z|x)\) exactly (E-step), then solve for \(\theta\) in closed form (M-step)
VAEOne continuous \(z\in\mathbb R^k\)No — so amortise it with an encoder net \(q_\phi(z|x)\)SGD on \(\theta,\phi\) jointly, made differentiable by the reparameterization trick
DDPM\(T\) continuous latents \(x_1..x_T\), each the same size as \(x_0\)Yes and fixed\(q\) is a hand-chosen noising chain with no parametersSGD on \(\theta\) only; the ELBO collapses into a plain L2 regression
GMM1 discrete latent
VAE1 continuous latent
HVAEmany latents
DDPMT latents, fixed encoder
The one-sentence version

A DDPM is a hierarchical VAE in which (i) there are many latent spaces, (ii) every latent has the same dimension as the data, and (iii) the encoder is fixed and non-learnable — only the decoder is trained. Those three bullets are the answer to a recurring exam question; memorise them as a triple.

02Foundation

Latent variables → MLE → ELBO

Four moves take you from "minimise a divergence" to the single objective that the rest of the quiz optimises. Every step here has been asked about directly.

Move 1 — minimising forward KL is maximum likelihood

Derivation in three lines \(\displaystyle \theta^*=\arg\min_\theta D_{KL}(p_x\|p_\theta)=\arg\min_\theta \int p_x\log\frac{p_x}{p_\theta}\)
\(\displaystyle =\arg\min_\theta\Big[\underbrace{\int p_x\log p_x}_{\text{no }\theta}-\int p_x\log p_\theta\Big]\)
\(\displaystyle \Rightarrow\;\; \theta^*=\arg\max_\theta\;\mathbb E_{p_x}\big[\log p_\theta(x)\big]\) The entropy term \(\int p_x \log p_x\) does not contain \(\theta\), so it drops. What is left is exactly MLE. \(\log p_\theta(x)\) is called the evidence.

Move 2 — insert a variational density and use Jensen

Write \(\ell(\theta)=\log p_\theta(x)=\log\int_z p_\theta(x,z)\,dz\). Multiply and divide by any density \(q(z|x)\) over the latent:

\(\displaystyle \ell(\theta)=\log\int_z q(z|x)\frac{p_\theta(x,z)}{q(z|x)}dz=\log\;\mathbb E_{q(z|x)}\!\left[\frac{p_\theta(x,z)}{q(z|x)}\right]\)
\(\displaystyle \ge\;\mathbb E_{q(z|x)}\log\frac{p_\theta(x,z)}{q(z|x)}\;\equiv\;J_\theta(q)\) Jensen's inequality \(\log\mathbb E[\cdot]\ge\mathbb E\log[\cdot]\) (log is concave) is the step that turns an intractable log-of-integral into a tractable expectation-of-log.
★ The universal objective
\(\displaystyle \theta^*,q^*=\arg\max_{\theta,q}\;\mathbb E_{q(z|x)}\log\frac{p_\theta(x,z)}{q(z|x)}\)

\(J_\theta(q)\) is the Evidence Lower Bound (ELBO). It is a function of both \(\theta\) and the density \(q(z|x)\), called the variational latent posterior. Every model in this quiz maximises this one expression.

Move 3 — the KL gap: how much is the bound losing?

The exact decomposition — memorise this identity \(\displaystyle \underbrace{\log p_\theta(x)}_{\text{evidence}}\;=\;\underbrace{J_\theta(q)}_{\text{ELBO}}\;+\;\underbrace{D_{KL}\big(q(z|x)\,\|\,p_\theta(z|x)\big)}_{\text{the gap}\;\ge\;0}\) The bound is tight exactly when \(q\) equals the true posterior. Because the left side does not depend on \(q\): smaller KL gap ⇔ tighter (larger) ELBO, and the difference between two ELBOs is exactly minus the difference between their KL gaps.
MSQPractice paper · Q10Multi-select
For fixed \(\theta\), two variational distributions satisfy \(D_{KL}(q_1\|p_\theta(z|x)) < D_{KL}(q_2\|p_\theta(z|x))\). Which statements necessarily follow?
  • \(q_2\) produces a tighter ELBO than \(q_1\)
  • \(q_1\) produces a tighter ELBO than \(q_2\)
  • The ELBO obtained using \(q_1\) must be negative
  • The difference between the two ELBOs is exactly the difference between the corresponding KL gaps
Reveal answer
Options 2 and 4

Straight from the identity. \(\log p_\theta(x)\) is a constant once \(\theta\) is fixed, so \(J = \log p_\theta(x) - \text{gap}\): a smaller gap gives a strictly larger ELBO, and subtracting the two equations kills the constant, leaving \(J_1-J_2=\text{gap}_2-\text{gap}_1\). Option 3 is a trap — nothing forces the ELBO to be negative; with a continuous density \(\log p_\theta(x)\) can be positive.

Move 4 — split the ELBO into reconstruction and regularisation

Factor the joint as \(p_\theta(x,z)=p_\theta(x|z)p_\theta(z)\) inside the log and separate:

The form you will actually compute with \(\displaystyle J_\theta(q)=\underbrace{\mathbb E_{q(z|x)}\log p_\theta(x|z)}_{\text{reconstruction}}\;-\;\underbrace{D_{KL}\big(q(z|x)\,\|\,p_\theta(z)\big)}_{\text{regularisation on }z}\)
NAT2025 T2 · Q123  &  2026 T1 · Q53 / 2 marks
The ELBO is \(J_\theta(q)=\mathbb E_{q(z|x)}[\log p(x,z)-\log q(z|x)]\). Given \(q(z{=}1|x)=0.8\), \(q(z{=}0|x)=0.2\), \(\log p(x,z{=}1)=-3.5\) and \(\log p(x,z{=}0)=-5.0\), what is the ELBO? (Use natural logs, 2 d.p.)
Reveal answer
−3.30

A discrete latent means the expectation is just a weighted sum over the two states — no sampling needed.

\(J = 0.8\,[-3.5-\ln 0.8] + 0.2\,[-5.0-\ln 0.2]\)
\(= 0.8\,[-3.5+0.2231] + 0.2\,[-5.0+1.6094] = -2.6215 - 0.6781 = -3.2996\)

This exact question, with these exact numbers, appeared in two separate papers. Bank it. The only trap is forgetting the \(-\log q\) term (which gives −3.80) or using \(\log_{10}\).

03★★★ 19 marks

GMM & the EM algorithm

The classical case where the ELBO can be maximised exactly. Every paper has asked for a responsibility or an M-step mean — these are the cheapest marks on the exam.

The model

Gaussian Mixture Model \(\displaystyle p_\theta(x)=\sum_{j=1}^{M}\underbrace{p_\theta(z{=}j)}_{\alpha_j}\;\underbrace{p_\theta(x|z{=}j)}_{\mathcal N(x;\mu_j,\Sigma_j)}=\sum_{j=1}^{M}\alpha_j\,\mathcal N(x;\mu_j,\Sigma_j)\) Parameters \(\theta=\{\alpha_j,\mu_j,\Sigma_j\}_{j=1}^M\) with \(0\le\alpha_j\le1\) and \(\sum_j\alpha_j=1\). \(x,\mu_j\in\mathbb R^d\), \(\Sigma_j\in\mathbb R^{d\times d}\).

EM = alternating maximisation of the ELBO

Maximise \(J_\theta(q)\) over \(\theta\) and \(q\) one at a time:

for t = 1 to T:
    # E-step: optimise q with theta frozen
    q*t+1 = argmaxq Jθt(q)   -> provably equals the true posterior pθ(z|x)
    # M-step: optimise theta with q frozen
    θ*t+1 = argmaxθ Jθ(q*t+1)  -> differentiate and solve in closed form
★ The four EM facts that get asked
  1. 1.EM optimises a lower bound of the objective — never an upper bound.
  2. 2.The objective is monotone non-decreasing: \(\ell(\theta_{t+1})\ge\ell(\theta_t)\) at every iteration.
  3. 3.It is not guaranteed to find the global optimum — only a local one.
  4. 4.The mixture weights \(\alpha_j\) do not become the per-sample posteriors \(P(z{=}j|x)\) at convergence. \(\alpha_j\) is the average responsibility over the dataset, not a per-point quantity.

E-step: responsibilities

\(\displaystyle \gamma_{nk}\;\equiv\;p_\theta(z{=}k\,|\,x_n)\;=\;\frac{\alpha_k\,\mathcal N(x_n;\mu_k,\Sigma_k)}{\sum_{j=1}^{M}\alpha_j\,\mathcal N(x_n;\mu_j,\Sigma_j)}\) "How much does component \(k\) own point \(x_n\)." Rows sum to 1 over \(k\). This is the optimal \(q^*\) — the E-step makes the ELBO exactly tight.

M-step: closed-form updates

\(\displaystyle \pi_k'=\frac1N\sum_{n=1}^{N}\gamma_{nk}\qquad\qquad \mu_k'=\frac{\sum_{n=1}^{N}\gamma_{nk}\,x_n}{\sum_{n=1}^{N}\gamma_{nk}}\) The denominator of \(\mu_k'\) is \(N_k=\sum_n\gamma_{nk}\), NOT \(N\). Swapping those two is the standard distractor (2025 T3 Q74). It is a responsibility-weighted average, so the weights must be normalised by their own sum.
NAT2025 T2 · Q121  &  2026 T1 · Q42 / 4 marks
In the M-step, for data points \(x_1=5\) and \(x_2=15\), the responsibilities are \(\gamma_{11}=0.9,\ \gamma_{12}=0.1,\ \gamma_{21}=0.2,\ \gamma_{22}=0.8\) where \(\gamma_{ij}=P(z{=}j|x_i)\). Calculate the updated mean \(\mu_2'\) for the second component. (2 d.p.)
Reveal answer
13.89
\(\displaystyle \mu_2'=\frac{\gamma_{12}x_1+\gamma_{22}x_2}{\gamma_{12}+\gamma_{22}}=\frac{0.1(5)+0.8(15)}{0.1+0.8}=\frac{12.5}{0.9}=13.8889\)

The whole difficulty is the index convention. \(\gamma_{ij}\) is "point \(i\), component \(j\)", so for component 2 you take the second subscript = 2 entries: \(\gamma_{12}=0.1\) and \(\gamma_{22}=0.8\) — a column, not a row. Grabbing the row \((0.2, 0.8)\) gives 13.0, which is wrong. This question appeared verbatim in two papers.

NAT2025 T2 · Q1202 marks
A GMM has \(\pi_1=0.6,\pi_2=0.4\), components \(\mathcal N(5,1)\) and \(\mathcal N(10,4)\). For \(x=7\), compute the responsibility \(\gamma(z_1)\). (3 d.p.)
Reveal answer
0.556

Evaluate both densities at \(x=7\) with \(\mathcal N(x|\mu,\sigma^2)=\frac{1}{\sqrt{2\pi\sigma^2}}e^{-(x-\mu)^2/2\sigma^2}\):

\(\mathcal N(7|5,1)=\frac{1}{\sqrt{2\pi}}e^{-4/2}=0.3989\times0.1353=0.05399\)
\(\mathcal N(7|10,4)=\frac{1}{\sqrt{8\pi}}e^{-9/8}=0.1995\times0.3247=0.06476\)
\(\displaystyle \gamma_1=\frac{0.6(0.05399)}{0.6(0.05399)+0.4(0.06476)}=\frac{0.032395}{0.058299}=0.5557\)

Watch the variance vs standard deviation. The second component has \(\sigma^2=4\Rightarrow\sigma=2\), so the normalising constant is \(1/\sqrt{2\pi\cdot4}=1/\sqrt{8\pi}\), not \(1/\sqrt{8\pi^2}\) and not \(1/\sqrt{2\pi}\).

NAT2025 T3 · Q783 marks
For a 2-component GMM at some iteration: \(\pi_1=0.4,\ \pi_2=0.6,\ \mathcal N(x_n;\mu_1,\Sigma_1)=0.10,\ \mathcal N(x_n;\mu_2,\Sigma_2)=0.05\). What is the responsibility \(\gamma_{n1}\)?
And a companion from the practice paper: if two components have identical means and covariances but \(\pi_1=2\pi_2\), what is \(\gamma(z_1)\) for any \(x\)?
  • \(\tfrac12\)
  • \(\tfrac23\)
  • \(1\)
  • It depends on \(x\)
Reveal answer
2025 T3 Q78: 0.571  ·  Practice Q5: 2/3

2025 T3: the densities are handed to you, so it is pure arithmetic: \(\gamma_{n1}=\frac{0.4(0.10)}{0.4(0.10)+0.6(0.05)}=\frac{0.04}{0.07}=0.5714\).

Practice Q5: if the Gaussians are identical they are a common factor in numerator and denominator and cancel, leaving \(\gamma_1=\pi_1/(\pi_1+\pi_2)=2\pi_2/3\pi_2=2/3\) — independent of \(x\). This is the cleanest possible test of whether you understand that responsibilities are a normalised product of prior and likelihood.

NAT2026 T1 · Q31 mark
For \(f(x)=\alpha_1\mathcal N(x|\mu_1,\sigma^2)+\alpha_2\mathcal N(x|\mu_2,\sigma^2)\) with \(\alpha_1+\alpha_2=1\) and \(\sigma^2\) known: how many parameters must be estimated?
Reveal answer
3

\(\mu_1,\mu_2\) and one mixing weight. \(\alpha_2=1-\alpha_1\) is determined by the constraint so it is not free, and \(\sigma^2\) is given as known and shared. Parameter-counting questions always hinge on spotting exactly these two things: which constraint removes a degree of freedom, and which quantity is declared known.

MSQ2026 T1 · Q23 marks
Which among the following is true for the EM algorithm?
  • At any iteration, the value of the objective function never decreases
  • It optimizes an upper bound of the objective function
  • It optimizes a lower bound of the objective function
  • The EM algorithm is guaranteed to give a global solution
  • Upon convergence for a GMM, the mixture proportions \(\alpha_j\) become equal to the posterior probabilities \(P(z{=}j|x)\) for all training samples \(x\)
Reveal answer
Options 1 and 3

The last option is the subtle one. At convergence \(\alpha_j = \frac1N\sum_n\gamma_{nj}\) — the average responsibility across the dataset. Individual points still have their own posteriors \(\gamma_{nj}\), which differ from each other and from \(\alpha_j\). They would only coincide if every point had identical responsibilities.

⚠ When EM fails — and why the VAE exists

The E-step needs \(q^*=p_\theta(z|x)=\frac{p_\theta(x|z)p_\theta(z)}{p_\theta(x)}\). For a GMM the denominator is a sum over \(M\) terms, so it is computable. For a model whose decoder is a deep net, that denominator is an intractable integral — the true posterior is unknown, and EM simply cannot run. The VAE's answer: give up on the exact posterior and learn an approximation to it with a second network. That is the entire motivation for the next section.

04★★★ Core

The Variational Auto-Encoder

Two networks, one sampling step between them, and an objective you already know. The exam tests three things: what each network outputs, which direction is which, and the ELBO's two terms.

Setup and the three goals

Data \(x\in\mathbb R^d\), latent \(z\in\mathbb R^k\) with \(k\ll d\). MLE on \(p_\theta(x)=\int p_\theta(x,z)dz\) is intractable, so maximise the ELBO instead. A neural latent-variable model must deliver:

  • (a) learn the model even though \(p_\theta(z|x)\) is unknown;
  • (b) enable sampling / generation from \(p_\theta(x|z)\);
  • (c) enable posterior inference — estimate \(q(z|x)\), i.e. produce embeddings.

Two ways a network can represent a distribution

(a) Deterministic

The net outputs samples from the distribution it models. \(z\sim\mathcal N(0,I)\to g_\theta(z)\to\hat x\). This is the GAN generator (and a classifier, viewed this way).

(b) Probabilistic

The net outputs the parameters of the distribution it models — not samples from it. \(x\to q_\phi(z|x)\to(\mu_\phi,\Sigma_\phi)\). A VAE uses this for both encoder and decoder.

xdata, d-dim
qφ(z|x)encoder
μφ, Σφk-dim params
z = μ + σ⊙εreparam. sample
pθ(x|z)decoder
θ(z)params of p(x|z)
★ There is no direct connection between encoder and decoder

A sampling step sits between them, and sampling happens outside both networks. That is precisely why the naive gradient breaks and why the reparameterization trick is needed (§05). \(\phi\) = encoder weights, \(\theta\) = decoder weights. Both are trained by gradient ascent on the ELBO:

\(\phi^{t+1}\leftarrow\phi^t+\alpha\nabla_\phi J_\theta(q_\phi)\qquad \theta^{t+1}\leftarrow\theta^t+\alpha\nabla_\theta J_\theta(q_\phi)\)
MCQPractice paper · Q1Concept
A VAE decoder models \(p_\theta(x|z)=\mathcal N\big(x;\mu_\theta(z),\Sigma_\theta(z)\big)\). Given a latent code \(z\), what does the decoder network output?
  • The latent code \(z\)
  • Only the mean \(\mu_\theta(z)\)
  • Only the covariance \(\Sigma_\theta(z)\)
  • Both the mean \(\mu_\theta(z)\) and the covariance \(\Sigma_\theta(z)\)
Reveal answer
Both the mean and the covariance

Direct test of "probabilistic representation": the net emits every parameter of the distribution it models. If the question had written \(p_\theta(x|z)=\mathcal N(x;\hat x_\theta(z), I)\) — covariance fixed to identity, as in the lecture notes — then the decoder would output the mean only, and the reconstruction term reduces to squared error.

MCQ2025 T3 · Q703 marks
In the standard VAE graphical model, what is the sequence of dependencies that constitutes the generative process?
  • Input \(x\to\) Decoder \(p(x|z)\to\) Latent \(z\to\) Prior \(p(z)\to\) Reconstruction
  • Encoder \(q(z|x)\to\) Latent \(z\to\) Prior \(p(z)\)
  • Prior \(p(z)\to\) Latent \(z\to\) Decoder \(p(x|z)\to\) Sample \(x\)
  • Input \(x\to\) Encoder \(q(z|x)\to\) Latent \(z\to\) Decoder \(p(x|z)\to\) Reconstruction
Reveal answer
Prior → z → Decoder → Sample x

The last option is the trap, and it is a good one: \(x\to\text{Enc}\to z\to\text{Dec}\to\tilde x\) is the training / reconstruction path, not the generative path. Generation never sees an input \(x\) — you draw \(z\) from the prior and push it through the decoder. The original paper's wording ("the generative process (Inference)") is confusing on purpose; go by generative = prior-first.

MCQ2025 T3 · Q724 marks
For a hierarchical VAE with two latent layers, \(p_\theta(z_2)=\mathcal N(0,I)\), \(p_\theta(z_1|z_2)\), \(p_\theta(x|z_1)\), and a recognition model factorising as \(q_\phi(z_2|x)\,q_\phi(z_1|z_2,x)\) — which expression is the correct ELBO?
  • \(\mathbb E_{q(z_1|x)}[\log p(x|z_1)]-\mathrm{KL}(q(z_1|x)\|p(z_1))-\mathrm{KL}(q(z_2|x)\|p(z_2))\)
  • \(\mathbb E_{q(z_2|x)}[\log p(x|z_2)]-\mathrm{KL}(q(z_1|z_2,x)\|p(z_1))-\mathrm{KL}(q(z_2|x)\|p(z_2))\)
  • \(\mathbb E_{q(z_2|x)}\big[\mathbb E_{q(z_1|z_2,x)}[\log p(x|z_1)]-\mathrm{KL}(q(z_1|z_2,x)\|p(z_1|z_2))\big]-\mathrm{KL}(q(z_2|x)\|p(z_2))\)
  • \(\mathbb E_{q(z_1,z_2|x)}[\log p(x|z_1)]+\mathrm{KL}(q(z_1|z_2,x)\|p(z_1|z_2))+\mathrm{KL}(q(z_2|x)\|p(z_2))\)
Reveal answer
Option 3

Three separate discriminators between the options, and you can kill three options without doing any algebra:

  1. 1.Nested expectations. Because \(q\) factorises as \(q(z_2|x)q(z_1|z_2,x)\), the inner expectation over \(z_1\) must sit inside the outer one over \(z_2\). Options 1 and 2 flatten it.
  2. 2.KL against the conditional prior. The \(z_1\) KL must be against \(p_\theta(z_1|z_2)\), not the marginal \(p_\theta(z_1)\) — that is what the model actually defines.
  3. 3.Signs. KL terms are always subtracted from an ELBO. Option 4 adds them.

This is the bridge question to diffusion: a DDPM is exactly this, with \(T\) layers instead of 2 and the \(q\)'s frozen.

05★★★ Every paper

The reparameterization trick

Why the obvious gradient does not exist, and the one-line fix. Asked both as "compute \(z\)" (numeric) and "why is it essential" (concept).

The problem: \(\nabla_\phi\) of an expectation whose distribution depends on \(\phi\)

The reconstruction term is \(\mathbb E_{q_\phi(z|x)}\log p_\theta(x|z)\). Abstract it as \(\nabla_\psi\,\mathbb E_{p_\psi(v)}f_\psi(v)\) and push the gradient inside:

\(\displaystyle \nabla_\psi\int_v p_\psi(v)f_\psi(v)dv=\underbrace{\int_v \big(\nabla_\psi f_\psi(v)\big)p_\psi(v)\,dv}_{=\;\mathbb E_{p_\psi}\nabla_\psi f_\psi(v)\;\;\checkmark}\;+\;\underbrace{\int_v \big(\nabla_\psi p_\psi(v)\big)f_\psi(v)\,dv}_{\textbf{not an expectation}\;\;\times}\) The second term is not of the form "density × function", so the Law of Large Numbers does not apply and it cannot be Monte-Carlo estimated from samples. Equivalently: the sampling operation is non-differentiable, so gradient cannot flow back into the encoder.

The fix: move the randomness into a parameter-free variable

★ The trick, stated properly

Find an auxiliary random variable \(\epsilon\sim p_\epsilon\) whose distribution does not depend on \(\psi\), and a deterministic map \(v=g(\epsilon)\). Then by the Law of the Unconscious Statistician (LOTUS):

\(\displaystyle \nabla_\psi\,\mathbb E_{p_\psi(v)}f_\psi(v)=\nabla_\psi\,\mathbb E_{p_\epsilon}f_\psi\big(g(\epsilon)\big)\approx\frac1M\sum_{j=1}^{M}\nabla_\psi f_\psi\big(g(\epsilon_j)\big)\)

Now the sampling distribution is \(\phi\)-free, so \(\nabla\) slides inside the expectation and the Monte-Carlo average is an unbiased gradient estimator.

Example (a) — Gaussian
\(\epsilon\sim\mathcal N(0,I)\)
\(z=\mu_\phi(x)+\Sigma_\phi(x)\odot\epsilon\)

An affine transformation of \(\epsilon\). \(\Sigma_\phi\) is diagonal in practice, so \(\odot\) is element-wise.

Example (b) — Inverse CDF
\(\epsilon\sim U[0,1]\)
\(z=F_z^{-1}(\epsilon)\)

Works for any 1-D distribution with an invertible CDF. \(g(\cdot)=F_z^{-1}(\cdot)\).

⚠ σ vs σ² vs logσ² — the deliberate distractor axis

Encoders almost always emit \(\log\sigma^2\) (unconstrained, numerically stable). To reparameterize you need \(\sigma\), so:

\(\sigma=\exp\!\big(\tfrac12\log\sigma^2\big)\qquad\Longrightarrow\qquad z=\mu+\exp\!\big(\tfrac12\log\sigma^2\big)\cdot\epsilon\)

Every wrong option in these questions comes from skipping the \(\tfrac12\), forgetting the \(\exp\), or using \(\sigma^2\) in place of \(\sigma\). Write down which of the three you have been given before you touch the calculator.

NAT2025 T2 · Q1222 marks
Using \(z=\mu+\sigma\cdot\epsilon\) with \(\epsilon\sim\mathcal N(0,1)\), a VAE encoder gives \(\mu=2.5\) and log-variance \(\log\sigma^2=1.6\). If \(\epsilon=-1.5\), what is \(z\)?
Reveal answer
−0.84
\(\sigma=e^{1.6/2}=e^{0.8}=2.2255\)
\(z=2.5+2.2255\times(-1.5)=2.5-3.3383=-0.8383\)

If you had used \(\sigma^2=e^{1.6}=4.953\) you would get \(-4.93\) — outside the accepted range. The \(\tfrac12\) is the whole question.

MCQ2026 T1 · Q73 marks
An encoder outputs \(\mu_\phi(x)=[0.6,-0.4,0.2]^T\) and \(\Sigma_\phi(x)=\mathrm{diag}(e^{-0.8},e^{-1.2},e^{0.4})\). With noise \(\epsilon=[1.0,-0.5,0.3]^T\), compute the latent sample \(z\).
  • \(z=[0.449,\,0.151,\,0.448]^T\)
  • \(z=[1.270,\,-0.674,\,0.566]^T\)
  • \(z=[1.600,\,-0.900,\,0.500]^T\)
  • \(z=[1.049,\,-0.551,\,0.648]^T\)
Reveal answer
z = [1.049, −0.551, 0.648]

The official key applies \(z=\mu+\Sigma\odot\epsilon\) literally, treating the diagonal entries as standard deviations:

\(0.6+e^{-0.8}(1.0)=0.6+0.4493=1.049\)
\(-0.4+e^{-1.2}(-0.5)=-0.4-0.1506=-0.551\)
\(0.2+e^{0.4}(0.3)=0.2+0.4475=0.648\)

Read the distractors — they are a map of the three conventions. Option 2 is what you get treating the entries as variances (\(\mu+\sqrt{\Sigma}\epsilon\)); option 3 is \(\mu+\epsilon\) with unit \(\sigma\). The examiner wants the formula as written on the slide: \(z=\mu_\phi(x)+\Sigma_\phi(x)\epsilon\). When a paper writes \(\Sigma\) and gives you the matrix directly, plug it in unchanged; reach for the square root only when the symbol is explicitly \(\sigma^2\) or \(\log\sigma^2\).

MCQPractice paper · Q6Concept
Why is the reparameterization trick essential?
  • It makes the KL divergence term computable
  • It makes the sampling process deterministic with respect to the encoder's parameters
  • It ensures the latent variable \(z\) has a unit Gaussian distribution
  • It reduces the variance of the stochastic gradients
Reveal answer
It makes the sampling process deterministic w.r.t. the encoder's parameters

All the randomness is pushed into \(\epsilon\), which is a constant as far as \(\phi\) is concerned; given \(\epsilon\), the map \(\phi\mapsto z\) is a differentiable deterministic function, so backprop reaches the encoder. Option 1 is false — the KL has a closed form and needs no sampling at all. Option 4 is a genuinely tempting near-miss: reparameterized ("pathwise") gradients are usually lower-variance than the score-function alternative, but that is a bonus, not the reason it is essential. Without the trick the gradient does not merely have high variance — it cannot be computed at all.

06★★★ The workhorse

The KL term, in closed form

This single formula has been examined in every paper and in the practice set. It is the most reliable 3–4 marks available. Learn it in both the vector and per-dimension forms.

Gaussian posterior vs standard-normal prior

Memorise the per-dimension form — it is the one you compute with \(\displaystyle D_{KL}\big(\mathcal N(\mu,\Sigma)\,\big\|\,\mathcal N(0,I)\big)=\tfrac12\Big[\mathrm{tr}(\Sigma)+\|\mu\|_2^2-k-\log|\Sigma|\Big]\)
\(\displaystyle \overset{\Sigma\ \text{diagonal}}{=}\;\;\tfrac12\sum_{j=1}^{k}\Big(\sigma_j^2+\mu_j^2-1-\log\sigma_j^2\Big)\) Mnemonic for the four terms, in order: variance + mean² − one − log-variance. Note it needs no sampling — it is an exact differentiable function of the encoder outputs, which is why only the reconstruction term needs Monte Carlo.
Sanity checks worth 10 seconds in the exam
  • If \(\mu=0\) and \(\sigma^2=1\) (so \(\log\sigma^2=0\)): each term gives \(\tfrac12(1+0-1-0)=0\). KL is exactly 0 — correct, the posterior is the prior.
  • KL is never negative. If you get a negative number, you have dropped a sign or a term.
  • When the input is \(\log\sigma^2\), you need \(\sigma^2=e^{\log\sigma^2}\) for the first term and \(\log\sigma^2\) as given for the last. Do not exponentiate twice.
NAT2025 T2 · Q1294 marks
A 2-D diagonal Gaussian posterior has \(\mu=[0.4,-0.3]\) and \(\log\sigma^2=[-1.8,-0.4]\). Compute \(D_{KL}(q(z|x)\|\mathcal N(0,I))\).
Reveal answer
0.643
\(\sigma_1^2=e^{-1.8}=0.16530,\quad \sigma_2^2=e^{-0.4}=0.67032\)
\(j{=}1:\;0.16530+0.16-1-(-1.8)=1.12530\)
\(j{=}2:\;0.67032+0.09-1-(-0.4)=0.16032\)
\(D_{KL}=\tfrac12(1.12530+0.16032)=\tfrac12(1.28562)=0.6428\)

Note how the two minus signs turn into plus when you subtract a negative log-variance. That is where nearly all the arithmetic errors live.

NATPractice paper · Q32 parts
A \(\beta\)-VAE with a 2-D latent gives \(\mu=[0.8,-0.6]\), \(\log\sigma^2=[-0.5,0.3]\). The reconstruction mismatch (NLL) is 5.2.
(a) Compute \(D_{KL}(q_\phi(z|x)\|p(z))\).   (b) With \(\beta=2\), compute \(J_\theta(q_\phi)=\mathbb E_{q}\log p_\theta(x|z)-\beta D_{KL}\).
Reveal answer
(a) 0.58  ·  (b) −6.36
\(\sigma_1^2=e^{-0.5}=0.60653,\quad\sigma_2^2=e^{0.3}=1.34986\)
\(j{=}1:\;0.60653+0.64-1+0.5=0.74653\)
\(j{=}2:\;1.34986+0.36-1-0.3=0.40986\)
\(D_{KL}=\tfrac12(1.15639)=0.5782\approx0.58\)
\(J=-5.2-2(0.58)=-6.36\)

Part (b) is the sign-convention question in miniature. "Reconstruction mismatch (negative log-likelihood) = 5.2" means \(\mathbb E_q\log p_\theta(x|z)=-5.2\). The ELBO is being maximised, so both contributions are negative and the answer is negative. In the minimisation convention the same numbers give \(+6.36\).

NAT2026 T1 · Q8, Q93 + 2 marks
Encoder outputs \(\mu=[0.6,-0.4,0.2]^T\), \(\Sigma=\mathrm{diag}(e^{-0.8},e^{-1.2},e^{0.4})\).
(a) Compute the latent regularisation contribution \(D_{KL}\).   (b) The total reconstruction mismatch is 186.4 — calculate the ELBO \(J_\theta(q_\phi)\), rounded to the nearest integer.
Reveal answer
(a) 0.70 by the standard formula  ·  (b) −187

Reading \(\Sigma\)'s diagonal as the variances, so \(\log\sigma_j^2=-0.8,-1.2,0.4\):

\(\textstyle\sum\mu_j^2=0.36+0.16+0.04=0.56\)
\(\textstyle\sum\sigma_j^2=0.44933+0.30119+1.49182=2.24234\)
\(\textstyle\sum\log\sigma_j^2=-0.8-1.2+0.4=-1.6\)
\(D_{KL}=\tfrac12\big(0.56+2.24234-3+1.6\big)=\tfrac12(1.40234)=0.7012\)
⚠ The official key here looks wrong

The paper accepted 0.58–0.62 for part (a). The formula the course teaches gives 0.7012, which falls outside that window. Trust the formula, not the key — and note the good news: part (b) is robust either way, since \(-186.4-0.70=-187.10\) and \(-186.4-0.60=-187.00\) both round to −187.

For (b), remember the sign: \(J=\underbrace{(-186.4)}_{\mathbb E\log p_\theta(x|z)}-\underbrace{D_{KL}}_{\ge0}\).

The general two-Gaussian KL (needed for diffusion)

Full form \(\displaystyle D_{KL}\big(\mathcal N(\mu_1,\Sigma_1)\|\mathcal N(\mu_2,\Sigma_2)\big)=\tfrac12\Big[\log\tfrac{|\Sigma_2|}{|\Sigma_1|}-d+\mathrm{tr}(\Sigma_2^{-1}\Sigma_1)+(\mu_2-\mu_1)^T\Sigma_2^{-1}(\mu_2-\mu_1)\Big]\) In diffusion we choose \(\Sigma_1=\Sigma_2=\sigma_q^2 I\). Then \(\log\frac{|\Sigma_2|}{|\Sigma_1|}=0\) and \(\mathrm{tr}(I)=d\) cancels the \(-d\), leaving only the quadratic term.
★ Equal-variance special case — appears constantly in diffusion questions \(\displaystyle D_{KL}\big(\mathcal N(\mu_1,\sigma^2 I)\,\|\,\mathcal N(\mu_2,\sigma^2 I)\big)=\frac{\|\mu_1-\mu_2\|_2^2}{2\sigma^2}\)
NATPractice paper · Q13, Q144 + 1 parts
At a reverse timestep, \(q(x_{t-1}|x_t,x_0)=\mathcal N(1.2,\,0.25)\) and \(p_\theta(x_{t-1}|x_t)=\mathcal N(\mu_\theta,\,0.25)\).
(a) KL when \(\mu_\theta=0.8\)?  (b) when \(\mu_\theta=1.0\)?  (c) By what factor is (a) larger than (b)?  (d) Which \(\mu_\theta\) minimises the KL?
Reveal answer
(a) 0.32  ·  (b) 0.08  ·  (c) 4  ·  (d) μθ = 1.2
\(\displaystyle \mathrm{KL}=\frac{(\mu_q-\mu_\theta)^2}{2\sigma^2}=\frac{(1.2-0.8)^2}{2(0.25)}=\frac{0.16}{0.5}=0.32\)
\(\displaystyle \frac{(1.2-1.0)^2}{0.5}=\frac{0.04}{0.5}=0.08\qquad\text{ratio}=\frac{0.32}{0.08}=4\)

Part (c) is the conceptual payload: the loss is quadratic in the mean error, so halving the error (0.4→0.2) divides the KL by 4. Part (d): the minimum is at zero error, \(\mu_\theta=\mu_q=1.2\), where KL = 0. Practice Q14 is the same idea as a comparison — the model whose mean is closer to \(\mu_q\) wins, so \(\mathcal N(1.3,0.16)\) beats \(\mathcal N(1.1,0.16)\) against a true mean of 1.5.

This is exactly the DDPM consistency term12) — which is why "training a diffusion model" is really just "regression onto \(\mu_q\)".

07Mechanics

Training a VAE, and using a trained one

The forward/backward passes, and the two things a trained VAE is for. Lower-frequency than the formulas, but it explains why the KL term is there — which is heavily examined.

Parameterizing the decoder

Choose a known distribution for \(p_\theta(x|z)\) and let the decoder output its parameters. With the standard choice \(p_\theta(x|z)=\mathcal N\big(x;\hat x_\theta(z),I\big)\):

\(\displaystyle \log p_\theta(x|z)=\log\frac{1}{(2\pi)^{d/2}}\exp\!\Big(-\tfrac12\|x-\hat x_\theta(z)\|_2^2\Big)\;\propto\;-\big\|x-\hat x_\theta(z)\big\|_2^2\) A Gaussian decoder with identity covariance turns the reconstruction term into plain squared error. A Bernoulli decoder (binary pixels) would instead give binary cross-entropy. Which one you get depends entirely on the assumed likelihood.

Forward pass

  1. Pass \(x_i\) through the encoder to get \(\mu_\phi(x_i)\) and \(\Sigma_\phi(x_i)\).
  2. Sample \(\epsilon_1..\epsilon_M\sim\mathcal N(0,I)\) outside the networks.
  3. Compute \(z_j=\mu_\phi(x_i)+\Sigma_\phi(x_i)\odot\epsilon_j\) via reparameterization.
  4. Push all \(z_j\) through the decoder to get \(\hat x_\theta(z_j)\).
  5. Form \(-\frac1M\sum_j\|x_i-\hat x_\theta(z_j)\|_2^2\), and add the closed-form KL.

Backward pass — the asymmetry that gets asked

Encoder update (φ)

Gradient flows backward through the decoder (with \(\theta\) held constant), then through the reparameterization node (which is a function of \(\phi\); the \(\epsilon_j\) are constants), then into the encoder. The KL gradient is added here too, since KL depends on \(\phi\).

Decoder update (θ)

Same forward pass, but the backward pass stops at the decoder — it does not propagate into the encoder. And \(\nabla_\theta D_{KL}=0\), because the KL term contains no \(\theta\). So the decoder is trained by the reconstruction term alone.

Inference 1 — generation

  • Sample \(z_{\text{new}}\sim p(z)=\mathcal N(0,I)\)the prior, not the encoder.
  • Push through the decoder to get \(\hat x_\theta(z_{\text{new}})\). Then either use \(\hat x_\theta(z_{\text{new}})\) itself as the generated point, or draw \(x_{\text{new}}\sim\mathcal N(\hat x_\theta(z_{\text{new}}),I)\).
★ Why generation works at all — and why it also causes collapse

Sampling from \(\mathcal N(0,I)\) only produces sensible outputs if the decoder has actually seen codes distributed like \(\mathcal N(0,I)\) during training. That is exactly what the KL term enforces: it pulls every \(q_\phi(z|x)\) toward the prior, so at convergence "sample from the prior" ≈ "sample from the aggregate posterior". Hold that thought — pushed too hard, this same force is posterior collapse08).

Inference 2 — embedding / posterior inference

Run the trained encoder on \(x_{\text{test}}\) and take either:

  • Deterministic: \(z_{\text{test}}=\mu_\phi(x_{\text{test}})\) — the posterior mean.
  • Stochastic: \(z_{\text{test}}\sim\mathcal N\big(\mu_\phi(x_{\text{test}}),\Sigma_\phi(x_{\text{test}})\big)\).
MCQ2026 T1 · Q112 marks
A factory flags a ceramic tile when the anomaly score \(A(x)=(\text{reconstruction mismatch})+0.5\times D_{KL}\) exceeds 196.0. For a suspicious tile the reconstruction mismatch is 194.8 and \(D_{KL}=3.6\). Is it flagged?
  • Flagged
  • Not flagged
Reveal answer
Flagged

\(194.8+0.5(3.6)=194.8+1.8=196.6>196.0\). Trivial arithmetic, but note the applied framing: a VAE trained only on normal data reconstructs normal inputs well and anomalies badly, so a high reconstruction error is an anomaly signal. Expect more application-flavoured wrappers like this — the maths inside stays elementary.

08★★★ 21 marks

Posterior collapse & the \(\beta\)-VAE

One pathology, one hyperparameter, and a numeric question in every single paper. Collapse was asked twice in 2025 T3 alone — once as cause, once as symptom.

The failure mode

⚠ Posterior collapse, mechanically

The ELBO minimises \(D_{KL}(q_\phi(z|x)\|p(z))\). Push that too hard and, for every \(x\), \(q_\phi(z|x)\to\mathcal N(0,I)\). But then

\(q_\phi(z|x_i)=q_\phi(z|x_j)=\mathcal N(0,I)\quad\forall i,j\)

so the codes carry no information about which input they came from, and the decoder cannot tell \(x_i\) from \(x_j\). The encoder has effectively been switched off. Diagnostic: per-dimension KL ≈ 0 means that dimension is dead.

The VAE as a regularized auto-encoder

Read the ELBO as the standard ML pattern "cost + \(\lambda\times\) regulariser":

\(\displaystyle \underbrace{\big\|x-\hat x_\theta(z)\big\|_2^2}_{\text{cost on }x}\;+\;\underbrace{D_{KL}\big(q_\phi(z|x)\|p(z)\big)}_{\text{regulariser on }z}\) The vanilla VAE has no regularisation constant — it is implicitly \(\lambda=1\). Adding one back is the entire idea of the \(\beta\)-VAE.
★ The \(\beta\)-VAE objective — both conventions \(\displaystyle J_\beta=\mathbb E_{q_\phi(z|x)}\log p_\theta(x|z)-\beta\,D_{KL}\big(q_\phi(z|x)\|p(z)\big)\quad\text{(maximise)}\)
\(\displaystyle \mathcal L_\beta=\mathcal L_{\text{recon}}+\beta\,\mathcal L_{KL}\quad\text{(minimise)}\) \(\beta\) multiplies the KL only, never the reconstruction term.
SettingEffect on KLReconstructionGeneration / latent
Higher \(\beta\) (\(\beta>1\))Strongly penalised → \(q_\phi(z|x)\to p(z)\)Worse, blurrierBetter prior match, stronger disentanglement, lower mutual information \(I(x;z)\); risks posterior collapse
Lower \(\beta\) (\(\beta<1\))Weakly penalised → \(q_\phi(z|x)\ne p(z)\)Better, sharperLatent no longer matches the prior, so generation suffers
A note on the range of β

The lecture notes present \(\beta\) as a hyperparameter in \([0,1]\) (dialling the regulariser down from the implicit 1). The exam papers, however, use \(\beta=2,\,4,\,8\) — the disentanglement regime from the \(\beta\)-VAE literature. Both directions are examinable; what matters is the monotone trade-off in the table, which reads the same way from either end.

NAT2025 T2 · Q124  ·  2025 T3 · Q69  ·  2026 T1 · Q103 / 3 / 2 marks
All three papers asked the same computation.
2025 T2: \(\beta=8\), reconstruction loss 12.4, KL 2.1. What is the objective being minimized?
2025 T3: latent \(D=5\), \(\mathcal L_{KL}=8\), \(\mathcal L_{Recon}=2\), \(\beta=2\). What is the total loss?
2026 T1: \(\beta=4\), reconstruction mismatch 194.8, \(D_{KL}=3.6\). Compute \(J_\beta\).
Reveal answer
2025 T2: 29.2  ·  2025 T3: 18  ·  2026 T1: −209.2
\(12.4+8(2.1)=12.4+16.8=29.2\)  (minimise form → positive)
\(2+2(8)=18\)  (minimise form → positive)
\(-194.8-4(3.6)=-194.8-14.4=-209.2\)  (ELBO form → negative)

Same arithmetic, three sign outcomes decided by one word in the stem. Also note 2025 T3 plants \(D=5\) as a pure distractor — the latent dimension does not enter the loss assembly at all once you are handed \(\mathcal L_{KL}\).

MSQ2025 T3 · Q764 marks
In a \(\beta\)-VAE with \(\mathcal L=\mathbb E_{q}[\log p_\theta(x|z)]-\beta D_{KL}(q_\phi(z|x)\|p(z))\), increasing \(\beta\) (i.e. \(\beta>1\)) tends to:
  • Strengthen disentanglement between latent dimensions
  • Encourage sharper image reconstructions
  • Reduce mutual information between \(x\) and \(z\)
  • Make the latent posterior \(q(z|x)\) closer to the prior \(p(z)\)
Reveal answer
Options 1, 3 and 4 — three of four

Everything except sharper reconstructions. A bigger \(\beta\) puts more weight on "make \(q\) look like the prior", which simultaneously (4) pulls the posterior to the prior, (3) squeezes information out of the channel from \(x\) to \(z\), and (1) — the empirical finding the \(\beta\)-VAE paper is famous for — encourages axis-aligned, disentangled factors. The cost is paid entirely by reconstruction quality, so option 2 is exactly backwards. With no negative marking, if you are confident of 4 and 3, take 1 as well.

MCQ2025 T3 · Q71 & Q77  ·  Practice Q43 + 3 marks
Q71 (cause): Which component of the VAE loss, when overwhelmingly strong, is the primary mathematical driver of posterior collapse?
  • The gradient flow from the decoder to the encoder
  • The reconstruction loss (expected log-likelihood)
  • The mean term \(\mu\) of the reparameterization trick
  • The \(\beta\)-weighted KL divergence term
Q77 (symptom): If \(\mathrm{KL}(q_\phi(z|x)\|p(z))\to0\) for all \(x\) during training, what is happening?
  • The encoder ignores the input \(x\) and outputs something close to the prior for all inputs
  • The encoder perfectly memorizes each input in \(z\)
  • The decoder has zero variance
  • The ELBO is maximized
Reveal answer
Q71: the β-weighted KL term  ·  Q77: the encoder ignores x

The same phenomenon from two directions — a favourite examiner move. The KL is the only force pushing \(q_\phi(z|x)\) toward a fixed distribution, so it is the driver; and KL \(\to0\) everywhere is the definition of the symptom.

Option 4 in Q77 is the interesting distractor: a maximal ELBO would be a good outcome, but collapse maximises only the KL half while the reconstruction half rots — the total ELBO is not at its best.

Practice Q4 flips it into a comparison: encoder A has per-dimension KL ≈ 0, encoder B ≈ 1.5, same reconstruction quality. The right reading is "B uses its latent more informatively; A has partially collapsed toward the prior." Higher KL = the posterior is allowed to move away from the prior = the code is carrying information.

09★★★ 14 marks

VQ-VAE — discrete latents

Appeared in all three papers. Small topic, reliably examined, and the two numeric templates (bit budget, commitment loss) are formulaic once seen.

In a VQ-VAE the latent space is discrete: the encoder's continuous output is snapped to the nearest entry of a learnable dictionary (codebook) \(L=\{z_1,\dots,z_M\}\), \(z_j\in\mathbb R^k\).

xidata
qφencoder
ze(xi)continuous, k×p
zq(xi)quantized
pθdecoder
ireconstruction
Vector quantization \(\displaystyle z_q(x_i)=z_{j^*},\qquad j^*=\arg\min_{j=1..M}\big\|z_e(x_i)-z_j\big\|_2^2\) In practice \(z_e(x_i)\) is a \(k\times p\) tensor and each column (a \(k\)-vector) is quantized independently against the dictionary.
Training objective \(\displaystyle J_\theta(q)=\underbrace{\big\|x-\hat x_\theta(z_q)\big\|_2^2}_{\text{reconstruction}}+\underbrace{\big\|z_e(x_i)-z_q(x)\big\|_2^2}_{\text{codebook / commitment}}\) The dictionary is learned alongside \(\phi,\theta\): \(z_j^{t+1}\leftarrow z_j^{t}-\alpha\nabla_{z_j}J_\theta(q)\). In the full formulation the commitment half carries its own weight \(\beta\) and a stop-gradient: \(\beta\|z_e(x)-\mathrm{sg}[e_k]\|_2^2\).
★ VQ-VAE vs VAE — the contrast that gets examined
VAEVQ-VAE
LatentContinuousDiscrete (codebook indices)
Reparameterization trickYesNo
KL divergence termYesNo
Reconstruction lossYesYes
Decoder networkYesYes
SamplingDraw \(z\sim\mathcal N(0,I)\)No fixed prior — fit a second generative model (e.g. a GMM) over observed \(z_q\), then sample from that
MSQ2025 T3 · Q67, Q683 + 3 marks
Q67: In a VQ-VAE, the encoder outputs a continuous vector which is then:
  • Sent directly to the decoder with no change
  • Thresholded element-wise at 0
  • Replaced by the nearest code vector from a learned codebook
  • Sorted in descending order
Q68: In contrast to a standard VAE, a VQ-VAE does NOT typically include which of the following?
  • The reparameterization trick \((z=\mu+\sigma\odot\epsilon)\)
  • A reconstruction loss term (like MSE or Cross-Entropy)
  • A decoder network
  • A KL divergence loss term
Reveal answer
Q67: nearest code vector  ·  Q68: reparameterization trick AND KL term

Both dropped items exist for the same reason: they are artefacts of a continuous Gaussian posterior. VQ-VAE's posterior is a deterministic \(\arg\min\) onto a finite codebook — there is no \(\sigma\) to sample and no continuous density to measure a KL against. What survives is what any autoencoder needs: a decoder and a reconstruction loss.

NAT2025 T2 · Q125  &  2026 T1 · Q123 / 2 marks
A VQ-VAE uses a codebook with \(K=256\) vectors, each of dimension \(D=32\). The encoder output \(z_e(x)\) has shape \([16,16,32]\). What is the total size in bits of the discrete latent representation (the indices sent to the decoder) for a single input?
Reveal answer
2048 bits  (graded exactly — no range)
\(\text{bits per index}=\log_2 K=\log_2 256=8\)
\(\text{number of positions}=16\times16=256\)
\(\text{total}=256\times8=2048\text{ bits}\)

\(D=32\) is a distractor and so is the trailing 32 in the shape. Once a vector has been quantized, all that travels to the decoder is which entry it was — an index. The codebook dimension affects the codebook's own storage, never the payload. Appeared in two papers, both times graded exact-match, so do the \(\log_2\) carefully.

NAT2025 T2 · Q1263 marks
The commitment loss is \(\beta\|z_e(x)-\mathrm{sg}[e_k]\|_2^2\). For \(z_e(x)=[1.2,-0.5,0.8]\) with nearest codebook vector \(e_k=[1.0,-0.9,1.1]\) and \(\beta=0.25\), what is the commitment loss?
Reveal answer
0.0725
\(\Delta=[0.2,\,0.4,\,-0.3]\)
\(\|\Delta\|_2^2=0.04+0.16+0.09=0.29\)
\(0.25\times0.29=0.0725\)

It is a squared L2 norm — no square root. The sg[·] (stop-gradient) changes nothing about the value; it only stops gradient flowing into the codebook from this term, so that the commitment loss pushes the encoder toward the codebook rather than the reverse.

10★★★ Biggest topic

Diffusion I — the forward (noising) process

Diffusion is 46 of the 140 marks. Nearly half of those come from one formula, plugged into small numbers. Get this section airtight before anything else.

A DDPM is a hierarchical VAE with three constraints

★ The defining triple — recite it
  1. 1.There are multiple latent spaces \(x_1,\dots,x_T\) (a Markov chain), not one.
  2. 2.\(\dim(x_t)=\dim(x_0)\) for all \(t\)latents are the same size as the data. There is no bottleneck, no dimensionality reduction.
  3. 3.The encoding process is fixed and non-learnable. A VAE learns \(q_\phi(z|x)\); a DDPM's \(q(x_t|x_{t-1})\) has no parameters at all. Only the decoder is learned.
⚠ Notation reset

In the diffusion literature \(x_0\) is the data point and \(x_1,\dots,x_T\) are its latentsnot a set of different data points as in earlier weeks. \(T\) is a hyperparameter, typically \(\approx1000\).

The recursion

One noising step \(\displaystyle x_t=\sqrt{\alpha_t}\;x_{t-1}+\sqrt{1-\alpha_t}\;\epsilon_t,\qquad \epsilon_t\sim\mathcal N(0,I)\)
\(\displaystyle \Longleftrightarrow\quad q(x_t|x_{t-1})=\mathcal N\big(x_t;\;\sqrt{\alpha_t}\,x_{t-1},\;(1-\alpha_t)I\big)\) The \(\alpha_1..\alpha_T\) are fixed scalars in \([0,1]\). The common alternative notation writes a variance schedule \(\beta_t\) with \(\alpha_t=1-\beta_t\); then the step variance is exactly \(\beta_t I\). Both appear in the papers — they are the same thing.
  • This is a first-order Markov chain: \(x_t\perp x_{t-2},\dots,x_0\mid x_{t-1}\). Hence \(q(x_t|x_{t-1})=q(x_t|x_{t-1},x_0)\) — a rewrite used constantly in the ELBO derivation.
  • It is probabilistic, not deterministic — fresh \(\epsilon_t\) is drawn at each step, so running it twice on the same \(x_0\) gives different \(x_T\).
  • The chain has stationary distribution \(q(x_T)\approx\mathcal N(0,I)\) — after enough steps all trace of \(x_0\) is gone.

The closed form: jump straight to any \(t\)

Unroll the recursion. Two independent Gaussians combine into one whose variances add:

\(\displaystyle x_t=\sqrt{\alpha_t}\big(\sqrt{\alpha_{t-1}}x_{t-2}+\sqrt{1-\alpha_{t-1}}\epsilon_{t-2}\big)+\sqrt{1-\alpha_t}\,\epsilon_{t-1}\)
\(\displaystyle =\sqrt{\alpha_t\alpha_{t-1}}\,x_{t-2}+\underbrace{\sqrt{\alpha_t-\alpha_t\alpha_{t-1}}\;\epsilon_{t-2}+\sqrt{1-\alpha_t}\;\epsilon_{t-1}}_{a\epsilon_1+b\epsilon_2\;\sim\;\mathcal N(0,(a^2+b^2)I)}\)
\(\displaystyle a^2+b^2=(\alpha_t-\alpha_t\alpha_{t-1})+(1-\alpha_t)=1-\alpha_t\alpha_{t-1}\) The two noise terms merge into a single Gaussian. Recurse all the way to \(x_0\) and the pattern continues.
★★★ The single most examined formula in Quiz 2 \(\displaystyle x_t=\sqrt{\bar\alpha_t}\;x_0+\sqrt{1-\bar\alpha_t}\;\epsilon,\qquad \epsilon\sim\mathcal N(0,I),\qquad \bar\alpha_t=\prod_{s=1}^{t}\alpha_s\)
\(\displaystyle \Longleftrightarrow\quad q(x_t|x_0)=\mathcal N\big(x_t;\;\sqrt{\bar\alpha_t}\,x_0,\;(1-\bar\alpha_t)I\big)\) The \(t\)-th latent is obtainable directly from \(x_0\) in one shot, without simulating \(t\) steps. This is what makes training a DDPM cheap: you can sample a random \(t\) and jump straight there.
Reading \(\bar\alpha_t\) as a signal/noise dial

\(\sqrt{\bar\alpha_t}\) is the fraction of original signal retained; \(\sqrt{1-\bar\alpha_t}\) is the noise contribution. They trade off along the chain:

\(t\) small (early)\(t\) large (late)
\(\bar\alpha_t\)close to 1close to 0
\(\sqrt{\bar\alpha_t}\) (signal)close to 1close to 0
\(\sqrt{1-\bar\alpha_t}\) (noise)close to 0close to 1
\(x_t\) looks likethe datapure \(\mathcal N(0,I)\) noise

Since \(\bar\alpha_t\) is a product of numbers in \([0,1]\), it is monotonically decreasing in \(t\) — always.

NAT2025 T2 · Q130  &  2026 T1 · Q64 / 2 marks
With a linear schedule from \(\beta_1=10^{-4}\) to \(\beta_T=0.02\), \(T=1000\): at \(t=200\), \(\bar\alpha_{200}\approx0.979\). Given \(x_0=0.8\) and \(\epsilon=-1.2\), what is \(x_{200}\)?
Reveal answer
0.62
\(x_{200}=\sqrt{0.979}(0.8)+\sqrt{1-0.979}(-1.2)\)
\(=0.98944(0.8)+\sqrt{0.021}\,(-1.2)=0.79155+0.14491(-1.2)\)
\(=0.79155-0.17389=0.61766\)

The entire schedule description is decoration. You are handed \(\bar\alpha_{200}\), so \(\beta_1,\beta_T,T\) never enter the arithmetic. Appeared in two papers with identical numbers. The only real trap is taking \(\sqrt{1-\bar\alpha_t}=1-\sqrt{\bar\alpha_t}\) — it is not.

NATPractice paper · Q72 parts
A DDPM uses per-step \(\alpha_t=(0.95,0.90,0.85,0.80)\) for \(t=1,2,3,4\).
(a) Compute \(\bar\alpha_4\) (2 d.p.).   (b) For \(x_0=1.5\) and \(\epsilon=-0.8\), compute \(x_4\) (2 d.p.).
Reveal answer
(a) 0.58  ·  (b) 0.62
\(\bar\alpha_4=0.95\times0.90\times0.85\times0.80=0.5814\approx0.58\)
\(x_4=\sqrt{0.58}(1.5)+\sqrt{0.42}(-0.8)=1.14237-0.51846=0.6239\)
Rounding note worth knowing

Carrying the unrounded \(\bar\alpha_4=0.5814\) gives \(x_4=0.6261\to\) 0.63; the official answer 0.62 comes from feeding the rounded 0.58 forward. When a multi-part question tells you to round part (a) to 2 d.p., use that rounded value in part (b) — the key was built that way.

MCQ2025 T3 · Q653 marks
A DDPM is trained with \(T=1000\). If \(t=1\) has a very small \(\beta_1\) and \(t=T\) has a very large \(\beta_T\), what is the value of the total added noise component \(\sqrt{1-\bar\alpha_t}\) at \(t=1\) and \(t=T\)?
  • \(t=1\): close to 0;  \(t=T\): close to 1
  • \(t=1\): close to 0.5;  \(t=T\): close to 0.5
  • \(t=1\): close to 1;  \(t=T\): close to 0
  • \(t=1\): close to \(\beta_1\);  \(t=T\): close to \(\beta_T\)
Reveal answer
t = 1: close to 0  ·  t = T: close to 1

At \(t=1\), \(\bar\alpha_1=\alpha_1=1-\beta_1\approx1\) (since \(\beta_1=10^{-4}\)), so \(\sqrt{1-\bar\alpha_1}\approx0\) — barely any noise, \(x_1\approx x_0\). At \(t=T\), \(\bar\alpha_T\) is a product of 1000 numbers each \(<1\), so \(\bar\alpha_T\approx0\) and \(\sqrt{1-\bar\alpha_T}\approx1\) — pure noise, matching \(q(x_T)\approx\mathcal N(0,I)\). Option 4 confuses the per-step variance \(\beta_t\) with the cumulative one.

MSQPractice paper · Q8, Q9, Q11Concept cluster
Q8: A DDPM is a hierarchical VAE with latents \(x_1..x_T\). Which statements are correct?
  • The forward process is a fixed first-order Markov chain, so \(q(x_t|x_{t-1})\) has no learnable parameters
  • \(x_t\) can be sampled directly from \(x_0\) without simulating all previous steps
  • Each latent \(x_t\) has the same dimensionality as the data variable \(x_0\)
  • The ELBO requires both the forward and reverse processes to be learned
Q9: Which statements correctly contrast the DDPM forward process with a standard VAE encoder?
  • The DDPM forward process is fixed by the noise schedule, whereas the VAE encoder is learned
  • A DDPM contains a sequence of latent variables in a Markov chain, whereas a VAE uses a single latent variable
  • Both models necessarily map the data to a lower-dimensional latent space
  • After sufficiently many forward steps the DDPM latent \(x_T\) approaches \(\mathcal N(0,I)\) — a VAE's prior is fixed by design
Q11: Model A has \(\bar\alpha_t=0.9\) at some timestep; Model B has \(\bar\alpha_t=0.1\). Which statements are correct?
  • Model A retains a larger fraction of the original signal \(x_0\)
  • Model B retains a larger fraction of the original signal \(x_0\)
  • Model A has a larger noise contribution in its forward marginal
  • Model B has a larger noise contribution in its forward marginal
  • The latent \(x_t\) in Model B contains no information about \(x_0\)
Reveal answer
Q8: 1, 2, 3  ·  Q9: 1, 2, 4  ·  Q11: 1 and 4

Q8 is the defining triple plus the closed form; the false option contradicts the whole point — only the reverse process is learned.

Q9's false option is a good one: a VAE's \(k\ll d\) compression is typical but not required, and a DDPM explicitly does not compress at all — so "necessarily lower-dimensional" fails for both.

Q11's last option is the subtle trap. With \(\bar\alpha_t=0.1\) the signal coefficient is \(\sqrt{0.1}\approx0.32\) — small, but not zero. Information about \(x_0\) is heavily degraded, never fully erased at finite \(t\). "Approaches \(\mathcal N(0,I)\)" is a limit statement, not an equality.

11Structure

Diffusion II — the DDPM ELBO and its three terms

The derivation is long, but the examinable payload is short: the reverse model's definition and the three-term decomposition, with one term crossed out.

The model: a learned reverse chain

\(\displaystyle p_\theta(x_{0:T})=p(x_T)\prod_{t=1}^{T}p_\theta(x_{t-1}|x_t),\qquad p(x_T)=\mathcal N(0,I)\)
\(\displaystyle p_\theta(x_{t-1}|x_t)\;\triangleq\;\mathcal N\big(x_{t-1};\;\mu_\theta(x_t),\;\Sigma_\theta(x_t)\big)\) First-order Markov in the reverse direction, with Gaussian transitions carrying learnable parameters. \(\mu_\theta\) and \(\Sigma_\theta\) are learned by maximising the ELBO.
The DDPM ELBO \(\displaystyle J_\theta(q)=\mathbb E_{q(x_{1:T}|x_0)}\log\frac{p_\theta(x_{0:T})}{q(x_{1:T}|x_0)},\qquad q(x_{1:T}|x_0)=\prod_{t=1}^{T}q(x_t|x_{t-1})\)

The three-term decomposition

Peel off the \(t=1\) factor, rewrite \(q(x_t|x_{t-1})\) by Bayes as \(\frac{q(x_{t-1}|x_t,x_0)q(x_t|x_0)}{q(x_{t-1}|x_0)}\), telescope the resulting product, and apply the tower property of conditional expectation. The full chain of algebra is in §16; the result is:

★ Boxed result — know the three names \(\displaystyle J_\theta(q)=\underbrace{\mathbb E_{q(x_1|x_0)}\log p_\theta(x_0|x_1)}_{\textbf{(i) reconstruction}}\;-\;\underbrace{D_{KL}\big(q(x_T|x_0)\,\|\,p(x_T)\big)}_{\textbf{(ii) prior matching}}\)
\(\displaystyle \qquad\qquad-\;\sum_{t=2}^{T}\mathbb E_{q(x_t|x_0)}\Big[\underbrace{D_{KL}\big(q(x_{t-1}|x_t,x_0)\,\big\|\,p_\theta(x_{t-1}|x_t)\big)}_{\textbf{(iii) consistency / denoising matching}}\Big]\)
TermWhat it saysDepends on \(\theta\)?
(i) Reconstruction \((L_0)\)The last decoding step must reproduce \(x_0\) from \(x_1\)Yes
(ii) Prior matching \((L_T)\)The fully-noised latent must match \(\mathcal N(0,I)\)No — it drops out of the optimisation entirely. Both \(q(x_T|x_0)\) and \(p(x_T)\) are fixed.
(iii) Consistency \((L_{t-1})\)The learnable denoising step \(p_\theta(x_{t-1}|x_t)\) must match the known denoising step \(q(x_{t-1}|x_t,x_0)\)Yes — this is where all the training happens
Why \(q(x_{t-1}|x_t,x_0)\) and not \(q(x_{t-1}|x_t)\)?

Reversing the chain without knowing where you started is intractable — that is the hard problem the network exists to solve. But conditioned additionally on \(x_0\), the reverse step becomes an exactly computable Gaussian (next section). So the training signal is: "here is the answer a denoiser that could see the original would give — match it." \(x_0\) is available at training time and unavailable at sampling time, which is exactly why we need a network.

12★★★ 4-mark questions live here

Diffusion III — the tractable posterior & four equivalent objectives

The heaviest-marked diffusion questions (2025 T3 Q73 and Q75, 4 marks each) are pure recall of the formulas below, with distractors that differ by a single bar over an alpha.

The known posterior

By Bayes, \(q(x_{t-1}|x_t,x_0)=\frac{q(x_t|x_{t-1})\,q(x_{t-1}|x_0)}{q(x_t|x_0)}\). All three factors are known Gaussians, so the product of exponentials can be reorganised by completing the square:

★ Boxed result \(\displaystyle q(x_{t-1}|x_t,x_0)=\mathcal N\big(x_{t-1};\;\mu_q(x_t,x_0),\;\Sigma_q\big)\)
\(\displaystyle \mu_q(x_t,x_0)=\frac{\sqrt{\alpha_t}\,(1-\bar\alpha_{t-1})\,x_t\;+\;\sqrt{\bar\alpha_{t-1}}\,(1-\alpha_t)\,x_0}{1-\bar\alpha_t}\)
\(\displaystyle \Sigma_q=\sigma_q^2 I,\qquad \sigma_q^2=\frac{(1-\alpha_t)(1-\bar\alpha_{t-1})}{1-\bar\alpha_t}\;\;\Big(=\tilde\beta_t=\frac{1-\bar\alpha_{t-1}}{1-\bar\alpha_t}\beta_t\Big)\) Note \(\mu_q\) is a convex-style blend of \(x_t\) and \(x_0\), and \(\sigma_q^2\) depends only on the schedule — no data, no parameters. That is why the variance can simply be fixed.
⚠ Which index carries the bar

In \(\tilde\beta_t=\frac{1-\bar\alpha_{t-1}}{1-\bar\alpha_t}\beta_t\) the \(t-1\) is on top. Since \(\bar\alpha\) is decreasing, \(1-\bar\alpha_{t-1}<1-\bar\alpha_t\), so the fraction is less than 1 and \(\tilde\beta_t<\beta_t\) — knowing which way the posterior variance moves is a free way to check yourself. 2025 T3 Q75's distractor is the identical expression with the fraction inverted.

The consistency KL collapses to a regression

Design choice: set the model's covariance equal to the posterior's, \(p_\theta(x_{t-1}|x_t)=\mathcal N(x_{t-1};\mu_\theta,\Sigma_q)\). Then in the general two-Gaussian KL the log-determinant vanishes and \(\mathrm{tr}(I)=d\) cancels \(-d\):

\(\displaystyle D_{KL}\big(q(x_{t-1}|x_t,x_0)\,\|\,p_\theta(x_{t-1}|x_t)\big)=\frac{1}{2\sigma_q^2}\big\|\mu_\theta-\mu_q\big\|_2^2\) Training a diffusion model is regression onto \(\mu_q\). That is the whole thing.

The reconstruction term reduces the same way — \(\log p_\theta(x_0|x_1)\propto-\frac{\|x_0-\mu_\theta(x_1)\|_2^2}{2\sigma_q^2}\), the constant \(\frac d2\log 2\pi\sigma_q^2\) being \(\theta\)-independent — so the whole ELBO is a weighted sum of squared errors.

The four equivalent parameterizations

Rather than have the net output \(\mu_\theta\) directly, substitute a known algebraic identity and let it predict something else. All four give the same optimum; they differ only in what the U-Net's output head means and how the loss is weighted.

#Network predictsLossKey identity
1\(\hat x_\theta(x_t)\) — the clean data\(\|\hat x_\theta(x_t)-x_0\|_2^2\)\(\mu_\theta=c_1x_t+c_2\hat x_\theta\) with the same \(c_1,c_2\) as \(\mu_q\)
2\(\mu_\theta(x_t)\) — the posterior mean\(\|\mu_\theta-\mu_q\|_2^2\)the objective as originally derived
3\(\hat\epsilon_\theta(x_t)\)the added noise\(\|\epsilon_t-\hat\epsilon_\theta(x_t)\|_2^2\)\(x_0=\frac{x_t-\sqrt{1-\bar\alpha_t}\,\epsilon_t}{\sqrt{\bar\alpha_t}}\)
4\(s_\theta(x_t)\) — the score\(\|\nabla_{x_t}\log p(x_t)-s_\theta(x_t)\|_2^2\)Tweedie: \(\nabla_{x_t}\log p(x_t)=\frac{-\epsilon_t}{\sqrt{1-\bar\alpha_t}}\)

The lecture notes derive #1 as the primary form; the exam papers most often use #3, the familiar DDPM \(L_{\text{simple}}\). The bridge between them is:

\(\displaystyle \mu_q(x_t,\epsilon_t)=\frac{1}{\sqrt{\alpha_t}}x_t-\frac{1-\alpha_t}{\sqrt{1-\bar\alpha_t}\sqrt{\alpha_t}}\,\epsilon_t,\qquad \mu_\theta(x_t)=\frac{1}{\sqrt{\alpha_t}}x_t-\frac{1-\alpha_t}{\sqrt{1-\bar\alpha_t}\sqrt{\alpha_t}}\,\hat\epsilon_\theta(x_t)\)
\(\displaystyle \Rightarrow\;\frac{1}{2\sigma_q^2}\|\mu_\theta-\mu_q\|^2=\frac{1}{2\sigma_q^2}\cdot\frac{(1-\alpha_t)^2}{(1-\bar\alpha_t)\alpha_t}\big\|\epsilon_t-\hat\epsilon_\theta(x_t)\big\|_2^2\;\propto\;\big\|\epsilon_t-\hat\epsilon_\theta(x_t)\big\|_2^2\) The two means share the same \(x_t\) coefficient, so it cancels in the difference — leaving only the noise error, scaled by a constant. Drop the constant and you have \(L_{\text{simple}}\).
★ Score = negatively-scaled noise

Equating the \(x_0\) expressions from Tweedie's formula and the forward process gives

\(\displaystyle \nabla_{x_t}\log p(x_t)=\frac{-1}{\sqrt{1-\bar\alpha_t}}\;\epsilon_t\)

So a DDPM trained to regress the added noise is implicitly a score predictor. This is the one-line link between diffusion models and score-based generative modelling.

MCQ2025 T3 · Q734 marks
Given the noise prediction \(\varepsilon_\theta(x_t,t)\) and the noisy input \(x_t\), which expression correctly estimates the clean image \(\hat x_0\)?
  • \(\hat x_0=\sqrt{\alpha_t}\,x_t-\sqrt{1-\alpha_t}\,\varepsilon_\theta\)
  • \(\hat x_0=\sqrt{\bar\alpha_t}\,x_t-\sqrt{1-\bar\alpha_t}\,\varepsilon_\theta\)
  • \(\hat x_0=\dfrac{x_t-\sqrt{1-\alpha_t}\,\varepsilon_\theta}{\sqrt{\alpha_t}}\)
  • \(\hat x_0=\dfrac{x_t-\sqrt{1-\bar\alpha_t}\,\varepsilon_\theta}{\sqrt{\bar\alpha_t}}\)
Reveal answer
Option 4

Just rearrange the forward marginal \(x_t=\sqrt{\bar\alpha_t}x_0+\sqrt{1-\bar\alpha_t}\epsilon\) for \(x_0\): subtract the noise term, divide by \(\sqrt{\bar\alpha_t}\).

Options 3 and 4 differ only by the bars. The marginal from \(x_0\) is governed by the cumulative \(\bar\alpha_t\), never the per-step \(\alpha_t\) — an unbarred alpha in a formula relating \(x_t\) to \(x_0\) is always wrong. Options 1 and 2 fail a cruder check: they multiply by \(\sqrt{\bar\alpha_t}\) where the inversion must divide.

NAT2026 T1 · Q153 marks
At \(t=500\), with \(\bar\alpha_{500}=0.7\), noisy latent \(x_{500}=0.5\) and predicted noise \(\epsilon_\theta=-0.2\), calculate \(\hat x_0=\frac{1}{\sqrt{\bar\alpha_t}}\big(x_t-\sqrt{1-\bar\alpha_t}\,\epsilon_\theta\big)\).
Reveal answer
0.73
\(\sqrt{0.7}=0.83666,\qquad\sqrt{0.3}=0.54772\)
\(\hat x_0=\frac{1}{0.83666}\big(0.5-0.54772\times(-0.2)\big)=\frac{0.5+0.10954}{0.83666}=\frac{0.60954}{0.83666}=0.7285\)

The negative predicted noise is the whole trap. Subtracting a negative adds. Rushing gives \(\frac{0.5-0.1095}{0.8367}=0.467\), outside the accepted band.

MCQ2025 T3 · Q754 marks
For the variance-preserving forward process with \(\alpha_t=1-\beta_t\), \(\bar\alpha_t=\prod_s\alpha_s\), what are the correct posterior mean and variance of \(q(x_{t-1}|x_t,x_0)\)?
  • \(\mu_q=\frac{1}{\sqrt{\alpha_t}}\Big(x_t-\frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\frac{x_t-\sqrt{\bar\alpha_t}x_0}{\sqrt{1-\bar\alpha_t}}\Big),\quad \tilde\beta_t=\frac{1-\bar\alpha_{t-1}}{1-\bar\alpha_t}\beta_t\)
  • same \(\mu_q\), but \(\tilde\beta_t=\frac{1-\bar\alpha_t}{1-\bar\alpha_{t-1}}\beta_t\)
  • \(\mu_q=\sqrt{\alpha_t}\,x_t,\quad \tilde\beta_t=\beta_t\)
  • \(\mu_q=\sqrt{\bar\alpha_{t-1}}\,x_0,\quad \tilde\beta_t=(1-\bar\alpha_{t-1})\)
Reveal answer
Option 1

Options 3 and 4 are eliminated instantly — the posterior mean must depend on both \(x_t\) and \(x_0\), and both of those drop one. That leaves the \(\tilde\beta_t\) fraction as the only real decision.

Two independent ways to settle it: (i) match the derived form \(\sigma_q^2=\frac{(1-\alpha_t)(1-\bar\alpha_{t-1})}{1-\bar\alpha_t}\) and read off \(1-\alpha_t=\beta_t\); (ii) the sanity check — conditioning on more information (both \(x_t\) and \(x_0\)) must reduce uncertainty, so we need \(\tilde\beta_t<\beta_t\), which forces the smaller quantity \(1-\bar\alpha_{t-1}\) on top.

Note the mean here is written in the \(\epsilon\)-substituted form, since \(\frac{x_t-\sqrt{\bar\alpha_t}x_0}{\sqrt{1-\bar\alpha_t}}=\epsilon\) — algebraically identical to the blend-of-\(x_t\)-and-\(x_0\) form above.

NAT2025 T2 · Q1273 marks
\(L_{\text{simple}}=\mathbb E\big[\|\epsilon-\epsilon_\theta(x_t,t)\|^2\big]\). For a 1-D sample with \(x_0=1.5\), at \(t=400\) with \(\bar\alpha_{400}=0.75\), a noise sample \(\epsilon=0.5\) is drawn and the model predicts \(\epsilon_\theta=0.3\). What is the squared error loss for this instance?
Reveal answer
0.04
\(L=(\epsilon-\epsilon_\theta)^2=(0.5-0.3)^2=(0.2)^2=0.04\)

\(x_0=1.5\) and \(\bar\alpha_{400}=0.75\) are pure distractors. \(L_{\text{simple}}\) compares noise to predicted noise and nothing else; you never need to form \(x_t\). The examiner supplies enough information to compute \(x_t\) precisely to see whether you know that you don't have to. Recognising which given quantities are irrelevant is a recurring skill in this paper.

13Algorithms

Diffusion IV — training & sampling

Two short algorithms, and one conceptual point that has now been asked in three different disguises: sampling cannot be done in one shot.

Training

# alpha_1 .. alpha_T fixed in [0,1];  T ~ 1000
repeat until convergence:
    pick  t ~ Uniform{1..T}                     # random timestep, not a full chain
    eps ~ N(0, I)
    x_t = sqrt(abar[t]) * x_0 + sqrt(1 - abar[t]) * eps   # one-shot jump
    loss = || xhat_theta(x_t, t) - x_0 ||^2      # or || eps - eps_theta(x_t, t) ||^2
    theta <- theta - lr * grad_theta(loss)

The sum over a minibatch of sampled timesteps is the Monte-Carlo estimate of the outer expectation. Training is cheap precisely because the closed form lets you jump to a random \(t\) without simulating the chain.

The network

  • A U-Net — an hourglass that decreases then increases spatial dimension — used as a regressor, not a sampler. Input \((x_t,\,t)\); output \(\hat x_\theta(x_t)\) (or \(\hat\epsilon_\theta\)).
  • One network for all \(t\) — a design choice. It therefore needs to be told which noise level it is looking at, hence \(t\) as an input.
  • \(t\) is a scalar and would be insignificant if passed raw, so it is expanded into a vector by sinusoidal positional embedding:
\(\displaystyle \hat t(t)_i=\begin{cases}\sin(\omega_k t)&i=2k\\ \cos(\omega_k t)&i=2k+1\end{cases}\qquad \omega_k=\frac{1}{(10000)^{2k/d}}\)
★ A DDPM is not a sampler — it is a denoiser

Unlike a GAN generator or a VAE decoder, the trained network never outputs a novel data point. It outputs a regression estimate — the clean image (or the noise) at the current noise level. Novelty comes from the random \(x_T\) and the noise injected at each reverse step, not from the network.

Sampling (inference)

x_T ~ N(0, I)
for t = T, T-1, ..., 1:
    xhat_0 = xhat_theta(x_t, t)                 # one forward pass through the U-Net
    mu = ((1 - abar[t-1]) * sqrt(a[t]) / (1 - abar[t])) * x_t \
       + ((1 - a[t]) * sqrt(abar[t-1]) / (1 - abar[t])) * xhat_0
    z = N(0, I) if t > 1 else 0            # no noise on the final step
    x_{t-1} = mu + sigma_q * z
return x_0                                      # a new generated sample
⚠ Sampling costs \(T\) forward passes

One U-Net evaluation per timestep, roughly 1000 of them for a single image. This is why DDPM inference is much slower than training a single step — and it is the limitation that motivates DDIM.

MCQ2026 T1 · Q16  ·  2025 T3 · Q64  ·  Practice Q123 + 3 marks
2026 T1 Q16: Which statement correctly describes DDPM inference?
  • A new sample is generated in a single forward pass by feeding \(z\sim\mathcal N(0,I)\) into the trained U-Net
  • The model must iteratively traverse the reverse process \(x_T\to x_{T-1}\to\cdots\to x_0\), starting from \(x_T\sim\mathcal N(0,I)\)
  • Inference is faster than training because it requires no forward passes through the network
  • The U-Net predicts \(x_0\) directly from \(x_T\) without intermediate steps
2025 T3 Q64: Are the reverse steps in a DDPM usually stochastic (involving random noise at each step) rather than purely deterministic?
  • Correct
  • Incorrect
Reveal answer
2026 T1 Q16: option 2  ·  2025 T3 Q64: Correct (they are stochastic)

This concept has now been examined three times in three different forms — as a plain MCQ, as a true/false, and (Practice Q12) as a student's wrong claim to be refuted. Learn the refutation sentence:

"The U-Net's prediction is only valid at the current noise level, so sampling must traverse the reverse chain step by step." A single evaluation at \(t=T\) would give a blurry average over everything the model could produce — the iteration is what sharpens it. And each step adds fresh \(\sigma_q z\), so the trajectory is stochastic.

MCQ2025 T2 · Q1323 marks
During inference in a DDPM, why do we add noise \(z\) only when \(t>0\)?
  • At \(t=0\), we assume perfect reconstruction
  • At \(t=0\), adding noise will corrupt the final output
  • At \(t=0\), the posterior is deterministic
  • All of these
Reveal answer
All of these

The final step must return a clean sample, so injecting noise at the very end would only degrade it. Practically: in the sampling loop the noise term is gated as z = randn_like(x) if t > 1 else 0. When one option is "All of these" and the others are each individually defensible, it is almost always the answer.

MCQ2025 T3 · Q663 marks
Why is \(p_\theta(x_{t-1}|x_t)\) modelled as a Gaussian whose variance \(\Sigma_\theta\) is often fixed to a constant (e.g. \(\tilde\beta_t I\)), with only the mean predicted?
  • The variance is fixed because it is independent of the data, determined only by the time step \(t\)
  • Fixing the variance simplifies the ELBO such that the objective only needs to predict the noise, which implicitly predicts the mean
  • The variance is not fixed; the U-Net predicts both simultaneously
  • The variance is fixed because the U-Net is designed only to denoise, not to estimate uncertainty
Reveal answer
Option 2

Setting \(\Sigma_\theta=\Sigma_q\) makes the log-det and trace terms of the Gaussian KL cancel, collapsing it to \(\frac{1}{2\sigma_q^2}\|\mu_\theta-\mu_q\|^2\) — and from there to \(\|\epsilon-\hat\epsilon_\theta\|^2\). The simplification of the objective is the reason.

Option 1 is the near-miss and deserves attention: it is a true statement\(\sigma_q^2\) genuinely depends only on the schedule — but it describes a consequence, not the motivation. When a distractor is true but answers a different question, it is still wrong.

14★ Lower yield — learn last

Guidance, latent diffusion, DDIM

In the lecture notes, so fair game — but not yet examined in any of the three past papers. One-line formula and one-line meaning is the right depth here.

Tweedie's formula & the score view

\(\mathbb E[\mu_t|t]=t+\Sigma_t\nabla_t\log p(t)\)   (Tweedie)
\(\displaystyle \Rightarrow\;\sqrt{\bar\alpha_t}\,x_0=x_t+(1-\bar\alpha_t)\nabla_{x_t}\log p(x_t)\;\Rightarrow\;\nabla_{x_t}\log p(x_t)=\frac{-\epsilon_t}{\sqrt{1-\bar\alpha_t}}\) The score function \(\nabla_x\log p(x)\) points in the direction of steepest increase of log-density.

Conditional generation: two guidance methods

Classifier guidance \(\displaystyle \underbrace{\nabla_{x_t}\log p(x_t|y)}_{\text{conditional score}}=\underbrace{\nabla_{x_t}\log p(x_t)}_{\text{uncond. score (the DDPM)}}+\underbrace{\nabla_{x_t}\log p(y|x_t)}_{\text{classifier gradient}}\) From Bayes inside the log; \(\nabla_{x_t}\log p(y)=0\) because it has no \(x_t\). Needs a separately trained classifier that works at every noise level — which is the method's practical weakness.
Classifier-free guidance \(\displaystyle \nabla\log p(x_t|y)\;\approx\;\lambda\,\nabla\log p(x_t|y)+(1-\lambda)\,\nabla\log p(x_t)\) Train one network to do both jobs by randomly replacing \(y\) with a null label \(y=\varnothing\) during training. At inference, run it twice (with and without \(y\)) and blend with the guidance weight \(\lambda\). No classifier needed.

Latent diffusion (Stable Diffusion)

  • Diffusion in high dimension is expensive. So: pretrain an encoder–decoder (e.g. a VQ-VAE) to get \(z_0=E_{\phi^*}(x_0)\in\mathbb R^k\) with \(k\ll d\).
  • Run the whole DDPM on the latent space of \(z_0\).
  • To generate: reverse-diffuse to get \(z_{\text{novel}}\), then decode \(x_{\text{novel}}=D_{\theta^*}(z_{\text{novel}})\).

DDIM — motivation only

Two limitations of DDPM motivate it: (i) sampling is slow (\(T=1000\) forward passes) and (ii) it cannot do deterministic posterior inference — the forward process is stochastic, so the same \(x_0\) run twice yields different \(x_T\). The lecture notes stop here; the deterministic DDIM update is not derived and has not been examined.

15★★ 23 marks

PyTorch, shapes & parameter counts

Roughly one mark in six. These are not maths questions — they test tensor shapes, broadcasting idioms and layer arithmetic. All of them are memorisable in ten minutes.

Counting parameters in a linear layer

\(\texttt{nn.Linear(in, out)}\;\Rightarrow\;\underbrace{\text{in}\times\text{out}}_{\text{weights}}+\underbrace{\text{out}}_{\text{biases}}\)
NAT2025 T2 · Q128  &  2026 T1 · Q143 marks each
A VAE encoder for \(32\times32\times3\) images flattens the input, applies nn.Linear(3072, 400), a ReLU, then two parallel linear layers producing \(\mu\) and \(\log\sigma^2\), both mapping 400 features to a latent dimension of 20. How many trainable weight and bias parameters are in the layer producing \(\mu\)?
Reveal answer
8020  (graded exactly)
\(400\times20+20=8000+20=8020\)

Everything else in the stem is a distractor: \(32\times32\times3=3072\), the trunk layer, the ReLU (zero parameters) and the second head for \(\log\sigma^2\). Only the \(\mu\)-head is asked for. If you had counted both heads you would get 16040; the trunk alone is \(3072\times400+400=1{,}229{,}200\). Same question, same numbers, two papers.

Shapes and broadcasting

QuestionAnswerWhy
Shape of sampled \(z\) if \(\mu,\log\sigma^2\) are [batch, latent] 25T2 Q131[batch_size, latent_dim]\(z=\mu+\sigma\odot\epsilon\) is element-wise; shape is preserved, no dimension is added
Shape of the timestep tensor t for one image 26T1 Q13[1]t is batch-shaped: one integer per item in the batch, so [B] and here \(B=1\)
Injecting a [B, C] time embedding into a [B, C, H, W] feature map 25T2 Q135Reshape to [B, C, 1, 1] and addBroadcasting expands the trailing 1s across \(H,W\) — the same offset for every spatial location. Not concatenation, not multiplication
β-VAE loss line 25T2 Q133loss = recon_loss + beta * kl_divβ multiplies the KL only
MCQ2025 T2 · Q134  &  2026 T1 · Q173 marks each
In a DDPM implementation with batched time indices:
betas = torch.linspace(1e-4, 0.02, T)
alpha_cumprod = torch.cumprod(1 - betas, dim=0)
sqrt_alpha_cumprod = torch.sqrt(alpha_cumprod)
x_t = sqrt_alpha_cumprod[t] * x_0 + ...
Assuming t is a [batch_size] tensor, what is the most memory-efficient and correct way to index sqrt_alpha_cumprod?
  • sqrt_alpha_cumprod.gather(0, t)
  • sqrt_alpha_cumprod[t].view(-1, 1, 1, 1)
  • sqrt_alpha_cumprod[t][:, None, None, None]
  • F.embedding(t, sqrt_alpha_cumprod)
Reveal answer
sqrt_alpha_cumprod[t][:, None, None, None]

Indexing a 1-D schedule with a [B] tensor gives a [B] result, which must become [B,1,1,1] to broadcast against [B,C,H,W]. [:, None, None, None] adds those three axes as a view — no data copied.

  • Option 1gather returns the right values but stays [B]: no broadcast axes, so the multiply against 4-D fails.
  • Option 4F.embedding expects a 2-D weight matrix, not a 1-D buffer.
  • Option 2 is the interesting one, and the two papers word it differently. 2026 T1 writes .view(-1, 0, 1, 1), which is plainly invalid — a zero-sized dimension. 2025 T2 writes .view(-1, 1, 1, 1), which is functionally correct; there it loses only on the "most memory-efficient" tiebreak. If you see the 0 variant, eliminate it instantly.
The two rules that answer most shape questions
  1. 1.Per-sample scalars are [B] — timesteps, schedule coefficients, class labels. To use one against [B,C,H,W] data, append trailing singleton axes.
  2. 2.Element-wise operations preserve shape. Reparameterization, adding noise, and applying a schedule coefficient never change the tensor's rank.
16Proof timelines

Worked derivations

The four derivations the course actually builds. You are rarely asked to reproduce them, but every conceptual MCQ is a checkpoint on one of these roads — and knowing why each step is legal is what kills the distractors.

Derivation 1 — the ELBO, and exactly what it loses

Prove \(\log p_\theta(x)=J_\theta(q)+D_{KL}\big(q(z|x)\,\|\,p_\theta(z|x)\big)\;\ge\;J_\theta(q)\)
  1. \(\displaystyle \ell(\theta)=\log p_\theta(x)=\log\int_z p_\theta(x,z)\,dz\)
    Start from the evidence. The integral over the latent is what makes this intractable — it sits inside a log.
  2. \(\displaystyle =\log\int_z p_\theta(x,z)\frac{q(z|x)}{q(z|x)}\,dz=\log\int_z q(z|x)\frac{p_\theta(x,z)}{q(z|x)}\,dz\)
    Multiply and divide by an arbitrary density \(q(z|x)\) over \(z\). Legal for any \(q\) with matching support — nothing has been assumed about it yet.
  3. \(\displaystyle =\log\;\mathbb E_{q(z|x)}\!\left[\frac{p_\theta(x,z)}{q(z|x)}\right]\)
    Because \(\int q(z|x)g(z)dz=\mathbb E_{q(z|x)}g(z)\). The point of step 2: an integral against a density is an expectation, and expectations can be estimated from samples.
  4. \(\displaystyle \ge\;\mathbb E_{q(z|x)}\log\frac{p_\theta(x,z)}{q(z|x)}\;\equiv\;J_\theta(q)\)
    Jensen's inequality: \(\log\) is concave, so \(\log\mathbb E[\cdot]\ge\mathbb E\log[\cdot]\). This swaps the log and the expectation — the move that makes the objective computable.
  5. \(\displaystyle J_\theta(q)=\mathbb E_{q}\log\frac{p_\theta(z|x)\,p_\theta(x)}{q(z|x)}=\log p_\theta(x)-\mathbb E_{q}\log\frac{q(z|x)}{p_\theta(z|x)}\)
    Now factor the joint the other way, as \(p_\theta(x,z)=p_\theta(z|x)p_\theta(x)\). The \(\log p_\theta(x)\) has no \(z\) in it, so it comes straight out of the expectation.
  6. \(\displaystyle \log p_\theta(x)=J_\theta(q)+D_{KL}\big(q(z|x)\,\|\,p_\theta(z|x)\big)\)
    The leftover expectation is a KL divergence, hence \(\ge0\). So the gap between evidence and ELBO is exactly the KL between your variational posterior and the true one. Tight iff \(q=p_\theta(z|x)\) — which is why the EM E-step sets precisely that, and why a VAE, unable to compute the true posterior, settles for a learned approximation.

Derivation 2 — the DDPM closed form \(q(x_t|x_0)\)

Prove \(x_t=\sqrt{\bar\alpha_t}\,x_0+\sqrt{1-\bar\alpha_t}\,\epsilon,\quad \bar\alpha_t=\prod_{s=1}^{t}\alpha_s\)
  1. \(x_t=\sqrt{\alpha_t}\,x_{t-1}+\sqrt{1-\alpha_t}\,\epsilon_{t-1},\qquad \epsilon_{t-1}\sim\mathcal N(0,I)\)
    The one-step forward recursion. Everything follows from unrolling it.
  2. \(=\sqrt{\alpha_t}\big(\sqrt{\alpha_{t-1}}x_{t-2}+\sqrt{1-\alpha_{t-1}}\,\epsilon_{t-2}\big)+\sqrt{1-\alpha_t}\,\epsilon_{t-1}\)
    Substitute the same recursion one level down.
  3. \(=\sqrt{\alpha_t\alpha_{t-1}}\;x_{t-2}+\underbrace{\sqrt{\alpha_t-\alpha_t\alpha_{t-1}}\,\epsilon_{t-2}+\sqrt{1-\alpha_t}\,\epsilon_{t-1}}_{T_2}\)
    Expand and group. The signal coefficient has become a product of the \(\sqrt{\alpha}\)'s; the two noise terms are collected into \(T_2\).
  4. \(T_2=a\epsilon_1+b\epsilon_2\;\sim\;\mathcal N\big(0,(a^2+b^2)I\big)\)  with  \(a^2+b^2=(\alpha_t-\alpha_t\alpha_{t-1})+(1-\alpha_t)=1-\alpha_t\alpha_{t-1}\)
    The key merge. A linear combination of independent Gaussians is Gaussian, with variances adding. Two noise draws become one. Note how neatly \(-\alpha_t\) and \(+\alpha_t\) cancel.
  5. \(x_t=\sqrt{\alpha_t\alpha_{t-1}}\;x_{t-2}+\sqrt{1-\alpha_t\alpha_{t-1}}\;\epsilon^*\)
    Same shape as step 1, with \(\alpha_t\) replaced by the product \(\alpha_t\alpha_{t-1}\). So the pattern is self-similar and the recursion can be run all the way down to \(x_0\).
  6. \(x_t=\sqrt{\bar\alpha_t}\,x_0+\sqrt{1-\bar\alpha_t}\,\epsilon\quad\Longleftrightarrow\quad q(x_t|x_0)=\mathcal N\big(\sqrt{\bar\alpha_t}x_0,(1-\bar\alpha_t)I\big)\)
    Any latent is reachable from the data in one step. This is what makes DDPM training tractable — and it is the formula the exam plugs numbers into more than any other.

Derivation 3 — the three-term DDPM ELBO

Prove \(J_\theta(q)=\mathbb E\log p_\theta(x_0|x_1)-D_{KL}\big(q(x_T|x_0)\|p(x_T)\big)-\sum_{t=2}^{T}\mathbb E\,D_{KL}\big(q(x_{t-1}|x_t,x_0)\|p_\theta(x_{t-1}|x_t)\big)\)
  1. \(\displaystyle J_\theta(q)=\mathbb E_q\log\frac{p(x_T)\prod_{t=1}^{T}p_\theta(x_{t-1}|x_t)}{\prod_{t=1}^{T}q(x_t|x_{t-1})}\)
    Substitute both Markov factorizations. \(q(x_{1:T}|x_0)=\prod_t q(x_t|x_{t-1})\) holds because the forward process is first-order Markov — the chain rule's conditioning on all earlier states collapses to the immediate predecessor.
  2. \(\displaystyle =\log\frac{p(x_T)p_\theta(x_0|x_1)}{q(x_1|x_0)}+\log\prod_{t=2}^{T}\frac{p_\theta(x_{t-1}|x_t)}{q(x_t|x_{t-1})}\)
    Peel off the \(t=1\) factor from both products. It has to be handled separately because it is the only one touching the data \(x_0\).
  3. \(\displaystyle q(x_t|x_{t-1})=q(x_t|x_{t-1},x_0)=\frac{q(x_{t-1}|x_t,x_0)\,q(x_t|x_0)}{q(x_{t-1}|x_0)}\)
    The pivotal move. First add \(x_0\) to the conditioning for free (Markov), then apply Bayes with three variables. The numerator and denominator of step 2 were running in opposite directions; this rewrites \(q\) in the reverse direction so the two can be compared.
  4. \(\displaystyle \Rightarrow\;\log\prod_{t=2}^{T}\frac{p_\theta(x_{t-1}|x_t)}{q(x_{t-1}|x_t,x_0)}\;+\;\log\prod_{t=2}^{T}\frac{q(x_{t-1}|x_0)}{q(x_t|x_0)}\)
    Split the rewritten ratio into two products.
  5. \(\displaystyle \log\prod_{t=2}^{T}\frac{q(x_{t-1}|x_0)}{q(x_t|x_0)}=\log\frac{q(x_1|x_0)}{q(x_T|x_0)}\)
    Telescoping. Every intermediate factor \(q(x_2|x_0),\dots,q(x_{T-1}|x_0)\) appears once on top and once below and cancels. The surviving \(q(x_1|x_0)\) then cancels the denominator left over from step 2.
  6. \(\displaystyle =\log p_\theta(x_0|x_1)+\log\frac{p(x_T)}{q(x_T|x_0)}+\sum_{t=2}^{T}\log\frac{p_\theta(x_{t-1}|x_t)}{q(x_{t-1}|x_t,x_0)}\)
    Three clean pieces. Now reinstate the outer expectation.
  7. \(\displaystyle \mathbb E_{q(x_{t-1},x_t|x_0)}(\cdot)=\mathbb E_{q(x_t|x_0)}\,\mathbb E_{q(x_{t-1}|x_t,x_0)}(\cdot)\)
    Each piece depends only on a subset of \(x_{1:T}\), so the big expectation marginalises down to just those variables. Then the tower property splits the joint expectation into nested ones — which is what exposes the inner expectation as a KL.
  8. \(\displaystyle J_\theta(q)=\underbrace{\mathbb E\log p_\theta(x_0|x_1)}_{\text{reconstruction}}-\underbrace{D_{KL}\big(q(x_T|x_0)\|p(x_T)\big)}_{\text{prior matching, no }\theta}-\sum_{t=2}^{T}\underbrace{\mathbb E\,D_{KL}\big(q(x_{t-1}|x_t,x_0)\|p_\theta(x_{t-1}|x_t)\big)}_{\text{consistency}}\)
    Each \(\mathbb E_q\log\frac{p}{q}\) is minus a KL. The middle term contains no \(\theta\) — both distributions are fixed — so it is dropped from the optimisation, leaving reconstruction + consistency.

Derivation 4 — the consistency KL is a plain L2 regression

Prove \(D_{KL}\big(\mathcal N(\mu_q,\Sigma_q)\,\|\,\mathcal N(\mu_\theta,\Sigma_q)\big)=\frac{1}{2\sigma_q^2}\|\mu_\theta-\mu_q\|_2^2\)
  1. \(\displaystyle D_{KL}=\tfrac12\Big[\log\tfrac{|\Sigma_2|}{|\Sigma_1|}-d+\mathrm{tr}(\Sigma_2^{-1}\Sigma_1)+(\mu_2-\mu_1)^T\Sigma_2^{-1}(\mu_2-\mu_1)\Big]\)
    The general Gaussian–Gaussian KL in \(d\) dimensions.
  2. \(\Sigma_1=\Sigma_2=\Sigma_q=\sigma_q^2 I\;\Rightarrow\;\log\tfrac{|\Sigma_q|}{|\Sigma_q|}=0,\qquad \mathrm{tr}(\Sigma_q^{-1}\Sigma_q)=\mathrm{tr}(I)=d\)
    The design choice: give the model the same covariance as the true posterior. The log-determinant term vanishes and the trace term becomes exactly \(d\).
  3. \(\displaystyle D_{KL}=\tfrac12\big[\underbrace{0}_{\log}\;\underbrace{-\,d+d}_{\text{cancel}}\;+\;(\mu_\theta-\mu_q)^T(\sigma_q^2 I)^{-1}(\mu_\theta-\mu_q)\big]\)
    \(-d\) cancels against \(\mathrm{tr}(I)=d\). Only the quadratic form survives.
  4. \(\displaystyle D_{KL}=\frac{1}{2\sigma_q^2}\big\|\mu_\theta-\mu_q\big\|_2^2\)
    \((\sigma_q^2I)^{-1}=\frac{1}{\sigma_q^2}I\) pulls out as a scalar. A KL between distributions has become squared error between two vectors — the entire reason a diffusion model trains with plain MSE. Substituting \(\mu_\theta,\mu_q\) in terms of \(\hat x_\theta\) or \(\hat\epsilon_\theta\) then gives the other three equivalent objectives.
17Final hour

Formula cheat sheet

Everything you might need to plug numbers into, on one screen.

ELBO (universal)
\(J_\theta(q)=\mathbb E_{q(z|x)}\log\frac{p_\theta(x,z)}{q(z|x)}\)

Discrete latent: just a weighted sum

ELBO (two-term)
\(\mathbb E_q\log p_\theta(x|z)-D_{KL}(q\|p(z))\)

reconstruction − regularisation

Evidence = ELBO + gap
\(\log p_\theta(x)=J_\theta(q)+D_{KL}(q\|p_\theta(z|x))\)

smaller gap ⇒ tighter ELBO

GMM responsibility
\(\gamma_{nk}=\frac{\pi_k\mathcal N(x_n;\mu_k,\Sigma_k)}{\sum_j\pi_j\mathcal N(x_n;\mu_j,\Sigma_j)}\)

E-step; equals the true posterior

GMM M-step
\(\pi_k'=\frac1N\sum_n\gamma_{nk}\)
\(\mu_k'=\frac{\sum_n\gamma_{nk}x_n}{\sum_n\gamma_{nk}}\)

denominator is \(N_k\), not \(N\)

Reparameterization
\(z=\mu+\sigma\odot\epsilon,\;\;\epsilon\sim\mathcal N(0,I)\)
\(\sigma=e^{\frac12\log\sigma^2}\)

halve the log-variance, then exp

KL to \(\mathcal N(0,I)\)
\(\tfrac12\sum_j(\sigma_j^2+\mu_j^2-1-\log\sigma_j^2)\)

var + mean² − 1 − log-var

Equal-variance Gaussian KL
\(\frac{\|\mu_1-\mu_2\|_2^2}{2\sigma^2}\)

quadratic: half the error ⇒ quarter the KL

\(\beta\)-VAE
\(J_\beta=\mathbb E_q\log p_\theta(x|z)-\beta D_{KL}\)
\(\mathcal L_\beta=\mathcal L_{\text{recon}}+\beta\mathcal L_{KL}\)

maximise (negative) vs minimise (positive)

VQ-VAE
\(j^*=\arg\min_j\|z_e(x)-z_j\|_2^2\)
\(\text{bits}=H\!\cdot\!W\!\cdot\!\log_2 K\)

codebook dim \(D\) is irrelevant to the payload

DDPM forward step
\(q(x_t|x_{t-1})=\mathcal N(\sqrt{\alpha_t}x_{t-1},(1-\alpha_t)I)\)

\(\alpha_t=1-\beta_t\)

DDPM forward marginal ★
\(x_t=\sqrt{\bar\alpha_t}x_0+\sqrt{1-\bar\alpha_t}\epsilon\)
\(\bar\alpha_t=\prod_{s=1}^{t}\alpha_s\)

signal \(\sqrt{\bar\alpha_t}\), noise \(\sqrt{1-\bar\alpha_t}\)

Invert for \(\hat x_0\)
\(\hat x_0=\frac{x_t-\sqrt{1-\bar\alpha_t}\,\epsilon_\theta}{\sqrt{\bar\alpha_t}}\)

bars on both alphas

DDPM posterior
\(\mu_q=\frac{\sqrt{\alpha_t}(1-\bar\alpha_{t-1})x_t+\sqrt{\bar\alpha_{t-1}}(1-\alpha_t)x_0}{1-\bar\alpha_t}\)

a blend of \(x_t\) and \(x_0\)

Posterior variance
\(\sigma_q^2=\tilde\beta_t=\frac{(1-\alpha_t)(1-\bar\alpha_{t-1})}{1-\bar\alpha_t}\)

\(t-1\) on top; \(\tilde\beta_t<\beta_t\)

DDPM ELBO
\(L_0-\underbrace{L_T}_{\text{no }\theta}-\sum_{t=2}^{T}L_{t-1}\)

recon − prior-match − consistency

Consistency term
\(\frac{1}{2\sigma_q^2}\|\mu_\theta-\mu_q\|_2^2\propto\|\epsilon-\hat\epsilon_\theta\|_2^2\)

regression on \(\mu_q\) / on the noise

Score
\(\nabla_{x_t}\log p(x_t)=\frac{-\epsilon_t}{\sqrt{1-\bar\alpha_t}}\)

score = negatively-scaled noise

Reverse sampling step
\(x_{t-1}=\mu_{\theta^*}(x_t)+\sigma_q\,\epsilon\)

no noise at the last step; \(T\) passes total

Time embedding
\(\sin(\omega_k t),\cos(\omega_k t)\), \(\omega_k=10000^{-2k/d}\)

injected as [B,C,1,1] and added

Linear layer params
\(\text{in}\times\text{out}+\text{out}\)

ReLU has zero parameters

Classifier guidance
\(\nabla\log p(x_t|y)=\nabla\log p(x_t)+\nabla\log p(y|x_t)\)

CFG: \(\lambda\,\nabla\log p(x_t|y)+(1-\lambda)\nabla\log p(x_t)\)

18Night before

Night-before recall list

Say each one out loud. If any makes you hesitate, jump back to its section.

  • Minimising forward KL \(\equiv\) MLE (entropy term drops)
  • ELBO from Jensen: \(\log\mathbb E\ge\mathbb E\log\)
  • evidence = ELBO + KL gap; tight iff \(q=p_\theta(z|x)\)
  • ELBO = reconstruction − KL(\(q\|p(z)\))
  • GMM responsibility formula, blind
  • M-step: \(\mu_k'\) divides by \(\sum_n\gamma_{nk}\), not \(N\)
  • EM: lower bound, monotone, local optimum only
  • \(\alpha_j\ne P(z{=}j|x)\) at convergence
  • EM fails when \(p_\theta(z|x)\) is uncomputable → VAE
  • Encoder/decoder output parameters, not samples
  • No direct wire between encoder and decoder
  • Why \(\nabla_\phi\mathbb E\) fails: second term isn't an expectation
  • Reparameterization: \(z=\mu+\sigma\odot\epsilon\), \(\sigma=e^{\frac12\log\sigma^2}\)
  • LOTUS & the Monte-Carlo gradient estimator
  • Diagonal-Gaussian KL, all four terms in order
  • KL = 0 when \(\mu=0,\log\sigma^2=0\)
  • \(\nabla_\theta D_{KL}=0\) — decoder never sees the KL
  • Gaussian decoder with \(I\) covariance ⇒ squared-error recon
  • Generation samples \(z\) from the prior, not the encoder
  • Posterior collapse: per-dimension KL ≈ 0 = dead latent
  • High \(\beta\): disentangled, lower \(I(x;z)\), worse reconstruction
  • \(\beta\) multiplies the KL only — never the recon term
  • Maximise form negative, minimise form positive
  • VQ-VAE drops the reparameterization trick and the KL
  • VQ-VAE bits \(=H\!\cdot\!W\!\cdot\!\log_2 K\); \(D\) irrelevant
  • VQ-VAE sampling needs a second model (e.g. GMM) over \(z_q\)
  • DDPM triple: many latents · same dim · fixed encoder
  • \(x_t=\sqrt{\bar\alpha_t}x_0+\sqrt{1-\bar\alpha_t}\epsilon\) — write it blind
  • \(\bar\alpha_t\) is a product, so it decreases with \(t\)
  • Early \(t\): noise ≈ 0. Late \(t\): noise ≈ 1
  • Three ELBO terms; the prior-matching one has no \(\theta\)
  • Why condition on \(x_0\): it makes the reverse step Gaussian
  • \(\mu_q\) blends \(x_t\) and \(x_0\); \(\tilde\beta_t\) has \(t-1\) on top
  • Same \(\Sigma\) ⇒ KL collapses to \(\frac{1}{2\sigma_q^2}\|\mu_\theta-\mu_q\|^2\)
  • Four equivalent targets: \(x_0\), \(\mu\), \(\epsilon\), score
  • \(L_{\text{simple}}=\|\epsilon-\epsilon_\theta\|^2\)\(x_0,\bar\alpha_t\) are distractors
  • Invert to \(\hat x_0\): bars on both alphas, divide by \(\sqrt{\bar\alpha_t}\)
  • Sampling = \(T\) forward passes, stochastic, never one-shot
  • No noise injected at the final step
  • Variance fixed ⇒ simplifies the ELBO (that's the reason)
  • U-Net is a regressor / denoiser, not a sampler
  • Timestep tensor is [B]; broadcast with [:,None,None,None]
  • nn.Linear params \(=\) in×out + out
  • Guidance: conditional score = uncond. score + classifier gradient
★ One-line summary of the whole quiz

You cannot maximise \(\log p_\theta(x)\) when a latent hides inside an integral — so maximise the ELBO instead. If the posterior is computable, that's EM. If it isn't, learn an approximation with an encoder and make it differentiable with reparameterization — that's the VAE. If instead you fix the encoder to a slow noising chain, the ELBO collapses into plain L2 regression — that's the DDPM. One inequality, three algorithms. Every question is a checkpoint on that one road.

Exam-day checklist
  1. 1.Answer everything. No negative marking, in any paper.
  2. 2.Before any \(\beta\)-VAE or ELBO arithmetic, circle the word "minimise" or "ELBO" in the stem and fix your sign.
  3. 3.Before any reparameterization or KL, write down whether you were given \(\sigma\), \(\sigma^2\), or \(\log\sigma^2\).
  4. 4.Check every alpha for a bar. Anything relating \(x_t\) to \(x_0\) uses \(\bar\alpha_t\); anything relating \(x_t\) to \(x_{t-1}\) uses \(\alpha_t\).
  5. 5.Scan for distractor givens. Several questions hand you numbers you must recognise as irrelevant.
  6. 6.Double-check exact-graded integers — parameter counts, bit budgets, rounded ELBOs. No tolerance band there.

GenAI Quiz 2 Study Guide. Built from the Week 5–8 lecture notes, the instructor's handwritten note sets (Parts 4–7: VAE 1–2 and Diffusion 1–2), the official Week 5–8 practice paper, and the three past papers (2025 T2, 2025 T3, 2026 T1). Every example question is reproduced from those sources with its official answer key. Where a paper's key appeared inconsistent with the taught formula — the 2026 T1 Q8 KL value, and the rounding in practice Q7(b) — the discrepancy is flagged so you trust the method rather than a possibly-typo'd number. Good luck.

© 2026 Pulkit Mangal  ·  github.com/PulkitMangal09  ·  All study guides