MatrixExponential is the simplest non-trivial time-indexed neural flow:
Compared with FFJORD this is closed-form — no ODE solver, no Hutchinson trace — but linear in , so a single layer can only realise an affine map. Stacking layers (with non-linear time gates between them) gives a tractable, ODE-free alternative for the same family of problems FFJORD handles.
Highlights of this notebook:
The map is the identity at because and the default time gate satisfies .
The log-det is exact and analytic — verified against
slogdet(jacrev).Stacking realises a richer family while keeping all three properties above.
from __future__ import annotations
import equinox as eqx
import jax
import jax.numpy as jnp
import jax.random as jr
import matplotlib.pyplot as plt
import numpy as np
import paramax
import seaborn as sns
from flowjax.bijections import Chain
from flowjax.distributions import Normal, Transformed
from pipekit_train import ArrayDataset, EarlyStopping, History, TrainingLoop
from pipekit_train.backends.equinox import EquinoxModelOp
from gauss_flows_models.inference import negative_log_likelihood
from gauss_flows_maps.bijections import MatrixExponential, TimeTanh
from gauss_flows_maps.conditioning import pack_time_control
sns.set_theme(context="poster", style="whitegrid", palette="deep", font_scale=0.85)
plt.rcParams.update(
{
"figure.dpi": 110,
"figure.constrained_layout.use": True,
"axes.grid.which": "both",
"grid.linewidth": 0.7,
"grid.alpha": 0.5,
"axes.titleweight": "semibold",
}
)
def style_axes(ax, *, aspect=None):
ax.minorticks_on()
ax.grid(True, which="major", linewidth=0.8, alpha=0.6)
ax.grid(True, which="minor", linewidth=0.4, alpha=0.3)
if aspect is not None:
ax.set_aspect(aspect)
return ax
palette = sns.color_palette("deep")/home/azureuser/localfiles/geo_repos/geo_ml/gauss_flows-fit/.venv/lib/python3.13/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
1. A single layer pushing a unit circle¶
Take the unit circle in , push it through for , and watch it morph from the identity at into a rotated/scaled/translated ellipse at .
key = jr.key(0)
bij_key, fit_key, sample_key = jr.split(key, 3)
single = MatrixExponential(
bij_key,
shape=(2,),
w_init_scale=0.6, # large enough to make the warp visible
)
theta = jnp.linspace(0.0, 2 * jnp.pi, 200)
ring = jnp.stack([jnp.cos(theta), jnp.sin(theta)], axis=-1)
times = jnp.linspace(0.0, 1.0, 5)
fig, axes = plt.subplots(1, 5, figsize=(18, 4), sharex=True, sharey=True)
for ax, t in zip(axes, times, strict=True):
cond = pack_time_control(float(t))
pushed = jax.vmap(lambda p: single.transform_and_log_det(p, cond)[0])(ring)
ax.plot(
np.asarray(ring[:, 0]),
np.asarray(ring[:, 1]),
color="0.6",
linewidth=1,
label=r"$t=0$",
)
ax.plot(
np.asarray(pushed[:, 0]),
np.asarray(pushed[:, 1]),
color=palette[0],
linewidth=2,
label=rf"$t={t:.2f}$",
)
ax.set_xlim(-2.5, 2.5)
ax.set_ylim(-2.5, 2.5)
ax.set_title(rf"$t = {float(t):.2f}$")
ax.set_xlabel("$x_1$")
ax.legend(loc="lower right", fontsize="small")
style_axes(ax, aspect="equal")
axes[0].set_ylabel("$x_2$")
plt.show()findfont: Failed to find font weight semibold for DejaVu Sans, now using 700.
findfont: Failed to find font weight semibold for DejaVu Sans, now using 700.

2. Closed-form log-det vs slogdet(jacrev)¶
The log-determinant has the analytic form , regardless of the time gate (the bias term shifts but doesn’t scale). We verify this matches autodiff on a non-trivial layer with a learned time gate.
gate_key, w_key = jr.split(jr.key(1), 2)
checked = MatrixExponential(
w_key,
shape=(4,),
time_bias_net=TimeTanh(gate_key, embedding_dim=8),
w_init_scale=0.2,
)
def forward(x, t):
cond = pack_time_control(t)
y, _ = checked.transform_and_log_det(x, cond)
return y
x_eval = jr.normal(jr.key(2), (4,))
ts = jnp.linspace(-1.0, 1.0, 21)
closed = jax.vmap(lambda t: t * jnp.trace(checked.W))(ts)
ad = jax.vmap(
lambda t: jnp.linalg.slogdet(jax.jacrev(forward, argnums=0)(x_eval, t))[1]
)(ts)
fig, ax = plt.subplots(figsize=(6.5, 4.5))
ax.plot(
np.asarray(ts),
np.asarray(ad),
"o",
color=palette[3],
label=r"slogdet$(\partial y/\partial x)$ via autodiff",
markersize=8,
)
ax.plot(
np.asarray(ts),
np.asarray(closed),
"-",
color=palette[0],
label=r"$t \cdot \mathrm{tr}(W)$ (closed-form)",
linewidth=2,
)
ax.set_xlabel("$t$")
ax.set_ylabel(r"$\log\,|\det \partial y/\partial x|$")
ax.set_title("Closed-form log-det matches autodiff")
ax.legend()
style_axes(ax)
plt.show()
max_gap = float(jnp.max(jnp.abs(closed - ad)))
print(f"max |closed - autodiff| over 21 query times: {max_gap:.2e}")
max |closed - autodiff| over 21 query times: 2.09e-07
3. Fit an anisotropic Gaussian target¶
Important caveat. A chain of MatrixExponential layers is still
affine in at any fixed query time (the composition of affine maps is
affine). It is genuinely a more flexible parameterisation than a single
linear layer — each block has its own , , and time gate — but it can
only realise the family of affine push-forwards of the base Gaussian. Use
this when your target is well-modelled by a Gaussian with arbitrary
covariance and mean.
To model multi-modal or otherwise non-Gaussian densities, pair
MatrixExponential with a non-linear layer (AffineCoupling,
DeepSigmoidCoupling, RQSplineCoupling, …) — the matrix exponential then
acts as a learned, exactly-invertible “rotation + scale” between non-linear
blocks.
The target here is a strongly tilted, anisotropic 2D Gaussian. That is
exactly the family MatrixExponential was designed for, so we expect a
tight fit. We freeze the time query at throughout training so the
flow behaves as a deterministic composition.
target_key = jr.key(7)
n_samples = 2000
# Tilted anisotropic Gaussian: rotate a (3, 0.5) covariance by 30 degrees.
angle = jnp.pi / 6
R = jnp.array([[jnp.cos(angle), -jnp.sin(angle)], [jnp.sin(angle), jnp.cos(angle)]])
scale = jnp.array([2.0, 0.4])
mean = jnp.array([0.5, -0.5])
z = jr.normal(target_key, (n_samples, 2))
target = (z * scale) @ R.T + meanclass FlowNLL:
"""pipekit-train's task: gauss_flows's negative log-likelihood of a batch."""
def loss_fn(self, flow, batch, key):
x, condition = batch
# an unconditional dataset has no targets, ()
condition = None if isinstance(condition, tuple) else condition
loss = negative_log_likelihood(flow, x, key=key, condition=condition)
return loss, {"loss": loss}
def fit_flow(
key,
flow,
x,
*,
condition=None,
learning_rate=5e-3,
clip_norm=None,
max_epochs=100,
max_patience=10,
batch_size=256,
val_prop=0.1,
):
"""Fit `flow` to the rows of `x` by maximum likelihood with pipekit-train.
A fraction `val_prop` of the rows is held out. Training stops after
`max_patience` epochs without a lower validation loss, and the flow of the
best epoch is returned with the per-epoch train and validation losses.
"""
split_key, loop_key = jr.split(key)
# one shuffle, then the last val_prop of the rows held out
order = np.asarray(jr.permutation(split_key, len(x)))
n_train = len(x) - round(val_prop * len(x))
train_rows, val_rows = order[:n_train], order[n_train:]
def rows(a, idx):
return () if a is None else np.asarray(a)[idx]
batch_size = min(batch_size, n_train)
steps_per_epoch = n_train // batch_size
optimizer = {"name": "adam", "lr": learning_rate}
if clip_norm is not None:
optimizer["clip_norm"] = clip_norm
# paramax.NonTrainable leaves (fixed rotations) stay out of the optimiser
trainable = jax.tree.map(
eqx.is_inexact_array,
flow,
is_leaf=lambda leaf: isinstance(leaf, paramax.NonTrainable),
)
history = History()
trained, _ = TrainingLoop(
EquinoxModelOp(flow, trainable=trainable),
ArrayDataset(rows(x, train_rows), rows(condition, train_rows)),
val_dataset=ArrayDataset(rows(x, val_rows), rows(condition, val_rows)),
task=FlowNLL(),
optimizer_config=optimizer,
max_steps=max_epochs * steps_per_epoch,
steps_per_epoch=steps_per_epoch,
eval_every_n_steps=steps_per_epoch,
log_every_n_steps=steps_per_epoch,
batch_size=batch_size,
seed=int(jr.randint(loop_key, (), 0, 2**31 - 1)),
callbacks=(
history,
EarlyStopping("loss", patience=max_patience, restore_best=True),
),
).run()
# the mean training loss of each epoch, and the validation loss after it
step_losses = np.array([metrics["loss"] for _, metrics in history.train])
n_epochs = len(step_losses) // steps_per_epoch
epoch_losses = step_losses[: n_epochs * steps_per_epoch].reshape(n_epochs, -1)
val_losses = np.array([metrics["loss"] for _, metrics in history.eval])
return trained.module, {"train": epoch_losses.mean(axis=1), "val": val_losses}n_layers = 4
layer_keys = jr.split(fit_key, n_layers)
layers = [
MatrixExponential(
k,
shape=(2,),
time_bias_net=TimeTanh(jr.fold_in(k, 99), embedding_dim=8),
w_init_scale=0.3,
)
for k in layer_keys
]
chain = Chain(layers)
dist = Transformed(Normal(jnp.zeros(2)), chain)
# Pin the time query at t = 1 for every training example.
t_one = jnp.broadcast_to(pack_time_control(1.0), (n_samples, 1))
trained_dist, losses = fit_flow(
fit_key,
dist,
target,
condition=t_one,
learning_rate=5e-3,
max_epochs=400,
max_patience=40,
batch_size=256,
val_prop=0.1,
)fig, ax = plt.subplots(figsize=(6, 4))
epochs = jnp.arange(len(losses["train"]))
ax.plot(epochs, losses["train"], label="train", color=palette[0])
ax.plot(epochs, losses["val"], label="val", color=palette[1])
ax.set_xlabel("Epoch")
ax.set_ylabel("Negative log-likelihood")
ax.set_title("MatrixExponential chain training curve")
ax.legend()
style_axes(ax)
plt.show()
Density and samples¶
The fitted chain produces a closed-form log-density on a grid in milliseconds (no ODE), and we sample by pushing base draws forward.
grid_n = 80
lim = 3.5
xs = jnp.linspace(-lim, lim, grid_n)
gx, gy = jnp.meshgrid(xs, xs)
grid_pts = jnp.stack([gx.ravel(), gy.ravel()], axis=-1)
grid_cond = jnp.broadcast_to(pack_time_control(1.0), (grid_pts.shape[0], 1))
@eqx.filter_jit
def grid_log_prob(pts, cond):
return jax.vmap(trained_dist.log_prob)(pts, cond)
log_probs = grid_log_prob(grid_pts, grid_cond).reshape(grid_n, grid_n)
density = jnp.exp(log_probs)
flow_samples = trained_dist.sample(
sample_key, (n_samples,), condition=pack_time_control(1.0)
)
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
ax_d, ax_s = axes
mesh = ax_d.contourf(
np.asarray(gx), np.asarray(gy), np.asarray(density), levels=20, cmap="viridis"
)
ax_d.set_title("Learned density (closed-form, $t=1$)")
ax_d.set_xlabel("$x_1$")
ax_d.set_ylabel("$x_2$")
fig.colorbar(mesh, ax=ax_d, label="$p_\\theta(x)$")
style_axes(ax_d, aspect="equal")
ax_s.scatter(
np.asarray(target[:, 0]),
np.asarray(target[:, 1]),
s=12,
alpha=0.5,
color=palette[0],
label="data",
edgecolors="none",
)
ax_s.scatter(
np.asarray(flow_samples[:, 0]),
np.asarray(flow_samples[:, 1]),
s=12,
alpha=0.5,
color=palette[3],
label="flow samples",
marker="x",
linewidths=0.8,
)
ax_s.set_xlim(-lim, lim)
ax_s.set_ylim(-lim, lim)
ax_s.set_title("Data vs. flow samples")
ax_s.set_xlabel("$x_1$")
ax_s.set_ylabel("$x_2$")
ax_s.legend(loc="upper right", framealpha=0.9)
style_axes(ax_s, aspect="equal")
plt.show()
4. The whole neural flow: distributions indexed by ¶
Because the bijection is parameterised by , training at also gives us — for free — a smooth one-parameter family of push-forwards
interpolating between the base Gaussian at and the fitted target at
. This is the “neural flow” perspective and the reason MatrixExponential
uses the packed-time conditioning contract: a single trained model encodes
the entire trajectory of distributions, and you can query any intermediate
without re-training.
trained_chain = trained_dist.bijection
sweep_times = jnp.linspace(0.0, 1.0, 5)
sweep_n = 800
sweep_base = jr.normal(jr.fold_in(sample_key, 17), (sweep_n, 2))
fig, axes = plt.subplots(1, 5, figsize=(20, 5), sharex=True, sharey=True)
for ax, t in zip(axes, sweep_times, strict=True):
cond_t = pack_time_control(float(t))
pushed = jax.vmap(lambda z: trained_chain.transform_and_log_det(z, cond_t)[0])(
sweep_base
)
ax.scatter(
np.asarray(pushed[:, 0]),
np.asarray(pushed[:, 1]),
s=10,
alpha=0.5,
color=palette[0],
edgecolors="none",
)
ax.set_xlim(-lim, lim)
ax.set_ylim(-lim, lim)
ax.set_title(rf"$t = {float(t):.2f}$")
ax.set_xlabel("$x_1$")
style_axes(ax, aspect="equal")
axes[0].set_ylabel("$x_2$")
plt.show()
At the chain is the identity, so is the base Normal. As moves from 0 to 1 the cloud smoothly tilts and stretches into the trained anisotropic target. The matrix exponential gives this interpolation exactly, with no ODE solve.
5. When to reach for which?¶
| property | MatrixExponential chain | FFJORD |
|---|---|---|
| exact log-det | yes ( per layer) | no (Hutchinson during training) |
| closed-form forward / inverse | yes (matrix exponential + linear solve) | no (ODE solve) |
| per-layer expressivity | affine in | universal in |
| training cost | dense matrix ops | many ODE steps × Hutchinson probes |
| scaling to high | dominated by matrix exp | dominated by ODE steps |
Use MatrixExponential chains when you want a fast, deterministic flow that
respects an explicit time index (e.g. a time-indexed continuous-in-spirit
model on tabular data, or as a cheap warm-start). Reach for FFJORD when the
data really wants a non-affine vector field per layer and you can afford the
ODE.