PIXELBANKv9.1.0
Menu

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)\hat{\varepsilon} = \varepsilon_{\text{neg}} + w\,(\varepsilon_{\text{cond}} - \varepsilon_{\text{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:

Input:
print(negative_prompt_guidance([1.0, 0.0], [0.0, 0.0], 2.0))
Output:
{'eps': [2.0, 0.0], 'shift': 1.0}
Reasoning:
  • Compute the guided noise vector ε^\hat{\varepsilon} using the formula ε^=εneg+w(εcond−εneg)\hat{\varepsilon} = \varepsilon_{\text{neg}} + w(\varepsilon_{\text{cond}} - \varepsilon_{\text{neg}}):

    • For the first element: 0.0+2.0×(1.0−0.0)=2.00.0 + 2.0 \times (1.0 - 0.0) = 2.0
    • For the second element: 0.0+2.0×(0.0−0.0)=0.00.0 + 2.0 \times (0.0 - 0.0) = 0.0
    • Resulting vector: [2.0,0.0][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]\hat{\varepsilon} - \varepsilon_{\text{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\text{shift} = \sqrt{1.0^2 + 0.0^2} = \sqrt{1.0} = 1.0
  • Round the results to 4 decimal places as required:

    • eps=[2.0,0.0]\text{eps} = [2.0, 0.0]
    • shift=1.0\text{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.
🔒

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.