Code Architecture — Wasserstein GAN Notebook

Overview

The notebook implements the WGAN algorithm (the paper’s Algorithm 1) from scratch in PyTorch on a toy 2D data set, a ring of 8 Gaussians. It tracks the critic’s estimate of the Wasserstein distance and how close the samples are to the 8 modes, then trains a standard GAN on the same data and tracks its estimate of the Jensen-Shannon divergence the same way. It runs on a GPU if there is one (the saved run used a Tesla T4).

Section-by-Section Breakdown

1. Setup & Imports

  • PyTorch, NumPy, matplotlib; nothing is installed
  • Seed 42; CUBLAS_WORKSPACE_CONFIG set before importing torch, cuDNN deterministic, torch.use_deterministic_algorithms(True, warn_only=True)
  • Figures as JPEG at 80 dpi; on Kaggle an atexit hook copies the executed notebook to executed_solution.ipynb

2. Toy Data Distribution

  • 8 mode centres on a circle of radius 2; sample_real(n) picks a mode at random and adds Gaussian noise with standard deviation 0.02
  • mode_distances(samples): distance from each sample to each mode centre, shape [n, 8]
  • quality(samples): mean distance to the nearest mode, share of samples within 0.1 of a mode, and modes covered (more than 5 samples within 0.5)
  • Plots 2,000 real samples and prints their quality (0.025, 100%, 8/8)

3. Generator Network

  • Generator: Linear(2→128) → LeakyReLU(0.2) → Linear(128→128) → LeakyReLU(0.2) → Linear(128→2) → Tanh, output × 3; 17,154 parameters

4. Critic Network

  • Critic: the same MLP shape with a single real-valued output and no sigmoid; 17,025 parameters

5. WGAN Training Loop (Algorithm 1)

  • N_CRITIC = 5, CLIP_VALUE = 0.01, LR = 5e-5, BATCH_SIZE = 64: the paper’s defaults
  • optim.RMSprop for both networks, as in Algorithm 1 (the paper found Adam unstable at times, Section 4.2)
  • 100 critic steps during the first 25 generator iterations and every 500, as in the authors’ released code
  • Each critic step: loss = mean(critic(fake)) − mean(critic(real)), RMSProp step, clamp all critic weights to [−0.01, 0.01]
  • Generator step: loss = −mean(critic(fake))
  • N_ITERATIONS = 10000 generator iterations; every 250, right after the critic update, evaluate_wgan() computes mean(critic(real)) − mean(critic(fake)) on 4,096 fresh points of each kind and the quality measures; samples are saved at iterations 0, 500, 1000, 2500, 5000 and 10000

6. Results

  • 6.1 The critic’s estimate and the mean distance to the nearest mode on two axes, and their Spearman rank correlation (spearman()) over all 41 evaluation points
  • 6.2 Generated vs real samples at the six saved iterations

7. Comparison: Standard GAN

  • Discriminator: the critic’s MLP with a sigmoid output
  • Same generator initialisation (seed 42), Adam (learning rate 2e-4, β1 = 0.5), batch size 64, one discriminator step per generator step, generator loss −log D(G(z))
  • Every 250 iterations, evaluate_vanilla() records the JS estimate ½·(E[log D(real)] + E[log(1 − D(fake))]) + log 2, as in the paper’s Figure 4, and the quality measures
  • 7.1 Both training signals next to the mean distance, with their Spearman correlations
  • 7.2 Final samples of both models next to real ones, and a table of the quality measures

8. What the Paper Found

  • The paper’s findings with section and figure references, labelled as not reproduced here

9. Summary

  • ### What the run above shows with the saved run’s numbers

Key Functions/Classes

Component Description
sample_real(n) Samples n points from the 8-mode ring
quality(samples) Mean distance to the nearest mode, share within 0.1, modes covered
Generator MLP from 2D noise to a 2D point
Critic MLP from a 2D point to a real-valued score (no sigmoid)
Discriminator MLP from a 2D point to a probability (for the standard GAN)
evaluate_wgan() Critic estimate and generated samples on 4,096 points
evaluate_vanilla() JS estimate and generated samples on 4,096 points
spearman(a, b) Rank correlation between two series

Data Flow / Shapes

Noise z: [64, 2]  →  Generator  →  fake: [64, 2]
Real:    [64, 2]  →  Critic     →  scores: [64, 1]
Fake:    [64, 2]  →  Critic     →  scores: [64, 1]
Critic loss    = mean(fake scores) − mean(real scores)   (then clip weights to ±0.01)
Generator loss = −mean(critic(fake))

Deliberate Simplifications vs Full Paper

  1. Toy data: a 2D ring of 8 Gaussians instead of 64×64 LSUN bedroom images (Section 4.1).
  2. Small networks: MLPs with two hidden layers of 128 units instead of DCGAN networks or 4-layer MLPs with 512 units (Sections 4.2 and 4.3).
  3. Short training: 10,000 generator iterations; the WGAN stopped improving at a mean distance of about 0.42 from the modes.
  4. Baseline settings: the standard GAN uses Adam with learning rate 2e-4, the usual DCGAN settings, not the WGAN’s RMSProp settings, so the two models differ in more than the loss.
  5. Simple quality measures: distance to the nearest mode and modes covered, instead of looking at image samples.