Sub topic 4
Turning Classification into an Entailment Question
Prerequisites
Assume you know basic Python, supervised text classification, and the general idea of a pretrained language model. You should also be able to install Python packages and read a model's output.
A normal classifier learns a fixed set of categories from labeled examples. For example, a support classifier might learn the labels billing, technical support, and sales. If you later add account security, you usually need new labeled examples and additional training.
A zero-shot classifier takes another route. It rewrites each possible label as a statement and asks whether the input text supports that statement. The model does not need a training example for each label. It needs a general ability to compare two pieces of language.
Natural language inference
Natural language inference, or NLI, is a task that compares a premise with a hypothesis.
- The premise is the original text or evidence.
- The hypothesis is a statement about that text.
- Entailment means the premise supports the hypothesis.
- Contradiction means the premise conflicts with the hypothesis.
- Neutral means the premise does not provide enough information.
For example:
| Premise | Hypothesis | Relation |
|---|---|---|
| “I was charged twice for my subscription.” | “This message is about billing.” | Entailment |
| “I was charged twice for my subscription.” | “This message is about gardening.” | Contradiction or low support |
| “I was charged twice for my subscription.” | “The customer lives in Canada.” | Neutral |
The exact neutral and contradiction scores depend on the model. The important point is that the model evaluates a relationship between texts rather than choosing from a permanent output layer.
Reformulating classification
Suppose the input is:
> I was charged twice for my subscription this month.
To classify it, create one hypothesis for each candidate label:
- This message is about billing.
- This message is about technical support.
- This message is about sales.
The NLI model scores how strongly the premise entails each hypothesis. The label with the strongest entailment score becomes the prediction.
from transformers import pipeline
nli_classifier = pipeline(
"text-classification",
model="facebook/bart-large-mnli",
)
premise = "I was charged twice for my subscription this month."
hypotheses = [
"This message is about billing.",
"This message is about technical support.",
"This message is about sales.",
]
for hypothesis in hypotheses:
result = nli_classifier(
{"text": premise, "text_pair": hypothesis},
top_k=None,
)
scores = {item["label"].lower(): item["score"] for item in result}
print(hypothesis, scores.get("entailment", 0.0))The Hugging Face zero-shot pipeline automates this loop. It creates hypotheses from labels, sends premise–hypothesis pairs to an NLI model, and returns labels ordered by score. The model was trained to recognize entailment, contradiction, and neutrality. It was not trained with your specific application labels, such as your company's support categories.
Why the wording matters
The hypothesis is part of the input, so its wording affects the score. Compare these templates:
This message is about {}.The customer needs help with {}.The topic of this message is {}.
A label such as billing may fit the first template well, while charged twice is a more concrete hypothesis for a particular task. Use a template that expresses the relationship you want to measure. Keep the template consistent across labels, then test alternatives on labeled data.
Exercise
Why can an NLI model classify a label that was not present as a fixed class during its original training?
Show answer
It compares the input premise with a natural-language hypothesis and transfers its learned entailment ability to the new label wording.