Sub topic 6
Hypothesis Templates and How They Change Scores
This chapter assumes you know basic Python, supervised text classification, and the idea of a pretrained transformer. You should also be able to install and import packages such as transformers.
A zero-shot classifier does not compare a label with text as a simple keyword match. An NLI model receives two pieces of text:
- The premise is the input text to classify.
- The hypothesis is a statement about that text.
- The model predicts whether the premise entails, contradicts, or is neutral toward the hypothesis.
The zero-shot pipeline turns each candidate label into a hypothesis. For example, the label billing might become:
> This text is about billing.
The model's entailment score becomes the label's classification score.
Why the template matters
The hypothesis provides context for the label. Different wording can make the same label easier or harder for the model to interpret.
For example, these templates are similar but not identical:
This text is about {}.This customer message concerns {}.The customer needs help with {}.This is an example of {}.
The first template describes a topic. The third describes a support need. If your labels represent topics, the first template is usually a better match. If your labels represent requested actions, the third may be more appropriate.
A template can also make a label grammatically unnatural. For example, The customer needs help with sales is reasonable, but The customer needs help with account cancellation may be more specific than intended. Test templates on representative examples instead of assuming that fluent wording will always produce better classifications.
Comparing templates with the pipeline
The hypothesis_template argument must contain {}. The pipeline replaces this placeholder with each candidate label.
from transformers import pipeline
classifier = pipeline(
"zero-shot-classification",
model="facebook/bart-large-mnli",
)
text = "The app crashes when I try to update my payment method."
labels = ["billing", "technical support", "sales"]
for template in [
"This customer message is about {}.",
"The customer needs help with {}.",
]:
result = classifier(
text,
candidate_labels=labels,
hypothesis_template=template,
multi_label=True,
)
print(f"\nTemplate: {template}")
for label, score in zip(result["labels"], result["scores"]):
print(f"{label}: {score:.3f}")With multi_label=True, the model scores each label independently. Changing the template can change every score because it changes the NLI hypothesis. A high score from one template does not mean that the score is directly comparable with a score from another template.
Practical template design
Use a template that matches the meaning of your labels.
| Label meaning | Useful template |
|---|---|
| Topic | `This text is about {}.` |
| User intent | `The user wants to {}.` |
| Support request | `The customer needs help with {}.` |
| Sentiment or quality | `This text expresses {}.` |
Keep templates consistent across all labels. Do not write one template for billing and a different template for technical support, because the model would then compare both label meaning and template wording.
Labels should also be clear and parallel. Prefer payment problems, app failures, and pricing questions over a mixture such as billing, the app is broken, and discounts for customers.
Evaluate several templates on a labeled validation sample. Choose the template that performs best for your actual task, not the one that produces the most confident-looking scores.
Exercise
Why might "This text is about {}." work better than "The customer needs help with {}." for labels such as billing, sales, and technical support?
Show answer
The first template describes topics, which matches the meaning of those labels. The second describes support needs and may shift the model toward interpreting each label as a request for assistance.