Phase 7 · Transformers/
Lesson 7.5core2 h 30 min

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

  1. Encoders attend bidirectionally, so they cannot generate but excel at representing a fixed text.
  2. 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.
  3. 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
  1. Fine-tuning adds a tiny head on [CLS] or on every token; one pretrained encoder serves classification, tagging and extractive QA.
  2. ELECTRA's replaced-token detection supervises every position and is far more compute-efficient than MLM.
  3. Raw BERT vectors are poor sentence embeddings; Sentence-BERT's siamese contrastive training makes cosine similarity meaningful — the origin of modern embedding models.
  4. Bi-encoders embed texts independently (fast retrieval); cross-encoders read both texts together (accurate reranking).

Suggested path through this lesson