Neural ODE forward EulerMedium

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 dhdt=f(h,t)\frac{dh}{dt}=f(h,t) and integrates it with an ODE solver. The simplest solver is forward Euler, which takes fixed steps hn+1=hn+Δtf(hn,tn)h_{n+1}=h_n + \Delta t\,f(h_n, t_n). 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 dhdt=f(h,t)\frac{dh}{dt}=f(h,t) from t0 to t1 with steps equal forward-Euler steps:

Δt=t1t0steps,hn+1=hn+Δtf(hn,tn),tn+1=tn+Δt\Delta t = \frac{t_1 - t_0}{\text{steps}}, \qquad h_{n+1} = h_n + \Delta t\, f(h_n, t_n), \qquad t_{n+1} = t_n + \Delta t

Return the final state hh at t1t_1.

Input

  • f — callable f(h, t) returning dh/dtdh/dt (same shape as h).
  • h0 — initial state, np.ndarray.
  • t0, t1float start/end times.
  • stepsint, 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: dh/dt=hdh/dt = h has exact solution h(t)=h0eth(t)=h_0 e^{t}, so h(1)=eh(1)=e. Forward Euler with 1000 steps approximates it closely; more steps reduce the discretization error.

Constraints

  • Use a fixed step size Δt=(t1t0)/steps\Delta t = (t_1 - t_0)/\text{steps}.
  • Update both the state and the time each step.
  • Evaluate f at the current state and time (explicit Euler).

Notes

  • One forward-Euler step h + dt*f(h) is exactly the form of a residual block h + g(h) — Neural ODEs make that analogy precise.
  • Euler's error per step is O(Δt2)O(\Delta t^2); higher-order solvers (RK4, adaptive) reach the same accuracy with far fewer steps.
Python
Loading...

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