Foundations to Frontier
Tests
H. Generative Models › H5 Normalizing Flows

Continuous normalizing flows and FFJORD

A continuous normalizing flow (CNF) defines the map from data to noise as the solution of an ordinary differential equation dz/dt = f(z, t) with a neural-network vector field. Any smooth field gives an invertible map (run the ODE backwards), with no architectural constraints such as coupling or masks. The density follows the instantaneous change of variables (Chen et al., 2018): d log p / dt = −tr(∂f/∂z), so a determinant becomes a trace integrated along the path. FFJORD (Grathwohl et al., 2019) made this scalable with Hutchinson's unbiased trace estimator, which needs one vector–Jacobian product per probe instead of D. This page covers neural ODEs, the instantaneous change of variables, Hutchinson's estimator, the adjoint method, solver choice and cost, regularisation, and the link to flow matching. In the exercises you compute exact and Hutchinson traces and integrate a linear ODE, then train a tiny CNF on two moons with a 10-step Euler solver. Its reported NLL (1.567 nats) looked as good as the true density (1.576), but that 'density' integrated to 1.28: the coarse solver was being exploited. Solving the same field accurately with RK4 gave a properly normalised density (mass 1.000) and an honest NLL of 1.924. Rademacher probes had lower variance than Gaussian ones (std 2.0 against 3.3).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Let the points flow like a current

Instead of stacking separate layers, imagine releasing data points into a river whose current is described by a neural network. After one unit of time, the current has carried the tangled data cloud into a neat bell-shaped cloud. Because a smooth current never makes two particles collide, the journey can always be reversed. To track density, you only need to know whether the current is spreading out or squeezing together at each point, its divergence, and add that up along each particle’s path. The demo moves a ring of particles with a swirling, contracting current.

Flow lab
dz/dt = A z rotates and contracts. ∫ tr A dt = -0.600; the log-det of 10 Euler steps is -0.373. They agree only as the step shrinks.
Why engineers care

Continuous flows connect generative modelling to dynamics: the same equations describe particle transport and fluid flow, and their descendants, flow matching and diffusion, now generate images, molecules and weather fields.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A CNF transforms data by…
  2. 2. Its density change along a path is governed by…
  3. 3. A CNF is inverted by…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Neural ODEs, traces and FFJORD

  • Neural ODE (Chen, Rubanova, Bettencourt and Duvenaud, 2018): z(1) = z(0) + ∫₀¹ f(z(t), t; θ) dt, solved numerically; invertible for any Lipschitz f.
  • Instantaneous change of variables: d log p(z(t))/dt = −tr(∂f/∂z(t)). So log p_X(x) = log p_Z(z(1)) + ∫₀¹ tr(∂f/∂z) dt, integrated jointly with z.
  • Cost of the trace: an exact trace needs D vector–Jacobian products (one per basis vector), O(D²) per function evaluation.
  • Hutchinson (FFJORD, Grathwohl et al., 2019): tr J = E[vᵀ J v] for probes with E[v vᵀ] = I (Gaussian or Rademacher); one VJP per probe, unbiased; use the same probe for the whole solve.
  • Adjoint method: gradients by solving an augmented ODE backwards, O(1) memory in the number of steps; or backpropagate through the solver (discretise-then-optimise), faster but memory-hungry.
  • Solvers: adaptive Dormand–Prince (dopri5) with tolerances; the number of function evaluations (NFE) grows during training. Regularising kinetic energy and Jacobian norms (Finlay et al., 2020; RNODE) keeps trajectories straight and NFE low.
  • Results: FFJORD reached 0.99 bits/dim on MNIST and 3.40 on CIFAR-10 with unrestricted architectures, at a high training cost.
  • Successors: flow matching and rectified flow (2022–23) train the same kind of vector field by simple regression, without simulating the ODE during training; they underpin many current image, video and science generators.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Hutchinson's estimator needs per probe…
  2. 2. The adjoint method trades…
  3. 3. FFJORD's CIFAR-10 result was…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Traces and solvers

Exercise 1.

  • The exact Jacobian trace of a one-hidden-layer field.
  • Hutchinson’s estimator from a Jacobian–vector product.
  • An Euler integrator that accumulates the trace (checked on a linear ODE).

Practice.

  • Use an adaptive solver with explicit tolerances (torchdiffeq odeint, diffrax) and report NFE.
  • Evaluate test likelihoods with tight tolerances, even if you train with looser ones; never trust a likelihood from the training discretisation alone.
  • Use Rademacher probes, fixed per sample for the whole solve.
  • Add kinetic-energy and Jacobian regularisation to cut NFE.
  • For sampling-only use cases, consider flow matching instead of maximum-likelihood CNF training.
import torch
from torchdiffeq import odeint_adjoint as odeint
def aug_dynamics(t, state, f, eps):                 # FFJORD: state = (z, log-det accumulator)
    z, _ = state
    with torch.enable_grad():
        z = z.requires_grad_(True); dz = f(t, z)
        vjp = torch.autograd.grad(dz, z, eps, create_graph=True)[0]
    return dz, (vjp * eps).sum(1)                  # Hutchinson estimate of tr(df/dz)
# z1, acc = odeint(lambda t, s: aug_dynamics(t, s, f, eps), (x, zeros), torch.tensor([0., 1.]), rtol=1e-5, atol=1e-5)
# log p(x) = log N(z1) + acc
🐍 Exercise 1: traces and the instantaneous change of variables
Write the exact trace of an MLP vector field, Hutchinson's estimator and an Euler integrator that accumulates the trace.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Test likelihoods of CNFs should be evaluated with…
  2. 2. Hutchinson probes should be…
  3. 3. A common PyTorch ODE library is…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Instantaneous change of variables. For a small step z ↦ z + h f(z), log |det(I + h J)| = h tr J + O(h²). Summing over steps and letting h → 0 gives log |det ∂z(1)/∂z(0)| = ∫ tr J dt (Liouville’s formula). For a linear field f = A z this is exactly tr A, while n Euler steps give n log |det(I + A/n)|, which differs by O(1/n).

Hutchinson’s estimator. For E[v vᵀ] = I, E[vᵀ J v] = tr(J E[v vᵀ]) = tr J. Variance: 2‖S‖_F² for Gaussian probes (S the symmetric part of J) and 2‖S‖_F² − 2 Σ J_ii² for Rademacher probes, never larger.

Why the discretisation can be gamed. If training uses a fixed coarse solver but accumulates h Σ tr J rather than the exact log-det of the discrete map, the computed ‘log p’ is not a normalised density; maximising it rewards fields whose true volume change differs from the reported one. Adaptive solvers with tight tolerances, or exact log-dets of the discrete steps, close this loophole.

Exercise check. Trained with 10 Euler steps, the CNF’s own NLL was 1.567 nats with a grid mass of 1.277; the same field solved with RK4 (40 steps) had mass 1.000 and NLL 1.924. For a linear field with tr A = 0.231, 10 Euler steps gave a log-det of 0.238 and 100 steps 0.231.

📝 Quick check (Level 3)
0/3 answered
  1. 1. For f = A z, the exact log-det after time 1 is…
  2. 2. Rademacher probe variance compared with Gaussian is…
  3. 3. A fixed coarse solver with h Σ tr J gives a 'density' that…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Is a CNF trained with a coarse fixed-step solver a valid density model, and which trace probes are better?

History.

  • R. T. Q. Chen, Y. Rubanova, J. Bettencourt and D. Duvenaud, “Neural ordinary differential equations” (NeurIPS 2018, best paper).
  • W. Grathwohl, R. T. Q. Chen, J. Bettencourt, I. Sutskever and D. Duvenaud, “FFJORD: free-form continuous dynamics for scalable reversible generative models” (ICLR 2019).
  • M. F. Hutchinson, “A stochastic estimator of the trace of the influence matrix for Laplacian smoothing splines” (1989).
  • C. Finlay, J.-H. Jacobsen, L. Nurbekyan and A. Oberman, “How to train your neural ODE” (ICML 2020).
  • Y. Lipman et al., “Flow matching for generative modeling” (ICLR 2023).

Exercise 2.

  • Linear check: random 2×2 A; ∫ tr A dt versus the log-det of 10 and 100 Euler steps.
  • CNF on moons: field f(z, t) = tanh(z W1 + t w + b1) W2 + b2 with 32 hidden units, exact trace; 10 Euler steps from data (t = 0) to noise (t = 1); Adam, learning rate 0.02, batch 128, 300 steps, backpropagating through the solver.
  • Evaluation: test NLL and total density mass on an 80×80 grid over [−4, 4]², with the training solver (Euler, 10 steps) and with RK4 (40 steps).
  • Hutchinson: on 500 test points, 200 Gaussian and 200 Rademacher probes each.
measure seed 0 seed 1 seed 2
tr A / Euler-10 / Euler-100 log-det 0.231 / 0.238 / 0.231 −0.958 / −1.087 / −0.970 −2.252 / −2.640 / −2.285
NLL, Euler 10 (training solver) 1.567 1.593 1.516
density mass, Euler 10 1.277 1.291 1.397
NLL, RK4 40 1.924 1.987 1.951
density mass, RK4 40 1.000 1.002 1.000
true density NLL 1.576 1.599 1.611
single-probe std, Gaussian / Rademacher 3.30 / 2.02 3.36 / 2.09 4.07 / 1.94
  • A misleading result: with the training solver the CNF seemed to match or beat the true density (1.516 against 1.611 on seed 2), but its ‘density’ integrated to 1.28–1.40. The model had learned to exploit the gap between the Euler map’s real volume change and the accumulated trace.
  • Honest evaluation: solved accurately, the same field is a valid density (mass 1.000) with NLL 1.92–1.99, about 0.34–0.39 nats worse than the truth.
  • Hutchinson: Rademacher probes cut the single-probe standard deviation by 1.6–2.1× (the mean absolute value of the trace was 1.8–3.1, so one probe is very noisy; FFJORD relies on averaging over many samples and steps).
  • Caveat: tiny model; real CNFs use adaptive solvers, which avoid most of this, at a higher cost.
🐍 Exercise 2 (research): is a CNF trained with a coarse fixed-step solver a valid density model, and which trace probes are better?
Train a tiny CNF on moons with a 10-step Euler solver, then check whether its density integrates to 1 and compare Gaussian and Rademacher trace probes.

Continuous flows in AI, robotics and aerospace

  • Flow matching: the modern descendant trains vector fields by regression and powers image, video, molecule and weather generators.
  • Physics and dynamics: neural ODEs model continuous-time dynamics of vehicles and spacecraft from irregularly sampled telemetry, and CNFs model densities that evolve in time.
  • Molecules and materials: equivariant CNFs sample molecular conformations with exact likelihoods for importance reweighting.

References

  1. R. T. Q. Chen, Y. Rubanova, J. Bettencourt and D. Duvenaud (2018). “Neural ordinary differential equations.” NeurIPS.
  2. W. Grathwohl, R. T. Q. Chen, J. Bettencourt, I. Sutskever and D. Duvenaud (2019). “FFJORD: free-form continuous dynamics for scalable reversible generative models.” ICLR.
  3. C. Finlay, J.-H. Jacobsen, L. Nurbekyan and A. Oberman (2020). “How to train your neural ODE: the world of Jacobian and kinetic regularization.” ICML.
  4. Y. Lipman, R. T. Q. Chen, H. Ben-Hamu, M. Nickel and M. Le (2023). “Flow matching for generative modeling.” ICLR.