Implement the cosine noise schedule for diffusion models, as proposed by Nichol & Dhariwal. The cosine schedule provides a smoother noise progression than the linear schedule, with more steps at low noise levels where details matter most.
import numpy as np
def cosine_noise_schedule(num_timesteps: int, s: float = 0.008, max_beta: float = 0.999) -> dict:
steps = np.arange(num_timesteps + 1)
f_t = np.cos(((steps / num_timesteps) + s) / (1 + s) * (np.pi / 2)) ** 2
alpha_bars = f_t / f_t[0]
# Clip to avoid numerical issues
alpha_bars = np.clip(alpha_bars, 1e-8, 1.0)
# Compute betas from alpha_bars
betas = np.zeros(num_timesteps)
for t in range(num_timesteps):
betas[t] = 1 - alpha_bars[t + 1] / alpha_bars[t]
betas = np.clip(betas, 0.0, max_beta)
alphas = 1.0 - betas
alpha_bars_final = np.cumprod(alphas)
return {
"betas": betas,
"alphas": alphas,
"alpha_bars": alpha_bars_final,
"sqrt_alpha_bars": np.sqrt(alpha_bars_final),
"sqrt_one_minus_alpha_bars": np.sqrt(1.0 - alpha_bars_final),
}f(t) = cos((t/T + s) / (1 + s) * pi/2)^2 where s is a small offset preventing beta from being too small near t=0.alpha_bar_t = f(t) / f(0), giving a smooth curve from 1 (no noise) to near 0 (full noise).beta_t = 1 - alpha_bar_t / alpha_bar_{t-1}.max_beta for numerical stability.