Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 20 — Lab: Generative Models

Open in Colab

Lab Overview

You will write, in NumPy, the formulas at the heart of the three families: the VAE’s sampling and KL term, the GAN’s losses, and the diffusion model’s noise schedule, noising and denoising steps.

ExerciseTopicPoints
1The reparameterisation trick10
2The KL term of a VAE15
3The GAN losses15
4A noise schedule10
5Noising in one step15
6One DDPM denoising step15
7One DDIM step10
8Counting covered clusters10
ChallengeClassifier-free guidance+10

How it works. Each exercise has a function to complete where you see # TODO. Run the check cell under it: ✅ means your function is right, ❌ tells you what is wrong. Use NumPy only. Do not change the check cells.


Exercise 1: The Reparameterisation Trick (10 points)

reparameterise(mu, logvar, eps) returns z=μ+σ⊙εz = \mu + \sigma \odot \varepsilon with σ=e12log⁡σ2\sigma = e^{\frac12 \log\sigma^2}.


Exercise 2: The KL Term (15 points)

kl_normal(mu, logvar) returns KL(N(μ,σ2)∥N(0,1))=−12∑(1+log⁡σ2−μ2−σ2)\mathrm{KL}(\mathcal{N}(\mu, \sigma^2) \| \mathcal{N}(0, 1)) = -\tfrac12 \sum (1 + \log\sigma^2 - \mu^2 - \sigma^2), summed over the last axis (one value per row).


Exercise 3: The GAN Losses (15 points)

The discriminator outputs logits ℓ\ell (before the sigmoid). Write the mean binary cross-entropy losses:

  • d_loss(l_real, l_fake): real labelled 1, fake labelled 0: −meanlog⁡σ(ℓreal)−meanlog⁡(1−σ(ℓfake))-\text{mean}\log\sigma(\ell_{\text{real}}) - \text{mean}\log(1 - \sigma(\ell_{\text{fake}}));

  • g_loss(l_fake): the non-saturating generator loss −meanlog⁡σ(ℓfake)-\text{mean}\log\sigma(\ell_{\text{fake}}).

Use the stable forms −log⁡σ(ℓ)=log⁡(1+e−ℓ)-\log\sigma(\ell) = \log(1 + e^{-\ell}) and −log⁡(1−σ(ℓ))=log⁡(1+eℓ)-\log(1 - \sigma(\ell)) = \log(1 + e^{\ell}), written with np.logaddexp(0, ...).


Exercise 4: A Noise Schedule (10 points)

alpha_bar(betas) returns αˉt=∏s≤t(1−βs)\bar\alpha_t = \prod_{s \le t}(1 - \beta_s) for every tt (an array of the same length as betas).


Exercise 5: Noising in One Step (15 points)

q_sample(x0, t, eps, ab) returns xt=αˉt x0+1−αˉt εx_t = \sqrt{\bar\alpha_t}\, x_0 + \sqrt{1 - \bar\alpha_t}\, \varepsilon, where t is an array of step indices (one per row of x0) into ab.


Exercise 6: One DDPM Step (15 points)

ddpm_step(x, t, eps_hat, betas, ab, z) returns one reverse step

xt−1=11−βt(xt−βt1−αˉt ε^)+βt z,x_{t-1} = \frac{1}{\sqrt{1-\beta_t}}\Big(x_t - \frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\,\hat\varepsilon\Big) + \sqrt{\beta_t}\, z,

where t is a single integer and z is the fresh noise (the caller passes zeros at the last step).


Exercise 7: One DDIM Step (10 points)

ddim_step(x, t, s, eps_hat, ab) estimates x^0=(xt−1−αˉt ε^)/αˉt\hat x_0 = (x_t - \sqrt{1-\bar\alpha_t}\,\hat\varepsilon)/\sqrt{\bar\alpha_t} and returns xs=αˉs x^0+1−αˉs ε^x_s = \sqrt{\bar\alpha_s}\,\hat x_0 + \sqrt{1-\bar\alpha_s}\,\hat\varepsilon. When s is None, use αˉs=1\bar\alpha_s = 1 (the final step to the data).


Exercise 8: Counting Covered Clusters (10 points)

coverage(samples, centres, radius) returns (clusters, share): how many centres have at least one sample within radius, and the share of samples within radius of some centre.


Challenge: Classifier-Free Guidance (+10 points)

guide(eps_cond, eps_uncond, w) returns (1+w) ε^cond−w ε^uncond(1 + w)\,\hat\varepsilon_{\text{cond}} - w\,\hat\varepsilon_{\text{uncond}}.


Lab Report

Submit this notebook with:

  • every check cell showing ✅,

  • one paragraph: Exercise 3 asks for the non-saturating generator loss. Using your g_loss and the saturating form meanlog⁡(1−σ(ℓ))\text{mean}\log(1 - \sigma(\ell)), compare their slopes with respect to ℓ\ell at ℓ=−5\ell = -5 (a discriminator that easily rejects the generator’s samples). Which one gives the generator something to learn from?