Dhariwal & Nichol (2021) condition an unconditional diffusion model on a class label by adding the gradient of a classifier's log-probability to each reverse step. Implement the guided noise estimate and one guided DDPM step.
guided_epsilon(eps, alpha_bar_t, grad_log_p, s)
eps is the unconditional noise prediction of shape (n, d), grad_log_p is with the same shape, alpha_bar_t is the scalar and s is the guidance scale.(n, d).classifier_guided_ddpm_step(x_t, t, eps_model, classifier_grad, betas, s, rng)
x_t has shape (n, d), t is an integer timestep, betas is the array (so and ).eps_model(x, t) returns and classifier_grad(x, t) returns , both of shape (n, d). Call each exactly once.guided_epsilon, then the DDPM mean
t > 0, draw with exactly one call to rng.standard_normal using the shape of x_t, and return ; if t == 0, return without touching rng."eps_guided" (), "mean" () and "x_prev" (), each of shape (n, d).The tests use an analytic classifier: the data is a two-component Gaussian mixture, so and its gradient have closed forms, and the exact is used as eps_model.
np.cumprod(1 - betas)[t] gives .s = 0 the step must reduce to a plain DDPM reverse step.