Diffusion forward processMediumnumpydiffusiongenerativeddpm
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 into pure Gaussian noise over steps, following a variance schedule . A key property is the closed form — you can jump directly to any timestep without iterating, which is what makes training efficient (you sample a random each step).
Problem statement
Implement forward_diffusion(x0, t, betas, noise) using the closed-form marginal:
where is the provided noise.
Input
x0— clean sample,np.ndarray.t—int, target timestep (0-indexed).betas—np.ndarray(T,), the variance schedule.noise—np.ndarray, Gaussian noise with the same shape asx0.
Output
An np.ndarray (same shape as x0): the noised sample .
Examples
Example 1
Input: x0 = [1, 1], betas = [0.1, 0.2], t = 1, noise = [0, 0]
Output: [0.8485, 0.8485]
Explanation: , so . With zero noise, .
Constraints
- is the cumulative product of up to and including index
t. - Scale the signal by and the noise by .
- Do not iterate step by step — use the closed-form marginal.
Notes
- As grows, , so the signal fades and approaches pure noise — exactly the endpoint the reverse model learns to undo.
- The two coefficients satisfy , preserving unit variance when and 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