Search

Search papers and lessons

Zero-Shot Learning

Level 3 · 9 min read

Zero-shot learning recognises classes it was never trained on by matching each input to a written description of every class, so a new class needs a description rather than labelled data.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Probability
  • Neural network basics
  • Language & LLMs
  • Learning basics
  • Sign in to see your progress

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 xx be an input, such as an image. Let Ytest\mathcal{Y}_{\text{test}} be the set of classes we want to predict at test time. None of those classes has labelled training examples. Each class cc comes with a description dcd_c, which can be a name, a sentence or a list of attributes. Two encoders map inputs and descriptions into the same kk-dimensional space:

u=f(x)∈Rk,vc=g(dc)∈Rk.\mathbf{u} = f(x) \in \mathbb{R}^k, \qquad \mathbf{v}_c = g(d_c) \in \mathbb{R}^k.

Here ff is the image encoder and gg 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:

sc=cos⁡(u,vc)=u⊤vc∥u∥ ∥vc∥.s_c = \cos(\mathbf{u}, \mathbf{v}_c) = \frac{\mathbf{u}^\top \mathbf{v}_c}{\lVert \mathbf{u} \rVert \, \lVert \mathbf{v}_c \rVert}.

Cosine similarity lies between −1-1 and 11 and ignores vector length, so it measures direction only. The prediction is c^=arg⁡max⁡csc\hat{c} = \arg\max_c s_c. To get probabilities, divide the scores by a temperature τ>0\tau \gt 0 and apply a Softmax over the candidate classes:

P(c∣x)=exp⁡(sc/τ)∑c′∈Ytestexp⁡(sc′/τ).P(c \mid x) = \frac{\exp(s_c / \tau)}{\sum_{c' \in \mathcal{Y}_{\text{test}}} \exp(s_{c'} / \tau)}.

A small τ\tau makes the distribution sharp, and a large τ\tau makes it flat. Note that dividing by a positive τ\tau never changes which score is largest, so the predicted class does not depend on τ\tau. Only the probabilities do.

0.20.40.60.810.40.60.81temperature τprobability of zebra
  • P(zebra | image)
The same three scores give a confident answer at τ = 0.1 and an almost flat one at τ = 1. The top class stays zebra throughout.

Training the encoders

The usual recipe is contrastive. Take a batch of NN paired inputs and descriptions. Let sijs_{ij} be the cosine similarity between input ii and description jj. The loss pushes each input toward its own description and away from the others:

L=−1N∑i=1Nlog⁡exp⁡(sii/τ)∑j=1Nexp⁡(sij/τ).\mathcal{L} = -\frac{1}{N} \sum_{i=1}^{N} \log \frac{\exp(s_{ii}/\tau)}{\sum_{j=1}^{N} \exp(s_{ij}/\tau)}.

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

Example
Scoring three classes for one image

An image of a zebra has cosine similarities 0.32 with "zebra", 0.18 with "horse" and 0.05 with "car". Use temperature τ=0.1\tau = 0.1.

  1. Divide by τ\tau: zebra 0.32/0.1=3.20.32/0.1 = 3.2, horse 1.81.8, car 0.50.5.
  2. Exponentiate: e3.2≈24.53e^{3.2} \approx 24.53, e1.8≈6.05e^{1.8} \approx 6.05, e0.5≈1.649e^{0.5} \approx 1.649.
  3. Sum: 24.53+6.05+1.649≈32.2324.53 + 6.05 + 1.649 \approx 32.23.
  4. Divide each term by the sum: zebra 24.53/32.23≈0.76124.53 / 32.23 \approx 0.761, horse 6.05/32.23≈0.1886.05 / 32.23 \approx 0.188, car 1.649/32.23≈0.0511.649 / 32.23 \approx 0.051.

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.

Example
A support-ticket router with four labels nobody trained on

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 τ=0.05\tau = 0.05.

  1. Divide by τ\tau: 8.28.2, 7.67.6, 1.21.2, 0.40.4.
  2. Exponentiate: e8.2≈3641e^{8.2} \approx 3641, e7.6≈1998e^{7.6} \approx 1998, e1.2≈3.32e^{1.2} \approx 3.32, e0.4≈1.49e^{0.4} \approx 1.49.
  3. Sum: ≈5644\approx 5644.
  4. Probabilities: billing 0.6450.645, refund 0.3540.354, outage 0.00060.0006, password reset 0.00030.0003.

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.

SituationWhat goes wrongBetter choice
Category name has several meanings ("bass" the fish or the instrument)The description matches the wrong senseAdd context to the description, or use a few labelled examples
Fine-grained classes (two similar bird species)Descriptions score almost the sameAttribute descriptions, or few-shot fine-tuning
Test images far from the pretraining data (medical scans, factory cameras)The encoders map them poorlyFine-tune on a small labelled set
New class added to the candidate listProbabilities for every other class shiftCalibrate per use case, or report scores not probabilities
Benchmark classes overlap the pretraining dataAccuracy is inflatedEvaluate only on classes absent from pretraining
Descriptions written by peopleThey carry the biases of the writers and the web textAudit 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 τ=0.5\tau = 0.5, what is the probability of the first class under softmax?Show answer

Divide by τ\tau: 1.21.2 and 0.40.4. Exponentiate: e1.2≈3.320e^{1.2} \approx 3.320 and e0.4≈1.492e^{0.4} \approx 1.492. Sum: 4.8124.812. Probability of the first class: 3.320/4.812≈0.6903.320 / 4.812 \approx 0.690. As a check, this equals the sigmoid of the difference 1.2−0.4=0.81.2 - 0.4 = 0.8, which is about 0.6900.690.

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