Sub topic 10
Thresholds and Abstaining on Low-Confidence Predictions
Assume you know basic Python, supervised text classification, and how to call a Hugging Face transformer pipeline. This chapter focuses on deciding when a zero-shot classifier should refuse to assign a label.
Why the Top Label Is Not Always Good Enough
The pipeline always returns a highest-scoring label. That does not mean the prediction is reliable. If the scores are close, the text may be ambiguous or may not fit any candidate label.
For single-label classification, the scores are normalized across the candidate labels. For example:
| Label | Score |
|---|---|
| billing | 0.46 |
| technical support | 0.42 |
| sales | 0.12 |
The classifier must choose billing, but its confidence is weak. A useful system can abstain instead. Abstaining means returning a special result such as unknown or sending the message to a human reviewer.
Applying a Threshold
A simple policy compares the highest score with a threshold. If the score is below the threshold, the system abstains.
from transformers import pipeline
classifier = pipeline(
"zero-shot-classification",
model="facebook/bart-large-mnli",
)
labels = ["billing", "technical support", "sales"]
threshold = 0.65
texts = [
"I was charged twice for my subscription.",
"Tell me about your company.",
]
for text in texts:
result = classifier(text, candidate_labels=labels)
label = result["labels"][0]
score = result["scores"][0]
if score >= threshold:
decision = label
else:
decision = "unknown"
print(f"{decision}: {score:.3f} <- {text}")The threshold is a policy choice. A lower threshold produces more automatic decisions, called higher coverage, but may allow more errors. A higher threshold produces fewer automatic decisions but sends more cases for review.
Selecting a Threshold from Validation Data
Do not choose a threshold only because it feels reasonable. Create a validation set: examples with trusted labels that were not used to design the policy. Run the classifier on those examples and inspect accuracy at several thresholds.
Useful measurements include:
- Coverage: the fraction of examples that receive an automatic label.
- Selective accuracy: accuracy among only the examples that were not rejected.
- Abstention rate: the fraction sent to review.
A threshold is useful only in relation to the cost of these outcomes. For customer support, a false billing label might be more costly than asking an agent to review the message.
Use Margins for Ambiguous Cases
The top score can be high even when two labels are nearly tied. The score margin is the difference between the top two scores. A small margin signals ambiguity.
def classify_or_abstain(result, score_threshold=0.65, margin_threshold=0.15):
labels = result["labels"]
scores = result["scores"]
top_score = scores[0]
margin = scores[0] - scores[1] if len(scores) > 1 else scores[0]
if top_score < score_threshold or margin < margin_threshold:
return "unknown", top_score, margin
return labels[0], top_score, marginUse score and margin together when confusing labels often compete. A message can pass the score threshold but still require review because the margin is small.
Multi-Label Thresholds
With multi_label=True, each candidate label is scored independently. The scores do not need to sum to one. Apply a threshold to every label, and return all labels that pass it. If none pass, abstain.
The threshold may need to differ by label. “Billing” might require a higher score than a broad label such as “account issue.” Tune these values on validation data.
Exercise
A prediction has a top score of 0.78, but the second score is 0.75. With a score threshold of 0.70 and a margin threshold of 0.10, should the system assign the top label?
Show answer
No. The top score passes, but the margin is 0.03, which is below 0.10, so the system should abstain.