Build the cosine noise schedule
The linear beta schedule destroys small images too fast. Improved DDPM proposed a cosine schedule for alpha_bar instead:
f(t) = cos(((t / T + s) / (1 + s)) * pi / 2) ** 2
alpha_bar(t) = f(t) / f(0)
beta_t = 1 - alpha_bar(t) / alpha_bar(t - 1), clipped to at most 0.999
Write cosine_schedule(T, s=0.008) returning (alpha_bars, betas), each of length T for t = 1..T.
def cosine_schedule(T, s=0.008)Understand the idea
A noise schedule says how quickly a diffusion model destroys the signal. The cosine schedule sets alpha_bar, the fraction of signal left, along a squared cosine curve so it drops gently at the start and end and faster in the middle, instead of wasting many steps on almost pure noise. betas are then recovered from the ratio of neighbouring alpha_bar values and clipped at 0.999. Worked example, using s = 0 for easy numbers and T = 2: f(0) = cos(0)^2 = 1, f(1) = cos(pi/4)^2 = 0.5, f(2) = cos(pi/2)^2 = 0. alpha_bars = [0.5, 0]. beta_1 = 1 - 0.5 / 1 = 0.5. beta_2 = 1 - 0 / 0.5 = 1, clipped to 0.999. The default small s = 0.008 keeps early betas from being too tiny. Rule: f(t) = cos(((t/T + s) / (1 + s)) x pi / 2)^2; alpha_bar(t) = f(t) / f(0); beta_t = min(1 - alpha_bar(t) / alpha_bar(t - 1), 0.999) for t = 1..T. Improved DDPM introduced this schedule.