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.

The coupling pattern

Split, condition, transform: why a coupling layer has a triangular Jacobian, a cheap log-determinant and an inverse that never inverts its network

00 · The coupling pattern

Open In Colab

The flows of Part 4 Gaussianize each coordinate with a map of its own: within a layer, the map of one coordinate never looks at the others, and only the rotations between layers mix them. A coupling layer Dinh et al., 2017 lets the map of one coordinate depend on the others. It splits the coordinates into two halves, copies the first, and transforms the second with a one-dimensional map whose parameters a neural network computes from the copied half. This one move gives the layer three properties: its Jacobian is triangular, so its log-determinant is a sum; its inverse never inverts the network; and the network can be as large as we like without changing either.

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

jax             : 0.12.1
numpy           : 2.4.3
equinox         : 0.13.8
flowjax         : 19.1.0
gauss_flows_maps: 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 coupling layer at a glance

A coupling layer acts on a point x∈RDx\vect{x} \in \mathbb{R}^{D_x}. A mask splits its coordinates into a passive half xA∈RDA\vect{x}_A \in \mathbb{R}^{D_A} and an active half xB∈RDB\vect{x}_B \in \mathbb{R}^{D_B}, with DA+DB=DxD_A + D_B = D_x. A conditioner cϕc_\phi, a neural network with weights ϕ\phi, reads the passive half and returns the parameters θ\theta of a transformer TθT_\theta, a monotone one-dimensional map applied to each active coordinate:

zA=xA,θ=cϕ(xA),zB=Tθ(xB)\begin{aligned} \vect{z}_A &= \vect{x}_A, \\ \theta &= c_\phi(\vect{x}_A), \\ \vect{z}_B &= T_\theta(\vect{x}_B) \end{aligned}

The passive half is copied to the output and also feeds the conditioner; the active half is the only part that changes, and how it changes depends on the passive half.

1. A fresh layer is the identity

AffineCoupling uses an affine transformer: the active half is scaled by σB∈R>0DB\gvect{\sigma}_B \in \mathbb{R}^{D_B}_{>0} and shifted by μB∈RDB\gvect{\mu}_B \in \mathbb{R}^{D_B}, both computed by an MLP from the passive half,

zB=σB(xA)⊙xB+μB(xA)\vect{z}_B = \gvect{\sigma}_B(\vect{x}_A) \odot \vect{x}_B + \gvect{\mu}_B(\vect{x}_A)

For Dx=4D_x = 4 the first two coordinates are passive and the last two active. We build one layer and apply it to one point.

x = [-1.184 -0.116  0.173  0.957]
z = [-1.184 -0.116  0.173  0.957]
J =
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

The fresh layer returns its input unchanged and its Jacobian is the identity. That is deliberate: gauss_flows starts its couplings as the identity (all but DeepSigmoidCoupling, see notebook 01) by giving the conditioner’s last layer zero weights (zero_readout), so a deep stack of fresh couplings is the identity too, and training moves it from there. The weights of the last layer still receive a gradient at the first step, so the layer can move away from the identity. A transformer with interchangeable components (the mixture CDF and the deep sigmoid) is the exception: its components all start alike, receive the same gradient and stay alike until that is fixed, so a mixture CDF trains as one Gaussian, which is an affine map, and a deep sigmoid as a line plus one sigmoid (gauss_flows#265), and the later notebooks move its weights a little before training.

To see what a coupling does once its conditioner has learned something, we move every weight of the layer by a random amount, a stand-in for a trained layer. Everything below holds for any weights.

We apply it to the fresh layer.

x = [-1.184 -0.116  0.173  0.957]
z = [-1.184 -0.116  0.114  1.084]
coordinates left unchanged: [0, 1]

The first two coordinates pass through exactly; the last two are scaled and shifted by amounts the conditioner computed from the first two. That conditioning is how a coupling models dependence between coordinates, which no per-coordinate map of Part 4 can do within one layer.

2. The triangular Jacobian

The Jacobian Jf(x)\mat{J}_f(\vect{x}) (2) of a coupling has a fixed pattern. The passive outputs zA=xA\vect{z}_A = \vect{x}_A do not depend on xB\vect{x}_B, and each active output depends on its own active input only through the elementwise transformer, so the Jacobian is block lower-triangular:

Jf(x)=(I0∂zB/∂xAdiag(σB(xA)))\mat{J}_f(\vect{x}) = \begin{pmatrix} \mat{I} & \mat{0} \\ \partial \vect{z}_B / \partial \vect{x}_A & \mathrm{diag}(\gvect{\sigma}_B(\vect{x}_A)) \end{pmatrix}

The determinant of a triangular matrix is the product of its diagonal, so the log-determinant is a sum over the active half, and the conditioner never has to be differentiated for it:

log⁡∣det⁡Jf(x)∣=∑d∈Blog⁡σB,d(xA)\log \vert \det \mat{J}_f(\vect{x}) \vert = \sum_{d \in B} \log \sigma_{B,d}(\vect{x}_A)

We check both against the Jacobian that autodiff builds.

J =
[[ 1.     0.     0.     0.   ]
 [ 0.     1.     0.     0.   ]
 [ 0.343 -0.583  0.312  0.   ]
 [-0.189  0.758  0.     0.354]]
largest entry above the diagonal: 0.0e+00
log |det J|, dense:     -2.201140
∑ log |J_dd|, diagonal: -2.201140
the layer's log_det:    -2.201140
<Figure size 500x440 with 2 Axes>

Figure 1:The magnitude of each entry of the coupling’s Jacobian. The upper-right block is zero, the passive diagonal is one and the active diagonal holds the scales σ_B; the lower-left block, which the conditioner fills, does not enter the determinant.

The block above the diagonal is exactly zero, and the three log-determinants agree to machine precision. This is why coupling flows scale: the log-determinant of a general Dx×DxD_x \times D_x Jacobian costs O(Dx3)O(D_x^3), and that of a coupling costs O(Dx)O(D_x). The conditioner fills only the block below the diagonal, which the determinant ignores, so it can be as large as we like at no cost to the log-determinant.

3. The inverse never inverts the network

Inverting the layer is just as cheap. Given z\vect{z}, the passive half comes back for free, and since it is the conditioner’s input we can recompute the same parameters with a forward pass and invert only the one-dimensional transformer:

xA=zA,θ=cϕ(zA),xB=Tθ−1(zB)\begin{aligned} \vect{x}_A &= \vect{z}_A, \\ \theta &= c_\phi(\vect{z}_A), \\ \vect{x}_B &= T_\theta^{-1}(\vect{z}_B) \end{aligned}

The conditioner, which may be a large network, is only ever run forwards. We round-trip 2,000 points through the layer, with the test of (4).

round trip, max |x̂ - x| = 8.9e-16

The inverse also returns its own log-determinant, which should be the negative of the forward one at matching points.

forward log_det = -2.201140
inverse log_det = +2.201140
sum             = 0.0e+00

The round trip is exact to machine precision and the two log-determinants cancel. Both directions run the conditioner forwards, so sampling and density evaluation cost the same, unlike an autoregressive flow, where one of the two directions has to be computed one coordinate at a time.

4. Swapping the transformer

Nothing above used the affine form of TθT_\theta, only that it acts on each active coordinate on its own and is invertible. MixtureGaussianCDFCoupling uses the Gaussianizing map of a Gaussian mixture from Part 1 as TθT_\theta, with the mixture’s weights, means and scales computed by the conditioner. We check the same three properties on a layer with random weights.

largest entry above the diagonal: 0.0e+00
log |det J|, dense: -6.221491
the layer's log_det: -6.221491
round trip, max |x̂ - x| = 1.2e-06

The Jacobian is triangular and the log-determinants agree, as for the affine layer. The round trip is no longer exact: a mixture CDF has no closed-form inverse, so the layer inverts TθT_\theta by bisection, and the error is the bisection’s tolerance of 10-6, not machine precision. The inverse still never touches the network; only the one-dimensional root find is new. The transformer and the conditioner are independent choices, which is why the next two notebooks can take them one at a time.

Recap

PieceEquationIn code
split, condition, transform(1)AffineCoupling, MixtureGaussianCDFCoupling
the affine transformer(2)AffineCoupling
a fresh layer is the identity (all but the deep sigmoid)zero_readout on the conditioner’s last layer
a triangular Jacobian(3)jax.jacfwd(layer.transform)
a log-determinant that is a sum(4)layer.transform_and_log_det
an inverse that runs the network forwards(5)layer.inverse, bisection for a mixture CDF

One coupling layer transforms only its active half, so a flow alternates the mask from layer to layer, and every coordinate is transformed in some layers and conditions the others in the rest (notebook 03, mask design).

Next. Any monotone one-dimensional map can be the transformer. Notebook 01 goes through the ones gauss_flows offers (affine, mixture CDF, deep sigmoid, rational-quadratic spline) and what each can express.

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