PIXELBANKv9.1.0
Menu

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

pt=E[Lt2]∑sE[Ls2]p_t = \frac{\sqrt{\mathbb{E}[L_t^2]}}{\sum_s \sqrt{\mathbb{E}[L_s^2]}}

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:

Input:
print(importance_sampling([4.0, 1.0]))
Output:
{'probs': [0.6667, 0.3333], 'weights': [0.75, 1.5]}
Reasoning:
  • Compute the square root of each input value to determine the unnormalized importance scores, as the distribution is proportional to E[Lt2]\sqrt{\mathbb{E}[L_t^2]}: 4.0=2.0\sqrt{4.0} = 2.0 and 1.0=1.0\sqrt{1.0} = 1.0.
  • Sum these scores to find the normalization constant required for a probability distribution: 2.0+1.0=3.02.0 + 1.0 = 3.0.
  • Calculate the sampling probabilities ptp_t by dividing each score by the total sum: p0=2.0/3.0≈0.6667p_0 = 2.0 / 3.0 \approx 0.6667 and p1=1.0/3.0≈0.3333p_1 = 1.0 / 3.0 \approx 0.3333.
  • Determine the importance weights using the formula 1/(Tâ‹…pt)1/(T \cdot p_t), where T=2T=2 is the number of timesteps: for the first timestep, 1/(2â‹…0.6667)=0.751 / (2 \cdot 0.6667) = 0.75, and for the second, 1/(2â‹…0.3333)=1.51 / (2 \cdot 0.3333) = 1.5.
  • The final output is {'probs': [0.6667, 0.3333], 'weights': [0.75, 1.5]}.

Constraints:

  • 1 <= T <= 100000, all sq_loss[t] >= 0, not all zero.
  • p_t is proportional to sqrt(sq_loss[t]), normalized.
  • weight_t = 1 / (T * p_t); round both to 4 decimals.
🔒

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.