Part III · Ch. 11 — Making Things Up

Part III · Chapter 11 of 13

Making Things Up

Generative models: VAEs, GANs, and diffusion


Review prerequisite concepts
  • Learning from Data
  • The Math Toolkit
  • The Neuron

Sorters and makers

The line-fitter predicted prices, the classifier chose categories, and our small network learned inside from outside a curved region. This chapter builds a machine that makes a new picture. How can arithmetic produce a face nobody photographed?

Two panels split by a vertical hairline. Left panel titled 'a SORTER — discriminative': a small coordinate square with a cluster of mint circles at lower-left and a cluster of amber squares at upper-right, and a single violet straight line slicing between them at a 45-degree angle; a label near the line reads 'learns the line that separates' and an annotation reads 'input arrow which class?'. Right panel titled 'a MAKER — generative': the same two clusters drawn as faint blue points, now overlaid with soft concentric muted contour rings around each cluster like a density landscape, and three fresh mint star markers placed inside the dense regions labeled 'new samples drawn from it'; an annotation reads 'learns where data LIVES makes more'. A centered italic caption reads 'one tells classes apart; the other learns the whole distribution, so it can sample new data'.
A sorter predicts an answer about an input; a maker learns a distribution from which it can generate new inputs.
Earlier ideaUsed here to…
TrainingAdjust a model to examples
ProbabilityDescribe and sample outcomes
Neural networkRepresent a flexible learned rule

A discriminative model predicts a label or value from an input. A generative model learns the distribution of the data itself so it can produce new samples; write input $x$ and answer $y$:

$$p(y \mid x) \qquad \text{vs.} \qquad p(x)$$

In words: a sorter models the probability of a label $y$ given an input $x$: "shown this photo, how likely is 'cat'?" A maker models $p(x)$, the probability of the input itself: "how likely is this arrangement of pixels to be a real photo at all?" Learn that second one and you can grade any image for realness, and generate new ones that score high.

FamilyLearned mechanismNew sample comes from…
VAECompression and reconstructionDecoding a sampled latent point
GANA generator and a judgeRunning the generator
DiffusionNoise predictionRepeatedly denoising fresh static

We will compare three ways to learn that distribution: VAEs, GANs, and diffusion.

Learn the distribution, then sample

To make new data, learn its distribution, then sample. In the math toolkit, a distribution spread belief across outcomes: common things get more, rare things less.

Outcomes A, B, C have probabilities 0.5, 0.3, 0.2; B is highlighted as one example draw.
One example draw gives B, even though A is more likely.

We write that draw with a tilde:

$$x \sim p(x)$$

In words: read the little tilde $\sim$ as "drawn from." We pick an $x$ at random, according to the distribution $p(x)$ rather than evenly, so common things come up often and rare things almost never. An LLM does the same thing when it samples a next token from a probability distribution (the sister site's prediction game). Here the "token" is an entire image.

OutcomeProbabilityOne possible draw
A$0.5$Not selected
B$0.3$Selected
C$0.2$Not selected

For faces, two eyes above a nose are common; random static rarely resembles a face. Learning that pattern across millions of pixel dimensions is difficult: plausible images occupy a thin, curved region, not a table we can fill by hand.

We can also condition sampling, steering it with extra information: a smiling face, a cat in Van Gogh's style, or a harbor at dusk from the air.

VAEs: a map you can wander

PCA compressed many features into a few useful directions. A variational autoencoder (VAE) turns compression and reconstruction into a generator using two trainable networks.

A left-to-right pipeline. On the left a simple face glyph labeled 'input image x'. An arrow leads into a trapezoid that narrows left to right, labeled 'encoder' with a note 'compress'. Its narrow end points to a small violet dot labeled 'latent code z = (0.3, -0.8)'. From that dot an arrow leads into a second trapezoid that widens left to right, labeled 'decoder' with a note 'rebuild', whose wide end points to a second face glyph labeled 'reconstruction x-hat'. Below the pipeline is a small inset square titled 'the latent map (smooth — no gaps)' with axes z-one horizontal and z-two vertical; several violet dots are scattered near the origin, one marked at (0.3, -0.8) with a small arrow along z-one toward (0.9, -0.8) labeled 'slide, smile widens', and a differently-styled mint star at (-0.5, 0.4) labeled 'sample a NEW point, a new face'.
An encoder compresses an image into a latent map; a decoder rebuilds images from points on that map.

The encoder compresses an image into a latent code, which is a small vector on a compact map. The decoder rebuilds the image from that code. Train both so encode-then-decode approximately restores the input.

$$x \;\xrightarrow{\text{encode}}\; z \;\xrightarrow{\text{decode}}\; \hat{x}$$

In words: the image $x$ is encoded down to the latent code $z$, then decoded back up to $\hat{x}$, read "x-hat," the model's rebuilt version of the image. The hat means "the model's version," the same convention we used for the prediction $\hat{y}$ in earlier chapters; here the model is rebuilding an input rather than guessing a label.

Latent point in the illustrationDecoder result
$(0.3,-0.8)$Original reconstruction
$(0.9,-0.8)$Wider smile
$(-0.5,0.4)$A new face

A second training pressure keeps the latent codes near a smooth distribution around the origin, so nearby samples tend to decode plausibly. In our illustration, a face has code $z=(0.3,-0.8)$. Moving its first coordinate from $0.3$ toward $0.9$ widens the smile; a new point $z=(-0.5,0.4)$ decodes to a new face. These are illustrative coordinates, not universal smile controls.

That is how a VAE generates: sample a latent point and decode it. VAEs are stable to train and give an organized map, though averaging over possible reconstructions can produce blurry outputs.

GANs: the forger and the detective

A generative adversarial network (GAN) trains two networks against each other.

A diagram of an adversarial loop. On the left, a small pile of muted chips labeled 'random noise' feeds an arrow into a box labeled 'generator (the forger)' with a mint border; its output arrow leads to a face glyph tagged mint 'fake'. In the center-right, a separate face glyph tagged blue 'real (from training data)'. Both the fake and the real feed via arrows into a box labeled 'discriminator (the detective)' with an amber border, whose output is a small dial reading 'real or fake?' currently at 'about 0.5 — can't tell'. A curved red feedback arrow loops from the discriminator back to the generator, labeled 'what gave you away'. A bottom caption strip reads 'they train against each other until the fakes fool the detective — then keep the forger, discard the detective'.
A generator makes fake samples, while a discriminator learns to distinguish them from real training examples.

The generator is the forger: random numbers go in, a fake image comes out. The discriminator is the detective: it sees real images mixed with fakes and learns which is which. Both use gradient descent, with opposing goals.

  1. Detect: early fakes are easy to recognize.
  2. Improve: the discriminator’s error signal teaches the generator what gave it away.
  3. Repeat: better fakes demand a sharper discriminator, which supplies harder feedback.
  4. Generate: keep the trained generator; generation no longer needs the discriminator.

In the ideal balanced outcome, the detective can do no better than a coin flip, outputting about $0.5$. That is the goal, not a guarantee that every training run reaches it.

GAN images can be sharp because blur gives the detective a clue. But the contest is delicate: in mode collapse, the generator repeats one successful image or a narrow set instead of covering the distribution; the players can also keep chasing each other without settling.

Diffusion: learning to un-blur static

Teach a network to predict noise, then use it repeatedly to turn fresh static into an image.

A horizontal filmstrip of five small square tiles left to right. Tile 1 shows a clear mint smiley glyph labeled 'clean'. Tile 2 shows the same glyph with a light speckle of gray noise, labeled 'a little noise'. Tile 3 shows the glyph half-buried in denser speckle, labeled 'more'. Tile 4 shows the glyph barely visible under heavy speckle, labeled 'more'. Tile 5 shows pure random gray speckle with no glyph, labeled 'pure static'. A top arrow spans the strip pointing left to right in muted, labeled 'forward: add noise (easy — free training data)'. A bottom arrow spans the strip pointing right to left in mint and thicker, labeled 'generate: learned denoising, one small step at a time'. A violet callout box near tile 3 reads 'worked step: noisy 2.8 minus predicted noise 0.8 = clean 2.0'.
Adding noise creates training examples; generation follows a learned denoising path from fresh static.

Worked example: un-noising one pixel

The clean pixel value is $x = 2.0$. This step's noise happened to be $+0.8$, so the noisy pixel is $2.8$:

$$x_{\text{noisy}} = x_{\text{clean}} + \text{noise} \;=\; 2.0 + 0.8 = 2.8$$

In words: the noisy pixel is the clean pixel plus the noise that got added, so clean $2.0$ plus noise $0.8$ makes the noisy value $2.8$.

  1. CleanPixel value $2.0$.
  2. Noisy$2.0+0.8=2.8$.
  3. Denoised$2.8-0.8=2.0$.

The network is shown only the $2.8$ and predicts the noise: $+0.8$. Subtract that guess, and the clean pixel is recovered:

$$x_{\text{clean}} = x_{\text{noisy}} - \text{predicted noise} \;=\; 2.8 - 0.8 = 2.0$$

In words: the clean pixel is the noisy pixel minus the noise the network predicted, so noisy $2.8$ minus predicted $0.8$ gives back the clean $2.0$. A real model does this for every one of a million pixels at once, and it never sees the clean answer at generation time. It only ever predicts the noise.

This additive pixel example isolates the arithmetic; a full sampler also uses a noise schedule and scaling. Repeating small steps, a few hundred of them in this illustration, allows gradual refinement without the GAN's adversarial contest.

  1. Start clean: choose a training image.
  2. Add noise: gradually obscure it until it resembles static.
  3. Learn: use noisy examples and their known noise to train a predictor.
  4. Start fresh: draw new random static for generation.
  5. Denoise: repeatedly apply the trained predictor, building a new image.

For text-to-image, encode “a red bicycle on the moon” into an embedding and supply it at every denoising step to steer the result toward the prompt. Meaning as Direction explains the text vectors. Conditioning guides a sample; it does not guarantee an exact picture.

How AI Makes Images gives the full derivation: noise schedules, prediction, compression, and text guidance.

A picture is destroyed one step at a time, and a network that learned to undo a single step walks static back into a boat. (2:56)

Choosing a generator

Choose by the kind of control and the training tradeoff the task needs. All three families share one aim, learning the distribution and then sampling from it, and reach it by different routes: a smooth latent map, an adversarial contest, or repeated refinement.

FamilyUseful propertyTradeoff
VAEA compact map to sample and exploreStable training; reconstructions can be soft.
GANSharp samples in a focused domainA delicate contest can become unstable or collapse.
DiffusionRepeated, controllable denoisingMany small generation steps instead of one pass.

Where you'll meet this

WhereWhat it looks likeChapter link
Text-to-imageA sentence embedding steers denoisingMeaning as Direction
Speech, music, missing data, protein structures, drug candidatesLearn plausible data and sample or fill gapsDistribution and sampling
Language modelsSample the next token from a learned distributionThe prediction game
Useful assistantsFeedback steers behavior beyond matching examplesLearning by doing

What you now know

  • Generative models learn data distributions and draw new samples from them.
  • The tilde in $x\sim p(x)$ means “drawn from.”
  • VAEs sample a compact latent map and decode its points.
  • GANs train a generator against a discriminator, then keep the generator.
  • Diffusion repeatedly predicts noise to build an image from fresh static.
  • Text embeddings condition generation, while learned patterns can include biases and convincing falsehoods.

Where we’re headed. Matching examples does not establish that an answer is helpful or an action safe. These models inherit patterns and biases, and can invent convincing falsehoods. Next, learning by doing uses rewards and penalties to shape behavior: playing Go, controlling robots, or refining an assistant.