Sub topic 7
Single-Label and Multi-Label Scoring
This chapter assumes that you know basic Python, supervised text classification, and the idea of a pretrained language model. You will use the Hugging Face zero-shot-classification pipeline.
A zero-shot classifier receives text and candidate labels at prediction time. It does not return a fixed class from the model's training task. Instead, it scores how well the text supports each candidate label.
Single-label scoring
Use single-label scoring when each input should receive one best category. This is the pipeline's default behavior:
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"]
result = classifier(text, candidate_labels=labels)
print(result["labels"])
print(result["scores"])
print("best label:", result["labels"][0])The pipeline compares the input with a hypothesis for each label. Conceptually, it asks questions such as:
- Does the text entail “This example is about billing”?
- Does the text entail “This example is about technical support”?
- Does the text entail “This example is about sales”?
In single-label mode, the candidate labels compete with one another. The scores form a probability distribution across the labels and approximately sum to 1.
| Label | Example score | Interpretation |
|---|---|---|
| billing | 0.91 | The strongest candidate |
| technical support | 0.06 | A weaker alternative |
| sales | 0.03 | The least supported candidate |
The result is sorted from the highest score to the lowest score. The first label is therefore the predicted class.
Multi-label scoring
Use multi-label scoring when one text can belong to several categories. Set multi_label=True:
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"]
result = classifier(
text,
candidate_labels=labels,
hypothesis_template="This customer message is about {}.",
multi_label=True,
)
for label, score in zip(result["labels"], result["scores"]):
print(f"{label}: {score:.3f}")In this mode, each label is evaluated independently. The classifier compares the entailment score with the contradiction score for that label. It does not force the labels to compete, so several labels can receive high scores.
For example, the message could be both about billing and technical support. A simple application might accept every label whose score is at least 0.70:
threshold = 0.70
accepted = [
label
for label, score in zip(result["labels"], result["scores"])
if score >= threshold
]
print(accepted)The threshold is an application decision, not a universal truth. A high threshold returns fewer labels but may miss valid categories. A low threshold improves coverage but creates more false positives.
Choosing the scoring mode
Use single-label mode for an intent field such as billing, sales, or technical support when exactly one value is required. Use multi-label mode for independent attributes such as urgent, billing-related, and account-access.
Exercise
A message describes a failed payment and an app crash. Which scoring mode should you try first, and why?
Show answer
Try multi-label scoring because the message may validly receive both billing and technical support labels. Apply a threshold to decide which labels to keep.