Part 4: Deep learning Lesson 10 of 15 in this part
More generative models, VAEs, flows and diffusion in detail
What you will learn. Explain the reparameterisation trick and the KL term in a VAE, the change-of-variables idea behind normalising flows and the forward noising step in a diffusion model, and place GAN training problems and modern image tools next to them.
Before this lesson: Generative models: autoencoders, GANs and diffusion. Already know these? Carry on.
In plain words
The first generative lesson gave the broad picture. This one opens up four of its parts. A VAE (variational autoencoder) turns each input into a bell-shaped cloud of codes, not one code, and draws a sample from it. To train with that random draw, it uses the reparameterisation trick: write the sample as the cloud's centre plus its width times a fixed random number, so the gradient can flow through the centre and the width. A second term, the KL divergence, keeps every cloud near a standard bell curve so that new samples drawn later make sense. A normalising flow transforms a simple distribution through steps that can be undone, and because each step stretches space by a known amount, the exact probability of any data point can be computed. A diffusion model gradually adds noise to training images and learns to remove it step by step, which lets it generate new images starting from pure noise.
Without the trick
- Sample directly from the cloud
- No gradient flows through a random draw
With the trick
- Sample is centre plus width times fixed noise
- Gradient reaches the centre and the width
The random part is moved outside, so the gradient can reach the centre and the width.
- InputStart with a real imageClean data
- ProcessMix in a little noise at each stepSignal shrinks, noise grows
- OutputAfter many steps only noise is leftAlmost no trace of the image
A schedule decides how much noise is added at each step. At the end only noise is left.
VAE
- Encodes to clouds, decodes samples
- Smooth, but images can look blurry
GAN
- Generator against discriminator
- Sharp, but training can be unstable
Flow
- Reversible steps with exact probabilities
- Needs special reversible layers
Diffusion
- Learns to remove noise step by step
- Very good images, slower to generate
They differ in how they turn training data into a way to create new data.
- ProblemMode collapseThe generator makes only a few kinds of output
- RiskUnstable trainingThe two networks fail to balance
- OutputFixesWasserstein loss, gradient penalty, careful learning rates
Three common problems, and the usual remedies.
Real-world example
A design studio wants new product images. A GAN gave sharp pictures, but the team found it produced a narrow range of shapes (mode collapse) and was hard to train reliably. A diffusion model, trained on the studio's own licensed images, takes longer per picture but gives varied results reliably. For quality checks, they sample a thousand images and compare their statistics with real ones, since no single picture shows whether the model is good. They also add a visible watermark and record how each image was made.
How it works
- VAE. The encoder gives a centre and a width for each input. Draw one fixed random number, form the sample as centre plus width times that number, and decode it. The loss is the reconstruction error plus the KL term that pulls each cloud towards a standard bell curve.
- Flow. Apply a reversible function to the data. Because the function stretches space by a known factor, the density after the function equals the old density divided by the stretch.
- Diffusion. Choose a schedule of small noise amounts. A noisy version of an image at any step is a mix: the image shrunk by a signal weight plus fixed random noise scaled by a noise weight. A network learns to predict the noise, and generation runs the process backwards.
- Other pieces. VQ-VAE uses a finite list of codes instead of clouds. DCGAN is the standard convolutional GAN. StyleGAN controls style at each layer, CycleGAN translates images between two sets without paired examples, and pix2pix needs pairs. ControlNet and inpainting let diffusion models follow an outline or fill a hole. Audio, music and video models use the same ideas with sequences of audio or frame codes.
Python example
Python: example run The example programme behind this lesson's quoted output ran on 2026-11-19 (Python 3.12.10). 1 code block in the lesson passed a syntax check, 1 also ran on their own.
Four short calculations with NumPy: the reparameterised sample, the KL term, a flow's density after a stretch, and the signal and noise weights across a 1000-step schedule. No generative model is trained here, and PyTorch was not installed.
"""Four generative-model calculations from scratch: the reparameterisation trick, the VAE's KL term, a normalising
flow's change of variables, and the forward noising of a diffusion model.
Run: python gen_more.py (needs numpy)
"""
import math
import numpy as np
# 1. Reparameterisation: sample z = mu + sigma * eps, so the sample depends smoothly on mu and sigma
mu, sigma, eps = 1.0, 0.5, 0.8 # eps is the fixed random draw
print("reparameterised sample z = %.2f" % (mu + sigma * eps))
# 2. VAE regulariser: KL divergence of N(mu, sigma^2) from N(0, 1) in closed form
def kl(mu, sigma):
return 0.5 * (mu ** 2 + sigma ** 2 - 1 - math.log(sigma ** 2))
print("KL for (mu 0, sigma 1): %.3f for (mu 1, sigma 0.5): %.3f for (mu 3, sigma 0.2): %.3f" % (kl(0, 1), kl(1, 0.5), kl(3, 0.2)))
# 3. Normalising flow, one step: y = 2x + 1 stretches the line, so the density must shrink by the same factor
def normal(x):
return math.exp(-0.5 * x * x) / math.sqrt(2 * math.pi)
x = 0.5
y = 2 * x + 1
print("density of x at %.1f: %.4f; density of y at %.1f: %.4f (divided by the stretch 2)" % (x, normal(x), y, normal(x) / 2))
# 4. Diffusion forward process: x_t = sqrt(abar_t) * x0 + sqrt(1 - abar_t) * eps
betas = np.linspace(0.0001, 0.02, 1000)
abar = np.cumprod(1 - betas)
x0, eps = 2.0, -0.5
print(" step abar signal weight noise weight noisy value")
for t in (1, 100, 500, 1000):
a = abar[t - 1]
print("%5d %.4f %13.3f %12.3f %11.3f" % (t, a, math.sqrt(a), math.sqrt(1 - a), math.sqrt(a) * x0 + math.sqrt(1 - a) * eps))reparameterised sample z = 1.40
KL for (mu 0, sigma 1): 0.000 for (mu 1, sigma 0.5): 0.818 for (mu 3, sigma 0.2): 5.629
density of x at 0.5: 0.3521; density of y at 2.0: 0.1760 (divided by the stretch 2)
step abar signal weight noise weight noisy value
1 0.9999 1.000 0.010 1.995
100 0.8970 0.947 0.321 1.734
500 0.0786 0.280 0.960 0.081
1000 0.0000 0.006 1.000 -0.487The sample is 1.40, and the KL is 0.000 for a standard cloud, 0.818 for centre 1 and width 0.5, and 5.629 for centre 3 and width 0.2. The density of y at 2.0 is half that of x at 0.5. In the schedule, step 1 is almost all signal, step 500 is about a quarter signal weight, and step 1000 has a signal weight of 0.006, so the value at step 1000 is almost pure noise.
.NET (C#) example
C#: compiled and run The C# sample compiled on 2026-11-19 with .NET SDK 10.0.400 (no extra packages). It also ran to the end without error.
The same four calculations from scratch in C#, with no packages, printing the same output. Training generative models is done in Python. In C#, you load a trained model and run it through ONNX Runtime.
// Reparameterisation, the VAE's KL term, a flow's change of variables and diffusion noising, from scratch.
// Same output as gen_more.py. No packages.
using System.Globalization;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
// 1. Reparameterisation: sample z = mu + sigma * eps, so the sample depends smoothly on mu and sigma
double mu = 1.0, sigma = 0.5, eps = 0.8; // eps is the fixed random draw
Console.WriteLine($"reparameterised sample z = {mu + sigma * eps:F2}");
// 2. VAE regulariser: KL divergence of N(mu, sigma^2) from N(0, 1) in closed form
double Kl(double m, double s) => 0.5 * (m * m + s * s - 1 - Math.Log(s * s));
Console.WriteLine($"KL for (mu 0, sigma 1): {Kl(0, 1):F3} for (mu 1, sigma 0.5): {Kl(1, 0.5):F3} for (mu 3, sigma 0.2): {Kl(3, 0.2):F3}");
// 3. Normalising flow, one step: y = 2x + 1 stretches the line, so the density must shrink by the same factor
double Normal(double v) => Math.Exp(-0.5 * v * v) / Math.Sqrt(2 * Math.PI);
double x = 0.5, y = 2 * x + 1;
Console.WriteLine($"density of x at {x:F1}: {Normal(x):F4}; density of y at {y:F1}: {Normal(x) / 2:F4} (divided by the stretch 2)");
// 4. Diffusion forward process: x_t = sqrt(abar_t) * x0 + sqrt(1 - abar_t) * eps
var abar = new double[1000]; double running = 1.0;
for (int i = 0; i < 1000; i++) { double beta = 0.0001 + i * (0.02 - 0.0001) / 999; running *= 1 - beta; abar[i] = running; }
double x0 = 2.0, e = -0.5;
Console.WriteLine(" step abar signal weight noise weight noisy value");
foreach (int t in new[] { 1, 100, 500, 1000 })
{
double a = abar[t - 1];
Console.WriteLine($"{t,5} {a:F4} {Math.Sqrt(a),13:F3} {Math.Sqrt(1 - a),12:F3} {Math.Sqrt(a) * x0 + Math.Sqrt(1 - a) * e,11:F3}");
}Maths behind it
- Mean, standard deviation and the bell curve (probability)
- KL divergence between two bell curves (information theory)
- Change of variables: density divided by the stretch (calculus)
- Square roots and products across a noise schedule (arithmetic)
Common mistakes
- Sampling without the reparameterisation trick and getting no gradient.
- Setting the KL weight so high that every cloud collapses to the standard curve and the output is blurry.
- Judging a generator by a few pretty samples. Use many samples and statistics.
- Using a diffusion model on data you have no right to use.
- Releasing generated images without a way to tell they are generated.
Quick check
What does the reparameterisation trick change?
Show answer
It writes the random sample as the cloud's centre plus its width times a fixed random draw, so the gradient can pass through the centre and the width.
What does the KL term in a VAE do?
Show answer
It penalises each cloud for straying from a standard bell curve, which keeps the code space tidy so that new samples decode into sensible outputs.
A flow step stretches space by a factor of 4. What happens to the density?
Show answer
It is divided by 4, because the same probability is spread over a length four times as long.
At the last step of a diffusion schedule the signal weight is almost zero. What is left?
Show answer
Almost pure noise. Generation runs the process backwards from pure noise to an image.
Completion
Evidence of completion. A worked reparameterised sample, one KL value, a flow density after a stretch, and a diffusion noising step at one time step, each matching a program's output.
Saved in this browser only. Reading and exercise are ticked separately because they are different achievements. A tick records your own work; it is not a test result. See all progress.