PIXELBANKv9.1.0
Menu

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:

xt=Ξ±Λ‰t x0+1βˆ’Ξ±Λ‰t Ρx_t = \sqrt{\bar{\alpha}_t}\, x_0 + \sqrt{1 - \bar{\alpha}_t}\, \varepsilon

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:

Input:
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]]
Reasoning:
  • Look up the noise schedule value for the given timestep: with t=[1]t = [1], we index into alpha_bar at position 1 to get Ξ±Λ‰=0.5\bar{\alpha} = 0.5.
  • Compute the signal and noise scale factors for this timestep: the signal weight is a=0.5β‰ˆ0.7071a = \sqrt{0.5} \approx 0.7071 and the noise weight is b=1βˆ’0.5=0.5β‰ˆ0.7071b = \sqrt{1 - 0.5} = \sqrt{0.5} \approx 0.7071.
  • Apply the forward diffusion formula xt=aβ‹…x0+bβ‹…Ο΅x_t = a \cdot x_0 + b \cdot \epsilon element-wise to the single sample:
    • For the first dimension: 0.7071β‹…1.0+0.7071β‹…1.0=1.41420.7071 \cdot 1.0 + 0.7071 \cdot 1.0 = 1.4142.
    • For the second dimension: 0.7071β‹…1.0+0.7071β‹…(βˆ’1.0)=0.00.7071 \cdot 1.0 + 0.7071 \cdot (-1.0) = 0.0.
  • The final output is [[1.4142, 0.0]]

Constraints:

  • x0 and eps share shape B x D; len(t) == B.
  • Gather alpha_bar[t_i] per row; scales are sqrt(alpha_bar) and sqrt(1 - alpha_bar).
  • Round to 4 decimals; avoid -0.0.
πŸ”’

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.