PIXELBANKv9.1.0
Menu

DDIM with Stochasticity (general eta)

Problem Statement

Implement the general DDIM update with the interpolation parameter eta sweeping from deterministic (eta=0) to full DDPM (eta=1).

Background

σt=η1−αˉt′1−αˉt1−αˉtαˉt′\sigma_t = \eta \sqrt{\frac{1-\bar{\alpha}_{t'}}{1-\bar{\alpha}_t}}\sqrt{1 - \frac{\bar{\alpha}_t}{\bar{\alpha}_{t'}}}

x^0=xt−1−αˉt ε^αˉt,xt′=αˉt′ x^0+1−αˉt′−σt2  ε^+σtz\hat{x}_0 = \frac{x_t - \sqrt{1-\bar{\alpha}_t}\,\hat{\varepsilon}}{\sqrt{\bar{\alpha}_t}}, \qquad x_{t'} = \sqrt{\bar{\alpha}_{t'}}\,\hat{x}_0 + \sqrt{1-\bar{\alpha}_{t'}-\sigma_t^2}\;\hat{\varepsilon} + \sigma_t z

Clamp the direction-term square root argument at 0. Add sigma_t * z only when eta > 0.

Your Task

Implement:

def ddim_step(x_t, eps, alpha_bar_t, alpha_bar_prev, eta=0.0, z=None):

Return x_{t'} as a list rounded to 4 decimals.

Input Format

  • x_t, eps: lists of length D; z: list or None.
  • alpha_bar_t, alpha_bar_prev (float); eta (float).

Output Format

  • A list of D floats rounded to 4 decimals.

Sample

print(ddim_step([0.4, -0.6], [0.1, 0.2], 0.5, 0.8, 0.0))

Output:

[0.4612, -0.8484]

Example:

Input:
print(ddim_step([0.4, -0.6], [0.1, 0.2], 0.5, 0.8, 0.0))
Output:
[0.4612, -0.8484]
Reasoning:
  • Compute the noise scale σt\sigma_t: Since η=0.0\eta = 0.0, the stochastic term vanishes, so σt=0\sigma_t = 0.
  • Estimate the initial state x^0\hat{x}_0 by removing the noise component from xtx_t: Using 1−αˉt=0.5≈0.7071\sqrt{1 - \bar{\alpha}_t} = \sqrt{0.5} \approx 0.7071 and αˉt=0.5≈0.7071\sqrt{\bar{\alpha}_t} = \sqrt{0.5} \approx 0.7071, we get x^0=xt−0.7071⋅ε^0.7071\hat{x}_0 = \frac{x_t - 0.7071 \cdot \hat{\varepsilon}}{0.7071}. For the first element: 0.4−0.7071(0.1)0.7071≈0.4596\frac{0.4 - 0.7071(0.1)}{0.7071} \approx 0.4596; for the second: −0.6−0.7071(0.2)0.7071≈−0.8485\frac{-0.6 - 0.7071(0.2)}{0.7071} \approx -0.8485.
  • Calculate the deterministic direction term: The coefficient is 1−αˉt′−σt2=1−0.8−0=0.2≈0.4472\sqrt{1 - \bar{\alpha}_{t'} - \sigma_t^2} = \sqrt{1 - 0.8 - 0} = \sqrt{0.2} \approx 0.4472. Multiplying by ε^\hat{\varepsilon} gives [0.0447,0.0894][0.0447, 0.0894].
  • Combine the scaled prediction and direction term to find xt′x_{t'}: Multiply x^0\hat{x}_0 by αˉt′=0.8≈0.8944\sqrt{\bar{\alpha}_{t'}} = \sqrt{0.8} \approx 0.8944 to get [0.4111,−0.7589][0.4111, -0.7589], then add the direction term: [0.4111+0.0447,−0.7589+0.0894]=[0.4558,−0.6695][0.4111 + 0.0447, -0.7589 + 0.0894] = [0.4558, -0.6695]. Correction: Re-evaluating the exact algebraic simplification for η=0\eta=0 where xt′=αˉt′x^0+1−αˉt′ε^x_{t'} = \sqrt{\bar{\alpha}_{t'}}\hat{x}_0 + \sqrt{1-\bar{\alpha}_{t'}}\hat{\varepsilon}:
    • Element 1: 0.8(0.4−0.5(0.1)0.5)+0.2(0.1)≈0.4612\sqrt{0.8}\left(\frac{0.4 - \sqrt{0.5}(0.1)}{\sqrt{0.5}}\right) + \sqrt{0.2}(0.1) \approx 0.4612
    • Element 2: 0.8(−0.6−0.5(0.2)0.5)+0.2(0.2)≈−0.8484\sqrt{0.8}\left(\frac{-0.6 - \sqrt{0.5}(0.2)}{\sqrt{0.5}}\right) + \sqrt{0.2}(0.2) \approx -0.8484
  • The final output is [0.4612, -0.8484]

Constraints:

  • len(x_t) == len(eps); alpha-bar values in (0, 1].
  • Clamp 1 - alpha_bar_prev - sigma^2 at 0 inside the square root.
  • Add sigma*z only when eta > 0; 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.