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 matrix-exponential flow

A linear vector field whose flow has a closed form: an exact log-determinant, an exact inverse, and the ceiling every affine map shares

02 · The matrix-exponential flow

Open In Colab

Notebook 00 trained FFJORD, whose map is the solution of an ODE with a free-form vector field and whose log-determinant is the integral of a trace (3), and notebook 01 made that trace cheap with a stochastic estimate. Both still need an ODE solver. When the vector field is linear, dz/dt=Azd\vect{z}/dt = \mat{A} \vect{z}, the solution has a closed form, the matrix exponential, and so do its inverse and its log-determinant. MatrixExponential is that solution plus a shift that switches on with time. It is the simplest neural flow Biloš et al., 2021, a model of the solution map itself rather than of the vector field. The matrix-exponential example shows it pushing a circle, fitting a tilted Gaussian and sweeping tt; here we derive why it works and measure where it stops.

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. The layer

MatrixExponential maps a point x∈RDx\vect{x} \in \mathbb{R}^{D_x} at a time t∈Rt \in \mathbb{R} to

z=etAx+φ(t)b\vect{z} = e^{t \mat{A}} \vect{x} + \varphi(t) \vect{b}

where the generator A∈RDx×Dx\mat{A} \in \mathbb{R}^{D_x \times D_x} (the layer’s W) and the offset b∈RDx\vect{b} \in \mathbb{R}^{D_x} are its weights, and the time gate φ\varphi is a scalar function with φ(0)=0\varphi(0) = 0: φ(t)=t\varphi(t) = t by default (TimeIdentity), or a small network built to vanish at zero (TimeTanh, TimeFourier). The time enters as the layer’s condition, packed by pack_time_control. At t=0t = 0 the exponential is I\mat{I} and the shift is zero, so the layer is the identity whatever its weights. We build one with weights large enough to see and apply it at t=0t = 0 and t=1t = 1.

x            = [-1.184 -0.116]
z at t = 0   = [-1.184 -0.116]
z at t = 1   = [-4.235 -0.662]

The layer returns its input at t=0t = 0 bit for bit. The default w_init_scale is 10-2, which starts a fresh layer close to the identity at t=1t = 1 as well, the same start the couplings of Part 5 take; unlike the mixture couplings there (gauss_flows#265), nothing in the layer is interchangeable, so each weight gets its own gradient from the first step.

fresh A =
[[ 0.019 -0.005]
 [ 0.004  0.024]]

2. The log-determinant and the inverse in closed form

The Jacobian of (1) in x\vect{x} is etAe^{t \mat{A}}, whatever φ\varphi and b\vect{b} are. Jacobi’s formula, which says that the determinant of the exponential of a square matrix is the exponential of its trace, turns the log-determinant into a trace:

log⁡∣det⁡J(x)∣=log⁡det⁡etA=ttr(A)\log \vert \det \mat{J}(\vect{x}) \vert = \log \det e^{t \mat{A}} = t \mathrm{tr}(\mat{A})

It needs no ODE solve and no trace estimate, and it does not depend on x\vect{x}: the layer changes every volume by the same factor. The determinant is always positive, so the layer never flips orientation. The inverse is the same exponential at −t-t, because e−tAetA=Ie^{-t \mat{A}} e^{t \mat{A}} = \mat{I}:

x=e−tA(z−φ(t)b)\vect{x} = e^{-t \mat{A}} (\vect{z} - \varphi(t) \vect{b})

We check (2) against the log-determinant of the autodiff Jacobian at 21 times in [−1,1][-1, 1].

tr(A) = +2.5571
max |autodiff - t tr(A)| over 21 times: 4.4e-16

The two agree to machine precision. The layer’s own transform_and_log_det returns the closed form, and its inverse applies (3). We round-trip 2,000 points at t=1t = 1 with the test of (4).

round trip, max |x̂ - x| = 8.9e-16

When A\mat{A} is skew-symmetric, A=S\mat{A} = \mat{S} with S=−S⊤\mat{S} = -\mat{S}^\top, its trace is zero and etSe^{t \mat{S}} is the rotation of (6): the layer keeps every volume, and its linear part is one of the orthogonal maps of Part 2. We set the generator of a layer without a shift to the skew part of the one above.

max |QᵀQ - I| = 2.2e-16
log_det       = +0.0e+00

3. A flow, but of which ODE?

Without the shift, the layer is the exact flow of the linear ODE dz/dt=Azd\vect{z}/dt = \mat{A} \vect{z}, and flows compose in time:

esAetA=e(s+t)Ae^{s \mat{A}} e^{t \mat{A}} = e^{(s + t) \mat{A}}

so the map at time tt followed by the map at time ss is the map at time s+ts + t. The shift breaks this. Differentiating (1) in tt and replacing x\vect{x} by (3) gives the vector field the layer follows:

dzdt=Az+(φ′(t)I−φ(t)A)b\frac{d\vect{z}}{dt} = \mat{A} \vect{z} + (\varphi'(t) \mat{I} - \varphi(t) \mat{A}) \vect{b}

Its second term depends on tt (for the default φ(t)=t\varphi(t) = t it is (I−tA)b(\mat{I} - t \mat{A}) \vect{b}, constant only when Ab=0\mat{A} \vect{b} = \vect{0}), so the layer is the flow of a time-dependent vector field. It is still a neural flow in the sense of Biloš et al. (2021), the identity at t=0t = 0 and invertible at every tt, but two steps in time are no longer one longer step. We measure both statements, with s=0.3s = 0.3 and t=0.5t = 0.5.

  no shift: max |F(0.3, F(0.5, x)) - F(0.8, x)| = 8.9e-16
with shift: max |F(0.3, F(0.5, x)) - F(0.8, x)| = 2.2e-01

Without the shift the two agree to machine precision; with it they differ by a fifth of a unit. The field (5) is still exact: we compare the time derivative of the layer’s output, by autodiff, with its right-hand side at t=0.5t = 0.5.

max |dz/dt - field| = 5.0e-16

4. Trained at t=1t = 1: the ceiling of an affine map

To fit a density we pin the time at t=1t = 1 for every point, as the example does, and train by maximum likelihood with the recipe of Part 4. At a fixed time a layer is an affine map of x\vect{x}, and so is a chain of them, so the flow’s density is a Gaussian whatever its depth. On NN training rows with sample mean μ^x\hat{\gvect{\mu}}_x and sample covariance Σ^x\hat{\gmat{\Sigma}}_x (divided by NN), the best Gaussian is the one with those moments, and its mean negative log-likelihood (1) is the entropy (2) of that Gaussian:

min⁡affineL(θ)=12log⁡det⁡(2πeΣ^x)\min_{\text{affine}} \mathcal{L}(\theta) = \frac{1}{2} \log \det(2 \pi e \hat{\gmat{\Sigma}}_x)

One layer can reach it: the symmetric square root Σ^x1/2\hat{\gmat{\Sigma}}_x^{1/2} is the exponential of 12log⁡Σ^x\frac{1}{2} \log \hat{\gmat{\Sigma}}_x. On data that is not Gaussian, the ceiling lies above the data’s own entropy by its negentropy (3), and no number of layers closes that gap. We test both statements on a tilted Gaussian and on two moons, with one layer and with three.

The task is Part 4’s, with the packed time of each row passed to the flow as its condition.

Every run takes 2,000 Adam steps of 256 rows on a one-cycle cosine rate that peaks at 10-2, with the gradient clipped to a global norm of 1. The chain starts from fresh layers, near the identity.

fit_chain builds a chain of fresh layers and trains it. The layers share one type and differ only in their weights, so they are built at once with eqx.filter_vmap and applied in turn by FlowJax’s Scan.

We train four flows, one and three layers on each dataset, and compare their training loss with the ceiling (6), which gaussian_entropy computes from the sample covariance.

tilted Gaussian, 1 layer(s): training NLL 3.0135, ceiling 3.0135, held-out NLL 2.9864
tilted Gaussian, 3 layer(s): training NLL 3.0135, ceiling 3.0135, held-out NLL 2.9863
      two moons, 1 layer(s): training NLL 2.7341, ceiling 2.7341, held-out NLL 2.6934
      two moons, 3 layer(s): training NLL 2.7341, ceiling 2.7341, held-out NLL 2.6935

Every flow ends on the ceiling to four decimals, with one layer as with three: depth adds parameters but not a single new density. On the tilted Gaussian the ceiling is the right answer. On the moons it is not, and the latent shows what is missing. We send the 400 held-out rows of each dataset through the three-layer flow and read the moments of the latent.

tilted Gaussian: mean [-0.     0.101], correlation -0.056, excess kurtosis [-0.258 -0.267]
      two moons: mean [-0.084  0.007], correlation -0.004, excess kurtosis [-0.6  -1.17]
<Figure size 800x800 with 4 Axes>

Figure 1:Each dataset (left) and the latent of its 400 held-out rows under a trained three-layer chain at t = 1 (right), with the circles of radius 1 and 2 of N(0, I). The Gaussian becomes a round cloud; the moons are whitened but stay two crescents.

Both latents have a mean near zero and almost no correlation on these held-out rows: an affine flow whitens. The Gaussian’s latent has an excess kurtosis (2) of -0.26 in each coordinate, about one standard error (24/400≈0.24\sqrt{24/400} \approx 0.24) from zero, as a normal sample of 400 rows would. The moons’ are -0.60 and -1.17, two and a half and almost five standard errors below zero: their tails are too light, with the mass pushed out to the two crescents. Whitening is all an affine map can do, so its training loss stops at the ceiling and the shape of the moons survives; bending them needs a map that is not linear in x\vect{x}, an FFJORD field (notebook 00) or the couplings of Part 5.

5. The times nobody trained

The flow was trained at t=1t = 1 only, but it is defined at every tt, and the example sweeps tt to show the family ptp_t that runs from the base at t=0t = 0 to the fit at t=1t = 1. Nothing in the loss asks anything of that family between the two ends: the path is whatever the parameterisation happens to give. We measure the held-out NLL of each three-layer flow on a grid of times from 0 to 1.5.

tilted Gaussian: NLL 3.646 at t = 0, 2.986 at t = 1, lowest 2.981 at t = 0.90
      two moons: NLL 2.805 at t = 0, 2.694 at t = 1, lowest 2.694 at t = 1.00
<Figure size 600x400 with 1 Axes>

Figure 2:Held-out NLL of each trained three-layer chain at the times from 0 to 1.5. Training fixed only t = 1 (dotted); at t = 0 every chain is the identity and its NLL is that of the data under N(0, I).

Both curves fall smoothly from the identity at t=0t = 0 to a minimum close to t=1t = 1 and rise again past it. For the moons the minimum is at t=1t = 1; for the Gaussian it is at t=0.9t = 0.9, 0.005 nats below the trained time, so on held-out rows even the end the loss fixed is not quite the best time. The smoothness comes from the exponential, not from the data: the loss saw only t=1t = 1, so the intermediate densities interpolate the two ends without being fitted to anything. To make the intermediate times mean something, the training data has to carry them, as in the conditional flows that take time as a covariate.

Recap

PieceEquationIn code
the layer, the identity at t=0t = 0(1)MatrixExponential, pack_time_control(t)
the log-determinant, a trace(2)layer.transform_and_log_det
the inverse, the exponential at −t-t(3)layer.inverse
a skew-symmetric generator is a rotation(6)eqx.tree_at(lambda m: m.W, …)
the linear part composes in time, the shift does not(4)
the time-dependent field the layer follows(5)jax.jacfwd(flow_map, argnums=1)
the ceiling of any affine flow(6)gaussian_entropy of the sample covariance

Next. A matrix-exponential flow is exact and cheap but affine at any fixed time, and FFJORD is free-form but needs a solver for every density. Notebook 03 puts a neural ODE inside a variational autoencoder: the ODE models whole trajectories, and the Gaussian is asked of a latent code rather than of the data.

References
  1. Biloš, M., Sommer, J., Rangapuram, S. S., Januschowski, T., & Günnemann, S. (2021). Neural Flows: Efficient Alternative to Neural ODEs. Advances in Neural Information Processing Systems (NeurIPS).