Level 5 — MasteryGenerative Models

Generative Models

90 minutes60 marksprintable — key stays hidden on paper

Time limit: 90 minutes Total marks: 60 Instructions: Answer all three questions. Full derivations required. You may use log\log base ee throughout.


Question 1 — The ELBO, the KL term, and the reparameterization trick (22 marks)

Let xx be an observed datapoint and zRdz\in\mathbb{R}^d a latent variable with prior p(z)=N(0,I)p(z)=\mathcal{N}(0,I). A VAE uses an approximate posterior qϕ(zx)=N(μϕ(x),diag(σϕ2(x)))q_\phi(z\mid x)=\mathcal{N}(\mu_\phi(x),\,\mathrm{diag}(\sigma_\phi^2(x))) and decoder pθ(xz)p_\theta(x\mid z).

(a) Starting from logpθ(x)\log p_\theta(x), derive the evidence lower bound (ELBO) and prove that logpθ(x)ELBO(x)=DKL ⁣(qϕ(zx)pθ(zx))0.\log p_\theta(x) - \mathrm{ELBO}(x) = D_{\mathrm{KL}}\!\big(q_\phi(z\mid x)\,\|\,p_\theta(z\mid x)\big)\ge 0. State precisely why this justifies maximizing the ELBO. (6)

(b) For diagonal-Gaussian qq and standard-normal prior, prove the closed form DKL(N(μ,diag(σ2))N(0,I))=12j=1d(μj2+σj2lnσj21).D_{\mathrm{KL}}\big(\mathcal{N}(\mu,\mathrm{diag}(\sigma^2))\,\|\,\mathcal{N}(0,I)\big)=\tfrac12\sum_{j=1}^{d}\big(\mu_j^2+\sigma_j^2-\ln\sigma_j^2-1\big). (6)

(c) Explain the reparameterization trick: write z=μϕ(x)+σϕ(x)ϵz=\mu_\phi(x)+\sigma_\phi(x)\odot\epsilon, ϵN(0,I)\epsilon\sim\mathcal N(0,I). Prove that for a differentiable ff, ϕEqϕ(zx)[f(z)]=EϵN(0,I)[ϕf(μϕ+σϕϵ)],\nabla_\phi\,\mathbb{E}_{q_\phi(z\mid x)}[f(z)] = \mathbb{E}_{\epsilon\sim\mathcal N(0,I)}\big[\nabla_\phi f(\mu_\phi+\sigma_\phi\odot\epsilon)\big], and explain why the naive score-function (REINFORCE) estimator has higher variance in practice. (6)

(d) A student sets σj0\sigma_j\to 0 for all jj. Using the result of (b), state numerically what happens to the KL term as σj0\sigma_j\to0 with μj\mu_j fixed, and explain the training consequence. (4)


Question 2 — GANs vs WGAN: optimal discriminator and the Earth-Mover objective (20 marks)

Let prp_r be the real data distribution and pgp_g the generator distribution.

(a) For the original (non-saturating not required) GAN value function V(D,G)=Expr[logD(x)]+Expg[log(1D(x))],V(D,G)=\mathbb{E}_{x\sim p_r}[\log D(x)]+\mathbb{E}_{x\sim p_g}[\log(1-D(x))], prove that the optimal discriminator is D(x)=pr(x)pr(x)+pg(x)D^*(x)=\dfrac{p_r(x)}{p_r(x)+p_g(x)}, and show that substituting DD^* yields V(D,G)=2DJS(prpg)2log2.V(D^*,G)=2\,D_{\mathrm{JS}}(p_r\|p_g)-2\log 2. (8)

(b) Explain, using the result of (a), why vanishing gradients arise when prp_r and pgp_g have disjoint supports, and how this connects to mode collapse. (4)

(c) The Wasserstein GAN replaces the objective with W(pr,pg)=supfL1Epr[f]Epg[f]W(p_r,p_g)=\sup_{\|f\|_L\le1}\mathbb{E}_{p_r}[f]-\mathbb{E}_{p_g}[f] (Kantorovich–Rubinstein). For two 1-D point masses pr=δ0p_r=\delta_0, pg=δθp_g=\delta_\theta, compute W(pr,pg)W(p_r,p_g) and DJS(prpg)D_{\mathrm{JS}}(p_r\|p_g) as functions of θ\theta, and use the two to argue why WGAN gives usable gradients where standard GAN does not. (5)

(d) State the role of the Lipschitz constraint and name one method used to enforce it. (3)


Question 3 — DDPM forward/reverse process and the training objective (18 marks)

The DDPM forward process is q(xtxt1)=N(1βtxt1,βtI)q(x_t\mid x_{t-1})=\mathcal N\big(\sqrt{1-\beta_t}\,x_{t-1},\,\beta_t I\big) with schedule {βt}t=1T\{\beta_t\}_{t=1}^T. Define αt=1βt\alpha_t=1-\beta_t and αˉt=s=1tαs\bar\alpha_t=\prod_{s=1}^t\alpha_s.

(a) Prove the closed-form marginal q(xtx0)=N(αˉtx0,(1αˉt)I),q(x_t\mid x_0)=\mathcal N\big(\sqrt{\bar\alpha_t}\,x_0,\,(1-\bar\alpha_t)I\big), by induction (or direct composition of Gaussians). (6)

(b) Using (a), write xt=αˉtx0+1αˉtϵx_t=\sqrt{\bar\alpha_t}\,x_0+\sqrt{1-\bar\alpha_t}\,\epsilon, ϵN(0,I)\epsilon\sim\mathcal N(0,I). Given the simplified DDPM loss Lsimple=Et,x0,ϵ[ϵϵθ(xt,t)2]L_{\text{simple}}=\mathbb{E}_{t,x_0,\epsilon}\big[\|\epsilon-\epsilon_\theta(x_t,t)\|^2\big], explain what ϵθ\epsilon_\theta predicts and why this is equivalent (up to weighting) to learning the score xtlogq(xtx0)\nabla_{x_t}\log q(x_t\mid x_0). (6)

(c) With a linear schedule βt\beta_t from β1=104\beta_1=10^{-4} to βT=0.02\beta_T=0.02, T=1000T=1000: qualitatively describe αˉt\bar\alpha_t from t=0t=0 to TT, and explain why αˉT0\bar\alpha_T\approx 0 is required for sampling to start from pure noise. Compute numerically αˉt\bar\alpha_t for a 3-step toy schedule β=(0.1,0.2,0.3)\beta=(0.1,0.2,0.3). (6)

Answer keyMark scheme & solutions

Question 1

(a) (6) Start with the identity for any qϕq_\phi: logpθ(x)=Eqϕ(zx)[logpθ(x,z)qϕ(zx)]+Eqϕ(zx)[logqϕ(zx)pθ(zx)].\log p_\theta(x)=\mathbb{E}_{q_\phi(z\mid x)}\Big[\log\frac{p_\theta(x,z)}{q_\phi(z\mid x)}\Big]+\mathbb{E}_{q_\phi(z\mid x)}\Big[\log\frac{q_\phi(z\mid x)}{p_\theta(z\mid x)}\Big]. (2 — inserting qq and using p(x,z)=p(zx)p(x)p(x,z)=p(z|x)p(x)) The first term is the ELBO; the second is DKL(qϕ(zx)pθ(zx))D_{\mathrm{KL}}(q_\phi(z|x)\|p_\theta(z|x)). (2) Since logpθ(x)\log p_\theta(x) is constant in the expectation (does not depend on zz), the identity holds; KL 0\ge0 by Gibbs/Jensen, so ELBO logpθ(x)\le\log p_\theta(x). (1) Maximizing ELBO over θ,ϕ\theta,\phi simultaneously (i) tightens the bound on the true log-likelihood and (ii) drives qϕq_\phi toward the true posterior — hence valid surrogate objective. (1)

(b) (6) KL between two dd-dim Gaussians N(μ,Σ1)\mathcal N(\mu,\Sigma_1) and N(0,I)\mathcal N(0,I): DKL=12[tr(Σ1)+μμdlndetΣ1].D_{KL}=\tfrac12\Big[\mathrm{tr}(\Sigma_1)+\mu^\top\mu-d-\ln\det\Sigma_1\Big]. (3 — general Gaussian KL) With Σ1=diag(σj2)\Sigma_1=\mathrm{diag}(\sigma_j^2): tr=σj2\mathrm{tr}=\sum\sigma_j^2, μμ=μj2\mu^\top\mu=\sum\mu_j^2, lndet=lnσj2\ln\det=\sum\ln\sigma_j^2, d=1d=\sum 1. (2) DKL=12j(μj2+σj2lnσj21).\Rightarrow D_{KL}=\tfrac12\sum_j(\mu_j^2+\sigma_j^2-\ln\sigma_j^2-1).\quad\blacksquare (1)

(c) (6) Under z=μϕ+σϕϵz=\mu_\phi+\sigma_\phi\odot\epsilon with ϵN(0,I)\epsilon\sim\mathcal N(0,I), the pushforward of N(0,I)\mathcal N(0,I) is exactly N(μϕ,diagσϕ2)=qϕ\mathcal N(\mu_\phi,\mathrm{diag}\sigma_\phi^2)=q_\phi. (2) Therefore Eqϕ[f(z)]=Eϵ[f(μϕ+σϕϵ)].\mathbb{E}_{q_\phi}[f(z)]=\mathbb{E}_{\epsilon}[f(\mu_\phi+\sigma_\phi\odot\epsilon)]. The RHS distribution (N(0,I))(\mathcal N(0,I)) is independent of ϕ\phi, so ϕ\nabla_\phi passes inside the expectation (dominated convergence / differentiating under the integral): ϕEqϕ[f]=Eϵ[ϕf(μϕ+σϕϵ)].\nabla_\phi\mathbb{E}_{q_\phi}[f]=\mathbb{E}_\epsilon[\nabla_\phi f(\mu_\phi+\sigma_\phi\odot\epsilon)].\quad\blacksquare (2) Score-function estimator ϕEq[f]=Eq[f(z)ϕlogqϕ(zx)]\nabla_\phi\mathbb E_q[f]=\mathbb E_q[f(z)\nabla_\phi\log q_\phi(z|x)] does not use f\nabla f; it treats ff as a black-box weight, so its variance scales with the magnitude of ff and does not shrink with a well-behaved gradient — empirically much higher variance. (2)

(d) (4) As σj0+\sigma_j\to0^+: σj20\sigma_j^2\to0 but lnσj2+-\ln\sigma_j^2\to+\infty, so the per-dim term 12(μj2+σj2lnσj21)+\tfrac12(\mu_j^2+\sigma_j^2-\ln\sigma_j^2-1)\to+\infty. (2) Thus KL diverges to ++\infty; the encoder becomes deterministic, ELBO \to-\infty, gradients blow up — training penalizes collapsing the posterior to a point. This is the KL regularizer's role: it keeps the latent stochastic and close to the prior. (2)


Question 2

(a) (8) V=pr(x)logD(x)+pg(x)log(1D(x))dxV=\int p_r(x)\log D(x)+p_g(x)\log(1-D(x))\,dx. (1) Pointwise maximize alogD+blog(1D)a\log D+b\log(1-D): derivative a/Db/(1D)=0D=a/(a+b)=prpr+pga/D-b/(1-D)=0\Rightarrow D^*=a/(a+b)=\dfrac{p_r}{p_r+p_g}. (3) Substitute: V(D)=Epr[logprpr+pg]+Epg[logpgpr+pg].V(D^*)=\mathbb E_{p_r}\Big[\log\frac{p_r}{p_r+p_g}\Big]+\mathbb E_{p_g}\Big[\log\frac{p_g}{p_r+p_g}\Big]. (2) Write pr+pg=2mp_r+p_g=2m where m=pr+pg2m=\tfrac{p_r+p_g}{2}: =2log2+DKL(prm)+DKL(pgm)=2DJS(prpg)2log2.=-2\log2+D_{KL}(p_r\|m)+D_{KL}(p_g\|m)=2D_{JS}(p_r\|p_g)-2\log2.\quad\blacksquare (2)

(b) (4) When supports are disjoint, DD^* can be made 11 on real, 00 on fake exactly ⇒ DJS=log2D_{JS}=\log2, so V(D)=0V(D^*)=0 constant, and generator gradient G\nabla_G through log(1D)\log(1-D) vanishes (saturation). (2) No useful signal reaches GG; it cannot spread mass, and any narrow region that fools DD is reinforced → generator maps to few modes = mode collapse. (2)

(c) (5) For pr=δ0p_r=\delta_0, pg=δθp_g=\delta_\theta: EM distance is θ|θ| (mass moved distance θ|\theta|): W=θW=|\theta|. (2) JS: for θ0\theta\ne0 supports disjoint so DJS=log2D_{JS}=\log2 (constant); at θ=0\theta=0, DJS=0D_{JS}=0. (2) W(θ)=θW(\theta)=|\theta| is continuous with a.e. nonzero gradient sign(θ)\mathrm{sign}(\theta) pointing toward θ=0\theta=0, while DJSD_{JS} is flat (zero gradient) for all θ0\theta\ne0. Hence WGAN provides usable gradients everywhere; standard GAN does not. (1)

(d) (3) The fL1\|f\|_L\le1 Lipschitz constraint is required for the Kantorovich–Rubinstein duality to equal WW; without it the sup is unbounded. (1.5) Enforcement: weight clipping (original WGAN) or gradient penalty (WGAN-GP) / spectral normalization. (1.5)


Question 3

(a) (6) Base: x1=α1x0+1α1ϵ1x_1=\sqrt{\alpha_1}x_0+\sqrt{1-\alpha_1}\epsilon_1, matches αˉ1=α1\bar\alpha_1=\alpha_1. (1) Inductive step: assume xt1=αˉt1x0+1αˉt1ϵˉt1x_{t-1}=\sqrt{\bar\alpha_{t-1}}x_0+\sqrt{1-\bar\alpha_{t-1}}\,\bar\epsilon_{t-1}. Then xt=αtxt1+1αtϵt=αtαˉt1x0+(αt(1αˉt1)ϵˉt1+1αtϵt).x_t=\sqrt{\alpha_t}x_{t-1}+\sqrt{1-\alpha_t}\epsilon_t=\sqrt{\alpha_t\bar\alpha_{t-1}}x_0+\big(\sqrt{\alpha_t(1-\bar\alpha_{t-1})}\,\bar\epsilon_{t-1}+\sqrt{1-\alpha_t}\,\epsilon_t\big). (2) The two independent Gaussians sum in variance: αt(1αˉt1)+(1αt)=1αtαˉt1=1αˉt\alpha_t(1-\bar\alpha_{t-1})+(1-\alpha_t)=1-\alpha_t\bar\alpha_{t-1}=1-\bar\alpha_t. (2) Mean coefficient =αˉt=\sqrt{\bar\alpha_t}. Hence q(xtx0)=N(αˉtx0,(1αˉt)I)q(x_t|x_0)=\mathcal N(\sqrt{\bar\alpha_t}x_0,(1-\bar\alpha_t)I). \blacksquare (1)

(b) (6) ϵθ(xt,t)\epsilon_\theta(x_t,t) predicts the noise ϵ\epsilon that was added to produce xtx_t from x0x_0. (2) Since xt=αˉtx0+1αˉtϵx_t=\sqrt{\bar\alpha_t}x_0+\sqrt{1-\bar\alpha_t}\epsilon, we have xtlogq(xtx0)=xtαˉtx01αˉt=ϵ1αˉt\nabla_{x_t}\log q(x_t|x_0)=-\dfrac{x_t-\sqrt{\bar\alpha_t}x_0}{1-\bar\alpha_t}=-\dfrac{\epsilon}{\sqrt{1-\bar\alpha_t}}. (2) Thus the score is a rescaling of ϵ-\epsilon; predicting ϵ\epsilon is equivalent to score matching with sθ=ϵθ/1αˉts_\theta=-\epsilon_\theta/\sqrt{1-\bar\alpha_t}, differing only by the tt-dependent weight that LsimpleL_{\text{simple}} drops. (2)

(c) (6) αˉt=(1βs)\bar\alpha_t=\prod(1-\beta_s) decreases monotonically from αˉ0=1\bar\alpha_0=1 toward ≈0. Early β\beta small ⇒ αˉt\bar\alpha_t near 1 (little noise); late steps drive it to ~0. (2) αˉT0\bar\alpha_T\approx0 makes q(xTx0)N(0,I)q(x_T|x_0)\approx\mathcal N(0,I) independent of x0x_0, so sampling can begin from pure Gaussian noise (matching the prior). (2) Toy schedule β=(0.1,0.2,0.3)\beta=(0.1,0.2,0.3): α=(0.9,0.8,0.7)\alpha=(0.9,0.8,0.7). αˉ1=0.9\bar\alpha_1=0.9, αˉ2=0.72\bar\alpha_2=0.72, αˉ3=0.504\bar\alpha_3=0.504. (2)

[
{"claim":"Diagonal-Gaussian KL to N(0,I) formula for mu=[1,2], sigma2=[0.5,1.5]",
 "code":"import sympy as sp\nmu=[sp.Integer(1),sp.