TL;DR
Zero-shot learning lets a model recognise categories it has never seen a labelled example of. A normal classifier has one output per label, fixed at training time. A zero-shot model instead learns to compare an input with a description of each possible class, and picks the description that matches best. Adding a class means writing its description, not collecting data and retraining.
The method is only as good as two things: the descriptions, and the link between inputs and descriptions that the model learned from other data.
Where you'll see it: image classifiers that take a free-text list of categories, such as CLIP, and search or routing systems that sort items into categories nobody labelled in advance.
Intuition
Think of a bird guide you have never used in the field. Someone tells you a bird is "about the size of a crow, black, with a white collar, and it lives by the sea". Later you see a bird that fits, and you name it correctly, though you have never looked at that species. You matched a description of the class to what you saw, not a picture of the class.
That is the idea. A standard classifier has a "cat" output and a "dog" output, trained together. To add "zebra" you need zebra images and a retrain. A zero-shot model has no per-class output at all. It has two encoders. One maps inputs into a shared space of vectors. The other maps each class description into the same space. Classification becomes: find the class vector closest to the input vector.
It exists because labels are the bottleneck. The world has far more categories than anyone can label: every new product, species, fault type or support topic. Zero-shot learning turns "collect a thousand examples and retrain" into "write one sentence".
flowchart LR I["Input image"] --> E1["Image encoder"] C["Class descriptions: zebra, horse, car"] --> E2["Text encoder"] E1 --> S["Similarity score for each class"] E2 --> S S --> P["Pick the best-matching class"]
The math
Setting
Let be an input, such as an image. Let be the set of classes we want to predict at test time. None of those classes has labelled training examples. Each class comes with a description , which can be a name, a sentence or a list of attributes. Two encoders map inputs and descriptions into the same -dimensional space:
Here is the image encoder and is the description encoder. Both are trained on pairs of inputs and descriptions of other classes, so the training labels and the test classes are disjoint. This is a form of transfer learning: what the encoders learn about matching images to text carries over to classes they never saw.
Scoring
Each class gets a score from the cosine similarity between the two vectors:
Cosine similarity lies between and and ignores vector length, so it measures direction only. The prediction is . To get probabilities, divide the scores by a temperature and apply a Softmax over the candidate classes:
A small makes the distribution sharp, and a large makes it flat. Note that dividing by a positive never changes which score is largest, so the predicted class does not depend on . Only the probabilities do.
- P(zebra | image)
Training the encoders
The usual recipe is contrastive. Take a batch of paired inputs and descriptions. Let be the cosine similarity between input and description . The loss pushes each input toward its own description and away from the others:
This is the same objective as in contrastive learning, with a Softmax over the batch. Descriptions in older work were attribute vectors, such as "striped: yes, has hooves: yes". Attribute-based methods learned one classifier per attribute and then scored a new class by how well its attribute vector fit. Modern systems use free text, which is cheaper to write and covers more ground.
Worked examples
An image of a zebra has cosine similarities 0.32 with "zebra", 0.18 with "horse" and 0.05 with "car". Use temperature .
- Divide by : zebra , horse , car .
- Exponentiate: , , .
- Sum: .
- Divide each term by the sum: zebra , horse , car .
The three probabilities add to 1.000. The model says zebra with about 76% confidence. Horse gets 19% because its description is close to zebra's, and car gets 5%. The model is not sure about the animal, but it is clearly not a car.
A helpdesk wants to route messages to four queues: "billing", "refund", "outage" and "password reset". None of these has labelled history in the new system. The message is "I was charged twice and I want my money back". The similarities are billing 0.41, refund 0.38, outage 0.06 and password reset 0.02. Use .
- Divide by : , , , .
- Exponentiate: , , , .
- Sum: .
- Probabilities: billing , refund , outage , password reset .
The router picks billing. But refund is close behind, so a sensible rule is to tag the ticket with both queues and send it to a person when the top two are within a set margin. The softmax gives the margin directly, which a raw similarity score does not.
Tradeoffs and failure modes
Use zero-shot when labels are scarce or the category list changes often. Do not use it when you have a thousand labelled examples per class: a supervised classifier will beat it. Use a few labelled examples (few-shot) when the classes are visually close and the descriptions cannot separate them.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Category name has several meanings ("bass" the fish or the instrument) | The description matches the wrong sense | Add context to the description, or use a few labelled examples |
| Fine-grained classes (two similar bird species) | Descriptions score almost the same | Attribute descriptions, or few-shot fine-tuning |
| Test images far from the pretraining data (medical scans, factory cameras) | The encoders map them poorly | Fine-tune on a small labelled set |
| New class added to the candidate list | Probabilities for every other class shift | Calibrate per use case, or report scores not probabilities |
| Benchmark classes overlap the pretraining data | Accuracy is inflated | Evaluate only on classes absent from pretraining |
| Descriptions written by people | They carry the biases of the writers and the web text | Audit outputs per group before deploying |
The last two rows are the ones most often missed. A zero-shot number is only meaningful when you know which classes the model saw in pretraining.
History
Zero-shot learning grew out of attribute-based recognition. Lampert and colleagues showed in 2009 that a model could recognise object classes it had no images for, by learning attributes such as "striped" and "hooves" from other classes. In the same year Palatucci and colleagues framed the problem as predicting a semantic code for each class.
In 2013 Socher and colleagues replaced hand-written attributes with word vectors. Methods such as Mikolov's word2vec had just made it cheap to learn a vector for every word from raw text. An image could be mapped into that vector space, and an unseen word such as "cat" could then be recognised from its position. Their model was one of the first to classify images into categories it had never been trained on, using only text.
Progress was slow until web-scale data arrived. In 2021 Radford and colleagues trained CLIP on about 400 million image-text pairs collected from the web. Its zero-shot classifier matched the accuracy of a supervised ResNet-50 trained on the ImageNet labels, without using those labels. That result made the approach standard. Text supervision removed the label bottleneck, and the scale of the web supplied the descriptions.
Check yourself
QuestionIn a standard classifier, what fixes the set of possible outputs, and what does a zero-shot model use in its place?Show answer
A standard classifier has one output unit per label, set when the network is built and trained. A zero-shot model has no per-class output. It scores each candidate by comparing an input embedding with an embedding of that class's description, so any class that can be described can be a candidate.
QuestionTwo classes score 0.6 and 0.2 cosine similarity with an input. With , what is the probability of the first class under softmax?Show answer
Divide by : and . Exponentiate: and . Sum: . Probability of the first class: . As a check, this equals the sigmoid of the difference , which is about .
QuestionWhy does changing the temperature never change the predicted class, even though it changes the probabilities?Show answer
Dividing every score by the same positive number preserves their order. The largest score stays the largest, so the argmax is unchanged. The temperature only controls how much the gap between scores is amplified before the softmax, so it affects the probabilities and any threshold you apply to them.
QuestionA team classifies product photos into 500 categories with a CLIP-style model. Accuracy is high on most categories. After they add a new category, 'e-bike', some photos of ordinary bicycles start being labelled 'e-bike'. Why, and what would you change?Show answer
The softmax is taken over the candidate list, so the new description takes some probability mass from every image that is near it. The bicycle photos sit close to the new "e-bike" description in the shared space, so the new class wins some of them. Better descriptions would help, for example adding that an e-bike has a battery and motor. You would also check the confusion between the two classes on labelled photos before rolling out the change, and consider a few labelled examples of e-bikes if the gap is still too small.
Sources
- Radford et al., Learning Transferable Visual Models From Natural Language Supervision (2021): https://arxiv.org/abs/2103.00020
- Mikolov et al., Efficient Estimation of Word Representations in Vector Space (2013): https://arxiv.org/abs/1301.3781
- Socher et al., Zero-Shot Learning Through Cross-Modal Transfer (2013): https://arxiv.org/abs/1301.3666
- Xian et al., Zero-Shot Learning: A Comprehensive Evaluation of the Good, the Bad and the Ugly (2017): https://arxiv.org/abs/1707.00600
- Zero-shot learning (Wikipedia): https://en.wikipedia.org/wiki/Zero-shot_learning