#155Diffusion forward processMediumNeural Networks
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...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example: zero noise at t=1 scales the signal
- •Reference: t=0 uses only the first beta
- •Sample: signal and noise mixed at t=2