Predict Noise from x0 and xt
Problem Statement
The three diffusion targets (eps, x0, v) are interconvertible given x_t and alpha_bar_t. Recover the noise eps from a known clean sample x0 and the noisy x_t.
Background
Since x_t = sqrt(alpha_bar) x0 + sqrt(1 - alpha_bar) eps, solving for the noise gives
Ξ΅=1βΞ±ΛβxtββΞ±Λβx0ββ
Your Task
Implement:
def eps_from_x0(x_t, x0, alpha_bar):
- x_t, x0: NumPy-compatible nested lists (or scalars) of equal shape.
Return the noise as a list rounded to 4 decimals.
Input Format
- x_t, x0: lists of equal length D.
- alpha_bar (float) in (0, 1).
Output Format
- A list of D floats rounded to 4 decimals.
Sample
print(eps_from_x0([1.4142, 0.0], [1.0, 1.0], 0.5))
Output:
[1.0, -1.0]
Example:
print(eps_from_x0([1.4142, 0.0], [1.0, 1.0], 0.5))
[1.0, -1.0]
- Compute the scaling factors from Ξ±Λ=0.5: Ξ±Λβ=0.5ββ0.7071 and 1βΞ±Λβ=0.5ββ0.7071.
- Calculate the numerator for the first dimension by subtracting the scaled clean sample from the noisy sample: 1.4142β(0.7071Γ1.0)β0.7071.
- Calculate the numerator for the second dimension: 0.0β(0.7071Γ1.0)=β0.7071.
- Divide each numerator by the noise scaling factor to recover the noise: 0.70710.7071β=1.0 and 0.7071β0.7071β=β1.0.
- The final output is [1.0, -1.0]
Constraints:
len(x_t) == len(x0),0 < alpha_bar < 1.eps = (x_t - sqrt(alpha_bar)*x0) / sqrt(1 - alpha_bar).- Round to 4 decimals; avoid
-0.0.
1. Background Knowledge
In denoising diffusion probabilistic models (DDPMs), the forward process gradually adds Gaussian noise to a clean data sample x0β over T discrete timesteps. At each step t, the noisy sample xtβ is a linear combination of the original signal and the injected noise Ξ΅βΌN(0,I):
xtβ=Ξ±Λtββx0β+1βΞ±ΛtββΞ΅Here, Ξ±Λtβ=βs=1tβΞ±sβ is the cumulative product of per-step signal retention factors, where Ξ±sβ=1βΞ²sβ and Ξ²sβ is the noise schedule. As t increases, Ξ±Λtβ decreases toward 0, meaning the signal contribution shrinks and the noise dominates.
The three common parameterizations of a diffusion model's output are:
- Epsilon (Ξ΅) prediction: The network predicts the noise added at step t.
- x0β prediction: The network predicts the original clean sample.
- Velocity (v) prediction: A reparameterization that interpolates between the two.
These are algebraically interconvertible given xtβ and Ξ±Λtβ. For instance, if you know x0β and xtβ, you can recover Ξ΅ by isolating it from the forward equation. This is useful in training pipelines where the model is trained with one parameterization but evaluated or analyzed with another.
2. Algorithm Approach
This is a direct algebraic inversion problem. Given the forward diffusion equation, solve for the unknown Ξ΅ by rearranging terms:
Ξ΅=1βΞ±ΛtββxtββΞ±Λtββx0ββThe approach is element-wise: for each dimension d, compute the numerator xt(d)ββΞ±Λtβββ x0(d)β, then divide by the scalar denominator 1βΞ±Λtββ. No iterative methods or optimization are neededβthis is a closed-form solution.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.