Sub topic 8
Choosing and Wording Candidate Labels
This chapter assumes you can run the zero-shot-classification pipeline and know that it scores each label by asking an NLI model whether the text entails a hypothesis built from that label. It also assumes you have seen the hypothesis_template argument. Here the focus is the label strings you pass in, since they are the only task specification the model gets.
Why labels act as the training data
A supervised classifier learns what "billing" means from thousands of labeled examples. A zero-shot classifier has none. It sees only the characters you put in candidate_labels, inserted into a sentence such as "This example is billing." The model then judges how plausible that sentence is given your text.
So label wording is a modeling decision. Two label sets that look equivalent to a person can give different accuracy, because the NLI model reads each label as an ordinary English phrase inside a sentence.
What the model actually reads
For each label, the pipeline builds a hypothesis with template.format(label). With the default template, "This example is {}.", the label "billing" produces "This example is billing." The label becomes a noun, an adjective, or a topic depending on the template, so the label and template must read as a grammatical, sensible sentence together.
Check this by formatting the hypotheses yourself:
template = "This customer message is about {}."
labels = ["billing", "technical support", "sales"]
for label in labels:
print(template.format(label))If any printed sentence sounds odd to you, it probably sounds odd to the model too.
Principles for good labels
Say what the category means, not only its name
A one-word label carries little information. A short descriptive phrase adds context the model can match against the text. "Billing" can become "a charge, refund, or payment problem". The phrase gives the model words such as "charge" and "refund" that often appear in or are implied by the text.
Keep labels distinct
In single-label mode the scores are a softmax across labels, so labels compete. If two labels overlap heavily, such as "complaint" and "negative feedback", probability splits between them and the top score becomes unstable. Choose labels whose meanings rarely both apply to one text. If they can both apply, use multi-label scoring instead (covered in the previous chapter).
Keep labels at the same level of detail
Mixing a broad label with a narrow one causes systematic errors. "Technology" and "smartphone batteries" are not peers: the narrow one is a subset of the broad one, and the model may pick either.
Prefer positive, concrete wording
Negations such as "not urgent" are hard for NLI models. Abstract labels such as "miscellaneous" or "other" have no clear content to entail, so their scores are erratic. For texts that fit none of your categories, use a score threshold and abstain, rather than adding an "other" label.
Match the label's grammar to the template
Template and label must agree. The template "This message is about {}." takes noun phrases ("a billing problem"). The template "This message is {}." takes adjectives ("urgent", "polite"). If you switch from topic labels to something like sentiment or tone, change the template as well.
Examples of rewording
| Weak label | Problem | Better label |
|---|---|---|
| `other` | No content to entail | Remove it and use a threshold |
| `negative feedback` and `complaint` | Overlapping meanings | One label: `a complaint about a product or service` |
| `tech` | Ambiguous: gadgets, software, or support? | `a technical problem with software or a device` |
| `not urgent` | Negation | `a routine question with no deadline` |
| `sales` | Short, but could mean revenue figures | `a question about buying a product or its price` |
Comparing label sets on a sample
Judge wording changes with data. The script below scores the same texts under two label sets and a template that fits both. It keeps a dictionary from each canonical class name to the phrase sent to the model, so you can map predictions back for scoring.
from sklearn.metrics import accuracy_score, f1_score
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
texts = [
"I was charged twice for my subscription this month.",
"The app freezes every time I open the settings page.",
"Do you offer a discount for annual plans?",
"My refund still has not arrived after two weeks.",
"I cannot log in since the latest update.",
"How much does the premium tier cost for a team of ten?",
]
y_true = [
"billing", "technical support", "sales",
"billing", "technical support", "sales",
]
label_sets = {
"short": {
"billing": "billing",
"technical support": "technical support",
"sales": "sales",
},
"descriptive": {
"billing": "a charge, refund, or payment problem",
"technical support": "a technical problem with software or a device",
"sales": "a question about buying a product or its price",
},
}
template = "This customer message is about {}."
for set_name, mapping in label_sets.items():
phrase_to_class = {phrase: cls for cls, phrase in mapping.items()}
y_pred = []
for text in texts:
result = classifier(
text,
candidate_labels=list(mapping.values()),
hypothesis_template=template,
)
y_pred.append(phrase_to_class[result["labels"][0]])
acc = accuracy_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred, average="macro")
print(f"{set_name:12s} accuracy={acc:.2f} macro F1={f1:.2f}")Six examples are far too few to trust a difference. Treat this as a template for the workflow, not as evidence. A real comparison needs dozens of examples per class, which the evaluation chapter covers. The pattern to keep is a fixed sample, a fixed template, and one change at a time.
How the number of labels affects results and cost
The pipeline runs the NLI model once per label for each text, so cost grows linearly with the number of labels. Fifty labels means fifty forward passes per text. Large label sets also hurt accuracy, because more labels give more chances that a wrong one scores slightly higher than the right one.
In single-label mode the scores sum to 1 across labels. Adding or removing a label therefore changes the scores of all the others. A threshold tuned on three labels will not carry over to ten. In multi-label mode each label is scored independently, so adding a label does not change existing scores, but the scores are not calibrated probabilities either.
For large taxonomies, consider a two-stage design. First classify into a handful of broad groups, then classify within the chosen group. This keeps each call small, and each label set stays at a consistent level of detail.
Label order and ties
Label order should not matter in principle, because each label is scored by its own premise-hypothesis pair. In single-label mode a near-tie between two labels is a signal about the label design rather than about the text. Look at the cases where the top two scores are close. If they are close for many texts, the labels overlap, and you should merge, reword, or switch to multi-label.
Common mistakes
- Writing labels without reading the formatted hypothesis. Print
template.format(label)for each label before running anything. - Changing labels and template together. You cannot tell which change helped. Change one thing per run.
- Tuning on the same few examples you report. After many rewordings the sample no longer measures real performance. Hold out a separate set for the final number.
- Using internal jargon. Labels like "Tier-2 escalation" mean nothing to a model that never saw your company's vocabulary. Spell out the meaning in plain words.
- Using an "other" label as a catch-all. Use a score threshold and abstain instead.
Summary of a workable procedure
- List the categories and write one plain-language phrase for each.
- Choose a template that makes each formatted hypothesis a natural sentence.
- Check that the labels are distinct and at the same level of detail.
- Score a labeled sample and read the confusions, not only the accuracy number.
- Reword the labels that confuse each other, re-run, and stop when the gains are within noise.
Exercise
Your single-label classifier uses the labels "complaint", "negative feedback", and "praise" with the default template. Many complaints are split almost evenly between "complaint" and "negative feedback". Explain why this happens and give two ways to fix it.
Show answer
The two labels mean nearly the same thing, so the NLI model finds both hypotheses about equally plausible. In single-label mode the scores come from a softmax across labels, so the probability splits between them and the top label flips from text to text. Fix 1: merge them into one label, such as "a complaint about a product or service", and keep "praise" as the other label. Fix 2: if you need both, switch to multi-label scoring so each label is scored independently, and accept that both can be high for the same text. Either way, confirm the change on a labeled sample.
Exercise
You switch from topic labels like "billing" to tone labels like "urgent" and "polite", but keep the template "This message is about {}." Why might the scores get worse, and what would you change?
Show answer
The formatted hypotheses read "This message is about urgent." and "This message is about polite.", which are ungrammatical, because the template expects a noun phrase and the labels are adjectives. The model may handle such sentences poorly. Change the template to something like "This message is {}." so the hypotheses read "This message is urgent." and "This message is polite.". Then print the formatted hypotheses and re-run on a labeled sample to check the effect.