Zero-Shot Prediction from Class Logits
Problem Statement
A zero-shot CLIP classifier scores an image against one text embedding per class. Given the per-class similarity scores for a single image, return the predicted class name.
Background
Zero-shot classification turns each class into a text prompt ("a photo of a dog"), embeds it, and scores the image against every class embedding. The prediction is simply the class with the highest score:
y^=argmaxcsim(image,promptc)
Ties go to the class that appears first in the list.
Your Task
Implement:
def zero_shot_predict(scores, class_names):
Return the predicted class name (a string).
Input Format
- scores: list of floats, one per class.
- class_names: list of strings, same length as scores.
Output Format
- A single string.
Sample
print(zero_shot_predict([0.2, 0.9, 0.5], ["cat", "dog", "bird"]))
Output:
dog
Example:
print(zero_shot_predict([0.2, 0.9, 0.5], ["cat", "dog", "bird"]))
dog
- Initialize the predicted index to the first class, setting the current maximum score to 0.2 (associated with "cat").
- Compare the second score, 0.9 (associated with "dog"), against the current maximum of 0.2. Since 0.9>0.2, update the predicted index to the second class.
- Compare the third score, 0.5 (associated with "bird"), against the new maximum of 0.9. Since 0.5<0.9, the predicted index remains at the second class.
- The final output is dog
Constraints:
1 <= len(scores) == len(class_names) <= 10000.- Ties go to the earliest class index.
- Return the class name string.
1. Background Knowledge
Zero-shot classification is a paradigm where a model makes predictions for classes it has never explicitly seen during training. In vision-language models like CLIP (Contrastive Language-Image Pre-training), this is achieved by encoding both images and text into a shared embedding space. Each class is represented by a text prompt (e.g., "a photo of a dog"), and the model computes a similarity score between the image embedding and each class's text embedding.
The core idea relies on contrastive learning, where the model is trained to maximize the similarity between matched image-text pairs and minimize it for mismatched pairs. At inference time, the prediction is determined by finding the class with the highest similarity score:
y^=argmaxcsim(image,promptc)
This approach is powerful because it eliminates the need for labeled image data for new classes. You only need to provide text descriptions, and the model can generalize to those classes immediately. The similarity is typically computed using cosine similarity or dot product in the normalized embedding space.
2. Algorithm Approach
This problem follows a simple argmax pattern with a tie-breaking rule. The algorithm involves:
- Iterating through the list of scores alongside their corresponding class names
- Tracking the index of the maximum score encountered so far
- Returning the class name at that index
The key constraint is that ties go to the first occurrence in the list. This means you should only update your "best" index when you find a strictly greater score, not when you find an equal one. This is a common pattern in competitive programming and data analysis tasks where deterministic behavior is required.
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.