The Math Behind the Machine/ Unit 20 · From Noise to Pictures Checks 0/2 Sign in
Unit 20 of 20 · by Prof. Saurabh

From Noise to Pictures: VAEs and Diffusion

In Unit 19 a machine wrote text one word at a time. Here a machine draws. The secret is almost too simple: start from random noise, and turn it, step by step, into something that looks like real data. First we meet the VAE, which squeezes pictures into a small map and learns to draw from any point on it. Then we meet diffusion, which learns to destroy a picture with noise and then runs the film backwards — a compass at every point showing the way to real data. Gaussians, KL, the chain rule and gradient descent from the whole course all come back here.

≈ 150 min read + play 14 interactive widgets · 3 in 3D · every diffusion picture computed exactly 32 inline checks 🧾 18 proofs, folded away — open "if you want the algebra" when you are ready ✍ 16 solved practice problems

← Unit 19 · The Maths Inside an LLM

static walks back into a picture · drag to orbit
1

Drawing from a hat of noise

Imagine this

Before Ganesh Chaturthi, a sculptor in a small workshop makes one idol after another. Every morning his helper hands him a lump of clay. No two lumps are the same: one is a little bigger, one a little lopsided, one a little wetter.

The sculptor's hands make the same moves every time. Yet every idol comes out slightly different, and every one of them still looks like Ganesha. The skill is fixed. The lump is random. Put the two together and new idols keep coming.

The question. How can a machine draw a face that has never existed?

Here is the whole idea of this unit in one line: to create is to sample. A machine that creates is a machine that takes easy randomness — the lump of clay — and turns it into something that looks like the real data. We call such a machine a generator. The randomness goes in, the sculptor's fixed skill is a function gg, and a new example comes out: x=g(z)x=g(z).

By hand, with heights. Say the heights of grown men in a town follow a bell curve with mean 170 cm and spread 7 cm, written N(170, 72)N(170,\ 7^2). How do we make up a new, realistic height? Draw a standard random number zz from N(0,1)N(0,1) — the easiest bell curve there is, centred at 0 with spread 1 — and then stretch and shift it:

x=μ+σz=170+7z.x=\mu+\sigma z=170+7z.

Four draws of the hat give z=−1, 0, 0.5, 2z=-1,\ 0,\ 0.5,\ 2. The machine turns them into

170+7(−1)=163170+7(0)=170170+7(0.5)=173.5170+7(2)=184.\begin{aligned}170+7(-1)&=163\\ 170+7(0)&=170\\ 170+7(0.5)&=173.5\\ 170+7(2)&=184.\end{aligned}

Four new men who never existed, each with a believable height. The skill (×7\times7, then +170+170) never changed. Only the lump zz did.

Not every shape is a bell. Waiting times at a bus stop, say, follow an exponential curve: short waits are common and long waits are rare. With λ=1\lambda=1 the chance that the wait is at most xx minutes is F(x)=1−e−xF(x)=1-e^{-x}. Now take the plainest noise of all, a number uu spread evenly between 0 and 1, and solve u=F(x)u=F(x) for xx:

x=−ln⁡(1−u).x=-\ln(1-u).

With u=0.5u=0.5: x=−ln⁡0.5≈0.6931x=-\ln 0.5\approx0.6931 minutes. This trick is called the inverse CDF (the CDF is the "chance of at most xx" curve, FF). It works for any shape of data in one dimension: run the curve FF backwards on even noise.

The hard part. For heights and waiting times someone can write the curve down. For pictures, nobody can. A 20 × 20 grey picture is already 400 numbers, and there is no formula for "all the pictures that look like a face". So the generator has to be learned from examples. That is what the whole unit is about: two ways to learn the sculptor's hands — the VAE (Act II) and diffusion (Act III).

The noise machineAlong the bottom: the easy noise that goes in (grey). The curve is the machine x=g(z)x=g(z). Up the left side: what comes out (green). Each ball drops in at the bottom, rises to the curve, and slides across to its output. The dashed lines show the shapes the two piles should take.

Try: On heights, press the four draws of the text: the balls come out at 163, 170, 173.5 and 184. Press draw 500 and watch the green pile take the bell shape of N(170, 72)N(170,\ 7^2). Switch to waiting times and press draw u = 0.5: it comes out at 0.6931. Then try two bumps: plain bell-curve noise in, two humps out — the steep part of the curve splits the pile in two.

Why does this work?

Look at the curve as a funnel. Where it is steep, a narrow band of inputs is stretched over a wide band of outputs, so the balls spread thin. Where it is flat, a wide band of inputs is squeezed into a narrow band of outputs, so the balls pile up. So the curve decides where the pile is high and where it is low: output density = input density ÷ slope. The inverse CDF picks exactly the curve whose slope makes the pile come out right. A learned generator does the same thing in hundreds of dimensions, with a neural network as the curve.

Four ways to learn the sculptor's hands. Every modern generator turns noise into data. They differ in how the noise goes in and how the machine is trained.

familyhow it drawswhere the noise goes ingood atweak at
autoregressive (Unit 19)one piece at a time, each piece chosen from a probability lista dice roll at every tokentext; exact likelihoodslow: one step per piece
VAE (§3–§7)pick a point on a small map, decode it in one gothe point z∼N(0,I)z\sim N(0,I)fast; a smooth map you can walkblurry pictures
GANone pass of a generator networka random vector zzsharp pictures, fastunstable training; can forget whole kinds of data
diffusion (§8–§13)start from static, remove a little noise many timesthe starting static, and a little at every stepsharp, varied pictures; easy to steermany steps (§12 makes it faster)

A GAN in one paragraph. A GAN (a generative adversarial network) trains two networks against each other, like a forger and a detective. The forger turns noise into fake pictures. The detective looks at real pictures and fakes and says "real" or "fake". The forger improves by fooling the detective, the detective improves by catching the forger. It gives sharp pictures, but the contest is hard to balance, and the forger may learn to draw only a few kinds of picture that fool the detective. We will not build one here: the VAE and diffusion are the ones whose maths this course has prepared you for.

Rule of thumb. Text: autoregressive. A fast generator with a smooth map of the data: VAE. The best pictures today: diffusion — often run inside a VAE's map (§13).

Trap

A machine that hands back the training pictures is not creating — it is copying. We want new examples that come from the same distribution as the training data: new heights that are believable, not the heights of the men we measured. A generator is judged by whether its samples look like they came from the same hat, not by whether they match any one example.

The realization

x=g(z),z∼easy noisex=g(z),\qquad z\sim\text{easy noise}

A generator is a fixed function applied to fresh noise. In one dimension we can write the function down: stretch-and-shift for a bell curve, the inverse CDF for anything else. For pictures the function must be learned — and learning it is the whole art.

Pause & predict

In the heights machine you change the curve from 170+7z170+7z to 170+14z170+14z. What happens to the green pile of outputs?

Pause & predict

In the waiting-times machine x=−ln⁡(1−u)x=-\ln(1-u), where do most of the outputs pile up, and why?

If you want the algebra · 2 proofs, step by step
Prove it · running the CDF backwards gives the right shape

Claim. Let FF be a CDF that rises steadily (so it has an inverse), and let uu be spread evenly on (0,1)(0,1). Then x=F−1(u)x=F^{-1}(u) has exactly the CDF FF.

1
Ask for the chance that the output is at most some value aa: P(x≤a)=P(F−1(u)≤a)=P(u≤F(a)).\begin{aligned}&P(x\le a)\\ &=P\big(F^{-1}(u)\le a\big)\\ &=P\big(u\le F(a)\big).\end{aligned} FF rises steadily, so applying it to both sides keeps the "≤\le" the right way round.
2
For even noise on (0,1)(0,1), the chance of landing below any number cc between 0 and 1 is just cc. So P(u≤F(a))=F(a)P(u\le F(a))=F(a) — exactly the CDF we wanted. ∎ Exponential with λ=1\lambda=1: F(x)=1−e−xF(x)=1-e^{-x}, so u=1−e−xu=1-e^{-x} gives x=−ln⁡(1−u)x=-\ln(1-u); u=0.5u=0.5 gives ln⁡2≈0.6931\ln2\approx0.6931.
Prove it · output density = input density ÷ slope

Claim. If x=g(z)x=g(z) with gg increasing, and zz has density pzp_z, then xx has density px(x)=pz(z) / g′(z)p_x(x)=p_z(z)\,/\,g'(z) at x=g(z)x=g(z).

1
A thin band of inputs [z, z+dz][z,\ z+dz] holds probability pz(z) dzp_z(z)\,dz. The curve sends it to the band [x, x+g′(z) dz][x,\ x+g'(z)\,dz], which must hold the same probability: px(x) g′(z) dz=pz(z) dz.p_x(x)\,g'(z)\,dz=p_z(z)\,dz. Balls are neither made nor lost on the way through the curve.
2
Divide by g′(z) dzg'(z)\,dz: px(x)=pz(z)/g′(z)p_x(x)=p_z(z)/g'(z). Steep curve, thin pile; flat curve, tall pile. ∎ For x=170+7zx=170+7z: g′=7g'=7, so the output bell is 7 times wider and 7 times lower than N(0,1)N(0,1) — that is N(170, 72)N(170,\ 7^2). In many dimensions the slope becomes a Jacobian determinant (Unit 4's volume factor).

The road ahead. The unit has four acts.

  1. To create is to sample (§1–§2): the noise machine, and the two facts about Gaussian noise that everything later uses.
  2. Squeeze, then rebuild (§3–§7): the autoencoder, the VAE's cloud, the reparameterisation trick, the ELBO, and walking the map of faces.
  3. Noise in, noise out (§8–§12): diffusion — destroy a picture with noise, learn to guess the noise, the score as a compass, walk back from static, and the fast jump.
  4. Steering, and the whole course (§13–§14): steering with words, latent diffusion, and every tool of the course inside the two machines.

In one sentence: A generator is a sculptor's fixed skill applied to a random lump of clay — x=g(z)x=g(z) with fresh noise zz — and since nobody can write gg down for pictures, the machine has to learn it.

Free preview · Unit 20 of 20

That was section 1. The rest of the unit opens when you unlock it.

13 more sections and the practice arena — 14 widgets, 32 checks and 16 solved problems in the whole unit (this preview had 1 widget and 2 checks).

Unlock Unit 20

  1. 2

    The Gaussian toolkit for noise

  2. 3

    The autoencoder: squeeze and rebuild

  3. 4

    The VAE: a cloud, not a point

  4. 5

    The reparameterisation trick

  5. 6

    The ELBO: the score a VAE maximises

  6. 7

    Walking the latent map

  7. 8

    Forward: destroy a picture one small step at a time

  8. 9

    The denoiser: guess the noise

  9. 10

    The score: a compass pointing at the data

  10. 11

    Walking back from static

  11. 12

    Faster: DDIM and the smooth path

  12. 13

    Steering with words: guidance and latent diffusion

  13. 14

    What to carry forward — and the whole course inside one picture

  14. 15

    Practice arena — sixteen problems, solved in full

Unlock this unit for ₹299, or all seven paid units for ₹999 — one-time payment, full refund within 7 days. See pricing.