4.1 Transformer Architecture Deep Dive
Self-attention: Query, Key, Value matrices → attention scores (softmax(QK^T/√d)) → weighted sum of values. Multi-head attention: capture different relationship types in parallel. Positional encoding: inject sequence order (Transformers have no inherent position awareness). Feed-forward network. Layer normalisation. Residual connections. Encoder (bidirectional — BERT) vs decoder (autoregressive — GPT). The architecture paper: "Attention Is All You Need."
4.2 BERT: Bidirectional Encoder
Pre-training: Masked Language Model (predict [MASK] tokens — 15% of input) + Next Sentence Prediction. BERT sees ALL tokens simultaneously (bidirectional). Fine-tuning: add task-specific head → train on small labelled data. BERT variants: DistilBERT (66% smaller, 97% performance), RoBERTa (better training recipe), ALBERT (parameter sharing), DeBERTa (disentangled attention — current state-of-the-art encoder). BPE tokenisation: WordPiece.
4.3 GPT: Autoregressive Decoder
Pre-training: next-token prediction (predict the next word given all previous). Causal (masked) self-attention: can only attend to previous tokens. GPT scaling: GPT-2 (1.5B) → GPT-3 (175B) → GPT-4 (estimated 1.7T MoE). Emergent abilities: in-context learning, chain-of-thought reasoning appear at scale. BPE tokenisation. Temperature and top-k/top-p for generation diversity. The architecture behind ChatGPT, Claude, Gemini.
4.4 Tokenisation Deep Dive: BPE, WordPiece, SentencePiece
BPE (Byte-Pair Encoding): merge most frequent character pairs iteratively → build vocabulary. Used by GPT. WordPiece: similar but maximises likelihood of training data. Used by BERT. SentencePiece: language-agnostic, treats text as raw bytes. Tokeniser effects: "tokenisation" → ["token", "isation"] (BPE) vs ["token", "##isation"] (WordPiece). Why tokenisation matters: some languages need 4x more tokens than English → cost + quality implications.
Placement relevance: "Explain the Transformer architecture" is THE most asked NLP/DL interview question. Understanding self-attention (Q, K, V) is non-negotiable. BERT vs GPT (encoder vs decoder, bidirectional vs autoregressive) is the defining distinction every NLP engineer must articulate. Tokenisation deep dive shows understanding beyond API calls.