PIXELBANKv9.1.0
Menu

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:

μθ=1αt(xt−βt1−αˉt ε^)\mu_\theta = \frac{1}{\sqrt{\alpha_t}}\left(x_t - \frac{\beta_t}{\sqrt{1 - \bar{\alpha}_t}}\, \hat{\varepsilon}\right)

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:

Input:
print(posterior_mean_from_eps([1.0, 1.0], [0.5, -0.5], 0.2, 0.5))
Output:
[0.9599, 1.2761]
Reasoning:
  • Compute the signal coefficient αt\alpha_t by subtracting the noise level from 1: αt=1−0.2=0.8\alpha_t = 1 - 0.2 = 0.8.
  • Determine the noise scaling factor by dividing βt\beta_t by the square root of the remaining variance: coef=0.21−0.5=0.20.5≈0.28284\text{coef} = \frac{0.2}{\sqrt{1 - 0.5}} = \frac{0.2}{\sqrt{0.5}} \approx 0.28284.
  • Calculate the numerator for each dimension by subtracting the scaled predicted noise from the current state xtx_t:
    • Dimension 1: 1.0−(0.28284×0.5)≈0.858581.0 - (0.28284 \times 0.5) \approx 0.85858
    • Dimension 2: 1.0−(0.28284×−0.5)≈1.141421.0 - (0.28284 \times -0.5) \approx 1.14142
  • Scale the results by the inverse square root of αt\alpha_t to obtain the posterior mean:
    • Dimension 1: 0.858580.8≈0.9599\frac{0.85858}{\sqrt{0.8}} \approx 0.9599
    • Dimension 2: 1.141420.8≈1.2761\frac{1.14142}{\sqrt{0.8}} \approx 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.
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.