A note on the adjoint method for neural ordinary differential equation network

Pipi Hu · DOI

Abstract

Perturbation and operator adjoint methods are used to rigorously derive the correct adjoint form for Neural Ordinary Differential Equations (Neural ODEs). From this derivation, several key results emerge: 1) The loss gradient is not an ODE but an integral; 2) The traditional adjoint form is not inherently equivalent to backpropagation results; 3) Adjoint operator analysis demonstrates that the adjoint method matches backpropagation if and only if the discrete adjoint uses the same numerical scheme as the discrete forward ODE.

Introduction

Since the introduction of Neural ODEs in 2018, the adjoint method has become the standard for training these models due to its constant memory cost. However, the original mathematical foundations were often viewed as ambiguous regarding variable dependence. This note addresses three critical points of confusion: the exact nature of the gradient, the rigorous calculation for multi-label losses, and the "discretization gap" between the adjoint method and standard backpropagation.

Theoretical Framework: Perturbation Analysis

To establish a rigorous foundation, the author employs calculus of variations and perturbation analysis. By introducing a small perturbation $\epsilon$ to the parameters $\theta$ and observing the resulting change in the state trajectory $z(t)$, the exact relationship between the adjoint variable $a(t)$ and the loss gradient can be derived.

Perturbation analysis of parameters (a) and state trajectories (b).

This analysis reveals that the adjoint variable $a(t)$ is equivalent to the partial derivative of the loss with respect to the hidden state ($-\partial L / \partial z$).

Key Insights

The Gradient as an Integral

A common misconception in the community is that the gradient itself is computed by "solving an ODE." Rigorously, while the adjoint variable $a(t)$ follows an ODE, the actual gradient with respect to the parameters $\theta$ is the integral of the adjoint variable multiplied by the partial derivative of the dynamics function $f$ with respect to $\theta$.

Multi-label Loss Correction

In many applications, such as time-series forecasting, the loss depends on multiple snapshots along a trajectory rather than just the final state. The author shows that for a loss $L(z(t_1), z(t_2), \dots, z(t_N))$, the adjoint variable must be updated piecewise.

Trajectory with multiple observation points (a) and the resulting piecewise adjoint variable $a(t)$ with jumps (b).

As shown in the figure above, the adjoint variable $a(t)$ experiences "jumps" at each observation point $t_i$. The resulting formula for the loss gradient clarifies how parameter influence accumulates over these intervals: $$\frac{dJ(\theta)}{d\theta} = -\sum_{i=N}^{1} (N - i + 1) \int_{t_{i-1}}^{t_i} a(t) \frac{\partial f}{\partial \theta} dt$$

The Discretization Gap

Perhaps the most significant practical insight is the "Same Scheme" requirement. The paper proves that the adjoint method and standard backpropagation (Auto-Diff) are only equivalent if the discrete scheme used for the adjoint ODE is the exact mathematical adjoint of the scheme used for the forward ODE. If a different solver or different step sizes are used for the backward pass, the result is merely an approximation of the true gradient of the discrete model.

Conclusion

This theoretical note provides the necessary mathematical rigor to ensure the adjoint method is applied correctly in Neural ODEs. While it identifies the discretization gap, it also notes that numerical stiffness remains a challenge for backward-in-time integration, suggesting that matching schemes is a necessary but not sufficient condition for stable training in all scenarios.

Summary updated

BibTeX
@misc{hu2024note,
  author = {Pipi Hu},
  title = {{A note on the adjoint method for neural ordinary differential equation network}},
  year = {2024},
  eprint = {2402.15141},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.15141},
}
Figure