Level 4 — ApplicationGenerative Models

Generative Models

60 minutes60 marksprintable — key stays hidden on paper

Level 4 (Application: novel problems, no hints) Time limit: 60 minutes Total marks: 60

Answer all questions. Show all derivations. Use ...... / ...... notation for mathematics.


Question 1 — VAE ELBO and the Reparameterization Trick (14 marks)

A team trains a VAE on 1-D data. Their encoder outputs, for a single input xx, a Gaussian posterior qϕ(zx)=N(z;μ,σ2)q_\phi(z\mid x) = \mathcal{N}(z;\mu, \sigma^2) with μ=2.0\mu = 2.0 and σ=0.5\sigma = 0.5. The prior is p(z)=N(0,1)p(z) = \mathcal{N}(0,1).

(a) Write the ELBO for a single datapoint in terms of the reconstruction term and the KL term. State clearly what each term encourages. (3)

(b) For a univariate diagonal Gaussian posterior and standard normal prior, the KL divergence is DKL(N(μ,σ2)N(0,1))=12(μ2+σ21lnσ2).D_{KL}\big(\mathcal{N}(\mu,\sigma^2)\,\|\,\mathcal{N}(0,1)\big) = \tfrac{1}{2}\left(\mu^2 + \sigma^2 - 1 - \ln \sigma^2\right). Compute its numeric value for the given μ,σ\mu,\sigma. (4)

(c) The team codes the sampling step as z = mu + sigma * eps where eps ~ N(0,1). Explain precisely why this reparameterization is necessary for training, and what would break if they instead sampled z ~ N(mu, sigma^2) directly inside the graph. (4)

(d) If they scale the KL term by a factor β=4\beta = 4 (a β\beta-VAE), predict the qualitative effect on latent-space structure and reconstruction quality, and justify. (3)


Question 2 — Diffusion Forward Process (13 marks)

A DDPM uses a forward process q(xtxt1)=N(xt;1βtxt1,βtI)q(x_t\mid x_{t-1}) = \mathcal{N}(x_t; \sqrt{1-\beta_t}\,x_{t-1}, \beta_t I) with the closed form q(xtx0)=N(xt;αˉtx0,(1αˉt)I)q(x_t\mid x_0)=\mathcal{N}(x_t; \sqrt{\bar\alpha_t}\,x_0, (1-\bar\alpha_t)I), where αt=1βt\alpha_t = 1-\beta_t and αˉt=s=1tαs\bar\alpha_t = \prod_{s=1}^t \alpha_s.

(a) A short schedule uses β1=0.1, β2=0.2, β3=0.5\beta_1 = 0.1,\ \beta_2 = 0.2,\ \beta_3 = 0.5. Compute αˉ3\bar\alpha_3. (4)

(b) Using the reparameterization xt=αˉtx0+1αˉtϵx_t = \sqrt{\bar\alpha_t}\,x_0 + \sqrt{1-\bar\alpha_t}\,\epsilon with ϵN(0,I)\epsilon\sim\mathcal{N}(0,I), and given x0=1.0x_0 = 1.0 and a drawn noise ϵ=0.5\epsilon = 0.5, compute x3x_3. (4)

(c) As tTt\to T, argue what αˉt\bar\alpha_t approaches and why this matters for sampling from the trained model. (2)

(d) A practitioner switches from a linear β\beta schedule to a cosine schedule. State one concrete benefit for image quality and briefly explain the mechanism. (3)


Question 3 — GAN Objective and Training Dynamics (12 marks)

The original GAN minimax objective is minGmaxD  Expdata[logD(x)]+Ezpz[log(1D(G(z)))].\min_G \max_D \; \mathbb{E}_{x\sim p_{data}}[\log D(x)] + \mathbb{E}_{z\sim p_z}[\log(1 - D(G(z)))].

(a) For a fixed generator, show that the optimal discriminator is D(x)=pdata(x)pdata(x)+pg(x).D^*(x) = \frac{p_{data}(x)}{p_{data}(x) + p_g(x)}. (5)

(b) At a point where the generator perfectly matches the data (pg=pdatap_g = p_{data}), evaluate D(x)D^*(x) and the value of the objective. (3)

(c) Explain mode collapse operationally: what does the generator do, why does the standard loss fail to punish it, and name one architectural/loss remedy with a one-line reason. (4)


Question 4 — Classifier-Free Guidance (11 marks)

In classifier-free guidance the guided noise prediction is ϵ^θ(xt,c)=ϵθ(xt,)+w(ϵθ(xt,c)ϵθ(xt,)),\hat\epsilon_\theta(x_t, c) = \epsilon_\theta(x_t, \varnothing) + w\big(\epsilon_\theta(x_t, c) - \epsilon_\theta(x_t, \varnothing)\big), where cc is the conditioning (e.g. text embedding), \varnothing is the null/unconditional token, and ww is the guidance scale.

(a) Suppose at some step ϵθ(xt,)=0.20\epsilon_\theta(x_t,\varnothing) = 0.20 and ϵθ(xt,c)=0.50\epsilon_\theta(x_t, c) = 0.50. Compute ϵ^θ\hat\epsilon_\theta for w=1w = 1 and for w=3w = 3. (4)

(b) Interpret what w=0w=0 and w=1w=1 each correspond to. (3)

(c) Explain the practical trade-off of increasing ww far above 1: what improves and what degrades, and why. (4)


Question 5 — Model Selection Reasoning (10 marks)

A startup must generate high-resolution product photos from text prompts, with a small GPU budget for inference.

(a) Compare a GAN vs a pixel-space diffusion model vs a latent diffusion model on: sample quality, training stability, and inference cost. Recommend one and justify. (6)

(b) They report an FID of 8.2 and an Inception Score of 4.1. Explain what each metric measures and why FID is generally preferred, including the direction (higher/lower better) of each. (4)


End of paper.

Answer keyMark scheme & solutions

Question 1 (14 marks)

(a) (3) ELBO=Eqϕ(zx)[logpθ(xz)]reconstructionDKL(qϕ(zx)p(z))regularizer.\text{ELBO} = \underbrace{\mathbb{E}_{q_\phi(z|x)}[\log p_\theta(x|z)]}_{\text{reconstruction}} - \underbrace{D_{KL}(q_\phi(z|x)\,\|\,p(z))}_{\text{regularizer}}.

  • Reconstruction term (1): encourages decoded samples to match input xx.
  • KL term (1): pulls the posterior toward the prior, regularizing/structuring the latent space (1).

(b) (4) μ2=4.0\mu^2 = 4.0 (1); σ2=0.25\sigma^2 = 0.25 (1); lnσ2=ln0.25=1.386294\ln\sigma^2 = \ln 0.25 = -1.386294 (1). DKL=12(4.0+0.251(1.386294))=12(4.636294)=2.318147.D_{KL} = \tfrac12(4.0 + 0.25 - 1 - (-1.386294)) = \tfrac12(4.636294) = 2.318147. (1)

(c) (4)

  • Sampling z ~ N(mu, sigma²) directly is a stochastic node; gradients cannot flow back through a random draw to μ,σ\mu,\sigma (1).
  • Reparameterization moves randomness to an external variable ϵN(0,1)\epsilon\sim\mathcal N(0,1) (1), making z=μ+σϵz = \mu + \sigma\epsilon a deterministic, differentiable function of parameters (1).
  • Without it, backprop to the encoder parameters is impossible / gradient is high-variance (score-function) and training stalls (1).

(d) (3)

  • Larger β\beta weights the KL more, forcing posterior closer to prior → more disentangled / smoother latent space (1).
  • Reconstruction quality degrades because capacity to encode information is penalized (1); risk of posterior collapse where latents become uninformative (1).

Question 2 (13 marks)

(a) (4) α1=0.9, α2=0.8, α3=0.5\alpha_1 = 0.9,\ \alpha_2 = 0.8,\ \alpha_3 = 0.5 (2). αˉ3=0.9×0.8×0.5=0.36.\bar\alpha_3 = 0.9\times0.8\times0.5 = 0.36. (2)

(b) (4) αˉ3=0.36=0.6\sqrt{\bar\alpha_3} = \sqrt{0.36} = 0.6 (1); 1αˉ3=0.64=0.8\sqrt{1-\bar\alpha_3} = \sqrt{0.64} = 0.8 (1). x3=0.6×1.0+0.8×0.5=0.6+0.4=1.0.x_3 = 0.6\times1.0 + 0.8\times0.5 = 0.6 + 0.4 = 1.0. (2)

(c) (2) αˉt0\bar\alpha_t \to 0 (1), so q(xTx0)N(0,I)q(x_T|x_0)\to\mathcal N(0,I); this lets sampling start from pure Gaussian noise independent of x0x_0 (1).

(d) (3)

  • Cosine schedule adds noise more gradually, avoiding the abrupt information destruction of linear schedules near the end (1).
  • Keeps more signal in later steps / better distributes difficulty across timesteps (1) → improved sample quality especially at high resolution (1).

Question 3 (12 marks)

(a) (5) For fixed GG, maximize over DD the integrand: pdata(x)logD(x)+pg(x)log(1D(x))dx.\int p_{data}(x)\log D(x) + p_g(x)\log(1-D(x))\,dx. (1) Pointwise, maximize f(D)=alogD+blog(1D)f(D) = a\log D + b\log(1-D) with a=pdata, b=pga=p_{data},\ b=p_g (1). f(D)=aDb1D=0f'(D) = \frac{a}{D} - \frac{b}{1-D} = 0 (1) a(1D)=bDD=aa+b\Rightarrow a(1-D) = bD \Rightarrow D = \frac{a}{a+b} (1). D(x)=pdata(x)pdata(x)+pg(x).D^*(x) = \frac{p_{data}(x)}{p_{data}(x)+p_g(x)}. (1)

(b) (3) pg=pdataD=12p_g = p_{data} \Rightarrow D^* = \tfrac12 (1). Objective value =log12+log12=2ln2=1.386294= \log\tfrac12 + \log\tfrac12 = -2\ln 2 = -1.386294 (i.e. log14\log\frac14) (2).

(c) (4)

  • Generator maps many/all zz to a few outputs, ignoring diversity (1).
  • Standard loss only rewards fooling DD; producing one convincing mode still lowers loss, so limited-diversity outputs aren't penalized (1).
  • Remedy e.g. WGAN / Wasserstein loss (1): provides smoother gradients and a distance that reflects distribution overlap, reducing collapse (or minibatch discrimination) (1).

Question 4 (11 marks)

(a) (4) w=1w=1: ϵ^=0.20+1(0.500.20)=0.50\hat\epsilon = 0.20 + 1(0.50-0.20) = 0.50 (2). w=3w=3: ϵ^=0.20+3(0.30)=0.20+0.90=1.10\hat\epsilon = 0.20 + 3(0.30) = 0.20 + 0.90 = 1.10 (2).

(b) (3)

  • w=0w=0: purely unconditional prediction ϵθ(xt,)\epsilon_\theta(x_t,\varnothing) — ignores the prompt (1.5).
  • w=1w=1: standard conditional prediction ϵθ(xt,c)\epsilon_\theta(x_t,c) (no extra guidance) (1.5).

(c) (4)

  • Larger ww pushes samples toward stronger alignment with cc → better prompt fidelity/sharper class features (2).
  • But reduces diversity and can cause over-saturation/artifacts and mode narrowing, since predictions are extrapolated beyond the model's learned distribution (2).

Question 5 (10 marks)

(a) (6)

  • GAN: high quality, fast single-pass inference, but unstable training / mode collapse (2).
  • Pixel diffusion: excellent quality & stable training, but very expensive inference (many denoising steps at full resolution) (2).
  • Latent diffusion: diffuses in a compressed latent space → near-diffusion quality, stable, and much cheaper inference. Recommend latent diffusion given the small GPU budget and text-to-image need (2).

(b) (4)

  • FID: Fréchet distance between Gaussian-fit features (Inception activations) of real vs generated images; captures quality and diversity; lower is better (2).
  • IS: uses classifier confidence + marginal diversity; higher is better but ignores real data statistics and is easily gamed. FID preferred because it compares against real data distribution and penalizes mode collapse (2).
[
  {"claim":"KL for N(2,0.25)||N(0,1) = 2.318147","code":"mu=2.0; s2=0.25; val=(mu**2+s2-1-sympy.log(s2))/2; result=abs(float(val)-2.3181471805599454)<1e-6"},
  {"claim":"alpha_bar_3 = 0.36","code":"ab=0.9*0.8*0.5; result=abs(ab-0.36)<1e-12"},
  {"claim":"x_3 = 1.0 given x0=1, eps=0.5","code":"ab=0.36; x3=sympy.sqrt(ab)*1.0+sympy.sqrt(1-ab)*0.5; result=abs(float(x3)-1.0)<1e-9"},
  {"claim":"GAN optimal objective at p_g=p_data equals -2 ln 2","code":"v=sympy.log(sympy.Rational(1,2))+sympy.log(sympy.Rational(1,2)); result=abs(float(v)-(-1.3862943611198906))<1e-9"},
  {"claim":"CFG guided eps at w=3 is 1.10","code":"eu=0.20; ec=0.50; w=3; g=eu+w*(ec-eu); result=abs(g-1.10)<1e-9"}
]