∑GateAndNet
PlannerSign Up
ADVANCEDFrequently Tested 4 min read 2 views

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 O=(o1,…,oT)O = (o_1, \dots, o_T), find the best tag sequence Y^=(y1,…,yT)\hat{Y} = (y_1, \dots, y_T):

  • Hidden Markov Model (Generative): Maximizes P(O,Y)=∏t=1TP(yt∣yt−1)⏟Transition aijP(ot∣yt)⏟Emission bj(ot)P(O, Y) = \prod_{t=1}^T \underbrace{P(y_t \mid y_{t-1})}_{\text{Transition } a_{ij}} \underbrace{P(o_t \mid y_t)}_{\text{Emission } b_j(o_t)} in O(T⋅∣S∣2)\mathcal{O}(T \cdot |S|^2) time using Viterbi (max-product) or computes marginal likelihood P(O)P(O) using the Forward Algorithm (sum-product).
  • Linear-Chain CRF (Discriminative): Directly models conditional P(Y∣O)P(Y \mid O) with arbitrary overlapping orthographic features (capitalization, suffixes, surrounding words) without independence assumptions on OO, avoiding the label bias problem of MEMMs.

2. Inside the Transformer Architecture (Vaswani et al.)

Given input token representations X∈Rn×dmodelX \in \mathbb{R}^{n \times d_{\text{model}}}:

  1. Query, Key, Value Projections: Q=XWQ,  K=XWK,  V=XWVQ = XW^Q,\; K = XW^K,\; V = XW^V with dk=dv=dmodel/hd_k = d_v = d_{\text{model}} / h.
  2. Why divide by dk\sqrt{d_k}? If components of q,kq, k are independent with mean 00 and variance 11, then q⋅k=∑i=1dkqikiq \cdot k = \sum_{i=1}^{d_k} q_i k_i has mean 00 and variance dkd_k (std dev dk\sqrt{d_k}). Dividing by dk\sqrt{d_k} restores unit variance so softmax⁡\operatorname{softmax} doesn't saturate into near-zero gradient regions!
  3. Permutation Equivariance   ⟹  \implies 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.
  4. Parameter Count of 1 Standard Transformer Encoder Block (ignoring biases & LayerNorm 2d2d):
    • Multi-Head Attention (WQ,WK,WV,WOW^Q, W^K, W^V, W^O): 4 dmodel24 \, d_{\text{model}}^2
    • Position-wise FFN (dmodel→4dmodel→dmodeld_{\text{model}} \to 4d_{\text{model}} \to d_{\text{model}}): 2×(dmodel×4dmodel)=8 dmodel22 \times (d_{\text{model}} \times 4d_{\text{model}}) = 8 \, d_{\text{model}}^2
    • Total per layer ≈12 dmodel2\approx \mathbf{12 \, d_{\text{model}}^2}!

3. Pretrained LLM Families: Encoder vs Decoder vs Encoder-Decoder

ArchitectureCanonical ModelsAttention MaskPretraining ObjectiveBest Suited Tasks
Encoder-OnlyBERT, RoBERTa, DeBERTaBidirectional (n×nn \times n full)Masked Language Modeling (MLM 15%)Classification, NER, Extractive QA, Embeddings
Decoder-OnlyGPT-4, Llama, Gemini, ClaudeCausal Upper-Triangular (−∞-\infty for j>ij > i)Autoregressive Next-Token PredictionOpen-ended Generation, Reasoning, Code, Chat
Encoder-DecoderT5, BART, Original TransformerFull Encoder + Causal Decoder + Cross-AttentionSpan Corruption / DenoisingTranslation, Seq2Seq Summarization
Key Formulas Summary
Viterbi Dynamic Programming Recurrence (Length T, |S| Hidden Tags)
vt(j)=max⁡i∈S[vt−1(i)⋅aij⋅bj(ot)],Time=O(T⋅∣S∣2)v_t(j) = \max_{i \in S} \Big[ v_{t-1}(i) \cdot a_{ij} \cdot b_j(o_t) \Big], \qquad \text{Time} = \mathcal{O}(T \cdot |S|^2)
Note: Beats brute-force O(|S|^T) tag sequence enumeration via optimal substructure!
Scaled Dot-Product & Multi-Head Attention
Attention⁡(Q,K,V)=softmax⁡ ⁣(QKTdk)V,MHA⁡(X)=Concat⁡(head1,…,headh)WO\operatorname{Attention}(Q, K, V) = \operatorname{softmax}\!\left(\frac{Q K^T}{\sqrt{d_k}}\right)V, \qquad \operatorname{MHA}(X) = \operatorname{Concat}(\text{head}_1, \dots, \text{head}_h)W^O
Note: With d_model = h * d_k, total projection parameters (W^Q, W^K, W^V, W^O) = 4 * d_model^2!
#NLP#HMM#Viterbi#Self-Attention#Transformer#BERT#GPT#Positional Encoding

Practice Questions on This Chapter (5)

Q1GATE DA TransformersMCQMEDIUM
+2M • -0.67

For an input sequence of length nn and model hidden dimension dd (where n>dn > d), what is the time complexity of computing global Scaled Dot-Product Self-Attention for one Transformer layer?

⚡ Click an option above to verify instantly
Q2GATE DA NLPMCQMEDIUM
+2M • -0.67

An HMM Part-of-Speech tagger has ∣S∣=12|S| = 12 distinct POS tags and processes a sentence of T=20T = 20 tokens. What is the asymptotic order of operations required by the Viterbi algorithm vs naive exhaustive tag enumeration?

⚡ Click an option above to verify instantly
Q3GATE DA LLMsMCQEASY
+1M • -0.33

How does GPT (Decoder-Only) prevent token position ii from attending to future tokens j>ij > i during parallelized training?

⚡ Click an option above to verify instantly
Q4GATE DA Transformer NATNATMEDIUM
+2M • 0 Neg

A Multi-Head Self-Attention layer has model dimension dmodel=512d_{\text{model}} = 512 and h=8h = 8 attention heads with dk=dv=dmodel/hd_k = d_v = d_{\text{model}} / h. What is the per-head key dimension dkd_k?

Q5GATE DA 2027 Expected MSQMSQHARD
+2M • 0 Neg

Which of the following statements about Transformer Self-Attention and Modern LLMs are TRUE?

☑️ Multiple Select Question — pick one or more options, then click Check Answer.
Previous TopicLexical & Vector Semantics: TF-IDF, BM25, Word2Vec (Skip-Gram / CBOW) & GloVeNext Topic Informed Search (A*, IDA*), Adversarial Game Trees & CSP Arc Consistency (AC-3)