K1Medium★ Core Essential · Industrial BedrockPart K · Generative Models & Diffusion
DDPM Forward Diffusion Process
Closed-form DDPM forward noising using cumulative alpha products to sample any timestep t in O(1).
⏱️ Time:
O(B * C * H * W) 单步闭式计算💾 Space:
O(T) 预计算调度缓存🎯 Interactive Self-Practice Loop (Attempt Before Peeking)
Practice in 4 steps: recall mechanism, complete core lines, check edge cases, and log mastery locally.
Step 1 · Mechanism Recall & Stability Self-Check
Step 2 · Complete Key Implementation Lines
import numpy as np
class DDPMForwardDiffusion:
def __init__(self, timesteps: int = 1000, beta_start: float = 1e-4, beta_end: float = 0.02):
self.timesteps = timesteps
# 1. 线性噪声调度 (Linear Beta Schedule)
self.betas =
self.alphas = 1.0 - self.betas
# 2. 累乘 alpha_bar
self.alphas_cumprod = np.cumprod(self.alphas, axis=0)
self.sqrt_alphas_cumprod = np.sqrt(self.alphas_cumprod)
self.sqrt_one_minus_alphas_cumprod = np.sqrt(1.0 - self.alphas_cumprod)
def q_sample(self, x_0: np.ndarray, t: np.ndarray, noise: np.ndarray = None) -> np.ndarray:
"""
闭式加噪:一步直接获得 x_t
参数:
x_0: (B, C, H, W) 原始清晰输入图像
t: (B,) 随机时间步索引 (0 <= t < timesteps)
noise: (B, C, H, W) 标准高斯噪声,若 None 则自动采样
"""
if noise is None:
noise = np.random.randn(*x_0.shape)
# 提取当前时间步的系数并重塑为 (B, 1, 1, 1) 供广播
sqrt_alpha_bar = self.sqrt_alphas_cumprod[t][:, np.newaxis, np.newaxis, np.newaxis]
sqrt_one_minus_alpha_bar = self.sqrt_one_minus_alphas_cumprod[t][:, np.newaxis, np.newaxis, np.newaxis]
# 核心闭式公式: sqrt(alpha_bar) * x_0 + sqrt(1 - alpha_bar) * noise
return
Step 3 · Verify Boundary Cases & Assertions
Step 4 · Follow-Up Drill & Review Outcome
Q1: What does a standard DDPM network predict in its training objective: x_0 or the added noise epsilon?
💡
Core Mental Anchor / Mnemonic
Multiply alphas into alpha_bar_t and sample x_t directly from x_0 in one closed-form step.
📐 Mathematical Derivation & Core Formula
### Markov Noise Schedule Closed-Form Marginals
Starting from single-step corruption () and recursively composing independent Gaussians yields the closed-form marginal with :
Training therefore samples any timestep directly in without simulating steps .
Starting from single-step corruption () and recursively composing independent Gaussians yields the closed-form marginal with :
Training therefore samples any timestep directly in without simulating steps .
🔄 Tensor Dimensions & Shape Flow
x_0: (B, C, H, W), t: (B,) -> gather sqrt_alpha_bar -> mix x_0 + noise -> x_t: (B, C, H, W)
🛡️ Numerical Stability & Pitfalls Checklist
- Precompute cumulative schedule products
cumprodin float64 to prevent cumulative drift across 1000+ steps. - Ensure discrete timesteps t stay within
[0, timesteps - 1]. - Verify
alpha_barapproaches 0 at max t sox_Tmatches isotropic Gaussian noise.
💻 Reference Code Implementation
import numpy as np
class DDPMForwardDiffusion:
def __init__(self, timesteps: int = 1000, beta_start: float = 1e-4, beta_end: float = 0.02):
self.timesteps = timesteps
# 1. 线性噪声调度 (Linear Beta Schedule)
self.betas = np.linspace(beta_start, beta_end, timesteps, dtype=np.float64)
self.alphas = 1.0 - self.betas
# 2. 累乘 alpha_bar
self.alphas_cumprod = np.cumprod(self.alphas, axis=0)
self.sqrt_alphas_cumprod = np.sqrt(self.alphas_cumprod)
self.sqrt_one_minus_alphas_cumprod = np.sqrt(1.0 - self.alphas_cumprod)
def q_sample(self, x_0: np.ndarray, t: np.ndarray, noise: np.ndarray = None) -> np.ndarray:
"""
闭式加噪:一步直接获得 x_t
参数:
x_0: (B, C, H, W) 原始清晰输入图像
t: (B,) 随机时间步索引 (0 <= t < timesteps)
noise: (B, C, H, W) 标准高斯噪声,若 None 则自动采样
"""
if noise is None:
noise = np.random.randn(*x_0.shape)
# 提取当前时间步的系数并重塑为 (B, 1, 1, 1) 供广播
sqrt_alpha_bar = self.sqrt_alphas_cumprod[t][:, np.newaxis, np.newaxis, np.newaxis]
sqrt_one_minus_alpha_bar = self.sqrt_one_minus_alphas_cumprod[t][:, np.newaxis, np.newaxis, np.newaxis]
# 核心闭式公式: sqrt(alpha_bar) * x_0 + sqrt(1 - alpha_bar) * noise
return sqrt_alpha_bar * x_0 + sqrt_one_minus_alpha_bar * noise
🧪 Runnable Assertions & Validation
Copy and run directly in Python / Jupyter:
import numpy as np
diff = DDPMForwardDiffusion(timesteps=100)
x_0 = np.ones((2, 3, 16, 16))
t = np.array([0, 99])
x_t = diff.q_sample(x_0, t)
assert x_t.shape == (2, 3, 16, 16)
# t=0 时 alpha_bar 接近 1,应非常接近 x_0
assert np.isclose(x_t[0].mean(), 1.0, atol=0.1)
print("✓ DDPM 前向加噪闭式计算自测通过")🎯 Core Follow-up Inquiries & Reference Answers
Q1:What does a standard DDPM network predict in its training objective: x_0 or the added noise epsilon?
Typically the injected standard Gaussian noise , minimizing , which Ho et al. showed balances gradient magnitudes cleanly across noise levels.
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
中文速览与面试要点
DDPM 前向加噪扩散过程(重参数化与闭式解)
扩散模型核心基石,高斯马尔可夫链的累乘属性,使得任意时间步 t 的加噪图像可由原图一步闭式直达。
核心要点
- ### 马尔可夫加噪链与方差累乘推导 Jonathan Ho 等人在 2020 年发表 DDPM。 单步加噪为:x_t = _t x_t-1 + 1 - _t _t-1,其中 _t = 1 - _t。 递归展开至初始真实图像 x_0: x_t = _t ( _t-1 x_t-2 + 1 - _t-1 _t-2) + 1 - _t _t-1 由于两个独立高斯变量相加依然是高斯变量: N(0, _1^2 I) + N(0, _2^2 I) = N(0, ( _1^2 + _2^2) I)。 定义累乘因子 _t = _s=1^t _s: x_t = _t x_0 + 1 - _t , N(0, I) 性质:训练时完全无需从 t=1 一步一步模拟前向,直接随机抽取一个时间步 t,一次公式计算即可生成对应的加噪带噪声训练样本!
- x_0: (B, C, H, W), t: (B,) -> 提取 sqrt_alpha_bar -> 与高斯噪声加权 -> x_t: (B, C, H, W)
- 调度系数在初始化时必须以 float64 双精度预计算 cumprod,防止上千步累乘发生极小下溢
- 时间步 t 是从 0 到 timesteps-1 的离散整数
- 在 t=timesteps-1 时,alpha_bar 趋近于 0,图像几乎完全沦为纯各向同性高斯白噪声
- DDPM 训练时的损失函数是什么?模型预测的目标是原始图像 x_0 还是噪声 epsilon?:模型预测的是注入的标准高斯噪声 。损失函数采用简化的均方误差: L_ simple = E_t, x_0, [\| - _ (x_t, t)\|^2]。Jonathan Ho 实验发现直接预测噪声 相比预测 x_0 能够让网络在各个时间步分配更均衡的梯度权重,画质显著更优。
参考实现
import numpy as np
class DDPMForwardDiffusion:
def __init__(self, timesteps: int = 1000, beta_start: float = 1e-4, beta_end: float = 0.02):
self.timesteps = timesteps
# 1. 线性噪声调度 (Linear Beta Schedule)
self.betas = np.linspace(beta_start, beta_end, timesteps, dtype=np.float64)
self.alphas = 1.0 - self.betas
# 2. 累乘 alpha_bar
self.alphas_cumprod = np.cumprod(self.alphas, axis=0)
self.sqrt_alphas_cumprod = np.sqrt(self.alphas_cumprod)
self.sqrt_one_minus_alphas_cumprod = np.sqrt(1.0 - self.alphas_cumprod)
def q_sample(self, x_0: np.ndarray, t: np.ndarray, noise: np.ndarray = None) -> np.ndarray:
"""
闭式加噪:一步直接获得 x_t
参数:
x_0: (B, C, H, W) 原始清晰输入图像
t: (B,) 随机时间步索引 (0 <= t < timesteps)
noise: (B, C, H, W) 标准高斯噪声,若 None 则自动采样
"""
if noise is None:
noise = np.random.randn(*x_0.shape)
# 提取当前时间步的系数并重塑为 (B, 1, 1, 1) 供广播
sqrt_alpha_bar = self.sqrt_alphas_cumprod[t][:, np.newaxis, np.newaxis, np.newaxis]
sqrt_one_minus_alpha_bar = self.sqrt_one_minus_alphas_cumprod[t][:, np.newaxis, np.newaxis, np.newaxis]
# 核心闭式公式: sqrt(alpha_bar) * x_0 + sqrt(1 - alpha_bar) * noise
return sqrt_alpha_bar * x_0 + sqrt_one_minus_alpha_bar * noise