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.

Layer-wise inspection

Following the data through a flow one block at a time, on training and held-out rows, for a greedy fit and a trained flow

02 · Layer-wise inspection

Open In Colab

The notebooks so far judged a flow by one number, its likelihood. A Gaussianization flow is a composition f=fK∘…∘f1f = f_K \circ \ldots \circ f_1 (1) of rotations and marginal maps, so we can also apply the blocks one at a time, keep each intermediate state xk\vect{x}_k (2), and run the diagnostics of Part 0 on it. The last section of the Gaussianization flow example shows the states of a trained flow as scatter plots; this notebook measures them, on training and on held-out rows, for a greedy fit and for a trained flow.

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 and the precision are those of notebooks 00 and 01: two moons, standardised, split into Nt=3000N_t = 3000 training rows and Nv=1000N_v = 1000 held-out rows, in float32 because fit_rbig cannot run in float64 today (gauss_flows#247).

1. Taking a flow apart

We fit RBIG greedily with fit_rbig, K=8K = 8 layers of a PCA rotation and an 8-component mixture marginal, as in notebook 01. Its bijection is Invert(Chain([...])), and the density direction x→z\vect{x} \to \vect{z} runs the Chain forwards, so its 16 blocks, in order, are the maps fkf_k of (1).

16 blocks: ['FixedRotation', 'MixtureGaussianCDF', 'FixedRotation', 'MixtureGaussianCDF'] ...

One function runs a list of blocks on a set of points and keeps every intermediate state and every block’s log-determinant.

The blocks should reproduce the flow: the last state is z=f(x)\vect{z} = f(\vect{x}), and the log-density of a deep flow (5) is the base log-density of the last state plus the sum of the blocks’ log-determinants. We check both on the training rows.

largest |x_K - f(x)|              : 0.0e+00
largest |block sum - log_prob|    : 7.6e-07

Both agree to float32 rounding, so anything we measure on the intermediate states is a measurement of the flow itself.

2. The greedy fit on its training rows

We look first at the states after whole layers, a rotation and a marginal map each.

<Figure size 1700x380 with 5 Axes>

Figure 1:The training rows before the greedy fit and after its layers 1, 2, 4 and 8, each layer a PCA rotation followed by a mixture marginal map.

The first layer unrolls the moons into an S-shaped band, and the later layers fold it into parallel stripes that are still there after layer 8. To measure what each block does we compute four numbers after every block: the correlation of the two coordinates, the mean absolute excess kurtosis ∣γ2,d∣\vert \gamma_{2,d} \vert of the coordinates (2), the joint test KSmax⁡\mathrm{KS}_{\max} over 64 projections (3), and the block’s mean log-determinant.

block  type       |corr|  mean|kurt|  KS_max  mean log|det J|
input              0.441      1.044   0.149
    1  FixedRota   0.000      1.233   0.151    +0.000
    2  MixtureGa   0.015      0.059   0.087    +0.527
    3  FixedRota   0.000      0.824   0.087    +0.000
    4  MixtureGa   0.009      0.172   0.057    +0.193
    5  FixedRota   0.000      0.775   0.058    +0.000
    6  MixtureGa   0.011      0.052   0.054    +0.088
    7  FixedRota   0.000      0.042   0.053    +0.000
    8  MixtureGa   0.008      0.201   0.056    +0.013
    9  FixedRota   0.000      0.102   0.056    +0.000
   10  MixtureGa   0.012      0.277   0.052    +0.020
   11  FixedRota   0.000      0.201   0.051    +0.000
   12  MixtureGa   0.001      0.199   0.050    +0.004
   13  FixedRota   0.000      0.943   0.049    +0.000
   14  MixtureGa   0.001      0.206   0.058    +0.052
   15  FixedRota   0.000      0.251   0.058    +0.000
   16  MixtureGa   0.005      0.120   0.049    +0.021

Each PCA rotation sets the correlation of the training rows to exactly zero, since it is fitted to them, and the marginal maps leave it below 0.02. The kurtosis alternates in the first three layers, as the source of this tutorial describes: each rotation raises it (to 1.23, 0.82 and 0.78) and each marginal map brings it back down (to 0.06, 0.17 and 0.05). After that the pattern breaks: the marginal maps of layers 4 and 5 raise it, and the rotation of layer 7 takes it to 0.94. The log-determinants say where the likelihood comes from: the marginal maps of the first three layers contribute 0.53, 0.19 and 0.09 nats per point, 0.81 of the 0.92 that all eight contribute, and KSmax⁡\mathrm{KS}_{\max} falls from 0.149 to 0.057 by block 4 and then stays between 0.049 and 0.058.

How small can KSmax⁡\mathrm{KS}_{\max} get? We compare it with its value on samples that really are standard normal, for the two sample sizes of this notebook.

95% quantile of KS_max under N(0, I): 0.030 for 3,000 points, 0.055 for 1,000

The fit ends at 0.049 on its own training rows, above the 0.030 of a standard normal of the same size, so eight layers of PCA rotations do not Gaussianize the moons. That is the stall of PCA on two moons that Part 3 found: the marginal steps leave the coordinates uncorrelated with unit variance, so the covariance that PCA diagonalises is already close to the identity and its frame is set by small differences. The stripes in the figure are what is left.

3. The greedy fit on held-out rows

The greedy fit’s held-out likelihood is half a nat worse than its training one (notebook 00). We run the held-out rows through the same blocks.

block  type       |corr|  mean|kurt|  KS_max  mean log|det J|
input              0.451      1.045   0.159
    1  FixedRota   0.046      1.220   0.158    +0.000
    2  MixtureGa   0.045      0.081   0.096    +0.510
    3  FixedRota   0.029      0.790   0.092    +0.000
    4  MixtureGa   0.061      0.601   0.076    +0.169
    5  FixedRota   0.004      1.357   0.074    +0.000
    6  MixtureGa   0.018      1.033   0.069    +0.099
    7  FixedRota   0.062      0.822   0.070    +0.000
    8  MixtureGa   0.078      1.268   0.073    +0.008
    9  FixedRota   0.021      1.410   0.072    +0.000
   10  MixtureGa   0.056      7.950   0.071    +0.024
   11  FixedRota   0.159      5.580   0.070    +0.000
   12  MixtureGa   0.155      9.908   0.075    -0.002
   13  FixedRota   0.039     12.680   0.075    +0.000
   14  MixtureGa   0.044     24.638   0.073    +0.043
   15  FixedRota   0.227     21.424   0.073    +0.000
   16  MixtureGa   0.306     27.837   0.069    +0.025

The bulk of the held-out rows behaves like the training rows: their KSmax⁡\mathrm{KS}_{\max} follows the training curve about 0.01 to 0.02 higher and ends at 0.069, above the 0.055 of a standard normal of their size, and the log-determinants are close to the training ones. The correlation is no longer zero after the rotations, which were fitted to other rows, and grows to 0.31 by the end. The kurtosis does something else: it falls to 0.08 after the first layer, as on the training rows, then climbs: 1.36 at block 5, between 0.8 and 1.4 up to block 9, 7.95 at block 10 and 27.8 at the end.

A kurtosis that large comes from a few points far out. We count the held-out points with a coordinate beyond 4 after each layer, and follow the three with the lowest log-density through the marginal maps, comparing each with the range of the training rows that the marginal map was fitted to.

lowest log p_x: [-188.  -107.5  -88.7], at [[1.350000023841858, -1.5499999523162842], [1.1799999475479126, -1.690000057220459], [1.2699999809265137, -1.600000023841858]]
held-out points beyond 4 after each layer: [0, 4, 5, 6, 8, 6, 8, 7]
layer 1: outside the training range [True, True, True]; largest |coordinate| in [2.1 2.  2. ], out [3.5 3.4 3.3]
layer 2: outside the training range [True, True, True]; largest |coordinate| in [2.6 2.6 2.5], out [4.3 4.5 4.2]
layer 3: outside the training range [True, True, True]; largest |coordinate| in [5.1 4.8 4.8], out [5.8 5.2 5.2]
layer 4: outside the training range [True, True, True]; largest |coordinate| in [4.9 5.  4.7], out [5.5 5.6 5.1]
layer 5: outside the training range [True, True, True]; largest |coordinate| in [6.4 5.5 5.5], out [11.2  9.   8.9]
layer 6: outside the training range [True, True, True]; largest |coordinate| in [8.4 7.8 7.2], out [11.2 10.1  9.1]
layer 7: outside the training range [True, True, True]; largest |coordinate| in [12.6 10.8 10.1], out [17.  14.2 13. ]
layer 8: outside the training range [True, True, True]; largest |coordinate| in [15.2 13.3 11.9], out [15.8 13.3 11.7]

The three worst points lie just outside the outer edge of the lower moon, and their log-densities, -188, -107.5 and -88.7, account for 0.38 nats of the held-out mean on their own, most of the half nat by which the held-out NLL exceeds the training one. Each enters every marginal map of the fit outside the range of the training rows that map was fitted to. The first map sends their largest coordinate from about 2 to about 3.4, and the later ones send them further, to between 11.7 and 15.8 by the end, while the number of held-out points beyond 4 grows from none after layer 1 to between 4 and 8. A marginal map fitted to the training rows knows the data only within their range, the boundary problem of Part 3, and a greedy stack compounds it: a point that one layer leaves just outside the range enters the next layer outside its range too.

<Figure size 1650x420 with 3 Axes>

Figure 2:The diagnostics after every block of the greedy fit, on the training rows (blue) and the held-out rows (orange): the correlation of the two coordinates, the mean absolute excess kurtosis on a log scale, and KS_max with the 95% quantile of its null distribution for each sample size (dotted). Odd blocks are PCA rotations, even blocks mixture marginal maps.

4. A trained flow, block by block

A flow trained by maximum likelihood is fitted as a whole, not layer by layer. We train the cold start of notebook 01, gaussianization_flow with a fixed base, at the peak learning rate that did best there, 3×10−23 \times 10^{-2}, with the loop of notebooks 00 and 01.

held-out NLL: trained flow 1.659, greedy fit 2.430

Its eight layers are stored once inside Scan, every array with a leading axis of length 8. eqx.partition splits the layer into those arrays and the rest; indexing the arrays at kk and combining them with the rest gives layer kk as an ordinary Chain of a marginal map and a Householder rotation.

largest |x_K - f(x)|: 5.0e-06
block  type       |corr|  mean|kurt|  KS_max  mean log|det J|
input              0.451      1.045   0.159
    1  MixtureGa   0.512      1.302   0.270    -0.763
    2  Household   0.402      1.399   0.271    +0.000
    3  MixtureGa   0.463      0.521   0.370    +1.100
    4  Household   0.282      0.835   0.370    +0.000
    5  MixtureGa   0.369      0.808   0.518    +0.478
    6  Household   0.008      0.376   0.517    +0.000
    7  MixtureGa   0.025      0.701   0.122    +0.076
    8  Household   0.161      1.386   0.122    +0.000
    9  MixtureGa   0.211      0.727   0.146    -0.344
   10  Household   0.167      0.751   0.146    +0.000
   11  MixtureGa   0.153      0.516   0.475    +0.553
   12  Household   0.151      0.516   0.477    +0.000
   13  MixtureGa   0.155      0.337   0.335    +0.143
   14  Household   0.027      0.728   0.335    +0.000
   15  MixtureGa   0.028      0.115   0.047    -0.063
   16  Household   0.006      0.073   0.047    +0.000

The blocks reproduce the trained flow to 5×10−65 \times 10^{-6}, float32 rounding of a different order of operations, and its held-out NLL is the 1.659 of notebook 01. Its intermediate states are nothing like a progression towards N(0,I)\mathcal{N}(\vect{0}, \mat{I}). KSmax⁡\mathrm{KS}_{\max} rises from 0.16 to 0.52 over the first three layers, falls to 0.12, rises again to 0.48, and drops under the null quantile only at the last layer, to 0.047 against 0.055. Three marginal maps have a negative mean log-determinant, -0.76, -0.34 and -0.06 nats: they squeeze the data, and other layers pay it back with up to +1.10. Nothing in the objective (1) asks an intermediate state to be Gaussian, only the output. The unrolled layers can be inspected exactly like the greedy ones, as the source of this tutorial says, but the reading of section 2, where the work is front-loaded and each layer brings the data closer, does not carry over.

<Figure size 1700x800 with 7 Axes>

Figure 3:Top: the held-out rows after layers 1, 2, 4, 6 and 8 of the trained flow, each layer a mixture marginal map followed by a Householder rotation. Bottom left: KS_max after every block on the held-out rows, for the greedy fit (orange) and the trained flow (green), with the null quantile for 1,000 points (dotted). Bottom right: each block’s mean log-determinant on the held-out rows.

After its first layer the trained flow has drawn the two moons into the outline of a narrow box; after layer 2 they are a band of clumps, and only by layer 8 a round cloud. The greedy fit’s log-determinants shrink layer by layer and are all positive but one of -0.002; the trained flow’s change sign, and add up to 1.18 nats per held-out point against the greedy fit’s 0.88.

Recap

PieceEquationIn code
a flow as a composition of blocks(1), (2)Invert(Chain) of fit_rbig, run_blocks
the blocks reproduce the log-density(5)transform_and_log_det per block
per-block diagnostics(2), (3)block_diagnostics
a layer taken out of a Scaneqx.partition, index the leading axis, eqx.combine

Next. This completes the diagonal flows of Part 4. Part 5 turns to coupling flows, whose marginal maps take their parameters from the other coordinates.