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.

Latent ODE on spirals

Gaussianization moves into a latent space: encode an irregular trajectory to a code z0, push the prior to N(0,I), and let a neural ODE carry the dynamics — a stochastic, amortized Gaussianizer and the bridge to time series

03 — Latent ODE on spirals

Notebooks 00-02 Gaussianized with bijections: a deterministic, invertible map carries each data point to N(0,I)\mathcal{N}(0,I) and back, with an exact (or estimated) log-det. That contract breaks the moment the data is a variable-length, irregularly-sampled trajectory — there is no fixed-dimensional xx to invert. The latent ODE (Rubanova, Chen & Duvenaud Rubanova et al. (2019), Kidger (2021)) Gaussianizes such data a different way:

  1. Encode the whole trajectory {(ti,xi)}\{(t_i, x_i)\} to a single latent code with a neural-ODE encoder, producing q(z0∣x1:N)=N(μ,σ2)q(z_0\mid x_{1:N}) = \mathcal{N}(\mu, \sigma^2).
  2. Gaussianize the code by pulling q(z0)q(z_0) toward the prior N(0,I)\mathcal{N}(0,I) through the ELBO’s KL term — targeting a standard-normal latent (how close it actually gets is itself instructive; see §6).
  3. Evolve z(t)z(t) with a second neural ODE z˙=fθ(z,t)\dot z = f_\theta(z,t) and decode z(ti)↦x^iz(t_i)\mapsto \hat x_i.

This is a stochastic Gaussianizer, not a bijection — the SurVAE-style cousin (Part 8) of the flows above. It trades exact invertibility for two things the bijections cannot do: ingest irregular sequences of any length, and change dimension (RN×2→Rdz\mathbb{R}^{N\times 2}\to\mathbb{R}^{d_z}). The ODE prior also makes extrapolation natural — integrate z(t)z(t) past the last observation. It is the bridge from Part 6 to the time-series Gaussianization of Part 11.

What you will see

1. Irregularly-sampled spiral dataset

Following Rubanova et al. §4.1: 2-D spirals with a random phase and a random direction (clockwise / counter-clockwise), defined densely on a common grid but then irregularly subsampled with additive noise — the situation real sensors produce. A held-out split backs the reconstruction and extrapolation figures.

250 spirals (200 train / 50 eval), 30 irregular obs each on t in [0, 5.0]
<Figure size 1150x500 with 2 Axes>

2. The model: encode → latent ODE → decode

Three small modules, both vector fields being gf.DiffeqMLP. The encoder runs a forward neural ODE on the linearly-interpolated observation path X^(t)\hat X(t) — the observations enter as the control of the time-control condition, so the same DiffeqMLP plumbing that FFJORD used carries over — and maps the final hidden state to (μ,log⁡σ2)(\mu, \log\sigma^2) of q(z0)q(z_0). The dynamics field evolves z(t)z(t); the decoder is an MLP z(ti)↦x^iz(t_i)\mapsto\hat x_i with fixed observation noise.

latent dim 4, encoder hidden 25, obs noise 0.2
trainable parameters: 6679

3. Train by the ELBO

Each step encodes every trajectory with one forward ODE solve, samples z0z_0 by the reparametrisation trick, decodes with a second ODE solve at the observed times, and adds the Gaussian log-likelihood to the closed-form KL. Two ODE solves per trajectory make this the heaviest cell in Part 6; a cosine learning-rate decay lets it settle inside a short budget.

epoch    1  -ELBO    964.21  -recon    963.58  KL   0.62
epoch   40  -ELBO    519.39  -recon    498.69  KL  20.70
epoch   80  -ELBO    471.89  -recon    457.85  KL  14.04
epoch  120  -ELBO    460.54  -recon    447.08  KL  13.47
epoch  160  -ELBO    399.27  -recon    385.68  KL  13.59
epoch  200  -ELBO     63.93  -recon     48.07  KL  15.86
epoch  240  -ELBO     44.38  -recon     28.63  KL  15.76
<Figure size 660x420 with 1 Axes>

4. Reconstruction from irregular observations

For each held-out spiral we encode its handful of irregular, noisy points, then decode on the dense grid. The latent ODE produces a smooth curve that threads the observations — interpolation in continuous time, learned from the trajectory family.

<Figure size 1400x850 with 6 Axes>

5. Extrapolation past the observed window

Because the latent prior is an ODE, the model extrapolates for free: encode using only the observations on [0,T/2][0, T/2], then integrate z(t)z(t) all the way to TT. The dashed arm is pure forward prediction — the model never saw those times for this trajectory.

<Figure size 1400x850 with 6 Axes>

6. The Gaussianization check — the latent code

Now the Gaussianization question for the whole part: how standard-normal is the latent? The ELBO’s KL pulls each q(z0∣x)q(z_0\mid x) toward the prior, so we plot the aggregate posterior — the encoded z0z_0 across the dataset — against N(0,I)\mathcal{N}(0,I) and read off its moments.

mean KL(q(z0) || N(0,I)) over dataset: 15.674  (final-epoch batch mean)
aggregate latent moments (target 0, 1, 0, 0):
  z0[0]: mean=+1.324  std=1.491  skew=+0.203  exc-kurt=-0.715
  z0[1]: mean=-1.270  std=0.995  skew=-0.105  exc-kurt=-1.654
  z0[2]: mean=-1.117  std=1.028  skew=+0.057  exc-kurt=-0.786
  z0[3]: mean=+0.510  std=1.150  skew=-0.296  exc-kurt=-0.490
<Figure size 1500x460 with 3 Axes>

The KL did its job in part — the codes are roughly centred and unit-scale, not the runaway spread an unregularised autoencoder would give — but they are plainly not N(0,I)\mathcal{N}(0,I): the scatter shows arc-like clusters, the QQ plots bend off the diagonal, and the strongly negative excess kurtosis is the fingerprint of a ring / bimodal distribution rather than a Gaussian. That is not just the usual VAE aggregate-posterior gap; it is a topological obstruction. The spiral family is indexed by a circular phase and a binary direction, and neither a circle nor two discrete modes can be pushed smoothly into a Euclidean N(0,I)\mathcal{N}(0,I) — the same wall the bijective flows of Parts 0-5 hit on periodic data.

This is the honest limit of stochastic Gaussianization, and it points three ways: to Part 8 (surjections / stochastic transforms that formalise this lossy encode-decode), to Part 10 (non-Euclidean Gaussianization — the right target geometry for a circular latent), and to Part 19 (replacing the fixed N(0,I)\mathcal{N}(0,I) prior with a learned flow prior that can absorb such structure). What the latent ODE delivers regardless is the practical payoff above: a single code, learned through a standard-normal prior, from which the ODE reconstructs and extrapolates irregular trajectories.

Recap — and the end of Part 6

bijective CNF (00-02)latent ODE (here)
mapsdata point ↔N(0,I)\leftrightarrow \mathcal{N}(0,I)whole trajectory →\to code z0z_0
invertibleyes, exactlyno — stochastic encode/decode (ELBO)
log-dettrace integral / t tr⁡(W)t\,\operatorname{tr}(W)n/a — KL to the prior instead
dimensionpreservedchanged (RN×2→Rdz\mathbb{R}^{N\times 2}\to\mathbb{R}^{d_z})
handlesfixed-dd vectorsirregular, variable-length sequences

Part 6 walked the continuous-time family from one end to the other: the free-form FFJORD with its trace-integral log-det (00), the Hutchinson estimator that makes that trace scale (01), the closed-form matrix-exponential linear flow (02), and now the latent ODE, which gives up bijectivity to Gaussianize structured, irregular trajectories into a latent code. The first three are continuous bijections; this last is a continuous stochastic transform — the hinge to Part 8 (SurVAE surjections & stochastic flows).

Where this goes next. The latent ODE is the template for Part 11 (time-series Gaussianization — autoregressive and latent-ODE models for sequences) and feeds the probabilistic-programming integrations of Part 19 (flows and latent-variable models as priors/guides). Part 6 is complete.

References
  1. Rubanova, Y., Chen, R. T. Q., & Duvenaud, D. (2019). Latent Ordinary Differential Equations for Irregularly-Sampled Time Series. Advances in Neural Information Processing Systems (NeurIPS).
  2. Kidger, P. (2021). On Neural Differential Equations [Phdthesis]. University of Oxford.