score matching和flow matching
区别
一句话区别
Score Matching:让网络学会“当前噪声图该往哪个方向走”——预测的是 对数概率密度的梯度 ∇log pₜ(x)(俗称 score)。
Flow Matching:让网络学会“当前噪声图该以多快的速度走到真图”——预测的是 确定性速度场 vₜ(x) = dx/d
数学目标
| 范式 | 训练损失(期望符号省略) | 物理量 | 路径性质 | |
|---|---|---|---|---|
| SM | ½‖sθ(xₜ,t) – ∇log pₜ(xₜ | x₀)‖² | score(梯度场) | 随机 SDE 驱动,必须多步积分 |
| FM | ½‖vθ(xₜ,t) – (x₁−x₀)/(1−t)‖² | 速度(向量场) | 人为设计的直线 ODE,可 1 步推到终点 |
python伪代码
# ===== Score Matching (DDPM) =====
def train_sm(model, x0): # x0: 真图
t = torch.rand(len(x0)) # t~U[0,1]
alpha, sigma = schedule(t) # 噪声调度
eps = torch.randn_like(x0)
xt = alpha * x0 + sigma * eps # 加噪
eps_pred = model(xt, t) # 网络预测噪声
loss = F.mse_loss(eps_pred, eps) # ← 去噪目标 [^48^]
# ===== Flow Matching (OT) =====
def train_fm(model, x1): # x1: 真图
t = torch.rand(len(x1))[:,None,None,None]
x0 = torch.randn_like(x1) # 采样噪声
xt = (1 - t) * x0 + t * x1 # 直线插值
target = x1 - x0 # 解析速度场
v_pred = model(xt, t) # 网络预测速度
loss = F.mse_loss(v_pred, target) # ← 回归目标 [^42^]
采样
# SM 必须循环 1000 步“去噪”
x = torch.randn(shape)
for i in reversed(range(1000)):
x = denoise_step(x, i) # 每步都调用 model
# FM 可 1 步 Euler 直达(也可多步 refine)
x = torch.randn(shape)
with torch.no_grad():
x = x + model(x, t=0) * 1.0 # 单步推到 t=1
为什么不一开始使用流匹配,而使用score匹配呢?
- 历史包袱,因为当时采用这条路径,是可以明确得到结果的。通过添加高斯扰动,利用socre去噪匹配得分可以显示的求解出来。后面就沿用 加噪-去噪 这个方案

浙公网安备 33010602011771号