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.

Depth, gradients and residual maps

How much stacking coupling layers buys on a spiral, how the gradient grows with depth at the identity start, and the residual maps that trade the triangular Jacobian for a fixed-point inverse

07 · Depth, gradients and residual maps

Open In Colab

A coupling layer transforms only its active half, so a coupling flow is a stack f=fK∘…∘f1f = f_K \circ \ldots \circ f_1 (1) of layers with alternating masks, and its power comes from depth. This notebook looks at that axis: what depth buys on a curved target, how the gradient that training sees grows as the stack deepens, and the residual maps that give up the coupling’s triangular Jacobian for more freedom in each layer.

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
optimistix        : 0.1.0
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 target is a two-armed spiral: a radius tt drawn uniformly from 0.5 to 3.5, the point (tcos⁡2.5t,tsin⁡2.5t)(t \cos 2.5 t, t \sin 2.5 t) or its mirror image through the origin, and noise of standard deviation 0.08, standardised per coordinate. We draw N=4000N = 4000 points and split them into Nt=3000N_t = 3000 training rows and Nv=1000N_v = 1000 held-out rows; the notebook runs in float32.

1. What depth buys

coupling_gaussianization_flow stacks KK rational-quadratic spline couplings with a flip after each, so consecutive layers have opposite active halves. Each coupling has 8 spline bins on [−4,4][-4, 4] and a conditioner MLP of width 32 and depth 2. The factory builds a trainable base (gauss_flows#256) and has no base_dist argument, so we swap the base for a fixed N(0,I)\mathcal{N}(\vect{0}, \mat{I}) with eqx.tree_at.

The training loop is that of Part 4, at one peak learning rate of 10-2: pipekit-train on the mean negative log-likelihood (1), Adam with gradients clipped to a global norm of 1 on a one-cycle cosine rate, 3,000 steps of 512 rows, and the held-out loss at the end.

We train flows of 2, 4, 8 and 16 layers; the four runs take a few minutes.

K =  2: NLL 1.420 on training rows, 1.505 held out
K =  4: NLL 1.338 on training rows, 1.463 held out
K =  8: NLL 1.308 on training rows, 1.486 held out
K = 16: NLL 1.286 on training rows, 1.507 held out
<Figure size 1800x390 with 5 Axes>

Figure 1:The training rows of the spiral (left) and the density of the trained spline coupling flows with 2, 4, 8 and 16 layers.

Depth helps up to a point. The training NLL falls with every doubling, from 1.420 at K=2K = 2 to 1.286 at K=16K = 16, but the held-out NLL is lowest at K=4K = 4 (1.463) and climbs back to 1.486 at K=8K = 8 and 1.507 at K=16K = 16, worse than two layers (1.505); the gap between training and held-out rows widens from 0.085 to 0.221. Two layers already trace both arms in the figure, and the deeper flows draw them with more ragged edges, which fits the overfitting the numbers show. On 3,000 points of a two-dimensional spiral, four spline layers of width 32 are enough, and more depth needs more data or regularisation to pay.

2. The gradient at the start of training

Gradients flow back through every layer, so a deeper stack can see a larger gradient. We measure the global norm ∥∇θL∥2\Vert \nabla_\theta \mathcal{L} \Vert_2 of the loss’s gradient over all the parameters at the start of training, on 1,000 training rows, for spline stacks and for stacks of AffineCoupling layers with a flip after each, at depths of 2 to 16.

spline: gradient norm [0.459 0.631 0.886 1.265], divided by √K [0.325 0.315 0.313 0.316]
affine: gradient norm [0.081 0.102 0.139 0.196], divided by √K [0.057 0.051 0.049 0.049]
<Figure size 750x440 with 1 Axes>

Figure 2:The global norm of the loss’s gradient at the start of training against the number of coupling layers, for spline (green) and affine (red) stacks, on log–log axes. The dashed lines grow like √K from each stack’s value at K = 2.

The gradient grows like K\sqrt{K}: divided by K\sqrt{K}, the spline norms are 0.325, 0.315, 0.313 and 0.316, and the affine norms 0.057, 0.051, 0.049 and 0.049. The spline and affine couplings of gauss_flows start as the identity, so every layer sees the data up to flips and contributes a gradient of about the same size, and KK such contributions add to a norm of about K\sqrt{K} times one of them. Deep coupling stacks are sometimes said to see gradients ten times larger and to need normalisation layers between couplings; that does not happen at the identity start, and we have not measured random starts. The growth still meets the training loop: clipping to a global norm of 1 leaves the shallow spline stacks alone but rescales the 16-layer one, whose norm is 1.265, from its first step. The affine stacks start with gradients about six times smaller than the spline stacks at every depth.

3. Residual maps

A coupling gets its cheap log-determinant from its triangular Jacobian, at the price of transforming only half the coordinates in each layer. A residual map transforms them all,

y=x+r(x)y = x + r(x)

where the residual branch rr is any network, with no split and no mask. It is invertible when rr is a contraction, Lip(r)<1\mathrm{Lip}(r) < 1: then 1+r′(x)>01 + r'(x) > 0 in one dimension, so the map is increasing. Its inverse has no closed form, but the contraction makes the fixed-point iteration

x(n+1)=y−r(x(n))x^{(n+1)} = y - r(x^{(n)})

converge to the xx with x+r(x)=yx + r(x) = y from any start. We try it on r(x)=arsin⁡(ωrx)r(x) = a_r \sin(\omega_r x), whose Lipschitz constant is arωra_r \omega_r, once with ar=0.3,ωr=1a_r = 0.3, \omega_r = 1 and once with ar=0.9,ωr=1.5a_r = 0.9, \omega_r = 1.5, with optimistix’s fixed-point solver.

a = 0.3, ω = 1.0: Lip(r) = 0.30, increasing True, converged True, max |x̂ - x| = 1.2e-06, most iterations 11
a = 0.9, ω = 1.5: Lip(r) = 1.35, increasing False, converged False, max |x̂ - x| = 1.7e+00, most iterations 200
<Figure size 1200x440 with 2 Axes>

Figure 3:The residual map y = x + a sin(ωx) for a contraction, Lip(r) = 0.3 (left), and for Lip(r) = 1.35 (right), with the identity dashed. The first is increasing and so invertible; the second folds back on itself, so some y have three preimages.

The contraction inverts: with Lip(r)=0.3\mathrm{Lip}(r) = 0.3 the map is increasing, the fixed-point iteration (2) converges at all 400 points in at most 11 steps, and the round trip is exact to 1.2×10−61.2 \times 10^{-6}, the solver’s tolerance in float32. With Lip(r)=1.35\mathrm{Lip}(r) = 1.35 the map folds back on itself (right panel), so it has no inverse, and the iteration fails at some points after 200 steps with errors up to 1.7. A residual layer pays for its freedom twice over: the inverse is an iteration rather than a formula, and the log-determinant log⁡∣1+r′(x)∣\log \vert 1 + r'(x) \vert is cheap only in one dimension, while in DxD_x dimensions it is the log-determinant of a full Dx×DxD_x \times D_x matrix. Invertible residual networks (i-ResNets) Behrmann et al., 2019 keep Lip(r)<1\mathrm{Lip}(r) < 1 by normalising the spectral norm of every weight matrix and estimate that log-determinant with a power series; a coupling layer avoids both costs by construction, which is why gauss_flows builds its flows from couplings.

Recap

PieceEquationIn code
a coupling flow is a stack of layers(1), (1)coupling_gaussianization_flow(key, n_dims=2, n_layers=K)
what depth buys(1)held-out NLL of 2 to 16 layers, pipekit-train
the gradient at the starteqx.filter_grad(negative_log_likelihood), optax.global_norm
a residual map and its inverse(1), (2)optx.fixed_point with optx.FixedPointIteration

This closes Part 5: the coupling pattern (00), the transformers (01), the conditioners (02), the masks (03), the comparison with diagonal flows (04), the warm start (05), the equivalence with a diagonal flow (06) and depth (07).

Next. Part 6 takes depth to its limit: instead of stacking layers, a continuous-time flow transports the data to N(0,I)\mathcal{N}(\vect{0}, \mat{I}) along an ordinary differential equation.

References
  1. Behrmann, J., Grathwohl, W., Chen, R. T. Q., Duvenaud, D., & Jacobsen, J.-H. (2019). Invertible Residual Networks. International Conference on Machine Learning (ICML).