Problems
Loading...
Step 5 of 5
Sampling Algorithm
Medium

Implement the complete deterministic-input DDPM sampling loop. Begin from the supplied noisy sample x_T and process timesteps T through 1. At loop index i, epsilon_preds[i] is the model prediction for timestep T-i. For steps T through 2, z_values[i] is the supplied stochastic noise.

At each timestep, compute

μt=1αt(xt−βt1−αˉt ϵ^t)\mu_t = \frac{1}{\sqrt{\alpha_t}}\left(x_t-\frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}}\,\hat{\epsilon}_t\right)

Then update the sample with

xt−1=μt+βt ztx_{t-1} = \mu_t + \sqrt{\beta_t}\,z_t

For t=1, omit the noise term and set x_0=mu_1. Here, T is the number of beta values, alpha_t=1-beta_t, bar-alpha_t is the cumulative product through t, hat-epsilon_t is the supplied model prediction, and z_t is the supplied stochastic noise. Use NumPy float64 calculations. Return the final x_0 as a nested list with the same shape as x_T, rounded to four decimals.

Loading visualization...

Examples

Input: x_T=[[0.5,-0.3]], betas=[0.001,0.0105,0.02], epsilon_preds=[[[0,0]],[[0,0]],[[0,0]]], z_values=[[[0.1,-0.2]],[[0.3,0.1]]]

Output: [[0.553,-0.323]]

Explanation: Three reverse steps are applied; supplied noise is added during the first two and omitted during the final step.

Input: x_T=[[1,-1]], betas=[0.001,0.0105,0.02], epsilon_preds=[[[0.5,0.5]],[[0.5,0.5]],[[0.5,0.5]]], z_values=[[[0,0]],[[0,0]]]

Output: [[0.8935,-1.1386]]

Input: x_T has shape (2,2), betas=[0.001,0.0105,0.02], epsilon_preds has shape (3,2,2), z_values has shape (2,2,2)

Output: [[0.1949,-0.4234],[0.632,0.0624]]

Hint 1

Use enumerate(range(T, 0, -1)) to align each prediction with its timestep.

Hint 2

Precompute alphas and their cumulative product before the reverse loop.

Hint 3

Read z_values[i] only when t is greater than 1.

Requirements

  • Start from the supplied x_T; do not generate a replacement sample.
  • Process beta values in reverse timestep order from T through 1.
  • Pair epsilon_preds in order with timesteps T through 1.
  • Pair z_values in order with timesteps T through 2 and omit noise at t=1.
  • Return the final nested list rounded to four decimals.

Constraints

  • x_T is a nonempty nested list of real numbers.
  • betas is a nonempty list of T float values strictly between 0 and 1.
  • epsilon_preds contains T arrays, each with the same shape as x_T.
  • z_values contains T-1 arrays, each with the same shape as x_T.
  • Numerical calculations use NumPy float64.