Search

Search papers and lessons

Neural Ordinary Differential Equation

Level 4 · 10 min read

A neural ODE defines a network's hidden state as the solution of a differential equation, so depth becomes a continuous time that an ODE solver chooses how finely to step through.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Neural network basics
  • Probability
  • Discrete math
  • Architectures
  • Sign in to see your progress

TL;DR

A neural ODE replaces a stack of layers with a single function ff that describes how a hidden state changes over time, dhdt=f(h,t,θ)\frac{dh}{dt} = f(h, t, \theta). The output is the state at the end time, found by an ODE solver. The solver decides how many steps to take, so the effective depth is set by the required accuracy rather than by a fixed count of layers. Gradients are computed by solving a second ODE backwards, which uses memory that does not grow with the number of steps.

Where you'll see it: in continuous-depth models for irregularly sampled time series, in continuous normalising flows that transform one distribution into another, and as a conceptual bridge between residual networks and differential equations.

Intuition

A residual network is a ladder of tiny steps

Look at a residual block from residual connections. It takes the state hh and produces h+F(h)h + F(h). Now compare that with the simplest possible step of a numerical method. To move a point along a curve with slope ff, Euler's method takes h+Δt⋅f(h)h + \Delta t \cdot f(h). Set Δt=1\Delta t = 1 and F=fF = f, and the two formulas are identical.

So a residual network is an Euler solver for an ODE, where each layer is one step and the number of layers is the number of steps. The neural ODE takes this seriously. It asks: what if the layers were infinitely many, each infinitesimally small, so the stack became a continuous flow? The answer is a differential equation, and the depth of the network becomes the length of time you run the flow.

Why this is useful

A fixed stack of layers has two awkward properties. First, the depth is a hyperparameter you pick by hand and cannot change without retraining. Second, the network can only read its output at the fixed layer boundaries, so it cannot naturally answer a question like "what is the state at time 0.3?"

A neural ODE fixes both. The state is defined at every time, so you can read it wherever you need it. The solver can use fine steps where the dynamics change quickly and coarse steps where they are gentle. And the same learned function ff can be run for more or less time without new parameters.

flowchart LR
  A[Input state at time zero] --> B[ODE solver]
  B --> C[Evaluate f at current state and time]
  C --> D[Take a step]
  D --> B
  D --> E[Output state at end time]

The math

The model

Definition: Neural ODE

A neural ODE takes an input x\mathbf{x}, sets the initial state h(t0)=x\mathbf{h}(t_0) = \mathbf{x}, and defines the output as the state at a final time t1t_1, where the state follows

dh(t)dt=f(h(t),t,θ).\frac{d\mathbf{h}(t)}{dt} = f(\mathbf{h}(t), t, \theta).

The output is h(t1)\mathbf{h}(t_1). The function ff is a neural network with parameters θ\theta.

Here h(t)\mathbf{h}(t) is the hidden state at time tt, and ff is the learned function that sets its rate of change. The notation dhdt\frac{d\mathbf{h}}{dt} is the derivative of the state with respect to time. The network does not need to be a function of tt; the time argument is optional and is included here for generality. The solution is the solution of an ordinary differential equation with initial condition x\mathbf{x}.

Recovering a residual network

Apply Euler's method with step size Δt\Delta t to the model above, from t0t_0 to t1t_1, using NN steps so that Δt=(t1−t0)/N\Delta t = (t_1 - t_0)/N:

hk+1=hk+Δt f(hk,tk,θ).\mathbf{h}_{k+1} = \mathbf{h}_k + \Delta t\, f(\mathbf{h}_k, t_k, \theta).

Here hk\mathbf{h}_k is the state after kk steps and tk=t0+k Δtt_k = t_0 + k\,\Delta t. Rename Δt f\Delta t\, f as the residual branch FF, and this is a stack of NN residual blocks. A residual network is a neural ODE solved with Euler's method at a fixed step size. Chen and colleagues made this observation, and it is the starting point for the rest of the lesson.

Gradients by the adjoint method

To train the model we need ∂L/∂θ\partial L / \partial \theta, where LL is the loss on the output h(t1)\mathbf{h}(t_1). Storing every intermediate state and backpropagating through the solver, as in Backpropagation, works but costs memory that grows with the number of steps. The adjoint method avoids this.

Define the adjoint state a(t)=∂L/∂h(t)\mathbf{a}(t) = \partial L / \partial \mathbf{h}(t). It obeys its own ODE, run backwards in time from t1t_1:

da(t)dt=−a(t)⊤∂f∂h(h(t),t,θ),\frac{d\mathbf{a}(t)}{dt} = -\mathbf{a}(t)^{\top} \frac{\partial f}{\partial \mathbf{h}}\big(\mathbf{h}(t), t, \theta\big),

with starting value a(t1)=∂L/∂h(t1)\mathbf{a}(t_1) = \partial L / \partial \mathbf{h}(t_1). The gradient with respect to the parameters is an integral over the same backward path:

∂L∂θ=−∫t1t0a(t)⊤∂f∂θ(h(t),t,θ) dt.\frac{\partial L}{\partial \theta} = -\int_{t_1}^{t_0} \mathbf{a}(t)^{\top} \frac{\partial f}{\partial \theta}\big(\mathbf{h}(t), t, \theta\big)\, dt.

In practice the backward solve runs a single augmented ODE that carries h\mathbf{h}, a\mathbf{a} and the parameter gradient together. Its memory cost does not depend on the number of steps the solver takes.

Here is where the adjoint equation comes from. Over a short time ε\varepsilon, h(t+ε)≈h(t)+εf(h(t),t,θ)\mathbf{h}(t+\varepsilon) \approx \mathbf{h}(t) + \varepsilon f(\mathbf{h}(t), t, \theta). By the chain rule, a(t)=a(t+ε) ∂h(t+ε)∂h(t)≈a(t+ε)(I+ε∂f∂h)\mathbf{a}(t) = \mathbf{a}(t+\varepsilon)\, \frac{\partial \mathbf{h}(t+\varepsilon)}{\partial \mathbf{h}(t)} \approx \mathbf{a}(t+\varepsilon)\big(I + \varepsilon \frac{\partial f}{\partial \mathbf{h}}\big). Rearranging gives a(t+ε)−a(t)ε≈−a(t+ε)∂f∂h\frac{\mathbf{a}(t+\varepsilon) - \mathbf{a}(t)}{\varepsilon} \approx -\mathbf{a}(t+\varepsilon)\frac{\partial f}{\partial \mathbf{h}}, and letting ε→0\varepsilon \to 0 yields the equation above. It is the same calculation as Backpropagation through one residual block, repeated over infinitely many infinitesimal blocks.

The price is accuracy. Solving the state backwards from h(t1)\mathbf{h}(t_1) does not reproduce the forward states exactly, because each solve has its own error. That error enters the gradient.

Worked examples

Example
One Euler step is one residual block

Take the simplest possible dynamics dhdt=−h\frac{dh}{dt} = -h, with h(0)=1h(0) = 1 and end time t1=1t_1 = 1. The exact solution is h(1)=e−1≈0.3679h(1) = e^{-1} \approx 0.3679.

  1. Use a single Euler step, so Δt=1\Delta t = 1.
  2. Evaluate the rate at the start: f(1)=−1f(1) = -1.
  3. Update: h1=1+1×(−1)=0h_1 = 1 + 1 \times (-1) = 0.

The update h+Δt f(h)h + \Delta t\, f(h) with Δt=1\Delta t = 1 and F=fF = f is exactly a residual block whose branch returns −h-h. Its output, 00, is far from the true 0.36790.3679. One block is a very coarse solver.

Example
Finer steps shrink the error

Same problem, now with more steps. The step size is Δt=1/N\Delta t = 1/N.

  1. Two steps, Δt=0.5\Delta t = 0.5: h1=1−0.5=0.5h_1 = 1 - 0.5 = 0.5, then h2=0.5+0.5×(−0.5)=0.25h_2 = 0.5 + 0.5 \times (-0.5) = 0.25.
  2. Four steps, Δt=0.25\Delta t = 0.25: the state is multiplied by 0.750.75 each step, so h4=0.754≈0.3164h_4 = 0.75^4 \approx 0.3164.
  3. Errors against 0.36790.3679: one step gives 0.3680.368, two steps give 0.1180.118, and four steps give 0.0520.052.

The error falls as the step shrinks, and for small steps it falls roughly in proportion to Δt\Delta t, because Euler's method is first order. A solver chooses NN to meet a tolerance. A residual network with four blocks would be fixed at four steps whatever accuracy the task needed.

Example
Reading the state at irregular times

The same dynamics, dhdt=−h\frac{dh}{dt} = -h with h(0)=1h(0) = 1, can describe a process observed at irregular times. Suppose measurements arrive at t=0.3t = 0.3 and t=1.1t = 1.1.

  1. At t=0.3t = 0.3, the exact state is e−0.3≈0.7408e^{-0.3} \approx 0.7408.
  2. At t=1.1t = 1.1, the exact state is e−1.1≈0.3329e^{-1.1} \approx 0.3329.
  3. A solver can be asked to return the state at exactly those times, so the model reads off both values directly (a numerical solver gives close approximations of these exact values).

A fixed stack of layers has no state at t=0.3t = 0.3, so it would need interpolation or a fixed grid. The neural ODE produces the state at each requested time without any change to its parameters. This is why the model suits time series with gaps.

Tradeoffs and failure modes

Neural ODEs trade fixed structure for adaptivity. That helps in some settings and costs in others.

SituationProblemBetter choice
Fixed-depth task on discrete tokensAdaptive solving gives no benefit, and each solve is slower than a fixed stackAn ordinary residual or transformer network
Stiff dynamicsThe solver needs very small steps, and training becomes slowAn implicit solver, or a different parameterisation of ff
Adjoint gradients with a loose toleranceThe backward solve drifts from the forward trajectory, so gradients are inaccurateTighter tolerances, or backpropagating through the solver
Batches with very different dynamicsThe solver takes the smallest step any item needs, which wastes computeBatching by similar dynamics, or a shared step schedule
Output needed at few timesThe solver still steps through every intervalFixed coarse steps when accuracy allows
Need for exact invertibilityInvertibility holds in theory, but numerical inversion adds errorA model that is invertible by construction

The main practical cost is that every forward and backward pass runs a numerical solver, and the number of function evaluations varies with the input. This makes timing less predictable than for a fixed network.

History

The link between residual networks and ODEs was noticed before neural ODEs were named. In 2017, Eldad Haber and Lars Ruthotto analysed deep residual networks as discretised differential equations, and used that view to design stable architectures.

Ricky Chen, Yulia Rubanova, Jesse Bettencourt and David Duvenaud made the step explicit in 2018. They used the adjoint method to train continuous-depth models with constant memory and showed them on classification, time series and density modelling. The paper received a best paper award at NeurIPS that year. The adjoint method itself is much older. It comes from optimal control theory, where it was used decades earlier to compute gradients of costs for dynamical systems.

The idea spread quickly. Continuous normalising flows, such as FFJORD in 2019, used neural ODEs to transform one distribution into another with a tractable likelihood. Latent ODE models later applied the same approach to irregular time series. The main lasting contribution is the view that depth can be a continuous quantity.

Check yourself

QuestionA residual block computes y=x+F(x)\mathbf{y} = \mathbf{x} + F(\mathbf{x}). Which step of which numerical method does it match, and what would make the match exact?Show answer

It matches one step of Euler's method, hk+1=hk+Δt f(hk)\mathbf{h}_{k+1} = \mathbf{h}_k + \Delta t\, f(\mathbf{h}_k), with Δt=1\Delta t = 1 and F=fF = f. The two formulas coincide when the residual branch is the rate function itself, F=fF = f, with unit step size. Stacking blocks is repeated Euler steps. A finer method needs smaller steps, which means more blocks and smaller branches.

QuestionTake dhdt=2h\frac{dh}{dt} = 2h with h(0)=3h(0) = 3. Run one Euler step with Δt=0.5\Delta t = 0.5. What is hh after that step?Show answer

The rate at the start is 2×3=62 \times 3 = 6. The update is h1=3+0.5×6=6h_1 = 3 + 0.5 \times 6 = 6. The exact solution at t=0.5t = 0.5 is 3e1≈8.153e^{1} \approx 8.15, so one step under-estimates the growth. Smaller steps would bring it closer.

QuestionWhy does the adjoint method use constant memory with respect to the number of solver steps, and what does it give up in return?Show answer

The backward pass solves the augmented ODE for the state, the adjoint, and the parameter gradient in one run. It does not store the intermediate states of the forward solve, because it can re-solve them backwards. The cost is accuracy: the backward solve may not retrace the forward path exactly, so the computed gradient carries numerical error that depends on the solver tolerance.

QuestionYou want to model patient heart-rate measurements recorded at uneven times. Why might a neural ODE be a better fit than a fixed-depth recurrent network?Show answer

A neural ODE has a state defined at every time, so it can be read at each measurement time without padding or interpolation, and the solver takes steps that match the gaps between observations. A standard recurrent network treats inputs as evenly spaced steps and must be adapted for irregular gaps. The neural ODE handles both the timing and the dynamics in one model.

Sources