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 transformer menu for coupling

Affine, mixture CDF, deep sigmoid and rational-quadratic spline: the one-dimensional maps a coupling layer can wrap, and what each buys

01 · The transformer menu for coupling

Open In Colab

Notebook 00 showed that a coupling layer (1) keeps its triangular Jacobian and its cheap inverse whatever its transformer TθT_\theta is, as long as TθT_\theta acts on each active coordinate on its own and is monotone. The transformer and the conditioner are separate choices. This notebook keeps the conditioner fixed, an MLP of width 32 and depth 2, and goes through the four transformers of gauss_flows:

TransformerTθ(x)T_\theta(x)InverseLayer
affine(2)closed formAffineCoupling, as in RealNVP Dinh et al., 2017
mixture CDFΦ−1(F(x))\Phi^{-1}(F(x)) with the mixture CDF (2)bisectionMixtureGaussianCDFCoupling
deep sigmoid(1)bisectionDeepSigmoidCoupling
rational-quadratic spline(2)closed form (4)RQSplineCoupling, as in neural spline flows Durkan et al., 2019

The deep sigmoid is the single-layer sigmoidal transformer of neural autoregressive flows Huang et al., 2018: a positive-slope line plus MM sigmoid steps,

Tθ(x)=r0x+t0+∑m=1Mrmς(kmx+tm)T_\theta(x) = r_0 x + t_0 + \sum_{m=1}^{M} r_m \varsigma(k_m x + t_m)

where ς(u)=1/(1+e−u)\varsigma(u) = 1 / (1 + e^{-u}) is the logistic sigmoid, r0,rm,km>0r_0, r_m, k_m > 0 are scales and slopes, and t0,tmt_0, t_m are shifts. Each term is increasing, so TθT_\theta is monotone.

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_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

1. Each transformer is a map of one coordinate

We work in two dimensions, Dx=2D_x = 2: the first coordinate is passive and the second active. Every layer gets the same conditioner, an MLP of width 32 and depth 2; the mixture and the deep sigmoid use M=8M = 8 components, the spline 8 bins on [−4,4][-4, 4].

A fresh coupling’s conditioner returns the transformer’s initial parameters for every input (notebook 00). We fix the passive coordinate at xA=0.7x_A = 0.7, sweep the active one and read off zB=Tθ(xB)z_B = T_\theta(x_B).

affine      : max |z_B - x_B| = 0.000
mixture CDF : max |z_B - x_B| = 0.000
deep sigmoid: max |z_B - x_B| = 7.621
RQ spline   : max |z_B - x_B| = 0.000

Three of the fresh layers are the identity. The deep sigmoid is not: its initial parameters are r0=rm=km=1r_0 = r_m = k_m = 1 and t0=tm=0t_0 = t_m = 0, so a fresh layer maps xx to x+∑mς(x)x + \sum_m \varsigma(x), which adds up to M=8M = 8 to every active coordinate. This is deliberate (the layer’s source says its initial transformer is not the identity map), and training moves it like any other start.

To see the shapes a trained transformer can take, we move every weight of each layer by a random amount, as in notebook 00, and measure how far each map is from its best straight line.

affine      : distance from a straight line 0.000
mixture CDF : distance from a straight line 0.597
deep sigmoid: distance from a straight line 1.326
RQ spline   : distance from a straight line 0.691
<Figure size 1200x500 with 2 Axes>

Figure 1:The active output z_B = T_θ(x_B) of each coupling with random conditioner weights, at x_A = 0.7. The affine map is a straight line; the mixture CDF and the spline bend into monotone curves; the deep sigmoid bends most and sits far above the identity (dashed), since its start already adds up to 8.

The affine transformer is a straight line to machine precision: a coupling with an affine transformer can only scale and shift a coordinate. The mixture CDF and the spline bend into smooth monotone curves, and the deep sigmoid bends most, from a start that already sits far from the identity. All four stay increasing, as they must to be invertible.

2. The family of maps the conditioner can choose from

The conditioner reads xAx_A and returns θ\theta, so as xAx_A moves the layer sweeps a whole family of one-dimensional maps. That family, not one map, is what a single layer can express. For the affine transformer it is a family of lines (the conditioner picks a slope and an intercept); for the spline it is a family of monotone curves.

<Figure size 1200x500 with 2 Axes>

Figure 2:The maps x_B ↦ z_B that one coupling layer with random weights produces as x_A runs over seven values in [−2, 2]. With an affine transformer (left) every map is a straight line; with a spline (right) each is a different monotone curve.

The affine panel is a sheaf of straight lines, so non-linear dependence between coordinates can only be built up over many stacked layers. In the spline panel each value of xAx_A gives a different curve, so one layer can already make a non-linear, context-dependent change. That is the case for expressive transformers: they need fewer layers.

3. Fitting a spiral with two coupling blocks

We make this concrete on a two-arm spiral, standardised, with Nt=3000N_t = 3000 training rows and Nv=1000N_v = 1000 held-out rows. With only two coupling blocks, the transformer, not the depth, decides the fit.

Each block is a rotation followed by two couplings with a flip between them, so both coordinates are transformed once per block. The rotation is a HouseholderRotation with random vectors, since its default vectors are all equal and it would start as the identity (gauss_flows#239). The base is a fixed standard normal.

A fresh mixture-CDF or deep-sigmoid coupling has all its components equal, and because its conditioner’s last layer starts at zero they stay equal through training: the mixture CDF trains as one Gaussian, which is an affine map, and the deep sigmoid as a line plus one sigmoid, with its units unable to move apart (gauss_flows#265); until that is fixed we move every weight of those two layers by 0.1 times a standard-normal draw with perturb_weights before training, and leave the affine and spline layers as they are.

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

We compare the mean negative log-likelihood on the training rows and on the held-out rows, and also the median over the held-out rows, which one badly fitted point cannot move.

affine      : NLL 2.418 on training rows, 2.518 held out (median 2.403, worst 28.6)
mixture CDF : NLL 1.620 on training rows, 1.695 held out (median 1.605, worst 9.2)
deep sigmoid: NLL 2.388 on training rows, 2.433 held out (median 2.379, worst 8.3)
RQ spline   : NLL 1.541 on training rows, 1.595 held out (median 1.527, worst 6.3)

The spline fits best, with 1.54 nats on the training rows and 1.60 held out, and the mixture CDF comes close behind, with 1.62 and 1.70. The deep sigmoid (2.39 and 2.43) and the affine transformer (2.42 and 2.52) end about 0.8 nats behind them. Every flow has a few held-out rows it fits much worse than the rest (the worst column, from 6.3 nats for the spline to 28.6 for the affine flow), which is why we also give the median.

The densities show the difference between the transformers at a glance.

<Figure size 2200x460 with 5 Axes>

Figure 3:The training rows of the spiral (left) and the density p_x of the flow of two coupling blocks with each transformer. The spline and the mixture CDF follow both arms, the mixture CDF with gaps; the affine flow spreads its mass over a disc around the spiral, and the deep-sigmoid flow puts a bright band on the top of the outer arm above a similar disc.

With two blocks, the spline and the mixture CDF put their mass on the two arms. The affine and deep-sigmoid flows fill a disc around the spiral and catch only parts of the outer arm. An affine map of each coordinate is a line, so its flow needs more blocks to follow the arms; the deep sigmoid can bend (the random-weight maps above), yet with two blocks and 1,500 steps its flow does not follow them either.

Recap

TransformerMap of one coordinateInverseHere
affine (2)a lineclosed formthe cheapest; needs depth for non-linear dependence
mixture CDF (2)a smooth monotone curvebisectionclose behind the spline, once its weights are moved off their symmetric start (gauss_flows#265)
deep sigmoid (1)a line plus sigmoid stepsbisectiondoes not start as the identity; also needs its weights moved (gauss_flows#265), and ends close to the affine transformer here
rational-quadratic spline (2)a piecewise rational-quadratic curveclosed form (4)the best fit with two blocks

The transformer sets what shape one coordinate’s map can take. The other half of a coupling layer, the conditioner, sets how that shape changes with the passive coordinates.

Next. Notebook 02 keeps the transformer fixed and changes the conditioner: its size, the bound on the log-scales it returns, and the networks that later parts put in its place.

References
  1. Dinh, L., Sohl-Dickstein, J., & Bengio, S. (2017). Density Estimation using Real NVP. International Conference on Learning Representations (ICLR).
  2. Durkan, C., Bekasov, A., Murray, I., & Papamakarios, G. (2019). Neural Spline Flows. Advances in Neural Information Processing Systems (NeurIPS).
  3. Huang, C.-W., Krueger, D., Lacoste, A., & Courville, A. (2018). Neural Autoregressive Flows. International Conference on Machine Learning (ICML).