BERT Architecture: Encoder-Only & Bidirectional Context
1270 words
6 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# BERT Architecture: Encoder-Only & Bidirectional Context ## 🎯 Learning Objectives - Understand BERT's encoder-only architecture and bidirectional attention - Explain how MLM + NSP pre-training works - Adapt BERT to downstream tasks via fine-tuning - Compare BERT with GPT on architecture and use cases ## 📋 Prerequ...

BERT Architecture: Encoder-Only & Bidirectional Context
🎯 Learning Objectives
- Understand BERT's encoder-only architecture and bidirectional attention
- Explain how MLM + NSP pre-training works
- Adapt BERT to downstream tasks via fine-tuning
- Compare BERT with GPT on architecture and use cases
📋 Prerequisites
- Pre-training objectives (MLM, NSP) from Week 3
- Transformer encoder architecture
- Fine-tuning concepts
1. 📖 Core Content
1.1 BERT Overview
BERT (Bidirectional Encoder Representations from Transformers) introduced in 2019 by Google. Key innovation: bidirectional context — unlike GPT which reads text left-to-right, BERT reads in both directions simultaneously.
Architecture: Encoder-only Transformer with:
- Bidirectional self-attention (no causal mask)
- Learned positional embeddings
- [CLS] token for classification
- [SEP] token for separating sentences (Diagram)
1.2 BERT Input Representation
Each input token has three embeddings:
| Embedding Type | Shape | Purpose | Learned? |
|---|---|---|---|
| Token Embedding | (seq_len, d_model) | Token identity | Yes |
| Segment Embedding | (seq_len, d_model) | Which sentence (A/B) | Yes |
| Position Embedding | (seq_len, d_model) | Token position | Yes |
1.3 Pre-training: MLM + NSP
BERT is pre-trained with two objectives:
- Masked Language Model (MLM): 15% tokens masked, predict original
- Next Sentence Prediction (NSP): Predict if sentence B follows sentence A Training data: BooksCorpus (800M words) + English Wikipedia (2,500M words) Training hyperparameters:
- BERT-Base: L=12, H=768, A=12, 110M params
- BERT-Large: L=24, H=1024, A=16, 340M params
- Batch size: 256 sequences
- Learning rate: 1e-4
- Training steps: 1,000,000
- Adam optimizer with weight decay
1.4 Fine-tuning BERT
BERT's real power is fine-tuning — taking the pre-trained model and adapting it to a downstream task with minimal changes.
(Diagram)
Classification
Add a classification head on top of [CLS] token:
Named Entity Recognition (NER)
Add a tagging head per token:
Question Answering
Predict answer span (start, end):
1.5 BERT Variants
| Model | Architecture | Innovation | Size |
|---|---|---|---|
| BERT | Encoder-only | MLM + NSP | 110M / 340M |
| RoBERTa | Encoder-only | No NSP, more data, longer training | 125M / 355M |
| ALBERT | Encoder-only | Factorized embeddings, cross-layer sharing | 12M / 18M |
| DistilBERT | Encoder-only | Knowledge distillation from BERT | 66M |
| SpanBERT | Encoder-only | Span-level masking, no NSP | 110M / 340M |
1.6 BERT vs GPT
| Aspect | BERT | GPT |
|---|---|---|
| Architecture | Encoder-only | Decoder-only |
| Attention | Bidirectional | Unidirectional (causal) |
| Pre-training | MLM + NSP | CLM |
| Best for | Understanding, classification | Generation |
| Context usage | Full bidirectional | Left-to-right |
| Fine-tuning | Task-specific heads | Few-shot / Prompting |
| Inference | Non-autoregressive | Autoregressive |
1.7 Why This Matters
BERT revolutionized NLP by showing that bidirectional pre-training captures richer representations. It remains the gold standard for:
- Text classification (sentiment, spam, topic)
- Named Entity Recognition
- Question Answering
- Sentence similarity / paraphrase detection Most modern "embedding models" (text-embedding-3-small, etc.) are based on the BERT architecture.
6. 📝 Practice Questions
Q1: If BERT-Base has d_model=768, A=12 heads, what is d_k per head?d_k = d_model / A = 768 / 12 = 64Each head in BERT-Base operates in 64-dimensional space. Q2: Why does BERT use [CLS] token for classification?The [CLS] token is designed to aggregate information from the entire sequence. Because BERT's attention is bidirectional, [CLS] attends to all tokens and all tokens attend to [CLS]. The final [CLS] representation is a contextualized summary of the entire input, making it suitable for classification.In contrast, GPT uses the last token (which has attended to all previous tokens) for classification. Q3: How does RoBERTa differ from BERT in pre-training?RoBERTa (Robustly Optimized BERT Approach) changes:
- Removes NSP objective
- Dynamic masking (different mask pattern per epoch vs BERT's static)
- Larger batches (8K vs 256)
- More data (160GB vs 16GB)
- Longer training (500K steps with larger batches)
- Higher learning rate
These changes make RoBERTa outperform BERT with the same architecture. Q4: For question answering with BERT, why are start and end logits computed separately?The answer span is defined by a start index i and end index j (i ≤ j). Instead of predicting all possible (i,j) pairs (O(n²)), BERT decomposes the problem:
- Compute P(start = i) for each position i
- Compute P(end = j) for each position j
- The best span is: argmax_{i ≤ j} P(start=i) × P(end=j)
This is O(n) per position and O(n²) for finding the best pair, which is tractable. Q5: BERT-Large has 340M parameters. If fine-tuning adds 10K parameters for a classification head, what fraction are task-specific?Fraction = 10,000 / 340,000,000 = 0.0000294 = 0.00294%Only ~0.003% of parameters are task-specific. The remaining 99.997% are shared from pre-training. This is why fine-tuning is so effective — we leverage the vast majority of parameters that already encode general language understanding. Q6: Why can't BERT be used for text generation?BERT is an encoder-only model with bidirectional attention. Generation requires:
- Autoregressive left-to-right processing (BERT processes all tokens at once)
- Causal masking (BERT has no masking)
- A way to produce the next token (BERT doesn't predict the next token)
To generate with BERT, you'd need to add a decoder on top (making it encoder-decoder) or use iterative masking/unmasking (slow and impractical). Q7: What is the WordPiece tokenization vocabulary size of BERT-Base?BERT-Base uses WordPiece with a vocabulary of 30,000 tokens. This was chosen to balance coverage (most words are in the vocabulary) with efficiency (rare words are split into subwords).Compare to:
- GPT-2: 50,257 BPE tokens
- LLaMA: 32,000 SentencePiece tokens
- T5: 32,000 SentencePiece tokens Q8: BERT processes sequences up to 512 tokens. If a document has 2000 tokens, how can BERT handle it?
Options:
- Sliding window: Process windows of 512 tokens with overlap, aggregate results
- Truncation: Keep first 512 tokens (loses later information)
- Hierarchical: Split into chunks, encode each, then combine representations
- Longformer/XLNet: Use specialized architectures designed for longer sequences
BERT itself can't process >512 tokens due to positional embedding limit. Later models (Longformer, BigBird) address this with sparse attention. Q9: In NER fine-tuning with BERT, why might the model perform poorly on rare entity types?For rare entity types (e.g., "LAW" or "PRODUCT" in a domain-specific dataset):
- Limited labeled examples: The fine-tuning head sees few examples
- Class imbalance: Rare types are dominated by "O" (non-entity) tags
- Pre-training bias: BERT was pre-trained on general text, not domain-specific entities
Solutions:
- Use data augmentation for rare types
- Apply class weighting in the loss function
- Use few-shot or prompting approaches instead of fine-tuning
- Domain-adapt the pre-trained model on relevant text before fine-tuning Q10
<strong>Q10</strong>: How would you modify BERT for a span-based question answering task where answers can span multiple sentences?Standard BERT QA extracts a contiguous span (start ≤ end) from a single context passage. For multi-sentence answers:
- Longer context: Use a model that supports longer sequences (XLNet, Longformer)
- Multiple spans: Add a "number of spans" classifier and extract multiple (start, end) pairs
- Two-stage: First identify relevant sentence(s), then extract span within each
- Sequence-to-sequence: Use T5 or BART which generate the answer token by token (allowing multi-sentence answers)
The last approach (seq2seq) is often preferred for multi-sentence outputs as it doesn't require the span constraint.
7. 🔗 Cross-References
- Previous: GPT Architecture
- Next: Tokenization (Week 6)
- Video: BSDA5004 Week 5 transcripts Join Discord PreviousGPT ArchitectureNextTokenization Methods