Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Mask design for coupling flows

A coupling layer touches only its active half, so the mask has to change from layer to layer for every coordinate to be both transformed and used as context

03 · Mask design for coupling flows

Open In Colab

A coupling layer (1) transforms its active half xB\vect{x}_B and copies its passive half xA\vect{x}_A (notebook 00). One layer can therefore change only half of the coordinates, and a stack whose layers all use the same split, the same mask, never changes the passive coordinates at all, however deep it is. The mask decides which coordinates are passive, and changing it from layer to layer is what lets a stack of couplings transform every coordinate: each one has to be transformed in some layers and condition the others in the rest.

Python implementation: CPython
Python version       : 3.13.12
IPython version      : 9.12.0

jax               : 0.12.1
numpy             : 2.4.3
optax             : 0.2.8
flowjax           : 19.1.0
pipekit_train     : 0.3.4
gauss_flows_models: 0.3.1

Compiler    : Clang 21.1.4 
OS          : Linux
Release     : 6.8.0-1044-azure
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

The data are the two moons of Part 4, standardised and split into Nt=3000N_t = 3000 training rows and Nv=1000N_v = 1000 held-out rows, here in float64.

1. The same mask in every layer

Write f=fK∘…∘f1f = f_K \circ \ldots \circ f_1 (1) for a stack of KK coupling layers. If every layer has the same passive half AA, each copies xA\vect{x}_A to its output, and so does the stack:

f(x)A=(fK∘…∘f1)(x)A=xAf(\vect{x})_A = (f_K \circ \ldots \circ f_1)(\vect{x})_A = \vect{x}_A

The flow’s latent then keeps the passive coordinates’ data marginal, and the base N(0,I)\mathcal{N}(\vect{0}, \mat{I}) scores them as if they were already standard normal. One function builds a stack of K=4K = 4 mixture-CDF couplings, with or without a flip of the two coordinates after each layer; the flip is the subject of section 2.

A fresh coupling is the identity (notebook 00), so both stacks start as the identity and only training can tell them apart. A fresh mixture-CDF coupling has a second problem: its zero readout gives all eight components the same parameters, every component then gets the same gradient, and the layer trains as an affine coupling, a bug in gauss_flows (gauss_flows#265). We break that symmetry by moving every weight of a stack by 0.1ε0.1 \varepsilon, with ε∼N(0,1)\varepsilon \sim \mathcal{N}(0, 1) drawn for each weight, before training, with the helper of notebook 00.

The training follows Part 4: pipekit-train runs Adam on the mean negative log-likelihood (1), with a one-cycle cosine learning rate peaking at 3×10−33 \times 10^{-3} and the gradient clipped to a global norm of 1, for 2,000 steps on batches of 512 rows.

We train the fixed-mask stack and map the held-out rows to the latent.

first latent coordinate equal to the first data coordinate: True
held-out NLL: 1.840

After 2,000 steps the first latent coordinate is still the first data coordinate, exactly, as (1) says. The excess kurtosis γ2,d\gamma_{2,d} (2), which is 0 for a normal, shows what that costs: the first data coordinate of two moons is flatter than a normal, and the stack passes it on unchanged.

data             : variances [1.026 0.963], excess kurtosis [-0.919 -1.171]
fixed-mask latent: variances [1.026 0.999], excess kurtosis [-0.919  0.15 ]

2. Flipping the coordinates between layers

The fix is to change the mask from layer to layer. With a permutation Π\gmat{\Pi} of the coordinates after each coupling, here the flip that swaps the two, a coordinate that is passive in one layer is active in the next:

f=(Π∘fK)∘…∘(Π∘f1)f = (\gmat{\Pi} \circ f_K) \circ \ldots \circ (\gmat{\Pi} \circ f_1)

A permutation has ∣det⁡Π∣=1\vert \det \gmat{\Pi} \vert = 1, so it adds nothing to the log-determinant (4) and has no parameters: the two stacks have exactly the same parameters, and differ only in which coordinates each coupling transforms.

trainable numbers: fixed mask 7648, flipped 7648

We train the flipped stack with the same settings.

flipped latent   : variances [1.018 0.994], excess kurtosis [-0.103 -0.041]
fixed mask: NLL 1.734 on training rows, 1.840 held out
flipped   : NLL 1.549 on training rows, 1.641 held out
<Figure size 1200x440 with 2 Axes>

Figure 1:Histograms of the first latent coordinate of the held-out rows, against the standard normal density (dashed). With a fixed mask (left) it is the first data coordinate, flat-topped and bumpy; with a flip between layers (right) it is close to the normal.

With the same parameters, flipping the coordinates between layers brings the first latent coordinate close to a standard normal, with an excess kurtosis of −0.10 against the data’s −0.92, and lowers the held-out negative log-likelihood from 1.84 to 1.64 nats. The gap is modest in two dimensions because the fixed-mask stack still models the second coordinate given the first, and loses only the first coordinate’s marginal; with more coordinates, a fixed mask leaves half of them unmodelled. Changing the mask is not a refinement: without it, a coupling flow cannot model part of its input at all.

3. The mask families

Which split to use depends on the structure of the data Dinh et al., 2017. For a vector, the channel-wise mask splits the coordinates into a first and a second half, and a permutation between layers changes the split; coupling_gaussianization_flow puts a flip after each coupling when Dx=2D_x = 2 and a random permutation otherwise. For an image, neighbouring pixels are strongly correlated, so a checkerboard mask makes every active pixel’s neighbours passive, and each transformed pixel is conditioned on its surroundings. The figure shows the three patterns; image flows come later in the curriculum.

<Figure size 1650x440 with 3 Axes>

Figure 2:Masks, with passive coordinates (copied, the conditioner’s input) in blue and active coordinates (transformed) in red. Left: a channel-wise mask on 8 coordinates. Centre: a checkerboard mask on an 8 × 8 image. Right: a channel-wise mask on 4 coordinates over three layers, flipped after each, so every coordinate is active in some layer.

Recap

PieceEquationIn code
a fixed mask copies the passive half through the whole stack(1)couplings chained with no permutation
a permutation between layers changes the mask(2)Flip for Dx=2D_x = 2, Permute otherwise
a permutation costs nothing(4)no parameters, log-determinant 0
channel-wise and checkerboard masks(1)vectors and images
training(1)pipekit-train’s TrainingLoop on negative_log_likelihood

Next. With the pattern, the transformers, the conditioners and the masks in hand, notebook 04 compares coupling flows with the diagonal flows of Part 4 at matched parameter counts.

References
  1. Dinh, L., Sohl-Dickstein, J., & Bengio, S. (2017). Density Estimation using Real NVP. International Conference on Learning Representations (ICLR).