Convert Loss Weights Between Parameterizations
Problem Statement
The simple eps-prediction loss corresponds to a specific SNR-dependent weight in the x0-prediction view. Given per-timestep SNRs, compute both the eps weight and the x0 weight.
Background
Training targets are related by x0-loss = SNR * eps-loss (per timestep). The DDPM "simple" objective uses a constant weight of 1 on the eps-loss. Reported in the x0 parameterization, that same objective carries weight SNR_t:
wtε​=1,wtx0​​=SNRt​
Your Task
Implement:
def loss_weights(snr):
- snr: list of per-timestep SNR values.
Return a dict with "eps" (list of 1.0s) and "x0" (list equal to the SNRs), values rounded to 4 decimals.
Input Format
- snr: list of non-negative floats.
Output Format
- A dict with two lists.
Sample
print(loss_weights([4.0, 1.0, 0.25]))
Output:
{'eps': [1.0, 1.0, 1.0], 'x0': [4.0, 1.0, 0.25]}
Example:
print(loss_weights([4.0, 1.0, 0.25]))
{'eps': [1.0, 1.0, 1.0], 'x0': [4.0, 1.0, 0.25]}- The input list of Signal-to-Noise Ratios (SNRs) is [4.0,1.0,0.25]. The function requires two weight lists corresponding to the length of this input, which is 3.
- For the
"eps"parameterization, the weight is defined as a constant 1 for all timesteps. Thus, we generate a list of three 1.0 values: [1.0,1.0,1.0]. - For the
"x0"parameterization, the weight at each timestep t is equal to the SNR at that timestep, i.e., wtx0​​=SNRt​. We map each input value to itself: 4.0→4.0, 1.0→1.0, and 0.25→0.25. - Each value in the
"x0"list is rounded to 4 decimal places. Since the input values 4.0, 1.0, and 0.25 are already precise to fewer than 4 decimal places, they remain unchanged: [4.0,1.0,0.25]. - The final output is
{'eps': [1.0, 1.0, 1.0], 'x0': [4.0, 1.0, 0.25]}
Constraints:
1 <= len(snr) <= 100000, allsnr[i] >= 0.epsweight is1.0everywhere;x0weight equals the SNR.- Round to 4 decimals.
1. Background Knowledge
In diffusion models, the training objective can be expressed in different parameterizations. The most common are epsilon-prediction (predicting the noise ε) and x0-prediction (predicting the clean data x0​). These are not independent choices; they are mathematically linked through the Signal-to-Noise Ratio (SNR) at each timestep t.
The key relationship is that the loss in the x0​-parameterization is a weighted version of the loss in the ε-parameterization. Specifically, if you have a loss function Lε​ for epsilon-prediction, the equivalent loss in the x0​ view is scaled by the SNR: Lx0​​=SNRt​⋅Lε​ This means that a constant weight of 1 in the ε-space (the standard DDPM objective) translates to a time-varying weight of SNRt​ in the x0​-space.
Understanding this conversion is crucial for comparing different training objectives or when switching between parameterizations during implementation. The SNR is typically defined as SNRt​=1−αt2​αt2​​ or similar forms depending on the noise schedule, but for this problem, the SNR values are provided directly.
2. Algorithm Approach
This is a straightforward element-wise transformation problem. You are given a list of SNR values and need to produce two corresponding lists of weights:
- Epsilon weights: A constant list where every element is 1.0.
- X0 weights: A list identical to the input SNR values.
The core logic involves:
- Iterating over the input list (or using vectorized operations).
- Constructing the first output list with a fixed value.
- Constructing the second output list by copying or referencing the input values.
- Applying rounding to 4 decimal places as specified.
- Packaging the results into a dictionary with the keys "eps" and "x0".
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.