4.5.6 · D2Generative Models

Visual walkthrough — Generative Adversarial Networks (GAN) framework

2,587 words12 min readBack to topic

This page rebuilds the single most important fact about the GAN framework entirely from pictures: when the counterfeiter becomes perfect, the detective is forced to flip a coin. We will earn every symbol before we use it. A twelve-year-old who has never seen an integral should be able to follow from line one.


Step 0 — What are we even measuring?

WHAT. Before any formula, meet the two characters as piles of data.

WHY. Everything downstream is a comparison of two piles. If you can see the piles, the algebra later is just bookkeeping.

PICTURE. Look at Figure s01. The cyan curve is — read it as "how often real examples land at value ." Tall = common, low = rare. The amber curve is — the same idea for the generator's fakes. The horizontal axis is "data space" (imagine every possible image squashed onto one line).

Figure — Generative Adversarial Networks (GAN) framework

Our entire goal, stated visually: push the amber curve until it sits perfectly on the cyan curve.


Step 1 — The generator, and where its fakes come from

WHAT. Before we can score anything, we must build the machine that makes the fakes: the generator , fed by a noise distribution .

WHY. The value function in Step 2 will contain the symbols , and . The contract says: never use a symbol before it is defined and pictured. So we define them here, first.

PICTURE. In Figure s02 a random seed (a little dot picked from the noise cloud on the left) is pushed through the box and lands as a fake sample on the -axis. Different seeds land in different places; collect all their landing spots and you rebuild the amber curve from Step 0.

Figure — Generative Adversarial Networks (GAN) framework

Step 2 — What the discriminator is, drawn as a dial

WHAT. We introduce — the discriminator — as a dial reading attached to every point .

WHY. People get lost because is a network with millions of weights. For this proof, none of that matters. All that matters is: at each , outputs one number in . So we draw it as a needle.

PICTURE. In Figure s03, at three sample points the white needle shows : near the cyan bump it points to ("surely real"), near the amber bump it points to ("surely fake"), and where the curves overlap it sits mid-scale.

Figure — Generative Adversarial Networks (GAN) framework

The symbol is now earned: it is just the needle position at location .


Step 3 — Scoring the detective: the value function

WHAT. We build , the number that says how well the detective is doing.

WHY. To find the "best" detective we need a scoreboard. Two things must go on it: reward for calling real things real, and reward for calling fakes fake.

PICTURE. Figure s04 splits the scoreboard into two coloured bars. Read the equation left-to-right as the picture is coloured (every symbol below was defined in Steps 0–2):

Figure — Generative Adversarial Networks (GAN) framework

Term by term, right where each symbol lives:

  • ::: "average over real samples." = expectation = weighted average, weighting each by how common it is under .
  • ::: the reward when is real. If , (best, no penalty). If , (catastrophe). So the detective is pushed to raise on reals.
  • ::: "average over noise seeds," drawn from the noise distribution we defined in Step 1.
  • ::: a fake sample — the generator's landing point for seed .
  • ::: probability the detective assigns to "this fake is fake." We want this near .
  • ::: reward for correctly doubting a fake.

The full game is : the detective climbs , the forger drags it down.


Step 4 — Freeze the forger, find the perfect detective

WHAT. Hold fixed (freeze the amber curve). Ask: what is the best the detective could possibly pick?

WHY. The minimax is two moving targets — impossible to attack at once. So we do the classic trick: solve the inner problem (best for a frozen ) first. This is the same "one variable at a time" idea behind alternating optimisation.

PICTURE. Figure s05 rewrites the fake-reward term so it lives on the same -axis as the reals. Here is why we may do that. The fake-reward term averages a function of over seeds . But by the push-forward idea from Step 1, drawing a seed and mapping it through is the same as drawing a landing point directly from . So the average over seeds equals an average over landing points:

  • Middle step ::: an expectation is an integral against the density — .
  • Right step (change of variables ) ::: this is exactly the definition of the push-forward density — the amount of -mass that lands near is . So every is relabelled by where it lands, and becomes .

The real-reward term is already an integral, . Both now sit on the same -axis, so we combine them:

Figure — Generative Adversarial Networks (GAN) framework
  • ::: "add up over every point ." An integral is just a sum over infinitely many thin slices.
  • The bracket is a separate little contest at each single . Nothing at point talks to point . So we can optimise one at a time — this is the key that unlocks everything.

Step 5 — The one-point tug-of-war

WHAT. Fix a single location . Let , , and let be the dial we get to turn. Maximise

WHY. By Step 4 the whole integral is maximised by winning every one of these tiny 1-D contests. So the giant functional problem collapses to ordinary calculus in one variable — the kind you can graph.

PICTURE. Figure s06 plots against for a sample and . It is a smooth hill with exactly one peak. The two anchors — pulling the dial up toward , pulling it down toward — balance at the top.

Figure — Generative Adversarial Networks (GAN) framework

To find the peak we ask "where is the slope zero?" That is why we take a derivative — the derivative is the tool that answers "which way is uphill, and where does uphill stop?"

Set the two pulls equal and solve:

Putting the names back:


Step 6 — Every case of , checked by eye

WHAT. Walk through all sign/zero/degenerate cases of so no scenario surprises the reader.

WHY. The contract: cover every case. A fraction with sums in the denominator has three interesting regimes and one forbidden one.

PICTURE. Figure s07 colours the -axis by which regime it is in.

Figure — Generative Adversarial Networks (GAN) framework
Case Meaning Picture region
Only reals here () genuine data, no fakes pure cyan zone
Only fakes here () forger hallucinated a region pure amber zone
Equal overlap () curves cross the shrug line
No data at all () neither curve is here undefined white gap

Note always (a share of a pie can't exceed the whole pie), so it is a valid probability — the definition of from Step 2 is never violated.


Step 7 — Now let the forger move: the finish line

WHAT. Plug back in and ask what the forger's best move does.

WHY. We froze in Step 4. Un-freeze it: the generator now drags down, i.e. drives toward . We show the endpoint.

PICTURE. Figure s08 shows the amber curve sliding until it lies exactly on the cyan curve. When everywhere, substitute into :

Figure — Generative Adversarial Networks (GAN) framework

The needle from Step 2 collapses to the middle of the dial everywhere. The detective, facing a perfect forger, can do no better than a coin flip. This flat is the Nash equilibrium of the game: neither player can improve by moving alone.


The one-picture summary

Figure s09 stacks the whole journey: two piles → a dial → a scoreboard → a one-point hill → its peak → curves merging → the flat line.

Figure — Generative Adversarial Networks (GAN) framework
Recall Feynman retelling — say it to a friend

"Picture two heaps of sand on a number line: real photos in cyan, fake ones in amber. The amber heap is built by a machine that grabs random dice-throws (the noise ) and flings each one to a spot on the line — pile up where they land and that's the amber curve. A judge stands over the line holding a dial that reads ('fake') to ('real'). We score the judge: full marks for pointing the dial up where cyan sand is, and down where amber sand is. If you freeze the forger and ask 'what dial setting scores best right here?', the answer is embarrassingly simple — it's just what fraction of the sand right here is real, . That's the best any judge can ever do. Now let the forger pile his amber sand exactly on top of the cyan. At every spot, half the sand is real and half is fake, so the best dial setting is — a shrug — everywhere. The forger has won by making the judge helpless, and that helpless flat line at is precisely the goal of training a GAN."

Recall Quick self-test

What is in words? ::: The fraction of the probability mass at that is real: . What is and is it learned? ::: The fixed noise distribution we draw seeds from; it is not learned — only and are. Why can we swap the average over for an integral over with ? ::: Because is the push-forward of through : sampling then mapping through is the same as sampling from . Why can we optimise one point at a time? ::: Because is an integral of independent per-point terms — nothing at affects the contest at any other . What does everywhere mean? ::: ; the generator's fakes are indistinguishable from real data — the Nash equilibrium. Where is undefined and why is that OK? ::: Where ; no sample lands there, so it contributes to .