Sub topic 3
Thresholds and Abstaining on Low-Confidence Predictions
This chapter assumes you have run the zero-shot pipeline and read its labels and scores output. Each score is the model's confidence for a label, between 0 and 1.
Setting a threshold
A threshold is the minimum score a prediction needs before you act on it. Abstaining means returning no label and sending the item to a person or a fallback rule.
from transformers import pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
labels = ["billing", "technical support", "sales"]
text = "The app freezes every time I open settings."
result = classifier(text, candidate_labels=labels)
best, score = result["labels"][0], result["scores"][0]
if score >= 0.6:
print(f"Route to {best} ({score:.2f})")
else:
print(f"Abstain, send to human review ({score:.2f})")Limitations: cost, ambiguity, domain shift and bias
Each candidate label needs one NLI forward pass per text, so 50 labels cost 50 passes. Ambiguous text can score high for two labels at once. A model trained on news articles may score support tickets poorly, which is domain shift. The model also inherits bias from its training data, so compare error rates across customer groups.
Alternative: embedding similarity with a bi-encoder
A bi-encoder is a model that turns text and label names into vectors, then compares them with cosine similarity. Label vectors are computed once, so this approach is fast with many labels. It usually separates subtle categories less precisely than NLI.
Alternative: prompting a generative model
Prompting asks a large language model (LLM) to pick one label from a list. It handles unusual instructions well. It costs more per request, and its answers can vary between runs. Constrain the output to your label list and reject anything else.
Choosing an approach and when to fine-tune
Start with zero-shot NLI when labels are few and stable. Use embedding similarity when speed matters across many labels. Use prompting for complex instructions. Fine-tune a model when you have a few hundred labeled examples per class and accuracy stays below target after you tune thresholds, templates, and label wording.
Exercise
Exercise
A classifier with a threshold of 0.8 abstains on 40% of items. What do you adjust first, and how do you check it?
Show answer
Lower the threshold in small steps. On a labeled validation sample, measure precision and abstain rate at each value, then pick the lowest threshold whose precision meets your target.