Sequence Labeling (HMM Viterbi) & Transformers (Self-Attention, BERT vs GPT)
HMM generative tagging & O(T * S^2) Viterbi DP decoding, Scaled Dot-Product & Multi-Head Self-Attention parameter counts, Causal Masking, Positional Encodings, and BERT vs GPT vs T5.
1. Sequence Labeling: HMM + Viterbi vs Linear-Chain CRF
In Part-of-Speech (POS) tagging or Named Entity Recognition (NER), given words , find the best tag sequence :
- Hidden Markov Model (Generative): Maximizes in time using Viterbi (max-product) or computes marginal likelihood using the Forward Algorithm (sum-product).
- Linear-Chain CRF (Discriminative): Directly models conditional with arbitrary overlapping orthographic features (capitalization, suffixes, surrounding words) without independence assumptions on , avoiding the label bias problem of MEMMs.
2. Inside the Transformer Architecture (Vaswani et al.)
Given input token representations :
- Query, Key, Value Projections: with .
- Why divide by ? If components of are independent with mean and variance , then has mean and variance (std dev ). Dividing by restores unit variance so doesn't saturate into near-zero gradient regions!
- Permutation Equivariance Positional Encoding Required: Self-attention without positional info treats input as an unordered bag/set! Sinusoidal, learned, or RoPE (Rotary Position Embeddings) inject token order.
- Parameter Count of 1 Standard Transformer Encoder Block (ignoring biases & LayerNorm ):
- Multi-Head Attention ():
- Position-wise FFN ():
- Total per layer !
3. Pretrained LLM Families: Encoder vs Decoder vs Encoder-Decoder
| Architecture | Canonical Models | Attention Mask | Pretraining Objective | Best Suited Tasks |
|---|---|---|---|---|
| Encoder-Only | BERT, RoBERTa, DeBERTa | Bidirectional ( full) | Masked Language Modeling (MLM 15%) | Classification, NER, Extractive QA, Embeddings |
| Decoder-Only | GPT-4, Llama, Gemini, Claude | Causal Upper-Triangular ( for ) | Autoregressive Next-Token Prediction | Open-ended Generation, Reasoning, Code, Chat |
| Encoder-Decoder | T5, BART, Original Transformer | Full Encoder + Causal Decoder + Cross-Attention | Span Corruption / Denoising | Translation, Seq2Seq Summarization |
Practice Questions on This Chapter (5)
For an input sequence of length and model hidden dimension (where ), what is the time complexity of computing global Scaled Dot-Product Self-Attention for one Transformer layer?
An HMM Part-of-Speech tagger has distinct POS tags and processes a sentence of tokens. What is the asymptotic order of operations required by the Viterbi algorithm vs naive exhaustive tag enumeration?
How does GPT (Decoder-Only) prevent token position from attending to future tokens during parallelized training?
A Multi-Head Self-Attention layer has model dimension and attention heads with . What is the per-head key dimension ?
Which of the following statements about Transformer Self-Attention and Modern LLMs are TRUE?