Sub topic 2
Hypothesis templates and how they change scores
This section assumes you know basic Python, can install packages with pip, and have run the zero-shot-classification pipeline once. You should also know that an NLI (natural language inference) model scores whether a premise entails a hypothesis.
The pipeline does not classify text directly. For each candidate label, it builds a hypothesis from a template such as "This example is {}.", then asks the NLI model whether the text entails that hypothesis. Changing the template changes the sentence the model reads, so it changes the scores.
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
text = "I was charged twice for my subscription this month."
labels = ["billing", "technical support", "sales"]
for template in ["This example is {}.", "This customer message is about {}."]:
result = classifier(text, candidate_labels=labels, hypothesis_template=template)
print(template)
for label, score in zip(result["labels"], result["scores"]):
print(f" {label}: {score:.3f}")Single-label and multi-label scoring
By default, the pipeline normalizes scores across labels with a softmax, so they sum to 1. Set multi_label=True to score each label on its own entailment probability. Use the default when each text has exactly one correct label. Use multi_label=True when a text can match several labels.
| Setting | Scores sum to 1 | Use when |
|---|---|---|
| Default (single-label) | Yes | Each text has exactly one correct label |
| `multi_label=True` | No | A text can match several labels |
Choosing and wording candidate labels
Short, concrete label names usually work best. Test two or three wordings on the same texts, because "tech" and "technical support" can produce different rankings. Keep the wording consistent with the template so the hypothesis reads naturally.
Measuring accuracy and macro F1 on a labeled sample
Accuracy is the share of texts whose top label is correct. Macro F1 averages the F1 score of each label with equal weight, so a rare label counts as much as a common one.
from sklearn.metrics import accuracy_score, f1_score
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
labels = ["billing", "technical support", "sales"]
template = "This customer message is about {}."
texts = [
"I was charged twice for my subscription this month.",
"The app freezes every time I open settings.",
"Do you offer a discount for annual plans?",
"My invoice shows the wrong amount.",
]
y_true = ["billing", "technical support", "sales", "billing"]
y_pred = [
classifier(t, candidate_labels=labels, hypothesis_template=template)["labels"][0]
for t in texts
]
print("accuracy:", accuracy_score(y_true, y_pred))
print("macro F1:", f1_score(y_true, y_pred, average="macro", labels=labels))Exercise
With the default settings and three labels, what do the scores add up to, and why does this change with multi_label=True?
Show answer
With the default settings, the softmax makes the three scores sum to 1. With multi_label=True, each label is scored on its own entailment probability, so the scores are not normalized and need not sum to 1.