Fine-Tuning and Prompting
A pretrained transformer is a general-purpose knowledge source; a task is what you do with it. There are two ways to adapt one, and this first part covers the one that updates the weights: fine-tuning.
╌╌╌╌
A large language model is pretrained once, on web-scale text, to do one thing: predict tokens. That single objective forces it to absorb syntax, facts, and a great deal of world knowledge, all folded into its weights. But prediction is rarely the target task itself — the target is sentiment labels, named entities, answers to questions, a helpful reply. Adaptation turns the general-purpose predictor into a task-specific model. Pretraining is paid for once; adaptation is cheap and is done many different ways on the same frozen starting point.1
There are two families of adaptation, and the difference between them is one bit: whether you change the weights.
- Fine-tuning updates the weights. Start from the pretrained model, add a small task-specific layer, and continue training on labelled examples for the target task. The result is a new, specialized model.
- Prompting leaves the weights frozen. Describe the task to the model in its input — an instruction, maybe a few worked examples — and read the answer off its next-token prediction. No gradient step, no new model.
These two parts build up both. This one covers fine-tuning, starting with the pretraining objective that makes it possible; the next covers prompting and the training stages that turn a base model into an assistant.
The pretrain-then-finetune paradigm
The scheme has two phases. In pretraining, a model learns from raw text with a self-supervised objective — a label mined from the text itself, no human annotation. In fine-tuning, that pretrained model is the starting point for supervised training on a specific labelled task.1 The weights carry over; only the final task-specific piece is new.
The cost asymmetry is large. Pretraining a modern model costs millions of dollars and weeks of compute; fine-tuning it for a new task takes hours and a few thousand labelled examples, because the general representations are already learned. The fine-tune only has to learn the thin mapping from the model's existing representations to the task's outputs.
The feedforward and recurrent language models
of earlier lessons were causal: they predict each word from the words to its
left, because a language model that generates text must not peek at the future.
That left-to-right constraint is right for generation but wrong for
understanding. To classify a word — is bank
a river bank or a financial one? —
you want to see the words on both sides. The first family of adaptation is built
on an encoder that does exactly that.
Bidirectional encoders and BERT
A bidirectional transformer encoder drops the causal mask. Every token attends to every other token, left and right, so each position's output vector is a contextual embedding that summarizes the whole sentence as seen from that word.2 This is the architecture of BERT (Bidirectional Encoder Representations from Transformers) and its many descendants: a stack of transformer encoder blocks, no decoder, producing one context-sensitive vector per input token.
Because it sees the whole context, an encoder is not a text generator — you cannot sample the next word from it. Its job is to represent, producing embeddings that a downstream layer turns into labels. This poses a problem: a left-to-right model trains by predicting the next word, a signal available for free from any text. What is the self-supervised signal for a model that already sees every word? If BERT could see the whole sentence, predicting a word it already has as input would be trivial. The answer is to hide part of the input.
Masked language modeling
The pretraining objective for a bidirectional encoder is masked language
modeling (MLM). Take a sentence, corrupt a random fraction of its tokens — in
BERT, about — and train the model to reconstruct the originals from the
surrounding context.3 The corruption is usually replacement with a special
[MASK] token, and the loss is the cross-entropy of predicting the true token at
each masked position, exactly the softmax-over-vocabulary loss of an ordinary
language model, but applied only at the hidden positions.
Masking is a clean way to force bidirectional understanding. To fill in a blank
correctly the model must use both the left context (the cat
) and any right
context, so it learns to build each position's embedding from the entire sentence.
And the labels are free: any text is its own answer key once part of it is hidden —
the same self-supervised principle from the taxonomy of learning.
BERT paired MLM with a second, sentence-level objective. In next-sentence prediction (NSP), the model is given two segments and asked to classify whether the second actually followed the first in the corpus or was randomly chosen.3 The goal was to teach the relationships between sentences that tasks like question answering and entailment need. Later encoders found NSP weak and often dropped or replaced it — for example with contrastive sentence objectives that pull the embeddings of related sentences together and push unrelated ones apart — but the pattern is the same: a cheap self-supervised signal invented from the raw text.
The tokens themselves are not words but wordpieces: sub-word units produced by
an algorithm like BPE or WordPiece, so a rare word splits into known pieces and the
vocabulary stays fixed and finite.4 Two special tokens matter for the tasks
below: a [CLS] token prepended to every input, whose final embedding serves as a
summary of the whole sequence, and a [SEP] token that separates two segments when
the input is a pair.
How subword vocabularies are built: a BPE trace
The vocabulary is not hand-designed; it is learned from the corpus by byte-pair encoding (BPE; Sennrich et al., 2016), the algorithm SLP3 works through in Chapter 2.4 BPE starts with a vocabulary of individual characters and repeatedly merges the most frequent adjacent pair into a new symbol, growing longer tokens until it has performed merges. It runs inside words, so each word is first split into characters plus an end-of-word marker .
Take Jurafsky & Martin's tiny corpus of five distinct words with counts: low (5), lowest (2),
newer (6), wider (3), new (2). The starting vocabulary is the eleven letters plus .
The learner counts adjacent pairs across the whole corpus and merges the most frequent:
- Merge 1. The pair
e roccurs innewer(6) andwider(3), total — the most frequent. Merge toer. Nownewerisn e w er,widerisw i d er. - Merge 2.
er _(word-finaler) occurs times; merge toer_. - Merge 3.
n eoccurs innewer(6) andnew(2), total ; merge tone. - Merge 4.
ne wtotals ; merge tonew. - Merge 5.
l ototals (low5 +lowest2); merge tolo. - Merge 6.
lo wtotals ; merge tolow.
After these merges the vocabulary contains the eleven characters plus er, er_, ne,
new, lo, low. To tokenize new text, the learned merges are applied greedily in the order
learned. The word newer collapses back to a single token; but an unseen word like lower
tokenizes as low + er_ — two known pieces — so the model never meets a true unknown word,
which is the entire point. The vocabulary size is fixed by , yet any string is
representable.
Fine-tuning: adding a task head
Pretraining gives one contextual embedding per token, plus the [CLS] summary.
Fine-tuning turns those embeddings into task outputs by adding a task head — a
small layer, often a single linear map plus softmax — on top of the frozen-then-
unfrozen encoder, and training the whole assembly on labelled data.5
Three task shapes cover most of NLP, and each reads off a different part of the
encoder's output.
Sequence classification (sentiment, topic, entailment) attaches a head to the
[CLS] summary vector, mapping it to the class scores. Sequence labeling
(part-of-speech tagging, named-entity recognition)
attaches the same head to every token's embedding, producing one label per token.
The third shape, span extraction, is used for extractive question answering; SLP3
gives it in full detail, so we work through it here.
Span-based question answering
In extractive (reading-comprehension) QA, the model is given a question and a
passage, and must return the span of the passage that answers it.6 The
input is built by concatenating the two segments — the question first, the passage
second, separated by [SEP] — and running them through the encoder, which produces
a contextual embedding for every passage token.
The head is two learned vectors: a span-start embedding and a span-end embedding . For each passage token , the probability that it begins the answer is a softmax of the dot product over all passage tokens, and the end probability uses the same way:
A candidate span from token to token scores , and the model predicts the highest-scoring span with . Only and are new; everything below them is the pretrained encoder, nudged by the same gradient. The fine-tuning loss is the negative log-likelihood of the correct start and end positions,
summed over the training examples.6 When a dataset includes questions with
no answer in the passage, the trick is to point the gold start and end at the
[CLS] token, so the model can learn to abstain.
For example, take a four-token passage Ada wrote the program and a question who wrote it?. Suppose the encoder's contextual embeddings, dotted with the learned and vectors, give start scores and end scores over the four tokens. Softmax turns these into start probabilities — the model strongly prefers to start the answer at token (wrote) — and end probabilities , peaking at token (the). The predicted span maximizes over ; the best pair is with score , returning the span wrote the. The constraint rules out impossible spans where the end precedes the start, and the two independent softmaxes let a single forward pass score every start and every end at once.
- 1input: pretrained encoder, labelled pairs , rate
- 2initialize span-start vector , span-end vector randomly
- 3repeat
- 4samplequestion, passage, gold start/end
- 5concat()
- 6contextual embeddings of passage tokens
- 7
- 8
- 9
- 10update encoder, , bygradient reaches the whole model
- 11until converged
The pattern generalizes: whatever the task, choose which embeddings to read, add a thin head, and let the gradient tune the head and the encoder together. Because the encoder starts from a rich pretrained state, a few thousand labelled examples usually suffice — the fine-tune is refining representations, not learning language from scratch.
Parameter-efficient fine-tuning
Full fine-tuning updates every weight, and produces a full-size copy of the model per task. For a billion-parameter encoder and a dozen tasks that is wasteful in both storage and compute. Parameter-efficient fine-tuning (PEFT) freezes the pretrained weights and trains only a small number of new ones, recovering most of the accuracy at a fraction of the cost.7
Two designs dominate. Adapters insert tiny bottleneck layers between the frozen transformer blocks — down-project to a small dimension, apply a non-linearity, up-project back — and train only those. LoRA (Low-Rank Adaptation) freezes each weight matrix and learns a low-rank correction , where and are thin matrices, so the effective weight is with only trainable.
At inference the correction folds back into , so PEFT adds no latency; only training and storage shrink. A single frozen base model can then serve many tasks, each with its own small adapter or LoRA module.
Where fine-tuning stops and prompting starts
Every form of fine-tuning changes the model. Even the parameter-efficient variants leave behind a small trained module that the base model needs in order to do the task. The second family of adaptation gives that up entirely — it specifies the task in the model's input and touches no weights at all. That family, together with the training stages that make a base model good at following instructions, is the subject of the next part. This continues in Prompting and Alignment.
Footnotes
- Jurafsky & Martin, Speech and Language Processing (3rd ed.), Ch. 11 — Fine-tuning and Masked Language Models: the pretrain-then-finetune paradigm, in which a self-supervised pretraining run produces a base model whose weights are the shared starting point for many cheap task-specific fine-tunes. ↩ ↩2
- Jurafsky & Martin, Ch. 11 — Bidirectional Transformer Encoders: dropping the causal mask so every token attends to the whole sequence, yielding contextual (context-sensitive) token embeddings; the encoder-only BERT architecture. ↩
- Jurafsky & Martin, Ch. 11 — Masked Language Modeling: corrupting a random ~15% of tokens with
[MASK]and predicting the originals from two-sided context, and the sentence-level next-sentence-prediction (later contrastive) objective. ↩ ↩2 - Jurafsky & Martin, §2 / Ch. 11 — sub-word tokenization (BPE / WordPiece) producing a fixed wordpiece vocabulary, and the special
[CLS](sequence summary) and[SEP](segment separator) tokens. ↩ ↩2 - Jurafsky & Martin, Ch. 11 — Fine-Tuning for Classification and Sequence Labeling: adding a task head (linear + softmax) on the
[CLS]vector for sequence classification or on every token embedding for sequence labeling, and training head plus encoder together. ↩ - Jurafsky & Martin, §23.2 — reading-comprehension / span-based question answering: concatenating question and passage with
[SEP], learned span-start and span-end vectors, the softmax start/end probabilities (eqs. 23.16–23.17), the span score , the fine-tuning loss (eq. 23.18), and pointing no-answer cases at[CLS]. ↩ ↩2 - Jurafsky & Martin, Ch. 11 — parameter-efficient fine-tuning: freezing the pretrained weights and training only inserted adapter bottlenecks or a low-rank LoRA correction , recovering most of full fine-tuning's accuracy at a fraction of the trainable parameters. ↩
╌╌ END ╌╌