PIXELBANKv9.1.0
Menu

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

ε^=εuncond+w (εcond−εuncond)\hat{\varepsilon} = \varepsilon_{\text{uncond}} + w\,(\varepsilon_{\text{cond}} - \varepsilon_{\text{uncond}})

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:

Input:
print(cfg_combine([0.0, 0.0], [1.0, 2.0], 7.5))
Output:
[7.5, 15.0]
Reasoning:
  • Identify the input vectors and guidance scale: the unconditional prediction is εuncond=[0.0,0.0]\varepsilon_{\text{uncond}} = [0.0, 0.0], the conditional prediction is εcond=[1.0,2.0]\varepsilon_{\text{cond}} = [1.0, 2.0], and the scale is w=7.5w = 7.5.
  • Compute the difference between the conditional and unconditional predictions element-wise to determine the direction of the prompt: [1.0−0.0,2.0−0.0]=[1.0,2.0][1.0 - 0.0, 2.0 - 0.0] = [1.0, 2.0].
  • Scale this difference by the guidance weight ww to amplify the conditional signal: 7.5×[1.0,2.0]=[7.5,15.0]7.5 \times [1.0, 2.0] = [7.5, 15.0].
  • Add the scaled difference to the unconditional baseline to obtain the guided noise: [0.0,0.0]+[7.5,15.0]=[7.5,15.0][0.0, 0.0] + [7.5, 15.0] = [7.5, 15.0].
  • Round the resulting values to 4 decimal places, which leaves them unchanged as they are already exact: [7.5,15.0][7.5, 15.0].
  • 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.
🔒

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.