1# x0: [B, C, H, W] real image in [-1,1] or [0,1]2t = sample_uniform_t(batch_size)3alpha_t = get_alpha(t) # precomputed schedule4eps = torch.randn_like(x0) # Gaussian noise5
6# forward noising7xt = (alpha_t.sqrt() * x0 +8 (1 - alpha_t).sqrt() * eps)9
10pred_eps = model(xt, t) # predict noise11
12loss = ((pred_eps - eps)**2).mean()1# x0: [B, C, H, W]2t = sample_uniform_t(batch_size)3x1 = torch.randn_like(x0) # Gaussian prior4
5# forward interpolation6xt = (1 - t) * x0 + t * x17
8# true velocity. 这是 x1 -> x0 的速度9v_true = x1 - x010
11# 从 x1 走向 x0,在 t 时刻的速度. 相当于 x1 也是一个条件12pred_v = model(xt, t)13
14loss = ((pred_v - v_true)**2).mean()