PIXELBANKv9.1.0
Menu

Temperature-Scaled Zero-Shot Confidence

Problem Statement

Zero-shot logits are cosine similarities in [-1, 1], so a raw softmax over them is nearly flat and useless as a confidence. Apply CLIP's logit scale (1/tau), then report the predicted class and its calibrated probability.

Background

CLIP multiplies cosine similarities by a learned logit_scale (equivalently divides by a temperature tau) before the softmax:

pc=softmax ⁣(simcτ)cp_c = \text{softmax}\!\left(\frac{\text{sim}_c}{\tau}\right)_c

A small tau (large logit scale, ~100 at convergence) sharpens the distribution so the top class gets a meaningful probability. Return the argmax class and its probability. Use a numerically stable softmax (subtract the max).

Your Task

Implement:

def zero_shot_confidence(sims, tau):

Return a tuple (pred_index, probability) where probability is rounded to 4 decimals. Ties in the argmax go to the smallest index.

Input Format

  • sims: list of cosine similarities (one per class).
  • tau (float): temperature, tau > 0.

Output Format

  • A tuple (int, float).

Sample

print(zero_shot_confidence([0.3, 0.2, 0.1], 0.01))

Output:

(0, 1.0)

Example:

Input:
print(zero_shot_confidence([0.3, 0.2, 0.1], 0.01))
Output:
(0, 1.0)
Reasoning:
  • Scale the similarities: Divide each cosine similarity by the temperature τ=0.01\tau = 0.01 to sharpen the distribution, resulting in logits z=[0.3/0.01,0.2/0.01,0.1/0.01]=[30,20,10]z = [0.3/0.01, 0.2/0.01, 0.1/0.01] = [30, 20, 10].
  • Stabilize for exponentiation: Subtract the maximum logit (3030) from each value to prevent numerical overflow, yielding shifted values [0,−10,−20][0, -10, -20].
  • Compute unnormalized probabilities: Apply the exponential function to the shifted values, giving e0=1e^0 = 1, e−10≈4.54×10−5e^{-10} \approx 4.54 \times 10^{-5}, and e−20≈2.06×10−9e^{-20} \approx 2.06 \times 10^{-9}.
  • Normalize to get probabilities: Divide each exponential value by their sum (≈1.0000454\approx 1.0000454), resulting in probabilities [0.9999546,4.54×10−5,2.06×10−9][0.9999546, 4.54 \times 10^{-5}, 2.06 \times 10^{-9}].
  • Determine prediction and confidence: The maximum probability occurs at index 00 with a value of ≈0.9999546\approx 0.9999546; rounding this to 4 decimal places yields 1.01.0.
  • The final output is (0, 1.0)

Constraints:

  • 1 <= len(sims) <= 10000, tau > 0.
  • Divide by tau before the softmax; use a stable softmax.
  • Argmax ties go to the smallest index; probability rounded to 4 decimals.
🔒

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.
Temperature-Scaled Zero-Shot Confidence - Medium | PixelBank