Differential Equations in ML (Neural ODEs, Dynamics)
Differential equations (DEs) describe how quantities change continuously, modeling dynamic systems from physics to biology. In machine learning, DEs integrate with neural networks via Neural Ordinary Differential Equations (Neural ODEs), treating hidden states as solutions to ODEs parameterized by networks. This enables continuous-depth models, efficient time-series forecasting, and physics-informed neural networks (PINNs) that respect physical laws. By blending calculus with ML, Neural ODEs offer memory-efficient training and better extrapolation.
This lecture advances from integration and probability, delving into ODE basics, solving methods, Neural ODE formulations, adjoint sensitivity for training, stability analysis, and ML applications. We'll fuse mathematical theory with practical implementations in Python and Rust, providing intuitions, derivations, and code to harness DEs in AI systems.
1. Intuition for Differential Equations
A DE relates a function to its derivatives: dy/dt = f(t,y)—rate of change depends on time and state.
Solutions: y(t) satisfying DE.
Order: Highest derivative.
ML Connection
- Time-series: Model evolution like stock prices.
- Neural ODEs: ResNet as discrete DE approx.
INFO
DEs predict futures from rates, like velocity to position.
Example
- Exponential growth: dy/dt = k y, solution y = C e^{k t}.
- In epidemics (SIR model): dS/dt = -β S I, etc.
2. Ordinary Differential Equations (ODEs): First-Order
dy/dt = f(t,y), initial y(t0)=y0.
Existence/uniqueness: Picard-Lindelöf if f Lipschitz.
Linear: dy/dt + P(t) y = Q(t), integrating factor e^{∫P dt}.
ML Insight
- Parameterize f with NN for flexible dynamics.
Example: dy/dt = -y, y= e^{-t} (decay).
3. Higher-Order and Systems of ODEs
Higher: Reduce to first-order system.
E.g., d²y/dt² = -g, to v=dy/dt, dv/dt=-g.
Matrix form for linear systems: dy/dt = A y + b.
Solutions
- Analytic: Eigen decomp for constant A.
- Numerical: Needed for complex.
ML Application
- In control: State-space models.
4. Numerical Methods for Solving ODEs
Euler: y_{n+1} = y_n + h f(t_n, y_n).
Runge-Kutta (RK4): Weighted averages for accuracy.
Adaptive: Adjust h for error.
Stiffness
Stiff: Need implicit methods like backward Euler.
In ML: ODE solvers in Neural ODE backprop.
5. Introduction to Neural ODEs
Neural ODE: dz/dt = f_θ(t,z), z(0)=input, output=z(T).
f_θ: NN with params θ.
Continuous analog of ResNet: z_{l+1} = z_l + f(z_l).
Advantages: Constant memory (adjoint), any-time evaluation.
Formulation
Solve via ODE solver: z(T) = ODESolve(z(0), f_θ, 0, T).
Train: Minimize loss on z(T).
6. Adjoint Method for Backpropagation in Neural ODEs
Direct backprop through solver steps memory-heavy.
Adjoint: Define a(t) = dL/dz(t), da/dt = -a^T ∂f/∂z, integrate backward.
Augment with dL/dθ = ∫ -a^T ∂f/∂θ dt.
Efficient: Solve augmented ODE backward.
Derivation
From chain rule on integral form.
ML Insight
- Enables training deep continuous models.
7. Stability and Equilibrium in Dynamic Systems
Equilibrium: f(y*)=0.
Stability: Perturbations decay (asymptotic) or stay bounded.
Lyapunov: Function V decreasing along trajectories.
In ML: Analyze RNN/Neural ODE stability for long sequences.
8. Phase Portraits and Bifurcations (Conceptual)
Plot trajectories in state space.
Bifurcations: Qualitative changes with params.
In ML: Understand model behavior shifts.
Visual: Predator-prey cycles.
9. Partial Differential Equations (PDEs) Basics
Involve partial derivs: u_t = u_{xx} (heat).
In ML: PINNs solve PDEs by enforcing in loss.
10. Implementing ODE Solvers and Neural ODEs
Euler, RK4; simple Neural ODE.
import numpy as np
from scipy.integrate import solve_ivp
import torch
import torch.nn as nn
# Numerical ODE: dy/dt = -y
def f(t, y):
return -y
sol = solve_ivp(f, [0, 5], [1], method='RK45')
print("t:", sol.t[-1], "y:", sol.y[0,-1]) # ~e^{-5}
# Neural ODE toy
class ODEFunc(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Linear(2, 50), nn.Tanh(), nn.Linear(50, 2)
)
def forward(self, t, y):
return self.net(y)
def neural_ode(func, y0, ts):
# Simple Euler
y = y0
ys = [y]
for i in range(1, len(ts)):
dt = ts[i] - ts[i-1]
y = y + func(None, y) * dt
ys.append(y)
return torch.stack(ys)
func = ODEFunc()
y0 = torch.tensor([1.0, 0.0])
ts = torch.linspace(0, 1, 10)
traj = neural_ode(func, y0, ts)
print("Trajectory shape:", traj.shape)
fn f(_t: f64, y: f64) -> f64 {
-y
}
fn euler(f: fn(f64, f64) -> f64, t0: f64, y0: f64, t1: f64, steps: usize) -> f64 {
let h = (t1 - t0) / steps as f64;
let mut y = y0;
let mut t = t0;
for _ in 0..steps {
y += h * f(t, y);
t += h;
}
y
}
fn main() {
let sol = euler(f, 0.0, 1.0, 5.0, 1000);
println!("y(5): {}", sol); // ~e^{-5}
// Neural ODE simplistic (no NN lib, simulate)
// Assume func returns [y1', y2']
fn func(_t: f64, y: [f64; 2]) -> [f64; 2] {
[y[1], -y[0]] // Harmonic
}
fn neural_ode_sim(func: fn(f64, [f64; 2]) -> [f64; 2], y0: [f64; 2], t0: f64, t1: f64, steps: usize) -> [f64; 2] {
let h = (t1 - t0) / steps as f64;
let mut y = y0;
let mut t = t0;
for _ in 0..steps {
let dy = func(t, y);
y[0] += h * dy[0];
y[1] += h * dy[1];
t += h;
}
y
}
let y_final = neural_ode_sim(func, [1.0, 0.0], 0.0, PI / 2.0, 100);
println!("y(π/2): {:?}", y_final); // ~[0,1] for sin, cos
}
Solves ODEs, simulates Neural ODE.
11. Physics-Informed Neural Networks (PINNs)
PINN: NN approx solution u(x,t), loss = data + λ ∫ |DE residual|^2.
Enforces physics.
Applications: Fluid dynamics, quantum.
12. Stochastic Differential Equations (SDEs) Intro
dy = f dt + g dW, W Wiener.
In ML: Langevin dynamics for sampling.
13. Key ML Takeaways
- ODEs model dynamics: Continuous transformations.
- Neural ODEs innovate: Depth without layers.
- Adjoint efficient: Train continuous models.
- Stability crucial: For reliable predictions.
- Code solves: Experiment with systems.
DEs enhance ML modeling.
14. Summary
Unpacked DEs from basics to Neural ODEs, adjoint, stability, with ML focus. Examples and Python/Rust code. Empowers dynamic AI.
Word count: ~2900.
Further Reading
- Chen et al., "Neural Ordinary Differential Equations".
- Rackauckas, "DifferentialEquations.jl" (Julia, but concepts).
- Hairer, Solving ODEs.
- Rust: 'differential-equation-solver' crates.