Forward-Diffuse a Batch to Timestep t
Problem Statement
Apply the closed-form forward process to a batch of clean samples at possibly different timesteps, the exact operation inside a training step. Each sample gets noised with its own t and its own noise vector.
Background
The forward marginal is q(x_t | x_0) = N(sqrt(alpha_bar_t) x_0, (1 - alpha_bar_t) I), so with a standard-normal noise eps:
In a training batch each row i has its own timestep t_i (drawn uniformly) and its own noise, so the per-row scale factors are gathered from the alpha_bar table.
Your Task
Implement:
def forward_diffuse(x0, eps, alpha_bar, t):
- x0, eps: B x D nested lists (clean samples and noise).
- alpha_bar: the full schedule (list).
- t: list of B integer timesteps, one per row.
Return the B x D noised batch as a nested list rounded to 4 decimals.
Input Format
- x0, eps: B x D.
- alpha_bar: list of cumulative products.
- t: list of B indices into alpha_bar.
Output Format
- A B x D nested list rounded to 4 decimals.
Sample
x0 = [[1.0, 1.0]]
eps = [[1.0, -1.0]]
print(forward_diffuse(x0, eps, [0.99, 0.5], [1]))
Output:
[[1.4142, 0.0]]
Example
x0 = [[1.0, 1.0]] eps = [[1.0, -1.0]] print(forward_diffuse(x0, eps, [0.99, 0.5], [1]))
[[1.4142, 0.0]]
- Look up the noise schedule value for the given timestep: with , we index into
alpha_barat position 1 to get . - Compute the signal and noise scale factors for this timestep: the signal weight is and the noise weight is .
- Apply the forward diffusion formula element-wise to the single sample:
- For the first dimension: .
- For the second dimension: .
- The final output is
[[1.4142, 0.0]]
Constraints
x0andepsshare shapeB x D;len(t) == B.- Gather
alpha_bar[t_i]per row; scales aresqrt(alpha_bar)andsqrt(1 - alpha_bar). - Round to 4 decimals; avoid
-0.0.