Neural ODE forward Euler
Background
A Neural ODE treats a network's depth as continuous: instead of stacking discrete layers, it defines the hidden state's rate of change and integrates it with an ODE solver. The simplest solver is forward Euler, which takes fixed steps . This connects deep residual networks (one Euler step ≈ one residual block) to the rich theory of differential equations.
Problem statement
Implement neural_ode_euler(f, h0, t0, t1, steps) that integrates from t0 to t1 with steps equal forward-Euler steps:
Return the final state at .
Input
f— callablef(h, t)returning (same shape ash).h0— initial state,np.ndarray.t0,t1—floatstart/end times.steps—int, number of Euler steps.
Output
An np.ndarray (same shape as h0): the integrated state at t1.
Examples
Example 1
Input: f(h, t) = h, h0 = [1.0], t0 = 0, t1 = 1, steps = 1000
Output: ~ [2.7196] (approaches e ≈ 2.71828)
Explanation: has exact solution , so . Forward Euler with 1000 steps approximates it closely; more steps reduce the discretization error.
Constraints
- Use a fixed step size .
- Update both the state and the time each step.
- Evaluate
fat the current state and time (explicit Euler).
Notes
- One forward-Euler step
h + dt*f(h)is exactly the form of a residual blockh + g(h)— Neural ODEs make that analogy precise. - Euler's error per step is ; higher-order solvers (RK4, adaptive) reach the same accuracy with far fewer steps.
This problem ships 5 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.
- •dh/dt = h approximates e over [0, 1]
- •Constant derivative gives exact linear growth
- •Zero derivative leaves the state unchanged
- •More steps reduce the error toward the exact solution
- •Time-dependent f: dh/dt = t integrates to t^2/2