Before you start
Sample a noisy image at step t of the diffusion forward process in one shot.
A diffusion model (DDPM) learns to undo a fixed noising process. The forward
process adds a little Gaussian noise at each step, with variance betas[t]:
x_t = sqrt(1 - beta_t) * x_{t-1} + sqrt(beta_t) * noise
You do not have to run all t steps. The composition of Gaussians is Gaussian,
so with alpha_s = 1 - beta_s and alpha_bar_t = prod_{s=0}^{t} alpha_s:
x_t = sqrt(alpha_bar_t) * x_0 + sqrt(1 - alpha_bar_t) * noise
Training uses this to jump to a random step, and the network learns to predict
noise from x_t.
def q_sample(x0, t, betas, noise):
Input
x0 = np.array([1.0, -1.0])
betas = np.array([0.1, 0.2, 0.3])
noise = np.array([0.5, 0.5])
q_sample(x0, t=1, betas=betas, noise=noise)
Output
array([ 1.11310327, -0.58395301])
Reasoning
Steps 0 and 1 keep 0.9 and 0.8 of the variance, so
alpha_bar_1 = 0.72. The signal is scaled by sqrt(0.72) = 0.8485 and the
noise by sqrt(0.28) = 0.5292. So x_t = 0.8485 * x0 + 0.2646.
t is a 0-based step index, so t = 0 uses only betas[0].x0 and noise have the same shape, which can be anything.sqrt(alpha_bar_t)^2 + sqrt(1 - alpha_bar_t)^2 = 1, so if x0 has unit
variance then so does x_t.Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell