v9.1.1
Menu

x0 and eps from a v-Prediction

mediumPROParameterizations and the Reverse PosteriorDiffusion ModelsDiffusion Models 1

Problem Statement

A v-prediction network outputs v; the sampler needs x0 and eps back. Given x_t, the predicted v, and alpha_bar, recover both.

Background

With a = sqrt(alpha_bar) and b = sqrt(1 - alpha_bar), the inverse relations are

x^0=a xt−b v,ε^=b xt+a v\hat{x}_0 = a\, x_t - b\, v, \qquad \hat{\varepsilon} = b\, x_t + a\, v

These follow from the orthogonal rotation that defines v; note it is an exact linear map, so no clipping is required here.

Your Task

Implement:

def x0_eps_from_v(x_t, v, alpha_bar):

Return a dict with "x0" and "eps", each a list rounded to 4 decimals.

Input Format

  • x_t, v: lists of equal length D.
  • alpha_bar (float) in (0, 1).

Output Format

  • A dict with two lists.

Sample

print(x0_eps_from_v([1.0, 0.0], [0.0, 1.0], 0.25))

Output:

{'x0': [0.5, -0.866], 'eps': [0.866, 0.5]}

Example

Input
print(x0_eps_from_v([1.0, 0.0], [0.0, 1.0], 0.25))
Output
{'x0': [0.5, -0.866], 'eps': [0.866, 0.5]}
Reasoning
  • Compute the scaling factors from αˉ=0.25\alpha_{\bar{}} = 0.25: a=0.25=0.5a = \sqrt{0.25} = 0.5 and b=1−0.25=0.75≈0.8660b = \sqrt{1 - 0.25} = \sqrt{0.75} \approx 0.8660.
  • Calculate the first element of x^0\hat{x}_0 using x^0=axt−bv\hat{x}_0 = a x_t - b v: 0.5(1.0)−0.8660(0.0)=0.50.5(1.0) - 0.8660(0.0) = 0.5.
  • Calculate the second element of x^0\hat{x}_0: 0.5(0.0)−0.8660(1.0)=−0.86600.5(0.0) - 0.8660(1.0) = -0.8660, which rounds to −0.866-0.866.
  • Calculate the first element of ε^\hat{\varepsilon} using ε^=bxt+av\hat{\varepsilon} = b x_t + a v: 0.8660(1.0)+0.5(0.0)=0.86600.8660(1.0) + 0.5(0.0) = 0.8660, which rounds to 0.8660.866.
  • Calculate the second element of ε^\hat{\varepsilon}: 0.8660(0.0)+0.5(1.0)=0.50.8660(0.0) + 0.5(1.0) = 0.5.
  • The final output is {'x0': [0.5, -0.866], 'eps': [0.866, 0.5]}

Constraints

  • len(x_t) == len(v), 0 < alpha_bar < 1.
  • x0 = a*x_t - b*v, eps = b*x_t + a*v, with a=sqrt(alpha_bar), b=sqrt(1-alpha_bar).
  • Round both to 4 decimals; avoid -0.0.

Learn this in

More from Diffusion Models 1

PRO

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

Sign in to run your code against the test cases.