Skip to content
BIASED
Back to topic

Optional

DDPM: β schedule and sampling x_t

Computes ᾱ_t for a linear β schedule and checks marginal variance. Self-check in the notebook.

Version
v1
Minutes
~20
Evidence
Self-check in the notebook

Until platform auto-check exists, compare cell output with the expected numbers in the notebook. This is not a debt and does not affect mastery.

Download .ipynb

Download the notebook and run it locally. Its environment requirements are listed inside.

Notebook contents

Readable on your phone. The code below has not been executed in the browser; download the notebook to run it.

GM · DDPM: прямой шум и αˉt\bar\alpha_t / forward noise schedule

Activity version: 1 · Seed: 0 · Данные: скалярный x0=1x_0=1.

Тема DDPM forward: xt=αˉt x0+1−αˉt εx_t=\sqrt{\bar\alpha_t}\,x_0+\sqrt{1-\bar\alpha_t}\,\varepsilon с αt=1−βt\alpha_t=1-\beta_t, αˉt=∏s≤tαs\bar\alpha_t=\prod_{s\le t}\alpha_s. При x0=1x_0=1: E[xt2]=1\mathbb E[x_t^2]=1, Var(xt)=1−αˉt\mathrm{Var}(x_t)=1-\bar\alpha_t.

Ожидаемые числа (linear β\beta from 10−410^{-4} to 0.020.02, T=1000T=1000)
Quantity Value Tolerance
αˉ1\bar\alpha_1 ≈ 0.9999 abs ≤ 1e-6
αˉ500\bar\alpha_{500} ≈ 0.0786 abs ≤ 0.01
αˉ1000\bar\alpha_{1000} ≈ 4⋅10−54\cdot10^{-5} within [1e-6, 1e-3]
emp. E[x5002]\mathbb E[x_{500}^2] (n=20000) ≈ 1 abs ≤ 0.05
emp. Var(x500)\mathrm{Var}(x_{500}) ≈ 1−αˉ5001-\bar\alpha_{500} abs ≤ 0.05
Самопроверка в ноутбуке

Не влияет на сертификат.

import numpy as np
 
T = 1000
beta = np.linspace(1e-4, 0.02, T)
alpha = 1.0 - beta
abar = np.cumprod(alpha)
print("abar[0]", abar[0], "abar[499]", abar[499], "abar[-1]", abar[-1])
assert abs(abar[0] - 0.9999) < 1e-6
assert abs(abar[499] - 0.078587) < 0.01
assert 1e-6 <= abar[-1] <= 1e-3
 
rng = np.random.default_rng(0)
t = 499
x0 = 1.0
eps = rng.standard_normal(20000)
xt = np.sqrt(abar[t]) * x0 + np.sqrt(1 - abar[t]) * eps
second = float((xt ** 2).mean())
var = float(xt.var())
print("emp E[x^2]", second, "emp var", var, "theory var", 1 - abar[t])
assert abs(second - 1.0) < 0.05
assert abs(var - (1 - abar[t])) < 0.05
print("OK: DDPM forward schedule")
 
Самопроверка в ноутбуке
  • αˉ\bar\alpha монотонно падает к ~0
  • E[xt2]≈1\mathbb E[x_t^2]\approx 1, Var≈1−αˉt\mathrm{Var}\approx 1-\bar\alpha_t