4.5.9 · D3Generative Models

Worked examples — DCGAN, WGAN, StyleGAN

3,191 words15 min readBack to topic

This page is a workbench. We take the ideas from the parent topic and we run the numbers — every sign, every corner case, every degenerate input — so that when you meet one of these in an exam or in code, you have already seen it.

Before we start, four tiny reminders written from zero, so no symbol surprises you:


The scenario matrix

Every problem this topic can throw lands in one of these cells. The worked examples below are each tagged with the cell they cover.

# Case class What makes it tricky Example
A Overlapping distributions small and positive Ex 1
B Disjoint distributions, far apart JS saturates but grows Ex 2
C Zero / identical distributions (degenerate) Ex 3
D Sign of the critic loss Which way does the number move? Ex 4
E Lipschitz violation Unbounded critic breaks everything Ex 5
F Weight-clipping arithmetic The blunt instrument Ex 6
G Transposed-conv size (normal) Doubling resolution Ex 7
H Transposed-conv degenerate (stride 1) Resolution doesn't grow Ex 8
I Real-world word problem Translate story → Ex 9
J Exam twist Combine JS-vs- + gradient Ex 10
K WGAN-GP gradient penalty Softer Lipschitz enforcement Ex 11

Example 1 — Cell A: two overlapping 1-D piles

Step 1 — Understand the pile. Each distribution is a single grain of mass . 's grain sits at , 's grain sits at . Why this step? Wasserstein is about moving mass; you must first see where the mass actually is.

Step 2 — There is only one way to couple them. With one grain each, the only transport plan moves that whole grain from to . Why this step? is an infimum over all couplings — but with single points there is exactly one coupling, so the infimum is trivial.

Step 3 — Cost = mass × distance. Why this step? This is literally the definition with a degenerate .

Verify: Units are "distance" (position units), correct for a distance metric. Move the grain the other direction and you'd get — symmetric, as a distance must be. ✓


Example 2 — Cell B: disjoint piles, and why JS goes blind

Figure — DCGAN, WGAN, StyleGAN

Figure guide: the horizontal axis is (how far apart the piles are); the vertical axis is the divergence value. The amber V-shaped curve is — it rises steadily as the piles separate. The flat cyan horizontal line is , pinned at for every non-zero .

Step 1 — Wasserstein. Same single-grain logic as Ex 1: Why this step? One grain must travel distance . This is the amber V-shaped curve — steadily rising, never flat.

Step 2 — Jensen–Shannon. When the two grains sit on different points (any ), the distributions share no overlap, so Why this step? JS measures overlap. No overlap ⇒ it hits its maximum constant and stays there no matter how far apart. That's the flat cyan horizontal line — completely level.

Step 3 — Take the slope (gradient) of each.

  • — always , never zero (except the kink at 0). Points the generator home.
  • for all . Says nothing.

Why this step? Training is following a gradient. A zero gradient = no learning signal.

Verify: At : (matches the moving-a-grain intuition of Ex 1), . Slope of is ; slope of is . ✓ This is the entire reason WGAN exists.


Example 3 — Cell C: the degenerate zero case

Step 1 — Couple every grain to itself. Choose the transport plan that maps each point to the same point (). Why this step? We need a valid coupling to upper-bound the infimum; the identity map is always valid when the marginals are equal.

Step 2 — Distance moved is zero. Why this step? No grain moves, so total work is , and always — an infimum of is the minimum.

Verify: A distance metric must satisfy . ✓ Sanity: the critic loss also when both expectations sample the same distribution. ✓


Example 4 — Cell D: the sign of the critic loss

Step 1 — Wasserstein estimate is the difference. Why this step? Kantorovich–Rubinstein duality says the maximized difference is the Wasserstein distance estimate.

Step 2 — The training loss is the negation. Why this step? Optimizers descend, so "maximize " is coded as "minimize ".

Step 3 — Interpret the sign. A large positive ⇒ a very negative ⇒ critic is confidently separating real from fake. Why this step? This is exactly the sign-flip students trip over: a "good" critic shows a negative loss number, which looks alarming but is correct.

Verify: . If critic couldn't tell them apart, both scores equal ⇒ , matching Ex 3's . ✓


Example 5 — Cell E: a Lipschitz violation blows up

Figure — DCGAN, WGAN, StyleGAN

Figure guide: horizontal axis is the input , vertical axis is the critic output . Three straight lines through the origin: the cyan 45° line is (the legal ceiling), the dashed amber line is (too steep — illegal), and the faint white line is (legal but gentle). Any line steeper than the cyan one violates 1-Lipschitz.

Step 1 — Compute the unconstrained gap. Why this step? With no ceiling on , multiply the critic's weights by and the estimate scales by — the number stops meaning "distance." Steeper amber lines in the figure keep pushing the gap larger without bound.

Step 2 — Why the critic wants a negative slope. The critic maximizes . This expression is largest when is as negative as allowed. So the optimal critic picks (steepest legal downhill ramp), giving . Why this step? The supremum in Kantorovich–Rubinstein is a maximization. Since real mass sits left of fake mass here, the critic scores left-points higher — a downward-sloping . This is the sign detail students skip: the optimal is , not .

Step 3 — At the optimal legal slope : Why this step? The constrained optimum recovers the true Wasserstein distance from Ex 2. The constraint isn't red tape — it's what makes the number a distance. (The absolute value in Ex 2 is exactly this: picks whichever slope sign, or , makes the difference positive.)

Verify: With , from Ex 2. With (illegal) you'd get a fake "." The constraint restores meaning; the negative slope maximizes the objective. ✓


Example 6 — Cell F: weight-clipping arithmetic

Step 1 — Apply element-wise.

  • (pinned high)
  • inside unchanged
  • (pinned high)
  • inside unchanged

Why this step? Clipping is a crude enforcement of Lipschitz: bound the weights ⇒ bound the gradient (chain rule) ⇒ roughly bound the slope.

Step 2 — Count the pinned. Two of four weights hit the boundary. Why this step? This exposes clipping's known flaw: weights pile up at , wasting capacity — the reason WGAN-GP (Ex 11) later replaced clipping with a gradient penalty.

Verify: Only entries with change: that's and → exactly pinned. ✓


Example 7 — Cell G: transposed-conv doubles a feature map

Figure — DCGAN, WGAN, StyleGAN

Figure guide: on the left, a coarse cyan grid (the input feature map). An amber arrow labelled ConvTranspose k=4 s=2 p=1 points right to a finer white grid (the output). The picture shows the side length doubling as the layer upsamples.

Step 1 — Plug into the size formula. Why this step? This is the exact rule for a ConvTranspose2d output side; the figure shows the tiles fanning out into a canvas.

Step 2 — Arithmetic. Why this step? Confirms the "double the resolution" claim: .

Verify: . The full chain each obeys this same formula. ✓


Example 8 — Cell H: the degenerate stride-1 mistake

Step 1 — Plug in. Why this step? Same formula, only changed. Notice the output side went : it crawls up by one pixel, not doubling.

Step 2 — Diagnose. To reach from you'd need dozens of these crawling layers — practically the generator gets stuck near its seed resolution. Why this step? This is exactly the parent's "Mistake 2": stride-1 for upsampling. The fix is (Ex 7).

Verify: , so stride-1 does not double resolution. To double you must use . ✓


Example 9 — Cell I: a real-world word problem

Step 1 — Match nearest-to-nearest (the optimal coupling). Move t from (distance ) and t from (distance ). Why this step? is the infimum — the cheapest plan. Sorting both piles and matching in order is optimal on the line.

Step 2 — Total cost = Σ mass × distance. Why this step? Direct evaluation of with the chosen .

Step 3 — Check the crossover plan is worse. Moving (dist ) and (dist ) costs — three times more. Why this step? Confirms our plan is the infimum, not just a plan.

Verify: Min cost ; crossover . Units tonne·km, a valid "work" quantity. ✓


Example 10 — Cell J: exam twist (combine everything)

Step 1 — (a) Diagnose the false conclusion. A zero JS gradient at is the saturation trap (Ex 2), not convergence. The true remaining distance is Why this step? Convergence means (Ex 3). Here , so the model is far from done — JS just went blind. Answer: False.

Step 2 — (b) Wasserstein estimate. Why this step? Kantorovich–Rubinstein again (Ex 4): the maximized difference of critic scores is the Wasserstein estimate.

Step 3 — (b) Code loss (the sign flip). Why this step? Optimizers descend, so "maximize " is coded as "minimize " (Ex 4). A large positive ⇒ a very negative ⇒ the critic is separating real from fake well.

Verify: from ; ; . Consistent with Ex 2 and Ex 4. ✓


Example 11 — Cell K: WGAN-GP gradient penalty

Step 1 — Understand why gradient norm is the target. A 1-Lipschitz function has slope magnitude at most everywhere; the optimal WGAN critic actually has slope exactly along the transport path. So instead of clipping weights (Ex 6, the blunt instrument), we nudge toward . Why this step? This is the smooth replacement for clipping — it enforces the same 1-Lipschitz idea (Ex 5) without pinning weights to .

Step 2 — Plug the measured norm into the penalty. Why this step? Direct evaluation. The gap is squared (so over- and under-shoot both cost) and scaled by .

Step 3 — Check the degenerate case. If the slope were exactly , the penalty is — no cost, matching the ideal 1-Lipschitz critic. Why this step? Confirms the penalty rewards precisely the constraint we want, and never punishes a perfect critic.

Verify: ; at norm the penalty is . Units are "loss" (same as critic loss), so it adds cleanly to . ✓


Recall Quick self-test

Wasserstein of at vs at ::: between two disjoint point-masses ::: Slope of for ::: (never zero — usable gradient) Transposed-conv output for in=8,k=4,s=2,p=1 ::: Same layer but stride=1, in=4 ::: (does NOT double — the bug) Critic scores real, fake: the code loss ::: Clip to : how many pinned ::: Optimal critic slope for real-left-of-fake (, 1-Lipschitz) ::: WGAN-GP penalty at norm ::: :::