Diffusion forward processMedium

Diffusion forward process

Background

Diffusion models generate data by learning to reverse a gradual noising process. The forward process is fixed: it slowly corrupts a clean sample x0x_0 into pure Gaussian noise over TT steps, following a variance schedule β1,,βT\beta_1,\dots,\beta_T. A key property is the closed form — you can jump directly to any timestep tt without iterating, which is what makes training efficient (you sample a random tt each step).

Problem statement

Implement forward_diffusion(x0, t, betas, noise) using the closed-form marginal:

αs=1βs,αˉt=s=0tαs,xt=αˉtx0+1αˉtϵ\alpha_s = 1-\beta_s, \quad \bar\alpha_t = \prod_{s=0}^{t}\alpha_s, \quad x_t = \sqrt{\bar\alpha_t}\,x_0 + \sqrt{1-\bar\alpha_t}\,\epsilon

where ϵ\epsilon is the provided noise.

Input

  • x0 — clean sample, np.ndarray.
  • tint, target timestep (0-indexed).
  • betasnp.ndarray (T,), the variance schedule.
  • noisenp.ndarray, Gaussian noise ϵ\epsilon with the same shape as x0.

Output

An np.ndarray (same shape as x0): the noised sample xtx_t.

Examples

Example 1

Input:  x0 = [1, 1], betas = [0.1, 0.2], t = 1, noise = [0, 0]
Output: [0.8485, 0.8485]

Explanation: α=[0.9,0.8]\alpha=[0.9,0.8], so αˉ1=0.90.8=0.72\bar\alpha_1 = 0.9\cdot0.8 = 0.72. With zero noise, x1=0.72[1,1]=[0.8485,0.8485]x_1 = \sqrt{0.72}\cdot[1,1] = [0.8485, 0.8485].

Constraints

  • αˉt\bar\alpha_t is the cumulative product of (1β)(1-\beta) up to and including index t.
  • Scale the signal by αˉt\sqrt{\bar\alpha_t} and the noise by 1αˉt\sqrt{1-\bar\alpha_t}.
  • Do not iterate step by step — use the closed-form marginal.

Notes

  • As tt grows, αˉt0\bar\alpha_t \to 0, so the signal fades and xtx_t approaches pure noise — exactly the endpoint the reverse model learns to undo.
  • The two coefficients satisfy αˉt+(1αˉt)=1\bar\alpha_t + (1-\bar\alpha_t) = 1, preserving unit variance when x0x_0 and ϵ\epsilon are unit-variance.
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.

  • Reference example (zero noise)
  • t = 0 uses only the first beta
  • Noise dominates as t grows large
  • Signal and noise coefficients square-sum to 1
  • Output shape matches the input