Negative Prompt as the Unconditional Branch
Problem Statement
In practice the "unconditional" branch of CFG is replaced by a negative prompt prediction, steering away from unwanted content. Implement guidance with an explicit negative branch and report the resulting noise plus how far it moved from the plain conditional prediction.
Background
With a negative-prompt prediction eps_neg in place of the unconditional one, the guided noise is
ε^=εneg​+w(εcond​−εneg​)
The larger the gap between eps_cond and eps_neg, the more the guidance pushes. Report the guided prediction and the L2 distance between it and eps_cond (how much guidance changed the base prediction).
Your Task
Implement:
def negative_prompt_guidance(eps_cond, eps_neg, w):
Return a dict with "eps" (list rounded to 4 decimals) and "shift" (L2 distance from eps_cond, rounded to 4 decimals).
Input Format
- eps_cond, eps_neg: lists of equal length D.
- w (float): guidance scale.
Output Format
- A dict with a list and a float.
Sample
print(negative_prompt_guidance([1.0, 0.0], [0.0, 0.0], 2.0))
Output:
{'eps': [2.0, 0.0], 'shift': 1.0}
Example:
print(negative_prompt_guidance([1.0, 0.0], [0.0, 0.0], 2.0))
{'eps': [2.0, 0.0], 'shift': 1.0}-
Compute the guided noise vector ε^ using the formula ε^=εneg​+w(εcond​−εneg​):
- For the first element: 0.0+2.0×(1.0−0.0)=2.0
- For the second element: 0.0+2.0×(0.0−0.0)=0.0
- Resulting vector: [2.0,0.0]
-
Calculate the difference between the guided noise and the original conditional prediction to determine the shift:
- ε^−εcond​=[2.0−1.0,0.0−0.0]=[1.0,0.0]
-
Compute the L2 distance (shift) by taking the square root of the sum of squared differences:
- shift=1.02+0.02​=1.0​=1.0
-
Round the results to 4 decimal places as required:
- eps=[2.0,0.0]
- shift=1.0
-
The final output is
{'eps': [2.0, 0.0], 'shift': 1.0}
Constraints:
len(eps_cond) == len(eps_neg).eps = eps_neg + w*(eps_cond - eps_neg).shift = ||eps - eps_cond||_2; round both to 4 decimals; avoid-0.0.
1. Background Knowledge
Classifier-Free Guidance (CFG) is the dominant technique for controlling diffusion model outputs without a separate classifier. During training, the model learns to predict noise conditioned on a prompt cond and unconditionally. At inference, instead of sampling from the conditional distribution alone, we extrapolate: ε^=εuncond​+w(εcond​−εuncond​), where w≥1 is the guidance scale. This linear combination amplifies the direction the conditional prediction points relative to the unconditional baseline, sharpening alignment with the prompt.
In practice, the "unconditional" branch is often replaced by a negative prompt prediction εneg​, which encodes what we want to avoid (e.g., "blurry, low quality"). The formula becomes ε^=εneg​+w(εcond​−εneg​). When w=1, the result equals εcond​ exactly. When w>1, the output is pushed further along the direction (εcond​−εneg​), increasing contrast between desired and undesired features.
The shift metric—defined as the L2 distance ∥ε^−εcond​∥2​—quantifies how much the guidance altered the base conditional prediction. A larger shift indicates stronger steering away from the negative prompt. This is useful for diagnosing whether guidance is too aggressive (overshooting) or too weak (insufficient control).
2. Algorithm Approach
This is a vector arithmetic problem with a single post-processing step. The core operation is a scaled linear combination of two vectors, followed by a norm computation. No iterative or recursive logic is needed. The approach is:
- Compute the difference vector d=εcond​−εneg​ element-wise.
- Scale it by w and add to εneg​ to obtain ε^.
- Compute the L2 norm of (ε^−εcond​), which simplifies algebraically to ∣w−1∣⋅∥d∥2​.
- Round both outputs to 4 decimal places and return as a dictionary.
The algebraic simplification in step 3 is optional but useful for verification: since ε^−εcond​=(w−1)(εcond​−εneg​), the shift is simply ∣w−1∣ times the L2 norm of the difference vector.
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.