Classifier-Free Guidance: Uncond/Cond Mixing
Problem Statement
Classifier-free guidance mixes the conditional and unconditional noise predictions to steer generation toward the prompt. Implement the standard linear combination.
Background
Given the unconditional prediction eps_uncond and the conditional prediction eps_cond, the guided noise at guidance scale w is
At w = 1 this is just the conditional prediction; larger w extrapolates further in the prompt direction (sharper adherence, less diversity).
Your Task
Implement:
def cfg_combine(eps_uncond, eps_cond, w):
Return the guided noise as a list rounded to 4 decimals.
Input Format
- eps_uncond, eps_cond: lists of length D.
- w (float): guidance scale.
Output Format
- A list of D floats rounded to 4 decimals.
Sample
print(cfg_combine([0.0, 0.0], [1.0, 2.0], 7.5))
Output:
[7.5, 15.0]
Example
print(cfg_combine([0.0, 0.0], [1.0, 2.0], 7.5))
[7.5, 15.0]
- Identify the input vectors and guidance scale: the unconditional prediction is , the conditional prediction is , and the scale is .
- Compute the difference between the conditional and unconditional predictions element-wise to determine the direction of the prompt: .
- Scale this difference by the guidance weight to amplify the conditional signal: .
- Add the scaled difference to the unconditional baseline to obtain the guided noise: .
- Round the resulting values to 4 decimal places, which leaves them unchanged as they are already exact: .
- The final output is [7.5, 15.0]
Constraints
len(eps_uncond) == len(eps_cond).eps = eps_uncond + w*(eps_cond - eps_uncond).- Round to 4 decimals; avoid
-0.0.