Fine-tuning a text encoder, and LoRA
When a frozen encoder can't tell your texts apart, let its top layers learn too. Fine-tuning trains them; LoRA trains small low-rank updates to them. Both start from the frozen model.
What runs in CodeSplash
- Text encoder (MiniLM + linear head) Not verified yet
You will be able to
- Say what fine-tuning and LoRA train, and what stays frozen
- Explain why each fit starts from the frozen model's head (linear probing, then fine-tuning)
- Choose between Frozen, Fine-tune and LoRA for a data set's size and your time
- Use the shared training options on a fine-tuned encoder, and say why Frozen hides them
Read first
When the head alone isn't enough
ReadA frozen encoder with a linear head trains only the head, so it can only use distinctions the encoder already makes. Product codes, a team's shorthand or a narrow field's jargon may need more.
The Text encoder learner's Training setting has three choices:
| Training | What learns | Cost |
|---|---|---|
| Frozen encoder (default) | The penalized linear head | Fastest; fewest rows |
| Fine-tune the top layers | Top Transformer layers and the head | Those layers run over every text in every epoch |
| LoRA | Low-rank updates to the top layers' query and value weights, and the head | Fewer numbers train |
The token embeddings and lower layers stay frozen; their output for each text is computed once.
Starting from the frozen model
ReadEach fine-tuned fit has two stages. First the head is fitted exactly as the frozen model fits it, with every embedding dimension kept. Then encoder and head train together with Adam on the same penalized loss. This is linear probing, then fine-tuningLinear probing, then fine-tuningFit the new head on frozen features first, then fine-tune encoder and head together starting from that head.: a fitted head keeps the first steps from scrambling features the encoder already gets right.
With Epochs set to 0, a fine-tuned run is the frozen model with every dimension kept, so whatever a run with epochs adds is what the encoder learned from your rows.
LoRA in one line
ReadLoRALoRAFine-tuning that freezes a weight matrix W and trains a small low-rank update B·A added to it, scaled by α/r. leaves each adapted weight matrix W as it is and trains an update beside it: W + (α / r)·B·A. With rank r = 8, a 384 × 384 query matrix trains 2 × 8 × 384 = 6,144 numbers instead of 147,456. B starts at zero, so training starts from the pretrained model.
Every encoder fine-tunes
ReadEncoder can be any of the learner's checkpoints: MiniLM, E5-small-v2, multilingual-E5-small, RoBERTa base, DistilBERT base or ELECTRA-small. Fine-tuning and LoRA work the same way on each; only the first step, which turns tokens into vectors, follows the checkpoint. On the same reviews and folds, frozen E5-small-v2 reaches a cross-validated log loss of 0.519; LoRA on its top layer, two epochs, gives 0.512.
The training options
ReadOnce Training is Fine-tune or LoRA, the training options every neural network shares appear under Advanced: optimizer, learning-rate scheduleLearning-rate scheduleA rule that changes the step size over training, usually large early and small late, so the weights settle. and warm-up, gradient clipping, weight averaging, label smoothingLabel smoothingTrains a classifier toward slightly softened targets, such as 0.95 instead of 1, so it is less overconfident. and mixup. Two texts have no average, so mixup here is representation mixupRepresentation mixupMixup for inputs that can't be blended, such as texts. It blends two rows' learned vectors where the head reads them.: it blends their sentence vectors where the head reads them. Left at their defaults, the options change nothing.
With Training at Frozen they are hidden: the head is fitted by Newton steps on fixed vectors.
A fine-tuning run also saves training checkpointsTraining checkpointA saved copy of a network mid-training (weights, optimizer state, epoch and random streams) that training can continue from.: a stopped run resumes where it stopped, ULMFiT-style schedules included, with the same numbers as an unbroken run.
Which would you try?
PredictPredict first
You have 300 labeled support tickets full of in-house product codes, and fine-tuning two layers overfits: training loss keeps falling while cross-validated accuracy drops. Which setting is the better next try?
Reveal
LoRA with a small rank. It can still adapt the top layers to your vocabulary, but with far fewer trainable numbers it overfits less on few rows. Fewer epochs or a smaller learning rate are the other levers; the frozen encoder is the fallback.
Reading a run
ReadA fine-tuned run adds an Encoder training table: the mode, the layers that adapted, how many numbers trained, the epochs run and the final training loss. Compare the cross-validated score with a frozen Text encoder's on the same folds: the difference is what fine-tuning bought. In the Parity compute profile, Python and R train the same network to the same numbers.
Check your understanding
CheckpointCheck yourself
You fine-tune the top 2 of the encoder's 6 layers. What stays frozen?
Show the answer
Answer: a
Fine-tuning here trains the top layers and the head. The embeddings and the lower layers stay fixed, and their output is computed once per text.
Check yourself
You set Training to Frozen encoder. What happens to the Label smoothing and Mixup options?
Show the answer
Answer: a
The training options act on the trainer's loop, which runs only when the encoder trains. A frozen fit has no such loop, so the options are hidden and can't change its result.
Check yourself
A fine-tuning run with 0 epochs gives the same predictions as which model?
Show the answer
Answer: a
Each fit first fits the head as the frozen model does, keeping every dimension. With no epochs nothing else trains, so that head is the model.
Check yourself
Why does LoRA start B at zero?
Show the answer
Answer: a
With B = 0 the update is zero, so the first predictions are the pretrained model's. B and A then learn together.
Run it
In the appIn Predict, add the Text encoder learner, then set Training to Fine-tune the top layers or LoRA. Add a second Text encoder with Training left at Frozen to compare them on the same folds.
Recap
Recap- Fine-tuning trains the encoder's top layers with the head; LoRA trains low-rank updates to them. Embeddings and lower layers stay frozen.
- Every fit starts from the frozen model's head; 0 epochs is the frozen model with every dimension kept.
- Every encoder fine-tunes and takes LoRA. The shared training options show only then, and a stopped run resumes from its checkpoints.
- Judge the extra training by the cross-validated score next to the frozen model's, not by the training loss.
Concepts in this lesson