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.

Composition and the additive log-determinant

Why deep flows work: stack maps, and their log-determinants add

01 · Composition and the additive log-determinant

Open In Colab

Notebook 00 gave us one invertible map and its log-determinant, the last term of (4). A single elementwise CDF or a single rotation cannot turn a tangled distribution into a standard Gaussian, so we stack many simple maps into a deep one. Stacking is cheap, and normalizing flows exist at all Rezende & Mohamed, 2015Dinh et al., 2017, because of a one-line consequence of the chain rule: when maps compose, their log-determinants add.

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

jax             : 0.12.1
numpy           : 2.4.3
matplotlib      : 3.11.2
flowjax         : 19.1.0
gauss_flows_maps: 0.3.0

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. The composition rule

Write a deep map as a composition of KK bijections,

f=fK∘fK−1∘…∘f1f = f_K \circ f_{K-1} \circ \ldots \circ f_1

where each layer fk:RDx→RDxf_k: \mathbb{R}^{D_x} \to \mathbb{R}^{D_x} acts on the state left by the one before it,

xk=fk(xk−1)\vect{x}_k = f_k(\vect{x}_{k-1})

starting from the input, x0=x\vect{x}_0 = \vect{x}, and ending at the latent, xK=z\vect{x}_K = \vect{z}. Every state is a vector xk∈RDx\vect{x}_k \in \mathbb{R}^{D_x}.

By the chain rule the Jacobian of the whole map is a product of the layer Jacobians, each evaluated at the intermediate state it acts on:

Jf(x)=JfK(xK−1)JfK−1(xK−2)…Jf1(x0)\mat{J}_f(\vect{x}) = \mat{J}_{f_K}(\vect{x}_{K-1}) \mat{J}_{f_{K-1}}(\vect{x}_{K-2}) \ldots \mat{J}_{f_1}(\vect{x}_0)

The log-determinant of a deep flow is therefore a sum of cheap per-layer terms, and no Dx×DxD_x \times D_x determinant of the whole composition is ever formed.

2. Check it from scratch

We compose three simple invertible maps in Dx=2D_x = 2 dimensions: an affine mixer f1(x)=A1x+bf_1(\vect{x}) = \mat{A}_1 \vect{x} + \vect{b}, an elementwise nonlinearity f2(x)=tanh⁡(0.8x)+0.3xf_2(\vect{x}) = \tanh(0.8 \vect{x}) + 0.3 \vect{x}, and a linear map f3(x)=A2xf_3(\vect{x}) = \mat{A}_2 \vect{x}, with A1,A2∈R2×2\mat{A}_1, \mat{A}_2 \in \mathbb{R}^{2 \times 2} and b∈R2\vect{b} \in \mathbb{R}^{2}. The total log-determinant is computed two ways: by autodiff of the whole composition, and by the running sum (4).

The three layers act on one state vector each.

Composing them walks the state through the layers in order, as in (2).

The log-determinant of one map comes from its autodiff Jacobian.

Now the two totals, at one random input.

whole composition, autodiff  = -0.5000126365
sum over layers              = -0.5000126365
absolute difference          = 0.00e+00

3. The intermediates matter

A common slip is to evaluate every layer at the original input x0\vect{x}_0. The linear layers do not care, since their Jacobians are constant, but the Jacobian of the nonlinear f2f_2 depends on where it is evaluated, so the shortcut gives the wrong total.

states x_(k-1), correct  = -0.50001264
all at x_0, wrong        = -0.35780308
gap from the nonlinearity = 0.1422

The whole gap comes from f2f_2: feeding it x0\vect{x}_0 instead of x1=f1(x0)\vect{x}_1 = f_1(\vect{x}_0) evaluates its stretch factor at the wrong point. Flow libraries get this right by construction, since each bijection receives the output of the one before it.

4. The log-density of a deep flow

Substituting (4) into the log-density (4) of notebook 00 gives the density of a deep flow with a standard-normal base:

log⁡px(x)=log⁡pz(f(x))+∑k=1Klog⁡∣det⁡Jfk(xk−1)∣\log p_x(\vect{x}) = \log p_z(f(\vect{x})) + \sum_{k=1}^{K} \log \vert \det \mat{J}_{f_k}(\vect{x}_{k-1}) \vert

Every layer adds one term. A layer with log⁡∣det⁡Jfk∣=0\log \vert \det \mat{J}_{f_k} \vert = 0 changes the geometry for free; orthogonal rotations are the standard example. That is why Gaussianization alternates rotations, which move mass between axes at no cost, with marginal transforms, which do the density work.

5. The bridge: a stack of gauss_flows blocks

A Gaussianization flow is a chain of rotation and marginal blocks. Here the rotation is a HouseholderRotation and the marginal a MixtureGaussianCDF, each the layer fkf_k of (1) through its transform_and_log_det. FlowJax’s Chain composes them with the rule (4). We push N=4000N = 4000 points of a skewed 2-D dataset through four blocks by hand. Each rotation gets random reflections, and each marginal is fitted to the state that reaches it with MixtureGaussianCDF.from_data, as RBIG does. We record the mean log-determinant of each layer and compare the total with Chain’s own.

Each rotation gets random reflections, since the default ones are equal and cancel to the identity.

One block rotates the state that reaches it, then fits a marginal to the rotated state and applies it.

Four blocks, each fitted to the output of the one before.

FlowJax’s Chain of the eight layers must give the same total.

sum of the layers' mean log-dets = -0.56291187
Chain's mean log-det             = -0.56291187
<Figure size 1200x450 with 1 Axes>

Figure 1:Mean log-determinant of each layer of four rotation and marginal blocks and its running total (line). The rotations (grey squares) contribute exactly zero; the marginals (orange bars) carry the whole log-determinant.

The grey rotation markers sit at exactly zero: an orthogonal map has ∣det⁡Jfk∣=1\vert \det \mat{J}_{f_k} \vert = 1, so it costs nothing in the density ledger. The orange marginal bars carry the whole log-determinant, and the blue line is their running total, the sum in (5) that a flow adds to log⁡pz(z)\log p_z(\vect{z}) when it scores a point.

Recap

ConceptEquationIn code
composition of layers(1), (2)compose(layers)
Jacobian of a composition(3)jax.jacfwd(compose(layers))
log-determinants add(4)a running sum over the layers
log-density of a deep flow(5)flow.log_prob(x)
rotations are freelog⁡∣det⁡Jfk∣=0\log \vert \det \mat{J}_{f_k} \vert = 0the grey markers
the library does it(4) inside Chain and ScanChain(bijections)

Next. We have built maps in the data-to-latent direction. The next notebook asks which direction a flow should store and which it should compute, a choice that splits flows into those good at density estimation and those good at fast sampling.

References
  1. Rezende, D. J., & Mohamed, S. (2015). Variational Inference with Normalizing Flows. International Conference on Machine Learning (ICML).
  2. Dinh, L., Sohl-Dickstein, J., & Bengio, S. (2017). Density Estimation using Real NVP. International Conference on Learning Representations (ICLR).