4.5.7 · D4Generative Models

Exercises — Generator and discriminator dynamics

2,260 words10 min readBack to topic

This page is a self-test. Every problem builds on the parent note on generator and discriminator dynamics. Read the problem, try it on paper, THEN open the collapsible solution. Difficulty climbs from L1 Recognition (can you spot the right formula?) up to L5 Mastery (can you invent and defend a design choice?).

Before we start, one reminder of the four objects we keep using, so no symbol appears un-earned:

Figure — Generator and discriminator dynamics

The value both players fight over is climbs it, sinks it. Keep this line in view — most exercises are just "which piece of this survives?".


Level 1 — Recognition

Exercise 1.1

In the objective , which single term does the Generator's gradient actually depend on, and why does the other term vanish for ?

Recall Solution

What we do: look at each term and ask "does it contain ?".

  • Term one, , only involves real samples and the network . There is no anywhere.
  • Term two, , contains explicitly.

Why the first term vanishes: when we take the gradient with respect to 's parameters , anything not containing is a constant, and the derivative of a constant is . Answer: only sees the second term, .

Exercise 1.2

receives a real image and outputs . What is its contribution to the objective? Is a larger or smaller number better for here?

Recall Solution

. maximizes , so it wants this term as large (closest to ) as possible. Pushing pushes (the largest it can be, since of a probability is ). So larger is better for — and larger means more confident the real image is real. ✓


Level 2 — Application

Exercise 2.1

Early training. For a fake sample, (D confidently calls it fake). Compute the Generator's contribution under the original minimax objective , and under the non-saturating objective .

Recall Solution

Original (minimax) term: This is already very close to — meaning 's objective is nearly flat here, so its slope (gradient) is tiny. Bad news early on.

Non-saturating loss: A large positive loss → a large gradient → actually learns. That's the whole point of switching objectives. See 4.5.06-GAN-training-algorithm for where these plug into the loop.

Exercise 2.2

Using the numerical example's numbers, compute 's loss (the negative of the objective it maximizes) when on a real sample and on a fake.

Recall Solution

's objective value on this pair: 's loss is the negative of that: A small loss → is doing well. This is exactly negative binary cross-entropy with real label and fake label .


Level 3 — Analysis

Exercise 3.1

1-D setup. Real data , generator output . Compute the optimal discriminator at (the midpoint between the two peaks).

Recall Solution

The standard normal density is .

  • .
  • (same, since is exactly halfway).

Interpretation: at the crossover point where real and fake are equally likely, even the best possible discriminator is reduced to a coin flip. Off to the sides it recovers confidence. See figure below.

Figure — Generator and discriminator dynamics

Exercise 3.2

At the global equilibrium , show that for every , and compute the value of the game there. (Hint: substitute back into .)

Recall Solution

Step 1 — collapses to a half. Put into the formula: valid wherever (i.e. everywhere data lives).

Step 2 — plug into . With : Meaning: the game "settles" at value . This is the Nash equilibrium — neither player can improve by moving alone.


Level 4 — Synthesis

Exercise 4.1

Derive from scratch. Starting from the pointwise integrand (a function of a single number for a fixed ), take , set it to zero, and solve.

Recall Solution

What we do & why: for a fixed , choosing is just choosing one number to maximize . Calculus maximization: set the derivative to . The two terms come from and .

Solve:

Is it a max not a min? The second derivative , so is concave — the stationary point is a maximum. ✓

Exercise 4.2

Plug back into and show the generator's objective becomes a Jensen–Shannon divergence up to a constant. Specifically, show and hence that the minimum over is , achieved exactly when .

Recall Solution

Setup. Let , the mixture of real and fake. Then and .

Substitute: Split each : Both densities integrate to , so the last two terms give .

Recognize JSD. By definition , so the two KL terms sum to : Minimum: with equality iff . So the smallest is , reached exactly at — matching Exercise 3.2. ∎


Level 5 — Mastery

Exercise 5.1

The minimax loss gives vanishing gradients early on. The non-saturating loss fixes gradients but is a heuristic, not the true minimax. Wasserstein GAN replaces the whole objective. Argue, using the JSD result from 4.2, why JSD-based training gives poor gradients when and barely overlap, and how WGAN's earth-mover distance avoids this.

Recall Solution

The JSD failure. From 4.2, the optimal- generator objective is (up to constants) . Suppose the fake and real distributions have disjoint supports — e.g. images on two thin manifolds that don't touch (extremely common early in training, and generic for high-dimensional data). Then for the mixture :

  • wherever we have , so ;
  • symmetrically for .

So , a constant, regardless of how far apart the two supports are. A constant has zero gradient — moving closer to produces no signal until they actually overlap. That is precisely the vanishing-gradient pathology.

WGAN's cure. The Wasserstein (earth-mover) distance measures the minimum cost of transporting mass from to . Unlike JSD, it is continuous and gives a distance proportional to the actual separation: two disjoint blobs a distance apart have , not a constant. Its gradient therefore always points toward , even with no overlap. That's why WGAN training is smoother and less prone to the dead-gradient regime.

Exercise 5.2

Design question. You observe that has collapsed onto a single output that keeps fooling . Explain, using the objective, why the standard loss permits this, and propose one mechanism that penalizes it. Defend why your fix targets the actual cause.

Recall Solution

Why the objective permits collapse. minimizes — an expectation over individual samples. Nothing in this term rewards diversity across the batch. If a single point happens to satisfy , then producing only drives the objective as low as producing a rich variety. The loss is blind to how many distinct modes covers; it only cares about per-sample foolingness.

A fix — minibatch discrimination / feature-matching. Give (or the loss) access to batch-level statistics so it can detect "all these samples are suspiciously identical". Concretely, feature-matching sets where is an intermediate -feature. This forces the distribution of 's features to match the distribution of real features — a single repeated output can't reproduce the spread/variance of real features, so it is penalized.

Why this targets the cause: the root problem was that the objective operated per-sample and ignored the set of outputs. Both minibatch discrimination and feature-matching lift the signal from single samples to batch statistics, restoring a term that explicitly cares about diversity. WGAN also helps indirectly by giving healthier gradients, but does not itself forbid collapse — the batch-statistics term does.


Recall Quick self-check ladder

L1 which term does G depend on ::: only L2 non-saturating loss at ::: L3 optimal at the midpoint of two equal-variance Gaussians ::: L3 game value at equilibrium ::: L4 generator objective under ::: L5 why JSD gives no gradient on disjoint supports ::: it locks to the constant