Encoder-only: BERT and friends (and sentence embeddings)
Bidirectional encoders, masked language modelling, and where RAG's retrievers come from
Encoders read text in both directions and turn it into representations rather than more text. BERT's masked-language-modelling recipe made "pretrain once, fine-tune cheaply" the standard of NLP, and its descendants — Sentence-BERT, E5, BGE, ModernBERT — are the bi-encoders and cross-encoders that retrieve and rerank documents in every RAG system (9.2 Embeddings deep dive, 9.5 Retrieval quality: hybrid search, reranking, query transforms and metrics). Knowing how they are trained tells you when to use a 100M-parameter encoder instead of a billion-parameter decoder.
You'll be able to
- Write the masked-LM objective and explain the 80/10/10 corruption rule and why NSP was dropped
- Describe how one pretrained encoder is fine-tuned for classification, tagging and extractive QA
- Explain why raw BERT vectors are poor sentence embeddings and how Sentence-BERT's siamese contrastive training fixes this
- Decide when an encoder beats a decoder LLM for a task in 2026
Key ideas
- Encoders attend bidirectionally, so they cannot generate but excel at representing a fixed text.
- Masked LM corrupts 15% of tokens (80% [MASK], 10% random, 10% unchanged) and predicts them from both sides; only masked positions contribute to the loss.
- RoBERTa showed NSP was unnecessary and BERT was under-trained: more data, longer training and dynamic masking gave large gains.
Show 4 more key ideas
- Fine-tuning adds a tiny head on [CLS] or on every token; one pretrained encoder serves classification, tagging and extractive QA.
- ELECTRA's replaced-token detection supervises every position and is far more compute-efficient than MLM.
- Raw BERT vectors are poor sentence embeddings; Sentence-BERT's siamese contrastive training makes cosine similarity meaningful — the origin of modern embedding models.
- Bi-encoders embed texts independently (fast retrieval); cross-encoders read both texts together (accurate reranking).
Suggested path through this lesson
Reading in both directions
A decoder predicts the next token, so position may only see positions . An encoder has no such constraint: every token attends to every other, left and right. That makes encoders useless for generation but ideal for understanding a fixed piece of text — classifying it, tagging each token, extracting an answer span, or compressing it into a vector. BERT (Devlin et al., 2018) is the transformer encoder of 7.1 Attention Is All You Need: the big picture trained with a self-supervised objective that exploits bidirectionality.
Masked language modelling
You cannot train a bidirectional model to predict the next word — it can already see it. BERT's solution is the masked LM (MLM): corrupt 15% of input tokens and predict the originals from the two-sided context. Of the selected tokens, 80% are replaced by a special [MASK] token, 10% by a random token, and 10% are left unchanged, so that the model cannot rely on seeing [MASK] at inference (where it never appears) and must keep a good representation of every token. The loss is cross-entropy on the masked positions only:
where is the set of corrupted positions and the corrupted sequence. Note the cost: only 15% of tokens produce a training signal per pass, versus 100% for a causal LM (7.6 Decoder-only: GPT and autoregressive generation) — one reason BERT-style pretraining fell behind at scale.
The original BERT added next-sentence prediction (NSP), a binary task on sentence pairs separated by [SEP]; RoBERTa (2019) showed NSP was unnecessary and that BERT was simply under-trained: more data (160 GB vs 16 GB), longer training, bigger batches and dynamic masking (a fresh mask each epoch) gave large gains with the identical architecture. Input embeddings are the sum of token, position and segment embeddings; the vocabulary is 30k WordPiece tokens (6.1 Text to numbers: tokenization). Sizes: BERT-base has , and 110M parameters; BERT-large has , and 340M.
Fine-tuning: one encoder, many heads
Pretraining produces contextual vectors for every token. Fine-tuning attaches a small head and trains everything end to end for a few epochs at a low learning rate (8.2 Fine-tuning and PEFT):
| task | input | head | example benchmark |
|---|---|---|---|
| sequence classification | [CLS] text [SEP] |
linear layer on the [CLS] vector |
GLUE, sentiment |
| token classification | one sentence | linear layer on every token | NER, POS tagging |
| extractive QA | [CLS] question [SEP] passage |
two scorers for start and end positions | SQuAD |
| sentence pair | [CLS] A [SEP] B |
linear layer on [CLS] |
NLI, paraphrase |
This "pretrain once, fine-tune cheaply" recipe (ELMo and ULMFiT pioneered it, 6.3 Word embeddings) made BERT the default NLP tool from 2019 to 2022, and it still powers countless production classifiers because a 110M-parameter encoder is cheap to run.
Better encoders
- ELECTRA (2020) replaces MLM with replaced-token detection: a small generator fills masks with plausible tokens and the main model classifies every token as original or replaced. Now 100% of positions give signal, so ELECTRA matches RoBERTa with far less compute.
- DeBERTa separates content and position in attention (disentangled attention) and was the strongest GLUE/SuperGLUE encoder of its era.
- ModernBERT (2024) retrofits the encoder with the decoder-world toolkit — RoPE (7.3 Positional information: sinusoidal, learned, RoPE, ALiBi), GeGLU, FlashAttention (7.10 Efficient inference: KV cache, GQA, FlashAttention, speculative decoding, quantization), an 8k context and 2T training tokens — showing that the encoder family had been neglected, not made obsolete.
From tokens to sentence embeddings
RAG needs one vector per passage (9.2 Embeddings deep dive). The naive choice — BERT's [CLS] vector or the mean of its token vectors — gives poor similarity scores, because MLM never asked for sentence-level geometry. Sentence-BERT (2019) fixes this with a siamese setup: the same encoder embeds two sentences, mean pooling gives and , and the model is fine-tuned so that cosine similarity (1.1 Vectors, dot products and norms) tracks semantic similarity, using NLI pairs or a contrastive loss that pulls positives together and pushes in-batch negatives apart (5.8 Self-supervised and contrastive learning). Modern embedding models (E5, BGE, GTE, Nomic) are exactly this — encoder transformers trained contrastively on hundreds of millions of pairs — and a cross-encoder (both texts in one input, score read from [CLS]) is the slower, more accurate reranker applied to the top- results (9.5 Retrieval quality: hybrid search, reranking, query transforms and metrics).
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2') # a 22M-parameter BERT-style encoder
emb = model.encode(['How do I reset my password?',
'Password recovery steps',
'Best pizza in Rome'], normalize_embeddings=True)
print(emb @ emb.T) # cosine similarities: first two high, third low
Intuition. MLM teaches an encoder what each word means in context; contrastive fine-tuning teaches it what a whole passage is about. Retrieval needs the second.
Encoder-only in 2026
Use an encoder when you need a representation rather than text: embeddings for retrieval and clustering, high-volume classification (moderation, routing, intent), token tagging, or reranking. A 100M–400M-parameter encoder processes thousands of passages per second on one GPU, orders of magnitude cheaper than prompting a decoder LLM for the same label — though zero-shot LLM classification wins when you have no labelled data at all (8.4 Prompting as programming).
Where this shows up later
- Bi-encoders and cross-encoders trained from these models are the retrievers and rerankers of RAG — 9.2 Embeddings deep dive, 9.5 Retrieval quality: hybrid search, reranking, query transforms and metrics.
- Masked modelling returns as MAE for images (5.8 Self-supervised and contrastive learning) and as span corruption in T5 (7.7 Encoder–decoder: T5, BART and choosing an architecture).
- Fine-tuning with a small head is the simplest form of adaptation; LoRA generalizes it — 8.2 Fine-tuning and PEFT.
- Contrastive training with in-batch negatives (InfoNCE) is the same objective behind SimCLR and CLIP — 5.8 Self-supervised and contrastive learning, 8.10 Multimodal models.
Common pitfalls (4)
- Using the raw [CLS] vector of a vanilla BERT for semantic search — similarity scores will be nearly meaningless without contrastive fine-tuning.
- Expecting BERT to generate text: without a causal structure it has no way to produce sequences token by token.
- Fine-tuning with a learning rate meant for training from scratch — encoders want small rates (around 2e-5) and few epochs or they forget their pretraining.
- Assuming encoders are obsolete because LLMs exist — for embeddings, reranking and high-volume classification they are still the cost-effective choice.
Recap: all 7 key points
- Encoders attend bidirectionally, so they cannot generate but excel at representing a fixed text.
- Masked LM corrupts 15% of tokens (80% [MASK], 10% random, 10% unchanged) and predicts them from both sides; only masked positions contribute to the loss.
- RoBERTa showed NSP was unnecessary and BERT was under-trained: more data, longer training and dynamic masking gave large gains.
- Fine-tuning adds a tiny head on [CLS] or on every token; one pretrained encoder serves classification, tagging and extractive QA.
- ELECTRA's replaced-token detection supervises every position and is far more compute-efficient than MLM.
- Raw BERT vectors are poor sentence embeddings; Sentence-BERT's siamese contrastive training makes cosine similarity meaningful — the origin of modern embedding models.
- Bi-encoders embed texts independently (fast retrieval); cross-encoders read both texts together (accurate reranking).
Watch
Complete treatment of MLM, NSP, the input embeddings and the fine-tuning heads.
Read
Exercises
- Watch MLM work
With the Hugging Face
fill-maskpipeline andbert-base-uncased, mask one word in ten sentences and inspect the top-5 predictions. Then mask a word whose meaning depends on the right context (for example the last word of "The bank approved my ___"). Expected: BERT uses the right-hand context, which a left-to-right model cannot. - Fine-tune a classifier
Fine-tune
distilbert-base-uncasedon 2,000 examples of a sentiment or topic dataset for 3 epochs with learning rate 2e-5. Expected: above 88% accuracy on held-out data in a few minutes on a free GPU; compare with a TF-IDF + logistic regression baseline (6.2 Classical NLP and n-gram language models). - Raw BERT vs Sentence-BERT
Embed 20 sentence pairs (10 paraphrases, 10 unrelated) with (a) mean-pooled
bert-base-uncasedand (b)all-MiniLM-L6-v2. Plot the cosine similarities of both groups for each model. Expected: with (a) the two groups overlap heavily; with (b) they separate cleanly.
Quiz
Flashcards
These cards are now in your spaced-repetition queue. Review all due cards →