x0 and eps from a v-Prediction
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β=axtββbv,Ξ΅^=bxtβ+av
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:
print(x0_eps_from_v([1.0, 0.0], [0.0, 1.0], 0.25))
{'x0': [0.5, -0.866], 'eps': [0.866, 0.5]}- Compute the scaling factors from Ξ±Λβ=0.25: a=0.25β=0.5 and b=1β0.25β=0.75ββ0.8660.
- Calculate the first element of x^0β using x^0β=axtββbv: 0.5(1.0)β0.8660(0.0)=0.5.
- Calculate the second element of x^0β: 0.5(0.0)β0.8660(1.0)=β0.8660, which rounds to β0.866.
- Calculate the first element of Ξ΅^ using Ξ΅^=bxtβ+av: 0.8660(1.0)+0.5(0.0)=0.8660, which rounds to 0.866.
- Calculate the second element of Ξ΅^: 0.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, witha=sqrt(alpha_bar),b=sqrt(1-alpha_bar).- Round both to 4 decimals; avoid
-0.0.
1. Background Knowledge
In diffusion models, the forward process gradually adds Gaussian noise to a data point x0β over T steps. At any discrete step t, the noisy sample xtβ can be expressed as a linear combination of the original data and the noise:
xtβ=Ξ±Λtββx0β+1βΞ±ΛtββΞ΅where Ξ±Λtββ(0,1) is the cumulative product of per-step signal retention factors, and Ξ΅βΌN(0,I). The coefficients a=Ξ±Λtββ and b = \sqrt{1 - \bar{\alpha}_t) form an orthonormal pair (a2+b2=1), which means the mapping between (x0β,Ξ΅) and (xtβ,v) is a rotation in the 2D subspace spanned by each coordinate.
The v-prediction parameterization, introduced by Kingma & Gao (2023), predicts the vector v=bx0ββaΞ΅ instead of directly predicting x0β or Ξ΅. This parameterization has been shown to improve training stability, especially at high noise levels. Because the transformation is a rotation, it is invertible: given xtβ and v, you can recover both x0β and Ξ΅ through a simple linear combination.
The inverse relations are:
x^0β=axtββbv,Ξ΅^=bxtβ+avThese follow directly from substituting the forward equation into the definition of v and solving the resulting 2Γ2 linear system. Since the rotation matrix is orthogonal, no clipping or normalization is neededβthe recovered values are exact (up to floating-point precision).
2. Algorithm Approach
This is a direct algebraic inversion problem. The core idea is:
- Compute the scalar coefficients a=Ξ±Λtββ and b=1βΞ±Λtββ.
- Apply the two linear combinations element-wise across all D dimensions.
- Round results to the required precision.
There is no iterative procedure, no optimization, and no branching logic. The entire computation is a vectorized linear map applied independently to each coordinate.
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.