Zero-shot classification is a linear classifier
For classes , write a prompt for each class, such as “a photo of a dog.”, and embed it, . For an image with embedding , predict the class whose prompt is most similar: This is a linear classifier on the image embedding. The weight matrix has one row per class, and the scores followed by a softmax give class probabilities, exactly as in logistic regression. The difference is where the weights come from. Logistic regression fits to labeled images. Zero-shot classification computes from text, so changing the classes means changing the prompts, and no image of the new classes is needed. The quality of the classifier then depends on two things: how well the image embedding separates the classes, and how well the prompts point at them.Code
Code
Embedding the data
The image encoder runs once over the CIFAR-10 test set and once over the training set. Every classifier below, zero-shot or trained, works on these frozen embeddings. The images are and are enlarged to by the processor, which makes this a harder test than the photos CLIP was trained on.Code
Code
Prompts
Radford et al. found that the bare class name is a weak prompt (Radford et al., 2021). Captions on the web are sentences, not single words, and a single word can be ambiguous: “crane” is a bird and a machine. Wrapping the name in a sentence such asa photo of a \{class\}. moves the prompt closer to the captions the text encoder was trained on. Averaging the embeddings of several such sentences, prompt ensembling, gives a weight vector that depends less on the wording of any one of them. The cell compares the three choices.
Code
Code

Zero-shot against a linear probe
How good is a classifier written in English, compared with one trained on labeled images? A linear probe answers this on the same embedding. It trains a logistic regression on the frozen image embeddings of labeled images per class and is tested on the same 10,000 test images. Both classifiers are linear in , so the comparison measures only where the weights come from. Radford et al. found that, averaged over 27 data sets, zero-shot CLIP matched a linear probe trained with 4 examples per class.Code
Code

Exercises
- Add the eight templates one at a time, in the order given, and plot the accuracy against the number of templates.
- Replace “automobile” with “car” and “airplane” with “plane” in the prompts. Does the accuracy change? What does this tell you about choosing class names?
- Initialize the weights of the logistic regression with the zero-shot weights (scaled by 100) and train it with 4 examples per class. Does starting from the text help?
- Evaluate the zero-shot classifier on CIFAR-100, which has 100 classes. How does the gap to the linear probe change with the number of classes?
Key references
Radford et al. introduced zero-shot transfer with CLIP and compared it with linear probes on 27 data sets (Radford et al., 2021).References
- Radford, A., Kim, J., Hallacy, C., Ramesh, A., Goh, G., et al. (2021). Learning transferable visual models from natural language supervision. arXiv [cs.CV]. Link
Found an error on this page? Report an issue.

