FAQ

NLP

Fine-tuning BERT: labelled data, not instructions

Encoder models need a completely different dataset shape from generative LLMs — and for classification they are still often the better choice.

Almost everything written about fine-tuning in the last few years assumes a generative model. BERT is an encoder, the data shape is different, and for a classification task it is frequently the right answer anyway.

The data is labels, not conversations

This is the whole difference and it trips people coming from LLM work. An encoder model does not generate text, so there is no prompt, no completion, no chat template, no loss masking and no stop token. There is a text input and a label.

For classification: text plus a class index. For token classification such as named entity recognition: tokens plus a label per token. For sentence-pair tasks: two texts plus a relationship label. For extractive question answering: a passage, a question, and character offsets of the answer span.

That makes the data much cheaper to produce and much easier to validate. A label is either right or wrong; there is no judging whether a generated answer was good. It also makes evaluation trivially decidable, which is the property our golden eval set guide spends most of its time trying to engineer for generative tasks.

the shapes an encoder wants
# sequence classification
{"text": "The delivery arrived three days late.", "label": 2}

# token classification (NER) - one label per token, aligned
{"tokens": ["Acme", "Ltd", "filed", "in", "Delaware"],
 "ner_tags": [3, 4, 0, 0, 5]}

# sentence pair
{"text_a": "The policy covers flood damage.",
 "text_b": "Flooding is excluded.",
 "label": 2}                      # MNLI convention: 0 entailment, 1 neutral, 2 contradiction

# extractive QA - offsets, not generated text
{"context": "...", "question": "When was it filed?",
 "answers": {"text": ["March 2024"], "answer_start": [412]}}
Two architecture sketches side by side. The left one, labelled Bi-Encoder, passes sentence A and sentence B through separate BERT towers into separate pooled vectors compared by cosine similarity. The right one, labelled Cross-Encoder, concatenates both sentences into a single BERT pass that emits one score between 0 and 1 through a classifier head.
Two ways an encoder consumes a labelled pair: separate embeddings compared by cosine, or a single scored pass over both. Encoder training data is labelled examples, not conversations, which is the thing most people get wrong when they arrive from LLM work. Image: UKPLab/sentence-transformers · Apache-2.0 repository

Why an encoder is often still the right choice

For a fixed classification or extraction task, an encoder model frequently beats a fine-tuned generative model on the metrics that matter, and beats it decisively on the ones nobody puts in the paper.

Latency and cost. A base-sized encoder is a fraction of the size of even a small generative model and produces a label in a single forward pass with no decoding loop. At volume, the cost difference is orders of magnitude.

Constrained output. It cannot produce an invalid label. A generative model asked to classify can return something off-schema, and defending against that is work you do not have to do here.

Calibrated scores. You get a probability per class, which you can threshold, rank and route on. Getting trustworthy confidence out of a generative model is substantially harder.

The case for a generative model is when you need the output to be text, when the task benefits from reasoning before answering, or when you want one model doing many tasks rather than a classifier per task. Those are real reasons. “It is what everyone uses now” is not.

How much data, and the label-quality question

Encoders need labelled examples per class, and the useful rule of thumb is low hundreds per class for a workable classifier on a reasonably distinct set of categories, scaling with how subtle the distinctions are.

Nine line charts in a three-by-three grid, one per dataset (DailyDialog, Emotion-Stimulus, Affective Text, CrowdFlower, ElectoralTweets, ISEAR, TEC, EmoInt, SSEC), each plotting F-score against training set size (100, 200, 500, 1000, max) for a grey baseline, a blue transfer line with frozen layers and an orange transfer line without. Orange is usually highest at small sizes and meets grey at max; the y-axis scale differs per panel.
BERT fine-tuned on nine emotion datasets with 100 to 1,000 labelled examples, or 80% of each set ('max'). Straight from BERT (grey), several sets are still near their floor at a few hundred examples; fine-tuning on GoEmotions first (orange) lifts that end, and the curves meet at max. How many labels are enough varies widely between datasets. Image (cropped): google-research/google-research · Apache-2.0 repository (GoEmotions data CC BY 4.0) · from “GoEmotions: A Dataset of Fine-Grained Emotions”, arXiv:2005.00547
Two line charts of macro-averaged F1 against average number of training documents per disease: (a) 805 diseases in HaoDaiFu, 5 to 50 documents, and (b) 44 diseases in ChinaRe, 200 to 1,500 documents. BERT's blue line is highest in both, above bag-of-words, CBOW and LSTM variants.
Macro-F1 for disease classification on two Chinese medical corpora as the number of training documents per disease grows, for BERT and seven other methods. BERT (blue) leads at every size, and with about a third of the training documents it already matches or beats every other method trained on all of them. A pretrained encoder needs fewer labels, not none. Image: Fig. 2 of the article · CC BY 4.0 · from Li et al. 2022, BMC Medical Informatics and Decision Making, “When BERT meets Bilbo: a learning curve analysis of pretrained language model on disease classification”, doi:10.1186/s12911-022-01829-2

Class balance matters more here than in instruction tuning. A classifier trained on heavily imbalanced data learns the prior and predicts the majority class — so check your per-class metrics rather than overall accuracy, which can look excellent while the model never predicts your minority classes at all.

Label quality is the ceiling. Two annotators disagreeing on your taxonomy means the model cannot do better than that disagreement rate, and the usual cause is an underspecified label definition rather than careless annotators. Measure inter-annotator agreement on a sample before scaling up a labelling effort; it is the cheapest way to discover that your categories need rewriting.

A left-to-right flow diagram. Few-shot training data feeds 'Generate sentence pairs' and 'Fine-tune pre-trained ST' under the heading 'ST Fine tuning'; a dashed arrow leads to 'Encode sentences with fine-tuned ST', producing a grid of sentence embeddings, then 'Train classification head', under the heading 'Classification head training'.
When you have tens of labelled examples per class rather than hundreds, SetFit stretches them: it builds sentence pairs from the labelled set to fine-tune an encoder, then trains a small classification head on the embeddings. The input is still just text and labels, which makes the quality of those few labels matter more, not less. Image: huggingface/setfit · Apache-2.0 repository · from “Efficient Few-Shot Learning Without Prompts”, arXiv:2209.11055

Practical notes

Encoder fine-tuning is full fine-tuning by default and is cheap enough that adapters are usually unnecessary — a base-sized model fine-tunes comfortably on a single modest GPU.

Typical settings differ from LLM conventions: learning rates in the 2e-5 to 5e-5 range rather than 2e-4, and two to four epochs. Maximum sequence length is a hard architectural limit rather than a budget choice, which matters if your documents are long — you will need to truncate, chunk and aggregate, or choose a long-context encoder variant.

Token classification has one specific pitfall: subword tokenisation splits words, so your word-level labels must be aligned to subword tokens, conventionally by labelling the first subword and masking the rest. Getting this wrong degrades results without erroring, which is the familiar pattern — check a decoded, aligned example before training.

Questions people ask

Is BERT obsolete?

For generation, it was never the tool. For classification, extraction and retrieval it remains widely deployed because it is small, fast, cheap and well understood, and modern encoder variants have continued to improve. ‘Obsolete’ usually means ‘not what the headlines are about’.

Can I use an LLM to label data for a BERT model?

Yes, and it is a very effective pattern: use a strong generative model to label at volume, have a person check a sample, then distil into a small encoder you can afford to run. You get the large model's judgement at the small model's cost. Our synthetic data post covers the caveats, chiefly that you inherit the labeller's systematic errors.

How do I handle more than a few hundred classes?

Flat classification degrades as classes multiply and get finer. Options are hierarchical classification, retrieval against class descriptions using embeddings, or reframing as a matching problem rather than a classification one. Hundreds of classes with few examples each is the case where a generative or retrieval approach genuinely wins.

Multi-label or multi-class?

Multi-class assumes exactly one correct label and uses a softmax; multi-label allows several and uses per-label sigmoids with a threshold. They need different data and different metrics, and a multi-class dataset cannot train a multi-label model without relabelling. Decide which your problem is before annotating.

Stop hunting. Start searching.

Tens of thousands of curated datasets, searchable in plain language — with the licence and size checked before you download anything.

Search Dataset Finder →