TensorTonic
Study Plans
Study Plan
Cosine Noise Schedule
Diffusion ModelsNoise Schedules
Medium

Build the cosine noise schedule with num_steps timesteps, clip its betas at max_beta, and return the betas together with the cumulative product rebuilt from the clipped betas.

f(u)=cos⁡2 ⁣(u/T+s1+s⋅π2),βt=min⁡ ⁣(1−f(t+1)f(t), βmax⁡),αˉt=∏k=0t(1−βk)f(u) = \cos^2\!\left(\frac{u / T + s}{1 + s} \cdot \frac{\pi}{2}\right), \qquad \beta_t = \min\!\left(1 - \frac{f(t + 1)}{f(t)},\ \beta_{\max}\right), \qquad \bar\alpha_t = \prod_{k=0}^{t} \left(1 - \beta_k\right)f(u)=cos2(1+su/T+s​⋅2π​),βt​=min(1−f(t)f(t+1)​, βmax​),αˉt​=k=0∏t​(1−βk​)

Here TTT is num_steps, sss is the offset s, βmax⁡\beta_{\max}βmax​ is max_beta, and the timestep ttt runs from 0 to T−1T - 1T−1. The function fff is evaluated at the T+1T + 1T+1 points u=0,1,…,Tu = 0, 1, \dots, Tu=0,1,…,T, and each beta compares two neighbouring points.

Return a dict with exactly the keys "betas" and "alpha_bars", where each value is a float64 NumPy array of shape (num_steps,) in timestep order.

Examples

Input: num_steps = 4, s = 0.008, max_beta = 0.999

Output: {"betas":[0.152988,0.416958,0.707859,0.999],"alpha_bars":[0.847012,0.493844,0.144272,0.000144]}

Explanation: The cosine curve gives alpha_bar ratios that turn into betas of about 0.153, 0.417 and 0.708, and the last raw beta is almost exactly 1, so it is clipped to 0.999 and the final alpha_bar becomes 0.144272 * 0.001.

Input: num_steps = 3, s = 0.008, max_beta = 0.5

Output: {"betas":[0.257116,0.5,0.5],"alpha_bars":[0.742884,0.371442,0.185721]}

Input: num_steps = 2, s = 0.0, max_beta = 0.999

Output: {"betas":[0.5,0.999],"alpha_bars":[0.5,0.0005]}

Hint 1

np.arange(num_steps + 1) / num_steps gives every value of u / T on the grid.

Hint 2

f[1:] / f[:-1] gives every neighbouring ratio at once.

Hint 3

np.minimum(raw, max_beta) clips every value from above at max_beta.

Hint 4

Call np.cumprod only after the betas are clipped.

Requirements

  • Use NumPy array operations.
  • Compute alpha_bars as the inclusive running product of one minus the clipped betas.

Constraints

  • num_steps is a Python int between 1 and 1000.
  • s is a Python float with 0 <= s <= 0.1.
  • max_beta is a Python float with 0 < max_beta <= 0.999.
Try Similar Problems
Rescale to Zero Terminal SNRMediumLinear Beta ScheduleEasyForward Diffusion SampleEasyGaussian Mixture ScoreMediumTime-Conditioned Residual BlockMediumSpatial Self-Attention BlockMediumPosterior Mean and VarianceMediumPrediction ParameterizationMediumMin-SNR Loss WeightingMediumDDPM Ancestral StepMediumClassifier-Free GuidanceMediumDenoising Score Matching LossMedium

Accepts: integer

Accepts: number

Accepts: number

You must run your code first.

Cosine Noise Schedule

Diffusion ModelsNoise Schedules
Medium

Build the cosine noise schedule with num_steps timesteps, clip its betas at max_beta, and return the betas together with the cumulative product rebuilt from the clipped betas.

f(u)=cos⁡2 ⁣(u/T+s1+s⋅π2),βt=min⁡ ⁣(1−f(t+1)f(t), βmax⁡),αˉt=∏k=0t(1−βk)f(u) = \cos^2\!\left(\frac{u / T + s}{1 + s} \cdot \frac{\pi}{2}\right), \qquad \beta_t = \min\!\left(1 - \frac{f(t + 1)}{f(t)},\ \beta_{\max}\right), \qquad \bar\alpha_t = \prod_{k=0}^{t} \left(1 - \beta_k\right)f(u)=cos2(1+su/T+s​⋅2π​),βt​=min(1−f(t)f(t+1)​, βmax​),αˉt​=k=0∏t​(1−βk​)

Here TTT is num_steps, sss is the offset s, βmax⁡\beta_{\max}βmax​ is max_beta, and the timestep ttt runs from 0 to T−1T - 1T−1. The function fff is evaluated at the T+1T + 1T+1 points u=0,1,…,Tu = 0, 1, \dots, Tu=0,1,…,T, and each beta compares two neighbouring points.

Return a dict with exactly the keys "betas" and "alpha_bars", where each value is a float64 NumPy array of shape (num_steps,) in timestep order.

Examples

Input: num_steps = 4, s = 0.008, max_beta = 0.999

Output: {"betas":[0.152988,0.416958,0.707859,0.999],"alpha_bars":[0.847012,0.493844,0.144272,0.000144]}

Explanation: The cosine curve gives alpha_bar ratios that turn into betas of about 0.153, 0.417 and 0.708, and the last raw beta is almost exactly 1, so it is clipped to 0.999 and the final alpha_bar becomes 0.144272 * 0.001.

Input: num_steps = 3, s = 0.008, max_beta = 0.5

Output: {"betas":[0.257116,0.5,0.5],"alpha_bars":[0.742884,0.371442,0.185721]}

Input: num_steps = 2, s = 0.0, max_beta = 0.999

Output: {"betas":[0.5,0.999],"alpha_bars":[0.5,0.0005]}

Hint 1

np.arange(num_steps + 1) / num_steps gives every value of u / T on the grid.

Hint 2

f[1:] / f[:-1] gives every neighbouring ratio at once.

Hint 3

np.minimum(raw, max_beta) clips every value from above at max_beta.

Hint 4

Call np.cumprod only after the betas are clipped.

Requirements

  • Use NumPy array operations.
  • Compute alpha_bars as the inclusive running product of one minus the clipped betas.

Constraints

  • num_steps is a Python int between 1 and 1000.
  • s is a Python float with 0 <= s <= 0.1.
  • max_beta is a Python float with 0 < max_beta <= 0.999.
Try Similar Problems
Rescale to Zero Terminal SNRMediumLinear Beta ScheduleEasyForward Diffusion SampleEasyGaussian Mixture ScoreMediumTime-Conditioned Residual BlockMediumSpatial Self-Attention BlockMediumPosterior Mean and VarianceMediumPrediction ParameterizationMediumMin-SNR Loss WeightingMediumDDPM Ancestral StepMediumClassifier-Free GuidanceMediumDenoising Score Matching LossMedium

Accepts: integer

Accepts: number

Accepts: number

You must run your code first.