Take one DDPM denoising step
Sampling from a diffusion model walks backwards from noise. Given the model's noise prediction eps for x_t, one DDPM step is:
mean = (1 / sqrt(alpha_t)) * (x_t - (beta_t / sqrt(1 - alpha_bar_t)) * eps)
x_{t-1} = mean + sqrt(beta_t) * z, but with no noise added at t = 1
where alpha_t = 1 - beta_t and alpha_bar_t is the product of alpha up to t.
Write ddpm_step(x_t, eps, betas, t, z) with 1-based t and betas indexed from 0 for step 1. Return x_{t-1}.
def ddpm_step(x_t, eps, betas, t, z)Understand the idea
Sampling from a diffusion model runs the noising process backwards. At step t the model predicts the noise eps inside x_t. One DDPM step removes a schedule-scaled part of that noise to get a mean, then adds a little fresh noise so sampling stays random, except at the very last step t = 1, where no noise is added. Worked example: betas [0.1, 0.2], t = 2, so beta_t = 0.2, alpha_t = 0.8, alpha_bar_t = 0.9 x 0.8 = 0.72. With x_t = 1, eps = 0.5, z = 1: noise coefficient = 0.2 / sqrt(0.28) = 0.378, so inner = 1 - 0.378 x 0.5 = 0.811. mean = 0.811 / sqrt(0.8) = 0.907. x_{t-1} = 0.907 + sqrt(0.2) x 1 = 0.907 + 0.447 = 1.354. Rule: mean = (x_t - beta_t / sqrt(1 - alpha_bar_t) x eps) / sqrt(alpha_t); x_{t-1} = mean + sqrt(beta_t) z for t > 1, else mean. Image generators like Stable Diffusion repeat steps like this many times.