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.

Householder and trainable orthogonals

Products of Householder reflections and the Cayley map, orthogonal for every value of their parameters

01 · Householder and trainable orthogonals

Open In Colab

Notebook 00 fitted a rotation once (PCA, random or ICA) and froze it. Inside a parametric Gaussianization flow we want to learn the rotation by gradient descent, together with the marginal maps. A rotation must satisfy Q⊤Q=I\mat{Q}^\top \mat{Q} = \mat{I}, and a general matrix updated by gradient descent does not stay orthogonal. We need a parameterisation, a smooth map from free parameters θ\theta to a matrix Q(θ)\mat{Q}(\theta), that is orthogonal for every θ\theta. Then the rotation is free in the log-determinant (2) at every step of training, with no constraint and no projection.

gauss_flows ships two: HouseholderRotation, a product of Householder reflections, and OrthogonalRotation, the Cayley map of a skew-symmetric matrix.

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

jax             : 0.12.1
equinox         : 0.13.8
optax           : 0.2.8
matplotlib      : 3.11.2
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 Householder reflection

Given a nonzero vector u\vect{u}, the Householder reflection

H(u)=I−2uu⊤u⊤u\mat{H}(\vect{u}) = \mat{I} - 2 \frac{\vect{u} \vect{u}^\top}{\vect{u}^\top \vect{u}}

mirrors every point across the hyperplane through the origin orthogonal to u\vect{u}. It is orthogonal, it is its own inverse (H2=I\mat{H}^2 = \mat{I}), and it has det⁡H=−1\det \mat{H} = -1, because it flips the one direction u\vect{u} and keeps the Dx−1D_x - 1 directions orthogonal to it. We build one by hand in two dimensions.

We reflect across the line orthogonal to u=(1,1)\vect{u} = (1, 1) and check the three properties.

max |H^T H - I| = 4.4e-16
max |H H - I|   = 4.4e-16
det H           = -1.000

To see it act, we reflect the six corners of a letter F, which has no mirror symmetry of its own.

<Figure size 450x450 with 1 Axes>

Figure 1:The Householder reflection across the line orthogonal to u=(1,1)\vect{u} = (1, 1) (dashed) sends the letter F to its mirror image, which no rotation can produce.

2. Products of reflections

One reflection is not much, but every orthogonal matrix in DxD_x dimensions is a product of at most DxD_x reflections; this is the Cartan–Dieudonné theorem, and it is what the Householder QR factorisation computes. A product of KK reflections

Q=H(uK)H(uK−1)…H(u1)\mat{Q} = \mat{H}(\vect{u}_K) \mat{H}(\vect{u}_{K-1}) \ldots \mat{H}(\vect{u}_1)

is orthogonal for any vectors uk\vect{u}_k, and its determinant is fixed by the parity of KK,

det⁡Q=(−1)K\det \mat{Q} = (-1)^K

so an even KK gives a rotation and an odd KK a reflection. HouseholderRotation(n_reflections=K, shape=(D_x,)) is (2) with the KK vectors as its parameters, the Householder flow of Tomczak & Welling (2016). It applies one event at a time, so we read its matrix back column by column, Q=[Qe1,…,QeDx]\mat{Q} = [\mat{Q} \vect{e}_1, \ldots, \mat{Q} \vect{e}_{D_x}].

For K=1,…,4K = 1, \ldots, 4 we check the determinant against (3), the orthogonality, and how far the freshly built layer is from the identity.

K = 1: det Q = -1.00, max |Q^T Q - I| = 6e-16, max |Q - I| = 1.09
K = 2: det Q = +1.00, max |Q^T Q - I| = 9e-16, max |Q - I| = 0.00
K = 3: det Q = -1.00, max |Q^T Q - I| = 1e-15, max |Q - I| = 1.09
K = 4: det Q = +1.00, max |Q^T Q - I| = 2e-15, max |Q - I| = 0.00

The determinant follows (−1)K(-1)^K and every product is orthogonal to machine precision. The initial layer is the identity for even KK and a single reflection for odd KK, because HouseholderRotation starts all KK vectors equal, and two equal reflections cancel. An even KK is therefore the natural choice for a flow layer: it starts as the identity, and it is a rotation for every value of its parameters.

3. Training a rotation

The parameters of (2) are unconstrained, so we can train them with any optimiser and Q(θ)\mat{Q}(\theta) stays orthogonal. We show it by fitting a target rotation Q⋆\mat{Q}_\star, minimising

∥Q(θ)−Q⋆∥F2=2Dx−2tr(Q(θ)⊤Q⋆)\Vert \mat{Q}(\theta) - \mat{Q}_\star \Vert_F^2 = 2 D_x - 2 \mathrm{tr}(\mat{Q}(\theta)^\top \mat{Q}_\star)

with Adam, where the equality holds for any two orthogonal matrices. As a baseline we train a general matrix the same way. gauss_flows returns a log-determinant of exactly zero for its orthogonal layers, by construction rather than by computation, so what we track during training is the orthogonality of Q(θ)\mat{Q}(\theta), the property that makes that zero correct.

The target is a random rotation: the Q\mat{Q} factor of the QR factorisation of a Gaussian matrix (5), with one column flipped when its determinant has the wrong sign.

The fitting loop takes any layer with a transform method, so it serves the Householder layer, the Cayley layer and the general matrix alike. It records the fit (4) and the orthogonality gap max⁡∣Q⊤Q−I∣\max \vert \mat{Q}^\top \mat{Q} - \mat{I} \vert at every step.

One Adam step is a gradient of (4) and an update, compiled once.

The general matrix is the identity to start with, like the Householder layer with K=2K = 2.

We fit both to the target rotation.

Householder (K = 2): final fit 5.3e-32, max |Q^T Q - I| over training 1.3e-15
general matrix     : final fit 1.2e-32, max |M^T M - I| over training 0.55
<Figure size 1100x400 with 2 Axes>

Figure 2:Fitting a random rotation with Adam. Left: both layers reach the target to machine precision. Right: the Householder layer stays orthogonal at every step, while the general matrix leaves the orthogonal group on the way and its log-determinant is no longer zero there.

Both fits reach machine precision. The Householder layer is orthogonal to about 10-15 at every one of the 1,500 steps, while the general matrix wanders up to about 0.55 away from orthogonality before it settles. Inside a flow that general matrix would have to pay its log-determinant at every step, and nothing would stop it from drifting towards a singular matrix.

4. The Cayley map and the matrix exponential

The other route starts from a skew-symmetric matrix S=−S⊤\mat{S} = -\mat{S}^\top, which has Dx(Dx−1)/2D_x (D_x - 1) / 2 free entries. Two maps send it to an orthogonal matrix, the Cayley map

Q=(I−S)(I+S)−1\mat{Q} = (\mat{I} - \mat{S}) (\mat{I} + \mat{S})^{-1}

and the matrix exponential

Q=exp⁡(S)\mat{Q} = \exp(\mat{S})

Both give det⁡Q=+1\det \mat{Q} = +1, so both produce rotations only, and both give the identity at S=0\mat{S} = \mat{0}. OrthogonalRotation is the Cayley map, with the strictly lower triangle of S\mat{S} as its parameters and S=0\mat{S} = \mat{0} at the start; it costs one linear solve per evaluation. We check both maps on a random S\mat{S}, then train OrthogonalRotation on the same target.

Cayley: max |Q^T Q - I| = 2e-16, det Q = +1.000
exp   : max |Q^T Q - I| = 2e-16, det Q = +1.000
OrthogonalRotation: final fit 6.2e-22, max |Q^T Q - I| over training 6.7e-16

Both maps are orthogonal to machine precision with determinant +1, and OrthogonalRotation fits the target rotation as well as the Householder layer did. For a rotation target the two layers are interchangeable; they differ in what they can reach.

5. What each can reach

A product of reflections with the wrong parity, or the Cayley map, can never produce a matrix of the other determinant. For two orthogonal matrices of opposite determinant, Q⊤Q⋆\mat{Q}^\top \mat{Q}_\star has determinant -1, so at least one of its eigenvalues is -1, its trace is at most Dx−2D_x - 2, and by (4)

∥Q−Q⋆∥F2≥4\Vert \mat{Q} - \mat{Q}_\star \Vert_F^2 \ge 4

whatever DxD_x is. A fit across the determinant stalls at 4.

The Cayley map has a second gap. The eigenvalues of a skew-symmetric S\mat{S} are imaginary, ±iψ\pm i \psi, so those of (5) are (1−iψ)/(1+iψ)(1 - i \psi) / (1 + i \psi), which is never -1. The Cayley map therefore misses every rotation with an eigenvalue -1, such as the half-turn diag(−1,−1,1)\mathrm{diag}(-1, -1, 1), and approaches it only as ψ→∞\psi \to \infty. The matrix exponential reaches it with ψ=π\psi = \pi.

We fit a random reflection (det⁡Q⋆=−1\det \mat{Q}_\star = -1) and the half-turn with each layer. The Cayley layer starts from a small random S\mat{S} for the half-turn, because at S=0\mat{S} = \mat{0} the gradient towards a symmetric target such as a half-turn is exactly zero.

max |exp(S) - half-turn| = 1e-16

Now the four fits.

Each run keeps its fit history and its trained layer.

Householder K = 3, reflection  final fit 1.0e-30
Cayley, reflection             final fit 4.0e+00
Householder K = 2, half-turn   final fit 9.9e-32
Cayley, half-turn              final fit 9.7e-02

For the Cayley layer on the half-turn we also look at how large its parameters have grown.

Cayley on the half-turn: largest |S_ij| after 1,500 steps = 9.0
<Figure size 700x420 with 1 Axes>

Figure 3:Fits across the two gaps. A reflection target is reached by three Householder reflections, while the Cayley layer stalls at the bound 4 of the determinant. A half-turn is reached by two Householder reflections, while the Cayley layer creeps towards it as its parameters grow.

Three Householder reflections reach the reflection to machine precision and the Cayley layer stops at exactly 4, the bound (7); no amount of training can cross it. On the half-turn, two reflections are exact while the Cayley layer is still about 0.1 away after 1,500 steps, with an entry of S\mat{S} near 9; the exponential map hits the half-turn with ψ=π\psi = \pi. Neither gap matters much for a Gaussianization flow, where the next marginal step can absorb a sign flip and a near half-turn is good enough, but they are why HouseholderRotation and OrthogonalRotation are not interchangeable in general.

Recap

IdeaEquationIn code
one reflection(1)householder above
a product of KK reflections, det⁡Q=(−1)K\det \mat{Q} = (-1)^K(2), (3)HouseholderRotation(n_reflections=K, shape=(D_x,))
fitting a target rotation(4)fit above, with optax.adam
the Cayley map, rotations only(5)OrthogonalRotation(shape=(D_x,))
the matrix exponential, every rotation(6)jax.scipy.linalg.expm
the gap across the determinant(7)Figure 3

Next. Sometimes we do not want to learn the rotation at all, but to fix it from the data (a PCA frame) and learn only the marginals, or to start a trainable stack from a fitted rotation. The next notebook covers FixedRotation and warm starts.

References
  1. Tomczak, J. M., & Welling, M. (2016). Improving Variational Auto-Encoders using Householder Flow. NeurIPS Workshop on Bayesian Deep Learning.