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.

Fixed rotations and warm starts

Freezing a PCA frame inside a flow, what goes wrong with a free matrix, and starting a trainable rotation at a given frame

02 · Fixed rotations and warm starts

Open In Colab

Notebook 01 made the rotation trainable. Often the rotation we want is one we do not train: the PCA frame (3) decorrelates the data in one step (notebook 00), so a flow can fix that frame and spend its parameters on the marginals. When we do want a trainable rotation, we can start it at the PCA frame instead of at an arbitrary one. This notebook does both with gauss_flows.

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
gauss_flows_models: 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

Throughout, the data are N=4000N = 4000 draws of a correlated Gaussian in Dx=3D_x = 3 dimensions, x∼N(0,Σx)\vect{x} \sim \mathcal{N}(\vect{0}, \gmat{\Sigma}_x), drawn as a fixed lower-triangular square root of Σx\gmat{\Sigma}_x times standard normal noise, with principal variances of about 5.7, 1.0 and 0.11.

1. The PCA frame as a fixed rotation

FixedRotation.from_data takes the eigenvectors of the sample covariance and stores the orthogonal matrix Q=V⊤\mat{Q} = \mat{V}^\top of (3), whose rows are the principal axes in order of decreasing variance. The rotated data are uncorrelated,

QΣxQ⊤=Λ\mat{Q} \gmat{\Sigma}_x \mat{Q}^\top = \gmat{\Lambda}

and since Q\mat{Q} is orthogonal its log-determinant is zero (2). It does not rescale: the variances along the new axes are the diagonal of Λ\gmat{\Lambda}, still unequal, and standardising them is left to the marginal step that follows.

We check the round trip and the log-determinant on one point, and the covariance before and after.

The largest off-diagonal entry of each covariance measures how correlated the coordinates are.

max |Q^T Q - I| = 2.2e-16
log|det Q| = +0.0e+00, round trip 2.2e-16
largest off-diagonal covariance: before 2.428, after 3.2e-15
variances after: [5.771 1.029 0.112]
<Figure size 1000x420 with 2 Axes>

Figure 1:The first two coordinates of the data (left) and of the data in the PCA frame of FixedRotation.from_data (right). The frame lines the cloud up with the axes, so the coordinates are uncorrelated, but their variances still differ.

The rotation is orthogonal and exact to machine precision, the off-diagonal covariance drops from 2.4 to rounding error, and the variances along the new axes are the principal variances 5.7, 1.0 and 0.11.

2. Why the frame is frozen

FixedRotation stores its matrix wrapped in paramax.NonTrainable, and fit_gaussianization_flow trains only the floating-point leaves outside such wrappers, so the frame does not move. The alternative, a plain matrix A\mat{A} that the optimiser may change while the flow still books its log-determinant as zero, as it would for a rotation, goes wrong in a specific way. With a standard normal base, the loss the flow reports is

L0(A)=−1N∑n=1Nlog⁡N(Axn;0,I)\mathcal{L}_0(\mat{A}) = -\frac{1}{N} \sum_{n=1}^{N} \log \mathcal{N}(\mat{A} \vect{x}_n; \vect{0}, \mat{I})

while the true negative log-likelihood of the change of variables (5) also counts the volume change,

L(A)=L0(A)−log⁡∣det⁡A∣\mathcal{L}(\mat{A}) = \mathcal{L}_0(\mat{A}) - \log \vert \det \mat{A} \vert

Minimising L0\mathcal{L}_0 rewards shrinking the data towards the origin, where the base density is highest, and nothing stops it, because the term that would charge for the shrinking is missing. We write such a layer, start it at the PCA frame, and train it with fit_gaussianization_flow.

The flow’s density direction is z=Ax\vect{z} = \mat{A} \vect{x}, so the layer goes in inverted, as a flowjax Transformed maps the base to the data.

We compare the loss the flow reports with the true one (3), and the trained matrix with a rotation.

max |A^T A - I| = 0.999, log|det A| = -58.8
largest standard deviation of its samples: 1.7e+19
reported NLL 2.757, true NLL 61.6, Gaussian bound 4.055

The matrix has shrunk almost to zero, with log⁡∣det⁡A∣\log \vert \det \mat{A} \vert around -59. The flow reports a negative log-likelihood of about 2.76, better than any Gaussian can achieve on this data (the bound of section 4, about 4.06), while the true value is about 62. Sampling inverts A\mat{A}, so its samples have standard deviations around 1019. Nothing in the reported loss flags the problem.

The same training leaves a FixedRotation exactly where it started. We put the PCA frame before a per-coordinate affine map, z=diag(a)Qx+b\vect{z} = \mathrm{diag}(\vect{a}) \mat{Q} \vect{x} + \vect{b}, which is a flow that can standardise the rotated coordinates, and train it.

Every flow in this notebook is trained the same way, with Adam until the held-out loss has not improved for five epochs, keeping the best epoch.

We train it with the frozen PCA frame, starting at a=1\vect{a} = \vect{1} and b=0\vect{b} = \vect{0}.

largest change in Q: 0.0e+00
NLL 4.0546 after 51 epochs, Gaussian bound 4.0545

The frame did not change in a single bit, and the flow reaches the best Gaussian fit to four decimal places. A rotation inside a flow should either be frozen like this or parametrised so that every update stays orthogonal, as the Householder and Cayley layers of notebook 01 are. It should never be a plain matrix.

3. Starting a trainable rotation at a given frame

To train the rotation and still start at the PCA frame, we set the reflection vectors of a HouseholderRotation so that their product is Q\mat{Q}. A Householder reflection H(u)=I−2uu⊤/∥u∥2\mat{H}(\vect{u}) = \mat{I} - 2 \vect{u} \vect{u}^\top / \Vert \vect{u} \Vert^2 can send any vector to a multiple of a coordinate axis, which is how QR factorisations are computed. Applied to the columns of Q\mat{Q} one at a time, each reflection ud\vect{u}_d moves column dd onto the axis dd without disturbing the columns already placed, and since Q\mat{Q} is orthogonal the reduction ends at the identity,

H(uDx)…H(u1)Q=I\mat{H}(\vect{u}_{D_x}) \ldots \mat{H}(\vect{u}_1) \mat{Q} = \mat{I}

where a step whose column is already in place is skipped. Each reflection is its own inverse, so Q=H(u1)…H(uDx)\mat{Q} = \mat{H}(\vect{u}_1) \ldots \mat{H}(\vect{u}_{D_x}), at most DxD_x reflections. HouseholderRotation applies its reflections in the order of its parameter rows, the first row first, so the rows are the vectors in reverse order.

We decompose the PCA frame and put the vectors into a HouseholderRotation with eqx.tree_at.

2 reflections, max |Q_warm - Q| = 2.2e-16

Two reflections suffice for this frame, and the trainable rotation reproduces it to machine precision.

4. Cold and warm starts under maximum likelihood

We train the flow z=diag(a)Qx+b\vect{z} = \mathrm{diag}(\vect{a}) \mat{Q} \vect{x} + \vect{b} again, now with a trainable HouseholderRotation for Q\mat{Q}. For a fixed rotation the best per-coordinate affine map fits a Gaussian to each rotated coordinate, so the best loss is a sum of one-dimensional Gaussian entropies, and Hadamard’s inequality bounds it by the entropy (2) of the Gaussian fit to all the data,

L⋆(Q)=12∑d=1Dxlog⁡(2πe(QΣxQ⊤)dd)≥12log⁡det⁡(2πeΣx)\mathcal{L}^\star(\mat{Q}) = \frac{1}{2} \sum_{d=1}^{D_x} \log(2 \pi e (\mat{Q} \gmat{\Sigma}_x \mat{Q}^\top)_{dd}) \ge \frac{1}{2} \log \det(2 \pi e \gmat{\Sigma}_x)

with equality exactly when QΣxQ⊤\mat{Q} \gmat{\Sigma}_x \mat{Q}^\top is diagonal (1). The PCA frame is therefore the rotation that maximum likelihood looks for. We compare three starts:

  • cold: the default HouseholderRotation(n_reflections=3), with a=1\vect{a} = \vect{1} and b=0\vect{b} = \vect{0};

  • warm rotation: the PCA frame of section 3, with a=1\vect{a} = \vect{1} and b=0\vect{b} = \vect{0};

  • warm layer: the PCA frame with a=Λ−1/2\vect{a} = \gmat{\Lambda}^{-1/2} and b=−Λ−1/2Qxˉ\vect{b} = -\gmat{\Lambda}^{-1/2} \mat{Q} \bar{\vect{x}}, which is PCA whitening, the solution itself.

The default HouseholderRotation repeats one vector in every row, so with an odd number of reflections it starts as a single reflection.

[[1.         0.70710678 0.57735027]
 [1.         0.70710678 0.57735027]
 [1.         0.70710678 0.57735027]]

Each start is trained the same way, and we record the held-out loss of every epoch.

cold          first epoch 5.736,  40 epochs, final NLL 4.0562
warm rotation first epoch 5.585,  44 epochs, final NLL 4.0559
warm layer    first epoch 3.989,   7 epochs, final NLL 4.0553
<Figure size 750x420 with 1 Axes>

Figure 2:Held-out negative log-likelihood per epoch for the three starts, against the entropy of the Gaussian fit (dashed), the best any rotation and per-coordinate affine map can do. Warming up the rotation alone barely helps while the scales start far off; warming up the whole layer starts at the solution. The held-out rows are a different sample from the data the bound is computed on, so the curves can settle slightly below it.

Warming up only the rotation changes little. Its first held-out loss (5.59) is close to the cold start’s (5.74), because the scales start at 1 while the smallest principal variance is 0.11, and both runs take about 15 epochs to reach the dashed line and stop after about 40. Warming up the whole layer starts at the solution, so early stopping ends its run after 7 epochs. All three end within 0.002 nats of the bound (5) on the full data. A warm start pays off when it sets every parameter the data determine, which is the recipe RBIG uses to initialise parametric flows in Part 3.

Recap

IdeaEquationIn code
the PCA frame decorrelates(3), (1)FixedRotation.from_data
it stays frozen in trainingparamax.NonTrainable, fit_gaussianization_flow
a free matrix booked as a rotation(2), (3)collapses; never do this
a frame as Householder reflections(4)eqx.tree_at on HouseholderRotation.params
the PCA frame maximises the likelihood(5)warm-start the rotation and the scales

Next. Rotations are the dense orthogonal mixers. Image and high-dimensional flows also need cheap structured linear layers whose log-determinant is not zero but still easy to compute. The next notebook covers the invertible 1×11 \times 1 convolution, parametrised by an LU factorisation, and ActNorm, the data-initialised affine layer.