PIXELBANKv9.1.0
Menu

Guidance Rescaling to Fix Over-Exposure

Problem Statement

High CFG scales inflate the predicted-noise magnitude, over-exposing images. Lin et al.'s guidance rescale trick renormalizes the guided prediction back toward the conditional prediction's standard deviation. Implement it.

Background

Let eps_cfg be the guided prediction and eps_cond the plain conditional one. Compute their standard deviations (over all elements, population std), rescale the guided prediction to the conditional's std, then blend by a factor phi:

εrescaled=εcfg⋅std(εcond)std(εcfg),εfinal=ϕ εrescaled+(1−ϕ) εcfg\varepsilon_{\text{rescaled}} = \varepsilon_{\text{cfg}} \cdot \frac{\text{std}(\varepsilon_{\text{cond}})}{\text{std}(\varepsilon_{\text{cfg}})}, \qquad \varepsilon_{\text{final}} = \phi\,\varepsilon_{\text{rescaled}} + (1 - \phi)\,\varepsilon_{\text{cfg}}

phi = 0 disables the fix; phi = 1 fully rescales. If std(eps_cfg) is 0, skip the rescale (return eps_cfg).

Your Task

Implement:

def guidance_rescale(eps_cfg, eps_cond, phi):

Return the final prediction as a list rounded to 4 decimals.

Input Format

  • eps_cfg, eps_cond: lists of equal length D.
  • phi (float) in [0, 1].

Output Format

  • A list of D floats rounded to 4 decimals.

Sample

print(guidance_rescale([2.0, -2.0], [1.0, -1.0], 1.0))

Output:

[1.0, -1.0]

Example:

Input:
print(guidance_rescale([2.0, -2.0], [1.0, -1.0], 1.0))
Output:
[1.0, -1.0]
Reasoning:
  • Compute the population standard deviation of the guided prediction eps_cfg = [2.0, -2.0]. The mean is 00, so std(εcfg)=(2.0)2+(−2.0)22=4=2.0\text{std}(\varepsilon_{\text{cfg}}) = \sqrt{\frac{(2.0)^2 + (-2.0)^2}{2}} = \sqrt{4} = 2.0.
  • Compute the population standard deviation of the conditional prediction eps_cond = [1.0, -1.0]. The mean is 00, so std(εcond)=(1.0)2+(−1.0)22=1=1.0\text{std}(\varepsilon_{\text{cond}}) = \sqrt{\frac{(1.0)^2 + (-1.0)^2}{2}} = \sqrt{1} = 1.0.
  • Since std(εcfg)≠0\text{std}(\varepsilon_{\text{cfg}}) \neq 0, calculate the rescaled prediction by scaling eps_cfg by the ratio of the standard deviations: εrescaled=[2.0,−2.0]â‹…1.02.0=[1.0,−1.0]\varepsilon_{\text{rescaled}} = [2.0, -2.0] \cdot \frac{1.0}{2.0} = [1.0, -1.0].
  • Blend the rescaled prediction with the original guided prediction using Ï•=1.0\phi = 1.0. This fully applies the rescaling: εfinal=1.0â‹…[1.0,−1.0]+(1−1.0)â‹…[2.0,−2.0]=[1.0,−1.0]\varepsilon_{\text{final}} = 1.0 \cdot [1.0, -1.0] + (1 - 1.0) \cdot [2.0, -2.0] = [1.0, -1.0].
  • The final output is [1.0, -1.0]

Constraints:

  • len(eps_cfg) == len(eps_cond), 0 <= phi <= 1.
  • Use population std (ddof=0) over all elements.
  • If std(eps_cfg) == 0, return eps_cfg unchanged.
  • 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.