Skip to content

6.3 GANs and normalizing flows (brief)

ready≈ 11.5 hoursNeeds: 6.2
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Flow density warper)
  4. 4 Do
  5. 5 Check

Two other answers to the same question. A VAE (6.2) turns noise into data with a decoder and trains on a bound. GANs and normalizing flows also map noise z to data x with a network, but train it differently. This module is brief: diffusion and flow matching (6.4–6.6) build on the flow idea, and you will not train a GAN (Prince, chapters 15–16).

GANs. A discriminator D learns to tell real samples from generated ones, and the generator learns to fool it (Goodfellow et al. 2014). For a fixed generator the best discriminator is D*(x) = pdata(x) / (pdata(x) + pg(x)), and against it the generator’s objective is 2 JS(pdata ‖ pg) − log 4 (their Proposition 1 and Theorem 1). No density is ever written down, which is why GAN samples can be sharp, and also why training can oscillate or drop modes. In practice the generator minimises −log D(g(z)) instead of log(1 − D(g(z))), which gives it a gradient even when the discriminator wins easily.

Flows. Make the generator invertible, with a Jacobian whose determinant is cheap. Where the generator f⁻¹ stretches space, density thins out by the same factor (where f stretches, it rises), so the change-of-variables formula gives an exact density, log p(x) = log pz(f(x)) + log |det ∂f/∂x|, and training is plain maximum likelihood (Prince, chapter 16). An affine coupling layer keeps half the coordinates fixed and scales and shifts the rest by amounts computed from the fixed half. The Jacobian is triangular, so its log-determinant is the sum of the log-scales, and inverting it needs no network inverse (Dinh et al. 2016, section 3).

The catch. Every layer must be invertible, so a flow keeps the data’s dimension throughout and cannot tear space: one connected Gaussian can only be stretched into two moons with a thin bridge between them (Dinh et al. 2016, figure 1 shows the same smooth warping). Diffusion and flow matching (6.6) keep the continuous, invertible picture but learn it one small time step at a time.

The best discriminator, and what the generator then minimisesstep 1 of 6
  1. V(D,g)=∫pdata(x)log⁡D(x)+pg(x)log⁡(1−D(x)) dxV(D, g) = \int p_{\text{data}}(x) \log D(x) + p_g(x) \log(1 - D(x))\, dx

Flow density warper

Change of variables

Static view on small screens. Open this page on a laptop to interact.

samples from N(0, I), coloured by their angle, and the grid lines z = −2 … 2, moved by no layers yet
the flow's exact density p(x) = N(f(x); 0, I) · |det ∂f/∂x| (square-root colour scale)
average −log p over the training data: flow 1.14 nats, standard normal 2.84

A normalizing flow is an invertible map with a cheap Jacobian. Each coupling layer leaves one coordinate alone and scales and shifts the other by amounts computed from the first, so it is easy to invert and its log-determinant is just the sum of the log-scales. Step through the layers from noise to data: each one bends the plane a little, and six of them fold a round Gaussian into two moons. Because every step is invertible, the right panel's density is exact, which is what lets a flow train by plain maximum likelihood. The thin bridge between the moons is the price of a continuous invertible map: it cannot tear the plane into two pieces.

Two square plots. The left one shows 500 samples from a 2D standard normal, coloured by their angle, and a grid of lines, after the last k of six coupling layers of a small trained flow have been inverted; a slider sets k and a play button steps from pure noise to the finished two-moons shape. The right one shows the flow’s exact density as a heatmap. Buttons draw new noise and overlay the training data. The readout gives the average negative log-likelihood of the data under the flow and under a plain normal, and how much the last inverted layer changed the log-density. Keyboard: Tab to the slider and buttons.

GANs and normalizing flows: edit exercises/stubs/m6_3_flows.py on your own machine. This task needs PyTorch, which the in-page runner does not have.

pip install torch   # CPU build is enough for the tests
mlc check 6.3
Hints · AffineCoupling 0/3 revealed
Hints · log_prob 0/3 revealed

Optionally, train a flow yourself. Open notebooks/6.3-flow.ipynb (a CPU is enough) and paste in your 6.3 file. It trains your flow on two moons and on a ring of eight Gaussians, plots samples and densities, and shows how depth and the tanh on the scale affect training.

1. For a fixed generator, what is the best discriminator?
2. Why do practical GANs train the generator on −log D(g(z)) instead of log(1 − D(g(z)))?
3. Why is an affine coupling layer's log-determinant just the sum of its log-scales?
4. What can a flow give you that a GAN cannot?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.