Four affine coupling layers (18.10)–(18.11) turn a standard Gaussian into the two moons. Layers 1 and 3 move only the vertical coordinate, by xB = exp(s(xA)) xB + b(xA), with s and b computed by a small network from the other coordinate; layers 2 and 4 move only the horizontal one. The weights were trained in Python by maximum likelihood (18.4) and are run here in both directions. Forwards (sampling): base points z are pushed through the layers. Backwards (density): click or drag anywhere in the left panel; the point is mapped back layer by layer (each step is a purely horizontal or vertical move) to z = g(x), and ln p(x) = ln N(z|0, I) − Σk sk is the change of variables formula (18.1) with the triangular Jacobian (18.14).
Each conditioner is a tanh network 1 → 32 → 32 → 2 whose outputs are (s, b); its last layer starts at zero, so at iteration 0 every layer is the identity. The flow was fitted to 5000 standardised two-moons points with Adam (4000 steps of 512 points). The left panel shows the density after k layers, pk(y) = N(g1∘···∘gk(y)) ∏ exp(−s), on a 140 × 140 grid over [−2.6, 2.6]²; "mass in the box" is its midpoint sum (the base Gaussian itself has mass 0.981 in this box). The Jacobian shown is that of the inverse of the last applied layer at the probe, computed by central differences: its zero above the diagonal and its diagonal entry exp(−s) are (18.14).
code/ch18_normalizing_flows.py (train_realnvp, CouplingFlow); the weights are in data/ch18-coupling.json and the page reproduces CouplingFlow.log_prob to rounding error.