Sub topic 12
Embedding-Similarity Zero-Shot Classification with a Bi-Encoder
Assume you know basic Python, supervised text classification, and the general idea of a pretrained language model. This chapter shows an alternative to NLI-based zero-shot classification. Instead of asking whether a text entails a hypothesis, you compare the meaning of the text with the meaning of each candidate label.
The bi-encoder working model
A bi-encoder is a model that encodes two inputs separately. An encoder turns each input into a vector, which is a list of numbers representing its meaning. Here, one input is the text and the other inputs are candidate labels.
The classifier then measures the similarity between the text vector and every label vector. Cosine similarity is a common measure. It gives a higher score when two vectors point in similar directions.
The model does not need a classification head trained specifically for your labels. It uses the general language knowledge learned during pretraining. This is why you can supply labels that were not present as categories during your project.
Running embedding-similarity classification
The sentence-transformers package provides convenient sentence encoders. The example uses all-MiniLM-L6-v2, a relatively small model that produces useful sentence embeddings.
from sentence_transformers import SentenceTransformer, util
model = SentenceTransformer("all-MiniLM-L6-v2")
text = "I was charged twice for my subscription this month."
labels = ["billing", "technical support", "sales"]
text_vector = model.encode(text, convert_to_tensor=True)
label_vectors = model.encode(labels, convert_to_tensor=True)
similarities = util.cos_sim(text_vector, label_vectors)[0]
best_index = int(similarities.argmax())
print("prediction:", labels[best_index])
print("score:", float(similarities[best_index]))The text and labels are encoded independently. This makes the method efficient when the candidate labels stay fixed. You can encode the labels once, store their vectors, and compare many new texts against them.
Wording labels as descriptions
A short label can be ambiguous. For example, support could mean technical support, customer service, or financial assistance. Descriptive labels often give the encoder more useful context.
labels = [
"billing and duplicate charges",
"technical problems with the application",
"questions about purchasing or sales",
]You should keep label wording consistent. If some labels are single words and others are long descriptions, the descriptions may contain extra clues that affect similarity. Test several phrasings on a labeled validation sample rather than choosing wording from one prediction.
Strengths and limitations
A bi-encoder is usually faster than scoring every text-label pair with a cross-encoder NLI model. It also supports large label collections because label vectors can be precomputed. However, independent encoding loses some detailed interaction between the text and label. A label that requires careful reasoning or negation handling may be ranked incorrectly.
| Approach | Main comparison | Useful strength |
|---|---|---|
| Bi-encoder | Text vector with label vector | Fast reuse of precomputed label embeddings |
| NLI classifier | Text and hypothesis together | More detailed pairwise reasoning |
For a production classifier, evaluate predictions on representative labeled data. Compare accuracy for single-label tasks and macro F1 when classes are uneven. If the labels are stable and you have enough examples, fine-tuning may outperform zero-shot methods.
Exercise
Why can precomputing label embeddings make a bi-encoder classifier faster than an NLI classifier?
Show answer
Each label is encoded once. New texts only need one text embedding and similarity calculations, while an NLI classifier must score a separate text-label pair for every candidate.