Search

Search papers and lessons

Neural Ordinary Differential Equations

Ricky T. Q. Chen, Yulia Rubanova, Jesse Bettencourt et al. · 2018

Chen et al. replace a stack of residual layers with a learned rate of change solved by a black-box ODE solver, which gives constant memory cost and lets the solver adapt its work to each input.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Probability
  • Generative models
  • ReadReady to readNot yet

TL;DR

Chen, Rubanova, Bettencourt and Duvenaud (2018) build a deep network whose hidden state is not produced by a fixed list of layers. A small neural network defines how fast the hidden state changes, and an off-the-shelf ODE solver computes where the state ends up. Gradients come from the Adjoint Sensitivity Method, which runs a second ODE backwards in time, so the memory cost does not grow with the number of solver steps. The paper tests this on MNIST digits, on density estimation with continuous normalizing flows, and on irregularly sampled time series.

Builds on: Neural Ordinary Differential Equation, Adjoint Sensitivity Method, Normalizing Flow · Leads to: Flow Matching, Flow Matching for Generative Modeling

Why it mattered

The paper made depth a continuous quantity. In its MNIST experiment, the six residual blocks of a small network were replaced by one call to a solver. The resulting ODE-Net reached 0.42 percent test error with 0.22 million parameters, while the residual network it was compared with reached 0.41 percent with 0.60 million parameters. The memory needed for training stayed constant as the solver took more steps, where the residual network's memory grew with its number of layers. A ResNet needs that memory to store every block's activations; the ODE-Net needs one state and one adjoint.

The paper also gave the community a usable tool. The authors released torchdiffeq, a PyTorch library with GPU implementations of standard solvers and the adjoint backward pass. Researchers building models in continuous time could then call a solver the way they call a layer.

Its generative part opened a line of work that continues today. A continuous normalizing flow moves samples from a simple distribution to a data distribution by following a learned flow, and it scores data by an exact formula. Flow Matching keeps the same picture of generation as an ODE from noise to data, but trains the velocity field directly. Much of the vocabulary of modern continuous-time generative models comes from this paper.

The problem

Before the paper, the standard deep networks were already a discrete approximation of an ODE. A residual block updates the state as ht+1=ht+f(ht,θt)\mathbf{h}_{t+1} = \mathbf{h}_t + f(\mathbf{h}_t, \theta_t), which is one Euler step of size 1 for the rule dhdt=f\frac{d\mathbf{h}}{dt} = f. Three problems followed from that.

The first is memory. Training a deep network means keeping the intermediate state of every layer, because backpropagation needs them. A network with LL layers stores LL states. An adaptive ODE solver may take many more steps than there are layers, and it chooses how many steps to take at run time, so the stored values are not even known in advance.

The second is that depth is fixed. A stack of layers spends the same computation on an easy input and on a hard one. An ODE solver can spend more steps where the dynamics change quickly and fewer where they change slowly, to hit a chosen error tolerance.

The third is irregular data. A recurrent network expects observations on a fixed grid of times, so measurements that arrive at uneven times have to be binned or padded. A differential equation has a state at every real-valued time, so it can be queried at each observation time directly.

For continuous normalizing flows there was a fourth obstacle. The change-of-variables formula needs the determinant of a Jacobian, whose cost grows with the cube of the dimension, which limits how wide each flow layer can be.

How it works

Take the smallest possible instance: a state with one number, dynamics f(z,t,θ)=θzf(z, t, \theta) = \theta z with one parameter, a start value z(0)=1z(0) = 1, an end time t1=1t_1 = 1, and the loss L=z(t1)L = z(t_1). Here θ=−1\theta = -1.

  1. Input. The start state is z(0)=1z(0) = 1 and the parameter is θ=−1\theta = -1. The network ff is a single multiplication by θ\theta. No hidden layer is needed to show the idea.

    Check: the rate of change at the start is θz=−1\theta z = -1, so the state should fall.

  2. Forward solve. A solver follows the rate from t=0t = 0 to t=1t = 1. The exact answer is z(t)=eθtz(t) = e^{\theta t}, so z(1)=e−1≈0.368z(1) = e^{-1} \approx 0.368. A crude Euler solver with two steps of size 0.5 gives z(0.5)=1+0.5×(−1)=0.5z(0.5) = 1 + 0.5 \times (-1) = 0.5 and then z(1)=0.5+0.5×(−0.5)=0.25z(1) = 0.5 + 0.5 \times (-0.5) = 0.25. The solver's job is to choose steps so that the error stays inside a tolerance. The result of this step is the output z(1)z(1).

    Check: with the exact answer the loss is L=0.368L = 0.368. The Euler answer 0.25 is off by about 0.12, which is why a serious solver adapts its step size.

  3. Loss gradient at the output. The loss is L=z(1)L = z(1), so its derivative with respect to the final state is ∂L∂z(1)=1\frac{\partial L}{\partial z(1)} = 1. This is the only thing the backward pass starts from.

    Check: increasing z(1)z(1) by 0.1 raises LL by 0.1, so the slope is 1.

  4. Backward solve for the adjoint. Define a(t)=∂L∂z(t)a(t) = \frac{\partial L}{\partial z(t)}, which starts at a(1)=1a(1) = 1. Its rule is dadt=−a⋅∂f∂z=−a⋅θ=a\frac{da}{dt} = -a \cdot \frac{\partial f}{\partial z} = -a \cdot \theta = a, since θ=−1\theta = -1. Solving backwards from t=1t = 1 gives a(t)=et−1a(t) = e^{t-1}, so a(0)=e−1≈0.368a(0) = e^{-1} \approx 0.368.

    Check: the loss is L=z0e−1L = z_0 e^{-1} for start value z0z_0, so ∂L∂z0=e−1\frac{\partial L}{\partial z_0} = e^{-1}. The adjoint gives the same number.

  5. Gradient with respect to the parameter. The parameter enters through ∂f∂θ=z(t)\frac{\partial f}{\partial \theta} = z(t). The gradient is dLdθ=∫01a(t)z(t) dt\frac{dL}{d\theta} = \int_0^1 a(t) z(t)\, dt. Here a(t)z(t)=et−1e−t=e−1a(t) z(t) = e^{t-1} e^{-t} = e^{-1} is constant, so the integral is e−1≈0.368e^{-1} \approx 0.368.

    Check: for z0=1z_0 = 1, L=eθL = e^{\theta} directly, so dLdθ=eθ=e−1\frac{dL}{d\theta} = e^{\theta} = e^{-1}. The two agree.

The whole training step is therefore one forward solve and one backward solve. The backward solve needs z(t)z(t) at each time, and the method gets it by running zz backwards alongside aa from its final value, so no forward state has to be stored.

flowchart LR
  Z0[z of 0] --> FWD[forward solve with f]
  FWD --> ZT[z of t1]
  ZT --> LOSS[loss L]
  LOSS --> ADJ[backward solve for a and the parameter gradient]
  ADJ --> G[gradient with respect to z of 0 and theta]

The math

The model defines the output as the end point of an initial value problem.

Definition: Neural ODE output

Given a start state z(t0)\mathbf{z}(t_0) and a network f(z,t,θ)f(\mathbf{z}, t, \theta), the output is

z(t1)=z(t0)+∫t0t1f(z(t),t,θ) dt=ODESolve(z(t0),f,t0,t1,θ).\mathbf{z}(t_1) = \mathbf{z}(t_0) + \int_{t_0}^{t_1} f(\mathbf{z}(t), t, \theta)\, dt = \text{ODESolve}(\mathbf{z}(t_0), f, t_0, t_1, \theta).

The right-hand side names the solver. Its internal steps are not part of the model.

Step 2 in symbols: the forward solve

The solver returns z(t1)\mathbf{z}(t_1) and the loss is L(z(t1))L(\mathbf{z}(t_1)). Nothing else from the solver's steps is kept for the backward pass, which is where the memory saving comes from.

Step 4 in symbols: the adjoint

The adjoint is the gradient of the loss with respect to the state at time tt:

a(t)=∂L∂z(t),da(t)dt=−a(t)T∂f(z(t),t,θ)∂z.\mathbf{a}(t) = \frac{\partial L}{\partial \mathbf{z}(t)}, \qquad \frac{d\mathbf{a}(t)}{dt} = -\mathbf{a}(t)^{\mathsf{T}} \frac{\partial f(\mathbf{z}(t), t, \theta)}{\partial \mathbf{z}}.

To see where the second line comes from, take one small step of size ε\varepsilon. The state moves by z(t+ε)≈z(t)+εf\mathbf{z}(t + \varepsilon) \approx \mathbf{z}(t) + \varepsilon f, so the map from z(t)\mathbf{z}(t) to z(t+ε)\mathbf{z}(t+\varepsilon) is the identity plus ε∂f∂z\varepsilon \frac{\partial f}{\partial \mathbf{z}}. The chain rule then gives a(t)T=a(t+ε)T(I+ε∂f∂z)\mathbf{a}(t)^{\mathsf{T}} = \mathbf{a}(t+\varepsilon)^{\mathsf{T}} \left(I + \varepsilon \frac{\partial f}{\partial \mathbf{z}}\right). Moving the ε\varepsilon term across and dividing by ε\varepsilon gives the equation above as ε\varepsilon goes to zero.

In the tiny example, ∂f∂z=θ=−1\frac{\partial f}{\partial \mathbf{z}} = \theta = -1, so dadt=a\frac{da}{dt} = a, which is the rule used in step 4.

Step 5 in symbols: the parameter gradient

The gradient with respect to the parameters is an integral of the adjoint against the parameter's effect on the dynamics:

dLdθ=−∫t1t0a(t)T∂f(z(t),t,θ)∂θ dt.\frac{dL}{d\theta} = -\int_{t_1}^{t_0} \mathbf{a}(t)^{\mathsf{T}} \frac{\partial f(\mathbf{z}(t), t, \theta)}{\partial \theta}\, dt.

The integral runs backwards from t1t_1 to t0t_0. The minus sign in front turns it into the ordinary forward integral from t0t_0 to t1t_1 of the same integrand.

In practice the paper solves all three quantities in a single backward call. It stacks them into one augmented state [z,a,g][\mathbf{z}, \mathbf{a}, \mathbf{g}], where g\mathbf{g} accumulates the parameter gradient, with g(t1)=0\mathbf{g}(t_1) = \mathbf{0}. Solving this backwards from t1t_1 to t0t_0 leaves the parameter gradient in g(t0)\mathbf{g}(t_0):

ddt(zag)=(f(z,t,θ)−aT∂f∂z−aT∂f∂θ).\frac{d}{dt}\begin{pmatrix} \mathbf{z} \\ \mathbf{a} \\ \mathbf{g} \end{pmatrix} = \begin{pmatrix} f(\mathbf{z}, t, \theta) \\ -\mathbf{a}^{\mathsf{T}} \frac{\partial f}{\partial \mathbf{z}} \\ -\mathbf{a}^{\mathsf{T}} \frac{\partial f}{\partial \theta} \end{pmatrix}.

Each product aT∂f∂z\mathbf{a}^{\mathsf{T}} \frac{\partial f}{\partial \mathbf{z}} is a vector-Jacobian product: a row vector times a matrix that is never written out. Automatic differentiation computes it at a cost close to one evaluation of ff (see Automatic Differentiation).

The instantaneous change of variables

For a continuous flow dzdt=f(z,t)\frac{d\mathbf{z}}{dt} = f(\mathbf{z}, t), the log density of the moving sample changes by

∂log⁡p(z(t))∂t=−tr⁡(∂f∂z).\frac{\partial \log p(\mathbf{z}(t))}{\partial t} = -\operatorname{tr}\left(\frac{\partial f}{\partial \mathbf{z}}\right).

The trace tr⁡\operatorname{tr} is the sum of the diagonal entries of a square matrix. The discrete formula needed a determinant, and the trace is much cheaper. Integrating this rate from t0t_0 to t1t_1 gives the total change in log density along the trajectory, so the density at the end follows from the density at the start. The trace is also linear, so if ff is a sum of MM terms, the log-density rate is the sum of the MM traces. A continuous layer with MM hidden units therefore costs time that grows only linearly with MM.

Results

MNIST classification (Table 1). The paper compared a one-layer multilayer perceptron, a residual network, a Runge-Kutta network that backpropagates through the solver, and an ODE-Net.

ModelTest errorParametersMemoryTime
1-layer MLP (from LeCun et al., 1998)1.60%0.24 Mnot reportednot reported
ResNet0.41%0.60 Mgrows with layers Lgrows with L
RK-Net0.47%0.22 Mgrows with solver evaluationsgrows with solver evaluations
ODE-Net0.42%0.22 Mconstant, O(1)grows with solver evaluations

The ODE-Net matches the residual network's error within 0.01 points, with fewer parameters and constant training memory. The paper's claim is parity in accuracy with a different cost profile, not a gain in accuracy.

Error control. The forward cost grows roughly in proportion to the number of function evaluations, so tightening the tolerance trades speed for precision. The paper also suggests training at high accuracy and testing at lower accuracy, which is possible because the tolerance is a run-time setting.

Continuous normalizing flows. On density matching, a planar continuous flow with MM hidden units reached a lower loss than a planar normalizing flow with MM layers, which the paper reports as generally lower loss in its figure. The two flows were not trained under identical budgets, so the comparison is about the model form more than the exact numbers. On maximum-likelihood density estimation, the continuous flow with 64 hidden units fitted the two-circles and two-moons data, while the discrete flow with 64 one-unit layers had trouble with the moons.

Latent ODE time series (Table 2). On 1000 two-dimensional spirals sampled at 100 times and corrupted by noise, the predictive root-mean-squared error on 100 later time points was as follows, when only 30, 50 or 100 of the observed points were used.

Observed pointsRNNLatent ODE
30 of 1000.39370.1642
50 of 1000.32020.1502
100 of 1000.18130.1346

The latent ODE is ahead at each sampling level, and its advantage is largest when the data are sparsest.

Limits. The experiments are small: MNIST, a handful of density datasets, and one synthetic spiral dataset. The paper's own section on scope lists three practical costs. Minibatching is less straightforward, because a batch shares one solver and may need more steps. Choosing tolerances is a user decision. And reversing the dynamics can add numerical error. The paper's results therefore show that the method works and has the right cost structure, not that it beats well-tuned discrete networks in general.

Tradeoffs and when to use it

A common mistake is to think that the adjoint method is always cheaper than backpropagating through the solver. It saves memory, but the backward solve recomputes the states, so each training step costs roughly one more solve. The paper reports that the adjoint method is memory efficient and, in its MNIST setting, comparable in time. Direct backpropagation through the solver is simpler and can be more exact when memory is not the bottleneck.

It is tempting to think a neural ODE has a fixed depth because the network ff is fixed. In fact the number of function evaluations depends on the input and on the tolerance, and the solver chooses it. Two inputs can use different amounts of computation through the same model. The paper treats this as a feature, but it makes timing harder to predict.

A second mistake is to think a small error tolerance is always better. Tight tolerances cost more evaluations. The authors chose 1e-3 for classification and 1e-5 for density estimation, and reported that these did not hurt performance. Sequence models used a much tighter tolerance.

A third mistake is to think that running the dynamics backwards recovers the forward trajectory exactly. It recovers it only up to numerical error, and the error can grow if the reconstruction drifts from the original. Checkpointing the forward states is one remedy, at the cost of some of the memory saving.

Use a neural ODE when the data arrives at irregular times, when you want an explicit accuracy-speed control, or when depth should adapt per input. Do not use it when a fixed, cheap stack of layers already works, because the solver adds overhead and tuning. Alternatives include residual networks with reversible blocks, which the paper cites as achieving constant memory with restricted architectures, and recurrent networks with time stamps concatenated to their inputs.

Reading guide

Check yourself

QuestionIn one sentence, what does the network f define in a neural ODE, and what is the output?Show answer

The network f(z,t,θ)f(\mathbf{z}, t, \theta) defines the rate of change of the hidden state, dzdt=f\frac{d\mathbf{z}}{dt} = f. The output is the state at the end time t1t_1, which the ODE solver computes from the start state by following that rate.

QuestionIn the tiny example with z(0)=1z(0) = 1, t1=1t_1 = 1, θ=−2\theta = -2 and L=z(1)L = z(1), what is dLdθ\frac{dL}{d\theta} to three decimal places?Show answer

The exact output is L=eθL = e^{\theta}, so dLdθ=eθ=e−2≈0.135\frac{dL}{d\theta} = e^{\theta} = e^{-2} \approx 0.135. The adjoint gives the same result: dadt=−a⋅θ=2a\frac{da}{dt} = -a \cdot \theta = 2a with a(1)=1a(1) = 1 gives a(t)=e2(t−1)a(t) = e^{2(t-1)}. Also ∂f∂θ=z(t)=e−2t\frac{\partial f}{\partial \theta} = z(t) = e^{-2t}, so a(t)z(t)=e−2a(t) z(t) = e^{-2} at every time, and the integral over [0,1][0, 1] is e−2e^{-2}.

QuestionWhy does the adjoint method use less memory than backpropagating through every solver step?Show answer

Backpropagation through the solver would store every intermediate state the solver visits, and the number of those grows with the number of steps. The adjoint method stores only the final state and the adjoint, then recomputes the earlier states by running the dynamics backwards from the end. Its memory therefore does not grow with the number of solver steps.

QuestionA sensor reports values at irregular times 0.2, 0.9 and 3.7. A recurrent network needs a fixed time grid. Why does the neural ODE not need one, and what would it do at time 5.0?Show answer

The neural ODE defines a state at every real-valued time, so the solver can stop at 0.2, 0.9, 3.7, and 5.0 as requested, with no bins or padding. Predicting at 5.0 means continuing the same solve past 3.7 to 5.0, with no new model and no new bins.

Sources

Read the full paper

Ricky T. Q. Chen, Yulia Rubanova, Jesse Bettencourt et al., 2018. arXiv:1806.07366

Neural ODE paper explained · ungoon.me