Full DDPM Ancestral Step
Problem Statement
Implement one complete DDPM ancestral sampling step: the eps-parameterized mean plus the injected posterior noise sigma_t * z.
Background
The reverse mean from a noise prediction is
μθ=αt1(xt−1−αˉtβtε^),αt=1−βt
Then a sample is drawn as x_{t-1} = mu + sigma_t * z where z is supplied and sigma_t = sqrt(beta_tilde_t) with the posterior variance beta_tilde_t = (1 - alpha_bar_prev)/(1 - alpha_bar_t) * beta_t. At the final step (alpha_bar_prev == 1) no noise is added.
Your Task
Implement:
def ddpm_step(x_t, eps, z, beta_t, alpha_bar_t, alpha_bar_prev):
Return x_{t-1} as a list rounded to 4 decimals. If alpha_bar_prev == 1.0, return the mean only.
Input Format
- x_t, eps, z: lists of length D.
- beta_t, alpha_bar_t, alpha_bar_prev (float).
Output Format
- A list of D floats rounded to 4 decimals.
Sample
print(ddpm_step([1.0, 1.0], [0.0, 0.0], [1.0, -1.0], 0.2, 0.5, 0.64))
Output:
[1.4975, 0.7386]
Example:
print(ddpm_step([1.0, 1.0], [0.0, 0.0], [1.0, -1.0], 0.2, 0.5, 0.64))
[1.4975, 0.7386]
eps=0 so mean = x_t/sqrt(0.8) = [1.1180,1.1180]. sigma = sqrt(0.36/0.50.2) = sqrt(0.144) = 0.3795. x = mean + 0.3795[1,-1] = [1.4975, 0.7386].
Constraints:
len(x_t) == len(eps) == len(z); schedule values in(0, 1).sigma_t = sqrt((1-alpha_bar_prev)/(1-alpha_bar_t)*beta_t).- No noise when
alpha_bar_prev == 1.0; round to 4 decimals; avoid-0.0.
1. Background Knowledge
DDPM (Denoising Diffusion Probabilistic Models) reverse a forward noising process by learning a noise-prediction network ϵθ(xt,t). Given a noisy sample xt and the predicted noise ε^, the model estimates the posterior mean μθ(xt,t), which points toward the cleaner sample xt−1. The mean formula is:
μθ=αt1(xt−1−αˉtβtε^)where αt=1−βt and αˉt=∏s=1tαs is the cumulative product of α values.
The full ancestral step does not just return the mean. It also injects posterior noise to preserve the stochastic nature of the reverse process. The posterior variance is:
β~t=1−αˉt1−αˉt−1βtand the standard deviation is σt=β~t. The sampled output is xt−1=μθ+σt⋅z, where z∼N(0,I) is supplied externally. At the final step (t=1), αˉ0=1, so β~1=0 and no noise is added.
2. Algorithm Approach
This is a direct formula evaluation problem. There is no iteration or search. The approach is:
- Compute αt=1−βt.
- Compute the scalar coefficient for the noise term: 1−αˉtβt.
- Compute the mean vector μθ element-wise.
- Compute σt from the posterior variance formula.
- If αˉt−1=1.0, return μθ; otherwise add σt⋅z element-wise.
- Round each element to 4 decimal places.
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.