Browse and solve 1,200+ machine learning coding challenges on Deep-ML. Filter by difficulty (beginner, intermediate, advanced) and category (linear algebra, machine learning, deep learning, NLP, computer vision). Write Python solutions in the browser and get instant feedback against test cases.
Deep-ML | Reverse-SDE and Probability-Flow ODE SamplersReverse-SDE and Probability-Flow ODE Samplers
With beta = 1 and score = -x/2 the ODE drift at x = 2 is 0.5 * 2 + 0.5 * (-1) = 0.5, so one step of dt = 0.1 moves x to 2.05. The SDE drift is 0.5 * 2 + 1 * (-1) = 0, so only the noise term moves the sample: sqrt(1 * 0.1) * z with z = 0.125730 (the first draw of default_rng(0)) adds 0.039759, giving 2.039759.
The variance-preserving SDE of Song et al. (2021) is dx=−21β(t)xdt+β(t)dw on t∈[0,1]. Given the score s(x,t)=∇xlogpt(x) it can be reversed in two ways:
Reverse SDE: dx=[−21β(t)x−β(t)s(x,t)]dt+β(t)dwˉ, run from t=1 down to 0.
Probability-flow ODE: dx=[−21β(t)x−21β(t)s(x,t)]dt, which has the same marginals pt but is deterministic.
Implement three functions. x has shape (N, d), beta_fn(t) returns a float and score_fn(x, t) returns an array like x. A step moves from time t to t - dt with dt > 0.
reverse_sde_step(x, t, dt, beta_fn, score_fn, rng) (Euler-Maruyama on the reverse SDE with Δt=−dt)
ts is a decreasing 1-D time grid, e.g. np.linspace(1.0, 0.0, n + 1). For each consecutive pair apply one step at t = ts[i] with dt = ts[i] - ts[i + 1], using pf_ode_step when mode == "ode" and reverse_sde_step (with rng) when mode == "sde".
Do not modify x_T. Return the final (N, d) array (unrounded).
Hints
Both steps evaluate β and the score at the current time t, before moving.
The ODE drift is the SDE drift with the score term halved; the noise term disappears.
When the score is that of a Gaussian pt, the ODE flow is an affine map, so a few hundred Euler steps reproduce the exact answer closely.