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)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:
print(zero_shot_confidence([0.3, 0.2, 0.1], 0.01))
(0, 1.0)
- Scale the similarities: Divide each cosine similarity by the temperature τ=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].
- Stabilize for exponentiation: Subtract the maximum logit (30) from each value to prevent numerical overflow, yielding shifted values [0,−10,−20].
- Compute unnormalized probabilities: Apply the exponential function to the shifted values, giving e0=1, e−10≈4.54×10−5, and e−20≈2.06×10−9.
- Normalize to get probabilities: Divide each exponential value by their sum (≈1.0000454), resulting in probabilities [0.9999546,4.54×10−5,2.06×10−9].
- Determine prediction and confidence: The maximum probability occurs at index 0 with a value of ≈0.9999546; rounding this to 4 decimal places yields 1.0.
- The final output is
(0, 1.0)
Constraints:
1 <= len(sims) <= 10000,tau > 0.- Divide by
taubefore the softmax; use a stable softmax. - Argmax ties go to the smallest index; probability rounded to 4 decimals.
1. Background Knowledge
In zero-shot classification with Vision-Language Models like CLIP, the model computes a cosine similarity between a visual embedding and a set of text embeddings (one per candidate class). These raw similarities lie in the range [−1,1]. If you apply a standard softmax directly to these values, the resulting probabilities are nearly uniform because the differences between classes are tiny relative to the scale of the input. For example, a difference of 0.1 in cosine similarity is negligible when the values themselves are around 0.5.
To fix this, CLIP introduces a logit scale parameter, often denoted as τ1, where τ is the temperature. The learned logit scale at convergence is typically around 100, meaning τ≈0.01. By dividing the similarities by this small τ, the differences between classes are amplified by a factor of 100. This sharpens the softmax distribution, allowing the model to express high confidence in the most similar class. The calibrated probability for class c is computed as:
pc=∑jesimj/τesimc/τThis process is known as temperature scaling. A small τ makes the distribution "peaky," while a large τ makes it "flat." In practice, τ is learned during training to optimize a contrastive loss, but for inference, it is fixed.
2. Algorithm Approach
The problem requires implementing a numerically stable softmax followed by an argmax operation. The core algorithmic pattern is:
- Scale: Divide each similarity score by the temperature τ.
- Stabilize: Subtract the maximum scaled value from all scaled values to prevent numerical overflow in the exponential function.
- Exponentiate: Compute exi for each stabilized value.
- Normalize: Divide each exponentiated value by the sum of all exponentiated values to get probabilities.
- Predict: Find the index of the maximum probability (which corresponds to the maximum similarity) and return that index along with its probability.
Note that the argmax of the softmax is identical to the argmax of the original similarities, since softmax is a monotonic transformation. However, you must compute the full probability distribution to report the calibrated confidence.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
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.