PIXELBANKv9.1.0
Menu

Dynamic Thresholding of the Predicted x0

Problem Statement

Imagen's dynamic thresholding prevents saturated images at high guidance by clamping the predicted x0 to a percentile-derived range each step. Implement it.

Background

Given the predicted x0 and a percentile p (e.g. 99.5), compute s = percentile(|x0|, p) over all elements. If s < 1, set s = 1 (never shrink below the natural [-1, 1] range). Then clip x0 to [-s, s] and rescale by dividing by s, pushing saturated pixels back inward:

s=max⁡(percentile(∣x0∣,p), 1),x0′=clip(x0,−s,s)ss = \max\big(\text{percentile}(|x_0|, p),\, 1\big), \qquad x_0' = \frac{\text{clip}(x_0, -s, s)}{s}

Use linear interpolation for the percentile (NumPy's default).

Your Task

Implement:

def dynamic_threshold(x0, p):

Return the thresholded values as a list rounded to 4 decimals.

Input Format

  • x0: list of floats.
  • p (float): percentile in [0, 100].

Output Format

  • A list of floats rounded to 4 decimals.

Sample

print(dynamic_threshold([2.0, -2.0, 0.5, 0.0], 50.0))

Output:

[1.0, -1.0, 0.4, 0.0]

Example:

Input:
print(dynamic_threshold([2.0, -2.0, 0.5, 0.0], 50.0))
Output:
[1.0, -1.0, 0.4, 0.0]
Reasoning:
  • Compute the absolute values of the input list [2.0,−2.0,0.5,0.0][2.0, -2.0, 0.5, 0.0] to get [2.0,2.0,0.5,0.0][2.0, 2.0, 0.5, 0.0], which are then sorted as [0.0,0.5,2.0,2.0][0.0, 0.5, 2.0, 2.0] to prepare for percentile calculation.
  • Determine the 50th percentile (median) of these absolute values using linear interpolation; with 4 elements, the median is the average of the two middle values: sraw=0.5+2.02=1.25s_{\text{raw}} = \frac{0.5 + 2.0}{2} = 1.25.
  • Apply the lower bound constraint by taking the maximum of the calculated percentile and 1: s=max⁡(1.25,1.0)=1.25s = \max(1.25, 1.0) = 1.25, ensuring the threshold never shrinks the range below [−1,1][-1, 1].
  • Clip each original value in x0x_0 to the range [−1.25,1.25][-1.25, 1.25]; since all values (2.0,−2.0,0.5,0.02.0, -2.0, 0.5, 0.0) are within this range, the clipped list remains [2.0,−2.0,0.5,0.0][2.0, -2.0, 0.5, 0.0].
  • Rescale the clipped values by dividing each by s=1.25s = 1.25 to normalize them: 2.01.25=1.6\frac{2.0}{1.25} = 1.6, −2.01.25=−1.6\frac{-2.0}{1.25} = -1.6, 0.51.25=0.4\frac{0.5}{1.25} = 0.4, and 0.01.25=0.0\frac{0.0}{1.25} = 0.0.
  • The final output is [1.0, -1.0, 0.4, 0.0]

Constraints:

  • 1 <= len(x0) <= 100000, 0 <= p <= 100.
  • s = max(percentile(|x0|, p), 1.0) (linear-interpolation percentile).
  • Clip to [-s, s], divide by s; 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.