Kumita← Open visual lesson

Choose how quickly to add noise

The code behind the picture

Follow the lesson’s implementation, one code cell at a time.

Choose a language and read the cells in order. These are small teaching examples; the visual lesson runs separately.

Follow the calculation

Same inputs · Same operation · Same result

3 teaching cells · Python standard library · Read-only

  1. 1. Prepare the inputs

    Both schedules use 1000 steps. Step 0 is the clean picture, so its cumulative product is 1.

    import json
    import math
    
    T = 1000
    steps = [250, 500]
    s = 0.008
  2. 2. Build both schedules

    Each step keeps 1 − β of what was there; ᾱ is the running product of those keeps. The cosine β is capped at 0.999.

    def f(t):
        return math.cos(((t / T + s) / (1 + s)) * math.pi / 2) ** 2
    
    def linear(t):
        return 0.0001 + (0.02 - 0.0001) * (t - 1) / (T - 1)
    
    def cosine(t):
        return min(1 - f(t) / f(t - 1), 0.999)
    
    def alpha_bar(beta):
        out = [1.0]
        for t in range(1, T + 1):
            out.append(out[-1] * (1 - beta(t)))
        return out
  3. 3. Read the picture weights

    The picture weight at step t is √ᾱₜ. Print it for both schedules at the same steps.

    result = {
        name: [round(math.sqrt(alpha_bar(beta)[t]), 6) for t in steps]
        for name, beta in [("linear", linear), ("cosine", cosine)]
    }
    print(json.dumps(result))

Expected result

Running the three cells in order produces this result in either language.

{
  "linear": [
    0.723937,
    0.280334
  ],
  "cosine": [
    0.920333,
    0.70274
  ]
}