PIXELBANKv9.1.0
Menu

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^=arg⁡max⁡c  sim(image,promptc)\hat{y} = \arg\max_c \; \text{sim}(image, \text{prompt}_c)

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:

Input:
print(zero_shot_predict([0.2, 0.9, 0.5], ["cat", "dog", "bird"]))
Output:
dog
Reasoning:
  • Initialize the predicted index to the first class, setting the current maximum score to 0.20.2 (associated with "cat").
  • Compare the second score, 0.90.9 (associated with "dog"), against the current maximum of 0.20.2. Since 0.9>0.20.9 > 0.2, update the predicted index to the second class.
  • Compare the third score, 0.50.5 (associated with "bird"), against the new maximum of 0.90.9. Since 0.5<0.90.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.
🔒

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.
Zero-Shot Prediction from Class Logits - Easy | PixelBank