TL;DR
Many models describe a quantity that changes continuously, through an ordinary differential equation (ODE) with some parameters . We often want to know how a final loss changes when changes. The adjoint sensitivity method answers this by running a second ODE backwards from the end time to the start. It needs to store only the final state, not every intermediate step, so it trades extra computation for much less memory.
Builds on: Ordinary Differential Equation, Jacobian Matrix, Backpropagation · Leads to: Neural Ordinary Differential Equation
Where it's used
Continuous-depth neural networks. A Neural Ordinary Differential Equation replaces a stack of layers with one ODE whose solver takes many small steps. Training such a model with ordinary backpropagation would store the state at every solver step. The adjoint method computes the same gradients while storing only the final state and the adjoint.
Scientific models with unknown constants. Pharmacokinetic models, climate and chemical-kinetics models are ODEs with constants that are fitted to measurements. Gradients from the adjoint method let an optimiser fit those constants without finite differences, which need one extra simulation per parameter.
Optimal control. Designing a rocket burn or a robot trajectory means choosing a control signal to minimise a cost while the system follows an ODE. The adjoint equations were introduced for exactly this problem, and they remain the standard tool in control engineering.
How it works
Take the simplest ODE with one parameter. The state changes with time by the rule . Start at and run to time . The loss is the final state, . We want .
- Forward pass. Solve the ODE from to . Here the exact solution is , so . With , the loss is . Check: a solver would produce this number, up to rounding.
- Start the backward pass at the end time. Define the adjoint , the sensitivity of the loss to the state at time . At the end, , because equals .
- Write the rule for the adjoint. A small change in the state at time propagates forward, and the adjoint must change so that the product of adjoint and perturbation stays constant. The rule is . Check: with , the adjoint falls at rate per unit time, so gives .
- Find the rate of change of the loss with respect to . The parameter enters the ODE through , where . The integrand is . At this is . At it is .
- The integrand is the constant over the interval , so the integral is .
- Check the answer. The exact loss is , so at . The adjoint result matches.
- Notice what the backward pass needed. The integrand uses at every time, and the forward pass did not store them. In general the backward pass recovers by solving the original ODE backwards from alongside the adjoint, so only the final state is kept.
flowchart LR A["Forward: solve dz/dt = f from 0 to T"] --> B["Loss L from z(T)"] B --> C["Backward: solve adjoint from T to 0"] C --> D["Integrate a times df/dtheta"] D --> E["Gradient of L with respect to theta"]
- state z(t) = e^t (forward)
- adjoint a(t) = e^(1 - t) (backward)
The math
Let the state follow with given. Let the loss depend on the final state, . The adjoint is the row vector , whose entries are partial derivatives, as in the Jacobian of the loss.
Step 3 in symbols: the adjoint equation
Perturb the state at time by a small vector . The perturbation then evolves by the linearised ODE , where is the Jacobian of with respect to the state. The loss change is .
Consider the product . It stays constant along trajectories, since the loss change does not depend on the time we measure at. Its derivative is zero:
For this to hold for every , the adjoint must obey
This is an ODE run backwards from . In the tiny example , which gives , as in step 3.
Step 4 in symbols: the sensitivity to the parameter
The parameter enters only through . Let be the matrix that maps a perturbation at time to one at time , so . Then , and
This is the formula the tiny example used in step 4. It needs and at each time. The method gets both by solving the state and adjoint ODEs backwards together, and a standard trick is to add and the running integral as extra state variables. Their derivatives are zero and , so one backward solve returns the gradient.
Step 5 in symbols: the closed form for the tiny case
With , and . The adjoint is . The state is . The product is at every , so the integral is , matching step 6.
Worked examples
Solve with up to . The loss is .
- Forward: , so and .
- Adjoint start: .
- Adjoint rule: , because does not depend on . So , and for all .
- Parameter term: .
- Integrate: .
- Interpretation: each unit of adds to the final state, which matches and its derivative .
Solve with , , , and loss .
- Forward: , so . The loss is .
- Adjoint start: .
- Adjoint rule: , so .
- Product with the state: . The terms cancel, so the product is constant.
- Integrate over : .
- Check directly: , so at , . The two results agree.
A neural ODE has a hidden state of numbers per example. Its solver takes steps forward. Compare the memory for the gradient.
- Ordinary backpropagation through the solver stores the state at every step: numbers per example.
- The adjoint method stores the final state, numbers, and then the adjoint, another numbers, for a total of . It recomputes the earlier states during the backward solve.
- The ratio is . The adjoint uses about one fiftieth of the memory here.
- The trade-off is compute. The backward solve must integrate two ODEs, and it recomputes states, so each training step costs more arithmetic.
- Interpretation: when the solver takes many steps, the adjoint method keeps memory flat in depth, while the cost of backpropagation grows with it.
Try it: a solver takes steps and the state has numbers. How many numbers does backpropagation store per example, and how many does the adjoint method store? Answer: backpropagation stores . The adjoint method stores . The ratio is .
Common mistakes and tradeoffs
It is tempting to think the adjoint method gives the gradient for free. In fact the backward solve has its own numerical error, and it may not reproduce the forward trajectory exactly. This matters when dynamics are stiff or chaotic, where small errors grow quickly.
It is tempting to think the adjoint is the gradient. In fact is a sensitivity of the loss to the state at time , and the gradient with respect to the parameters is the integral in step 4.
It is tempting to think the adjoint method is always the better choice. In fact for a few solver steps, backpropagation through the solver is cheaper and exact up to the solver's own error. Use the adjoint method when memory is the binding constraint, many solver steps are needed, or the solver is an adaptive black box. Checkpointing offers a middle path: store some states and recompute the rest.
Alternatives include simple discretisation, which unrolls the solver and backpropagates through each step, and direct finite differences, which cost one extra simulation per parameter and lose precision.
History
Pontryagin and colleagues in the 1950s and early 1960s introduced adjoint variables for optimal control. Their maximum principle used a backward ODE to find the best control, and it is the origin of the idea.
The same chain-rule structure appeared in neural networks as Backpropagation, popularised by Rumelhart, Hinton and Williams in 1986. For discrete layers, backpropagation stores each layer's activations.
In 2018, Chen, Rubanova, Bettencourt and Duvenaud applied the adjoint method to neural networks with continuous depth. Their neural ODE paper made the method a standard option in deep learning, because it showed that memory could stay constant as the number of steps grew.
Recap
- An ODE with parameters defines a state that changes continuously, and a loss depends on its final value.
- The adjoint is the sensitivity of the loss to the state. It obeys a linear ODE that runs backwards from the end time.
- The gradient with respect to the parameters is the integral over time of the adjoint times the derivative of the rule with respect to the parameters.
- Solving the state and adjoint backwards together needs memory for one state, not one per step, at the cost of more arithmetic and possible reconstruction error.
- Next, read Neural Ordinary Differential Equation to see the method applied to a network, and Checkpointing for the memory trade-off in discrete models.
Check yourself
QuestionIn the tiny example with and , why is the adjoint constant?Show answer
The adjoint rule is . Here does not depend on , so and . Since , the adjoint stays for all .
QuestionFor with , and , what is the loss and its gradient ?Show answer
The solution is , so . The gradient is . The adjoint integral gives the same value: and at every time, so the integral over is .
QuestionWhy does the adjoint method use less memory than backpropagation through the solver steps?Show answer
Backpropagation through the steps needs the state at each solver step to compute the backward pass, so memory grows with the number of steps. The adjoint method stores only the final state and the adjoint, and it recovers earlier states by solving the state ODE backwards alongside the adjoint. Memory then does not depend on the number of steps.
QuestionA continuous-depth model has a state of numbers and its solver takes steps. Your training runs out of memory with naive backpropagation. Is the adjoint method a sensible next step, and what is the cost?Show answer
Yes, it is a sensible next step. Naive backpropagation stores numbers per example, while the adjoint method stores about . The cost is that each training step solves two ODEs backwards and recomputes states, so it runs slower, and its gradient can carry extra numerical error if the dynamics are stiff or chaotic.