One Step Out of the Static

Diffusion ModelsMedium+20 XP+20

One Step Out of the Static

In the lessons, each reverse step takes the noisy input, subtracts most of the predicted noise, and moves one step back along the chain. The original diffusion sampler is a different, random version of that walk. It does one extra thing at every step except the last: after removing noise, it mixes a small amount of fresh random noise back in. Randomness then enters all the way along the walk, not only in the starting static.

The schedule is a list of noise amounts β1,β2,…,βT\beta_1, \beta_2, \dots, \beta_Tβ1​,β2​,…,βT​, one per step. From it, define

αt=1−βt,αˉt=α1⋅α2⋯αt\alpha_t = 1 - \beta_t, \qquad \bar\alpha_t = \alpha_1 \cdot \alpha_2 \cdots \alpha_tαt​=1−βt​,αˉt​=α1​⋅α2​⋯αt​

You have the noisy pixels xtx_txt​ at step ttt, the network's predicted noise ϵ^\hat\epsilonϵ^, and a fresh noise draw zzz (one standard-normal number per pixel). One step back is, for every pixel,

xt−1=1αt(xt−βt1−αˉt ϵ^)+βt  zx_{t-1} = \frac{1}{\sqrt{\alpha_t}}\left(x_t - \frac{\beta_t}{\sqrt{1 - \bar\alpha_t}}\,\hat\epsilon\right) + \sqrt{\beta_t}\; zxt−1​=αt​​1​(xt​−1−αˉt​​βt​​ϵ^)+βt​​z

At the very last step, t=1t = 1t=1, the fresh-noise term is left out. That step produces the finished image, and nothing is added on top of it.

Task: write reverse_step(x_t, eps_pred, z, betas, t).

  • x_t, eps_pred and z are equal-length lists with one entry per pixel.
  • betas is the whole schedule: betas[0] is β1\beta_1β1​, betas[1] is β2\beta_2β2​, and so on. It can be longer than t.
  • t is the current step, counting from 1.

Return the list xt−1x_{t-1}xt−1​, with each value rounded to 4 decimal places.

In a real sampler, z comes from a random-number generator at every step. Here it is passed in so the result can be checked.

Examples

x_t = [0.5, -1.2]
eps_pred = [0.4, -0.9]
z = [1.0, -0.5]
betas = [0.1, 0.2, 0.3]
t = 3
returns [0.9417, -1.2499]
x_t = [0.3, 0.8]
eps_pred = [0.6, 1.1]
z = [0.7, -1.4]
betas = [0.1, 0.2, 0.3]
t = 1
returns [0.1162, 0.4766]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run