Posterior Mean Directly from Noise
Problem Statement
In practice the network predicts eps, and the posterior mean has a compact form that skips reconstructing x0 explicitly. Implement that direct eps-based mean.
Background
The DDPM reverse mean can be written directly in terms of the predicted noise:
μθ=αt1(xt−1−αˉtβtε^)
with alpha_t = 1 - beta_t. This is the p_theta(x_{t-1}|x_t) mean used in the standard ancestral sampler (before adding the noise term).
Your Task
Implement:
def posterior_mean_from_eps(x_t, eps, beta_t, alpha_bar_t):
Return the mean as a list rounded to 4 decimals.
Input Format
- x_t, eps: lists of length D.
- beta_t, alpha_bar_t (float).
Output Format
- A list of D floats rounded to 4 decimals.
Sample
print(posterior_mean_from_eps([1.0, 1.0], [0.5, -0.5], 0.2, 0.5))
Output:
[0.9599, 1.2761]
Example:
print(posterior_mean_from_eps([1.0, 1.0], [0.5, -0.5], 0.2, 0.5))
[0.9599, 1.2761]
- Compute the signal coefficient αt by subtracting the noise level from 1: αt=1−0.2=0.8.
- Determine the noise scaling factor by dividing βt by the square root of the remaining variance: coef=1−0.50.2=0.50.2≈0.28284.
- Calculate the numerator for each dimension by subtracting the scaled predicted noise from the current state xt:
- Dimension 1: 1.0−(0.28284×0.5)≈0.85858
- Dimension 2: 1.0−(0.28284×−0.5)≈1.14142
- Scale the results by the inverse square root of αt to obtain the posterior mean:
- Dimension 1: 0.80.85858≈0.9599
- Dimension 2: 0.81.14142≈1.2761
- The final output is [0.9599, 1.2761]
Constraints:
len(x_t) == len(eps),0 < beta_t < 1,0 < alpha_bar_t < 1.alpha_t = 1 - beta_t; apply the formula above.- Round to 4 decimals; avoid
-0.0.
1. Background Knowledge
In DDPM (Denoising Diffusion Probabilistic Models), the forward process gradually adds Gaussian noise to data, while the reverse process learns to denoise. The reverse step pθ(xt−1∣xt) is a Gaussian whose mean depends on the predicted noise ε^ rather than an explicit reconstruction of x0. This "direct-from-noise" parameterization is numerically stable and avoids an extra division by αˉt that can amplify floating-point error.
The key schedule variables are:
- βt: the per-step noise variance, with αt=1−βt.
- αˉt=∏s=1tαs: the cumulative product, so xt=αˉtx0+1−αˉtε.
The posterior mean formula combines two terms: a scaling of the noisy observation xt by 1/αt, and a correction proportional to the predicted noise ε^, weighted by βt/1−αˉt. Intuitively, the first term "undoes" the single-step noise, while the second term nudges the estimate toward the learned denoised direction.
2. Algorithm Approach
This is a direct formula evaluation problem. There is no iterative or search-based algorithm; you simply plug the inputs into the closed-form expression element-wise. The pattern is:
- Compute the scalar coefficients that depend only on βt and αˉt.
- Apply the per-element affine transformation to each component of xt and ε^.
- Round and return.
Because the formula is element-wise, the implementation is a simple vectorized (or loop-based) operation over the D-dimensional vectors.
3. Step-by-Step Strategy
- Compute αt: αt=1−βt.
- Compute the scaling factor: s=1/αt.
- Compute the noise-correction weight: w=βt/1−αˉt.
- For each dimension i:
- μi=s⋅(xt[i]−w⋅ε[i])
- Round each μi to 4 decimal places.
- Return the resulting list.
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.