4.5.6 · D4Generative Models

Exercises — Generative Adversarial Networks (GAN) framework

2,930 words13 min readBack to topic

This page is a ladder. Start at Level 1 (can you recognise the pieces?) and climb to Level 5 (can you invent and repair?). Every problem has a full solution hidden inside a collapsible callout — try first, then reveal.

Everything here builds on Generative Adversarial Networks (GAN) framework. If a symbol below feels unfamiliar, that parent note defines it; here we use it.

The value function we will reuse everywhere:

Read as "the average of over many samples". That is the only thing the expectation symbol means here.


Level 1 — Recognition

Exercise 1.1 — Reading 's output

receives a real photo and outputs . Then it receives a generated fake and outputs . (a) Which output shows is doing its job well? (b) What single number would output on every input once the GAN has fully converged?

Recall Solution

(a) Both outputs are correct behaviour. 's job: high on real, low on fake. on a real photo = "I think it's real" ✓. on a fake = "I think it's fake" ✓. A well-trained-against-a-weak-generator discriminator looks exactly like this. (b) At full convergence the generator matches the data, so genuinely cannot tell them apart. The safest guess for a coin it cannot read is — maximum uncertainty. It outputs on real and fake.

Exercise 1.2 — Naming the two goals

Match each line to its owner (Generator or Discriminator):

  1. wants large for real
  2. wants large for fake
  3. wants small... no wait — which direction does push this term?
Recall Solution
  1. Discriminator — it should say "real" (high) on real data.
  2. Generator — it wants its fakes to fool into saying "real" (high).
  3. The term is large when is small (fake caught) and small when is large (fake fools ). Since wants to fool , wants this term small minimises . wants fakes caught → maximises . Consistent with .

Level 2 — Application

Exercise 2.1 — Compute the discriminator loss

You run a minibatch of . The discriminator gives:

  • reals:
  • fakes:

The discriminator loss (the thing we minimise, so it's averaged) is Compute (natural log).

Recall Solution

Real term : Fake term , using : Sum . Then

Exercise 2.2 — Compute the generator loss (non-saturating form)

Same fakes as above: . The non-saturating generator loss is Compute .

Recall Solution

Notice is large here: is confidently rejecting the fakes, so the generator is under heavy pressure — exactly the "strong gradient signal" the non-saturating trick was designed to keep.


Level 3 — Analysis

Exercise 3.1 — The optimal discriminator, pointwise

For a fixed generator, the parent note shows the optimal discriminator is At a specific point , suppose and . (a) Compute . (b) Interpret the value in one sentence. (c) What does become at a point where ?

About the figure below. The horizontal axis is the ratio — "how much more the generator produces than the real world does." The vertical axis is the optimal score . The blue curve slides from (far left: almost no fakes here, so real) down through the green dot at where (equal amounts → coin flip) toward (far right: mostly fakes). The orange dot is this exercise's point. Takeaway: the best possible discriminator is not a yes/no gate — it is a smooth read-out of the real-vs-fake mixing ratio at each .

Figure — Generative Adversarial Networks (GAN) framework
Recall Solution

(a) (b) At real data is 3× more likely than fake data, so the best any discriminator can do is be 75% confident it's real — that is the true posterior probability of "real". On the figure this is the orange dot at . (c) If then . The two distributions coincide → no information → forced coin-flip. This is precisely the convergence value from Exercise 1.1(b), and on the figure it is the green dot where the curve crosses .

Exercise 3.2 — Value at the optimum reduces to a divergence

(a) Substitute into and show, step by step, that where (Jensen–Shannon divergence) is a symmetric relative of the Kullback-Leibler Divergence, always , equal to only when . (b) What is the global minimum value of over all generators, and which generator achieves it?

Recall Solution

(a) — the algebra, and why it becomes a divergence. Start from the combined-integral form (parent note, change of variables ): Note . Substitute both: The trick: we want a divergence, and divergences compare a distribution to the mixture . So force the denominator to be by writing and pulling out the : Do the same for the term. The two pieces collect (since ) into : Those two remaining integrals are exactly and , and their sum is by definition: Why a divergence appears: plugging in turned the score into "how far apart are and , each measured against their average." That is a distance between distributions — the game literally minimises a statistical distance. (b) Since , the smallest can be is when : Achieved by the generator with — its output distribution exactly equals the real data distribution. That is the theoretical goal of the whole framework.


Level 4 — Synthesis

Exercise 4.1 — Diagnose a training curve

You observe: for many epochs, and , and the generator's samples never improve. Which classic failure is this, why does the generator get stuck, and which loss form rescues it?

Recall Solution

This is the vanishing-gradient failure. When , the original generator objective sits on a flat part of the log curve (its slope ), so is tiny — the generator receives almost no learning signal and freezes. Rescue: switch to the non-saturating loss, maximising (minimise ). When , the slope of is huge (), giving a strong gradient exactly when the generator most needs it. Same optimum (), better dynamics. See the figure comparing the two curves.

About the figure below. The horizontal axis is , the score the discriminator gives to a fake (0 = "caught", 1 = "fooled"). The vertical axis is the generator's loss term. The red curve is the original saturating loss ; notice it is nearly flat on the left (near ) — flat means near-zero slope means near-zero gradient, so a struggling generator gets almost no push (red arrow). The green curve is the non-saturating loss ; it is steep on the same left region (green arrow), delivering a strong gradient precisely when the generator is losing badly. Takeaway: both losses share the same goal, but only the green one gives usable feedback early in training.

Figure — Generative Adversarial Networks (GAN) framework

Exercise 4.2 — Design a stopping condition

You are asked to write an automatic rule that decides when the two-player game has reached Nash Equilibrium. Using only (average on real batches) and (average on fakes), propose a numeric rule and justify it — including how to pick the tolerance and window.

Recall Solution

At equilibrium so on both streams. A sensible rule with tolerance and window epochs: Justification: the parent's numerical trace lands at near convergence — both drifting toward . Requiring both near prevents the false positive where a lazy outputs everywhere while is still bad. How to choose . Batch estimates of are noisy: with batch size and per-sample variance (a probability in ), the standard error of the mean is . For this is . You want a bit larger than the noise floor so genuine equilibrium is not rejected by random jitter, but smaller than the gap you saw during training ( vs the early ). sits in that band for . If your batch is smaller, raise (more noise) or average over several batches first (shrinks the noise floor by ). How to choose . A single lucky epoch can pass by chance; requiring the condition for consecutive epochs makes a false trigger exponentially unlikely (if a random pass has probability per epoch, in a row has probability ). is typical — long enough to reject flukes, short enough not to overshoot into mode collapse. Sensitivity: the rule is robust — too-small or too-large merely makes you stop later (safe); too-large or too-small risks stopping early on a transient, so err on the strict side.


Level 5 — Mastery

Exercise 5.1 — Prove the optimal- formula yourself

For a single point , the integrand of is Show that is maximised at , and confirm it is a maximum (not a minimum).

Recall Solution

Differentiate w.r.t. : Set : Second derivative: Why concavity forces a maximum (the intuition). only tells us the curve is momentarily flat at — that flat point could be a peak, a valley, or a shoulder. The second derivative reads the bending: means the slope is always decreasing as grows. So just left of the slope is positive (curve rising), and just right of the slope is negative (curve falling). A curve that rises then falls has a peak at the turning point — never a valley. Because for every in (not just at ), there is no second turning point and no minimum hiding elsewhere: is the unique global maximum. Substituting recovers .

Exercise 5.2 — Verify the JSD form numerically

Take a toy two-point world. There are only two possible pictures, and .

  • Real distribution: .
  • Generator distribution: .

Compute as a weighted average over the two points (this is what the expectation becomes on a discrete world) and check it equals .

Recall with , and .

Recall Solution

Optimal at each point ():

  • : ; : .

Direct . The expectation over a discrete world is a plain weighted sum. The first term weights by ; the second weights by : With : Via JSD. : at , ; at , . By symmetry , so Direct vs JSD-form — they agree (the tiny gap is rounding; in exact arithmetic they are identical). ✓ The value function at the optimal really is a shifted Jensen–Shannon divergence.


Recall Self-check ladder — reveal after finishing

Which convergence value do all these exercises point to? ::: on both real and fake, where and . Which loss form fixes vanishing generator gradients? ::: Non-saturating: maximise .

Connections