Importance-Sampled Timesteps for Loss Estimation
Problem Statement
Improved-DDPM reduces variance of the loss estimate by sampling timesteps proportional to their recent per-step loss, then reweighting each sample by the inverse probability. Compute those sampling probabilities and importance weights.
Background
Given the running mean loss L_t at each timestep, the importance-sampling distribution is
Here you are given sq_loss[t] = E[L_t^2] directly. The unbiased loss estimator then weights a sample at t by 1/(T * p_t). Return both the distribution and the per-timestep importance weight.
Your Task
Implement:
def importance_sampling(sq_loss):
Return a dict with "probs" (the p_t) and "weights" (the 1/(T*p_t)), each rounded to 4 decimals. T = len(sq_loss).
Input Format
- sq_loss: list of T non-negative values E[L_t^2], not all zero.
Output Format
- A dict with two lists.
Sample
print(importance_sampling([4.0, 1.0]))
Output:
{'probs': [0.6667, 0.3333], 'weights': [0.75, 1.5]}
Example
print(importance_sampling([4.0, 1.0]))
{'probs': [0.6667, 0.3333], 'weights': [0.75, 1.5]}- Compute the square root of each input value to determine the unnormalized importance scores, as the distribution is proportional to : and .
- Sum these scores to find the normalization constant required for a probability distribution: .
- Calculate the sampling probabilities by dividing each score by the total sum: and .
- Determine the importance weights using the formula , where is the number of timesteps: for the first timestep, , and for the second, .
- The final output is
{'probs': [0.6667, 0.3333], 'weights': [0.75, 1.5]}.
Constraints
1 <= T <= 100000, allsq_loss[t] >= 0, not all zero.p_tis proportional tosqrt(sq_loss[t]), normalized.weight_t = 1 / (T * p_t); round both to 4 decimals.