Sub topic 14
Choosing an approach and when to fine-tune instead
The earlier chapters covered the NLI-based zero-shot pipeline, hypothesis templates, multi-label scoring, evaluation, thresholds, and the embedding and LLM alternatives. This chapter helps you decide which approach to use for a real task, and when to stop doing zero-shot and train a model. It assumes you can run the Hugging Face pipeline, that you know what macro F1 measures, and that you have seen the bi-encoder example from the embedding-similarity chapter.
The four candidates
You now have four ways to put text into categories.
- NLI zero-shot. A cross-encoder reads the text and one hypothesis per label together, and the entailment probability becomes the label score.
- Embedding similarity. A bi-encoder embeds the text and each label name separately, and you compare them with cosine similarity.
- LLM prompting. You describe the labels in a prompt and a generative model answers with a label.
- Fine-tuning. You train a classifier on labeled examples of your own.
The first three need no task-specific training data. Fine-tuning does, and it usually wins on accuracy once you have enough data.
How the approaches differ
| Property | NLI zero-shot | Embedding similarity | LLM prompting | Fine-tuned classifier |
|---|---|---|---|---|
| Labeled data needed | None (some for evaluation) | None (some for evaluation) | None, or a few examples in the prompt | Tens to thousands, depending on method |
| Forward passes per text | One per label | One for the text; label vectors can be cached | One generation | One |
| Cost grows with label count | Linearly | Almost not at all | Prompt length grows | Almost not at all |
| Reads text and label together | Yes | No | Yes | Not applicable, labels are fixed outputs |
| Handles subtle or compositional labels | Moderately | Poorly | Best of the zero-shot options | Well, if the data covers them |
| Output is a calibrated score | Rough score, not calibrated | Similarity, not a probability | Text, scores need extra work | Probabilities, calibratable |
| Changing the label set | Edit a list | Edit a list | Edit the prompt | Retrain |
| Typical failure | Sensitive to label wording | Confuses topically close labels | Format drift, inconsistent answers | Overfits small datasets |
Two details in the table matter most in practice.
- Cross-encoder versus bi-encoder. The NLI model sees the text and the label together, so it can use word-level interactions. That makes it more accurate than embedding similarity on many tasks. The cost is that no work can be precomputed. With 50 labels, each text needs 50 forward passes of a large model.
- Scores are not probabilities of being correct. With
multi_label=False, the pipeline normalizes entailment logits across labels, so scores sum to 1 even when no label fits. A high score means "best of these options", not "correct". Treat all thresholds as something you tune on labeled data.
A decision flow
The flow ends in evaluation on every zero-shot path. Choosing by reputation instead of measurement is the most common mistake.
Compare approaches on your own data
Label 50 to 200 real examples, then run each candidate against the same sample. The script below compares the NLI pipeline and the bi-encoder on a shared sample. Replace the toy data with your own.
from sentence_transformers import SentenceTransformer, util
from sklearn.metrics import f1_score
from transformers import pipeline
labels = ["billing", "technical support", "sales"]
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 invoice shows the wrong company name.",
"I cannot log in after the latest update.",
"Can I get a quote for 200 seats?",
]
y_true = [
"billing", "technical support", "sales",
"billing", "technical support", "sales",
]
nli = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
encoder = SentenceTransformer("all-MiniLM-L6-v2")
label_vecs = encoder.encode(labels, convert_to_tensor=True)
def predict_nli(text):
return nli(text, candidate_labels=labels)["labels"][0]
def predict_embedding(text):
vec = encoder.encode(text, convert_to_tensor=True)
return labels[int(util.cos_sim(vec, label_vecs)[0].argmax())]
for name, predict in [("nli", predict_nli), ("embedding", predict_embedding)]:
y_pred = [predict(t) for t in texts]
score = f1_score(y_true, y_pred, average="macro")
print(f"{name}: macro F1 = {score:.3f}")Add timing around each call as well. A method that is 3 points better on macro F1 but 40 times slower may be the wrong choice for a high-volume service. The reverse can also hold: a slow method is fine for a nightly batch of 5,000 documents.
Also look at per-class results, not only the macro average. Run classification_report from scikit-learn and check whether one label absorbs most of the errors. A single confusable label pair is often fixed by rewording, which is cheaper than switching approaches.
When zero-shot is the right choice
Zero-shot is a good fit in these cases.
- Cold start. You have no labeled data, and you need a first version or a way to estimate how hard the task is.
- Unstable labels. The category set changes often, so retraining every time would be expensive.
- Low volume. You classify a few thousand texts, so the per-label cost does not matter.
- Bootstrapping. You use zero-shot predictions to pre-label data, then have people correct them. Correcting is faster than labeling from scratch, and the corrected data later trains a fine-tuned model.
When to fine-tune instead
Fine-tune when one or more of these signals appear.
- A measured accuracy gap. Macro F1 on your labeled sample stays below what the business needs after you have tried better label wording, templates and a different model.
- Domain shift. Your text uses jargon, abbreviations or a style the NLI model has not seen, such as clinical notes, legal clauses or support tickets full of product codes.
- Labels defined by your policy. Categories like "escalate" or "high risk" mean something specific to your organization. A model cannot infer that from the label name.
- Latency or cost at scale. One forward pass of a small fine-tuned model is far cheaper than K passes of a large NLI model.
- You need calibrated confidence. Thresholds and abstention work better when the scores track how often the model is right, and a model trained on your data can be calibrated against it.
How much data is enough depends on the method. As a rule of thumb, a few dozen examples per label can be enough for few-shot methods such as SetFit, which fine-tunes a bi-encoder with contrastive learning. Standard fine-tuning of an encoder like DistilBERT or DeBERTa generally wants hundreds of examples per label. Check these numbers on your task by plotting macro F1 against training set size. If the curve is still rising at your largest size, more labels will help.
Combining approaches
The approaches work well together.
- Embedding retrieval, then NLI. Use a bi-encoder to shortlist the top 5 labels out of 200, then run the NLI model on only those 5. This cuts cost while keeping the cross-encoder's accuracy on the final decision.
- Zero-shot labels, then a small model. Pre-label a large unlabeled corpus with NLI or an LLM, review a sample for quality, and train a small classifier on the result. You get fast inference, and the label noise stays a risk to check.
- Confidence routing. Send confident predictions through automatically, and send low-scoring ones to an LLM or a human reviewer. The thresholds chapter covers the mechanics.
Common errors in the decision
- Choosing by benchmark leaderboards. Public benchmark results rarely transfer to your label wording and domain. Your own 100 labeled examples are better evidence.
- Evaluating on too few examples. With 20 examples, a single change moves accuracy by 5 points. Use at least 100, and more if some classes are rare.
- Ignoring class imbalance. Accuracy can look high while a rare class is never predicted. This is why the evaluation chapter uses macro F1.
- Comparing without the same labels and templates. Tune templates and label wording for every candidate under the same effort, or the comparison is unfair.
- Skipping the cost model. Count forward passes per text, texts per day and hardware cost before you commit.
Exercise
A team classifies 2 million support messages per day into 40 categories. They have no labeled data yet. The NLI pipeline reaches macro F1 0.71 on a 150-example sample, and the business needs 0.85. Name two problems with using the NLI pipeline in production, and give a plan that gets them to the target.
Show answer
First, cost: the pipeline runs one forward pass per label, so each message needs 40 passes of a large model. At 2 million messages per day that is 80 million passes, which is expensive and slow. Second, accuracy: 0.71 is below the 0.85 target, and label wording changes alone are unlikely to close that gap. Plan: use the zero-shot system (or an embedding shortlist followed by NLI) to pre-label a sample of messages, have people correct the labels, and keep a separate human-labeled test set. Train a fine-tuned classifier on the corrected data (or SetFit if labels are scarce), compare its macro F1 against the 0.71 baseline on the held-out set, and check the learning curve to decide whether to label more. The fine-tuned model needs one forward pass per message, which fixes the cost problem too.
Exercise
Your product adds new categories every week, you classify about 3,000 documents per day, and you have no labeled data. Which approach would you start with, and what would make you change your mind?
Show answer
Start with the NLI zero-shot pipeline (or embedding similarity if latency matters). Editing a label list is cheap and 3,000 documents per day is a low volume, so the per-label cost is acceptable. Evaluate on a small hand-labeled sample first. Change the plan if the categories stop changing and you have collected enough corrected predictions (hundreds per label), or if measured macro F1 stays below the requirement. In those cases, fine-tune on the collected data. If the labels turn out to be subtle and need reasoning, test LLM prompting as well.