βˆ‘GateAndNet
PlannerSign Up
INTERMEDIATEFrequently Tested 4 min read 3 views

N-Gram Language Models, Laplace/Kneser-Ney Smoothing & Perplexity

Markov chain N-gram MLE probabilities, Add-1 (Laplace) & Add-k smoothing formulas, Interpolation vs Backoff, Kneser-Ney continuation probability, and Cross-Entropy / Perplexity numericals.

1. Chain Rule & NN-Gram Markov Assumption

Exact joint probability of a token sequence W=(w1,w2,…,wT)W = (w_1, w_2, \dots, w_T) factors via the chain rule P(W)=∏t=1TP(wt∣w1,…,wtβˆ’1)P(W) = \prod_{t=1}^T P(w_t \mid w_1, \dots, w_{t-1}). An NN-gram model applies a (Nβˆ’1)(N-1)-th order Markov assumption, looking back only Nβˆ’1N - 1 tokens:

  • Unigram (N=1N=1): P(wt)P(w_t)
  • Bigram (N=2N=2): P(wt∣wtβˆ’1)P(w_t \mid w_{t-1})
  • Trigram (N=3N=3): P(wt∣wtβˆ’2,wtβˆ’1)P(w_t \mid w_{t-2}, w_{t-1})

2. Handling Zero Counts: Smoothing & Kneser-Ney

Any unseen test NN-gram gives PMLE=0β€…β€ŠβŸΉβ€…β€ŠP(W)=0β€…β€ŠβŸΉβ€…β€ŠPPL⁑(W)=∞P_{\text{MLE}} = 0 \implies P(W) = 0 \implies \operatorname{PPL}(W) = \infty!

  1. Add-kk Smoothing: Padd-k(wi∣wiβˆ’1)=C(wiβˆ’1,wi)+kC(wiβˆ’1)+k∣V∣P_{\text{add-}k}(w_i \mid w_{i-1}) = \frac{C(w_{i-1}, w_i) + k}{C(w_{i-1}) + k|V|} (k=1k=1 is Laplace). Note βˆ‘w∈VPadd-k(w∣wiβˆ’1)=C(wiβˆ’1)+k∣V∣C(wiβˆ’1)+k∣V∣=1\sum_{w \in V} P_{\text{add-}k}(w \mid w_{i-1}) = \frac{C(w_{i-1}) + k|V|}{C(w_{i-1}) + k|V|} = 1.
  2. Linear Interpolation: P^(wi∣wiβˆ’2,wiβˆ’1)=Ξ»3P3+Ξ»2P2+Ξ»1P1\hat{P}(w_i \mid w_{i-2}, w_{i-1}) = \lambda_3 P_3 + \lambda_2 P_2 + \lambda_1 P_1 with βˆ‘Ξ»j=1\sum \lambda_j = 1.
  3. Absolute Discounting & Kneser-Ney: Subtracts a fixed discount d∈(0,1)d \in (0,1) (e.g., 0.750.75) from non-zero counts and backs off to continuation probability Pcont(w)∝∣{u:C(u,w)>0}∣P_{\text{cont}}(w) \propto |\{u : C(u, w) > 0\}| (how versatile ww is across distinct preceding contexts β€” solves the "San Francisco" trap where "Francisco" has high raw frequency but only ever follows "San"!).

3. Worked Perplexity Numerical

Key Formula

Suppose a language model assigns probabilities P(w1)=12,β€…β€ŠP(w2∣w1)=14,β€…β€ŠP(w3∣w1,w2)=14,β€…β€ŠP(EOSβˆ£β€¦β€‰)=12P(w_1)=\frac{1}{2},\; P(w_2\mid w_1)=\frac{1}{4},\; P(w_3\mid w_1,w_2)=\frac{1}{4},\; P(\text{EOS}\mid \dots)=\frac{1}{2} over N=4N = 4 tokens: P(W)=12Γ—14Γ—14Γ—12=164=2βˆ’6β€…β€ŠβŸΉβ€…β€ŠPPL⁑(W)=(2βˆ’6)βˆ’1/4=26/4=21.5=22β‰ˆ2.83P(W) = \frac{1}{2} \times \frac{1}{4} \times \frac{1}{4} \times \frac{1}{2} = \frac{1}{64} = 2^{-6} \implies \operatorname{PPL}(W) = \left(2^{-6}\right)^{-1/4} = 2^{6/4} = 2^{1.5} = 2\sqrt{2} \approx \mathbf{2.83} Intuitively, Perplexity equals the weighted average branching factor of the language model (PPL⁑=∣V∣\operatorname{PPL} = |V| for a uniform model P=1/∣V∣P = 1/|V|).

Key Formulas Summary
Bigram MLE vs Laplace (Add-1) Smoothed Probability
PMLE(wi∣wiβˆ’1)=C(wiβˆ’1,wi)C(wiβˆ’1),PLaplace(wi∣wiβˆ’1)=C(wiβˆ’1,wi)+1C(wiβˆ’1)+∣V∣P_{\text{MLE}}(w_i \mid w_{i-1}) = \frac{C(w_{i-1}, w_i)}{C(w_{i-1})}, \qquad P_{\text{Laplace}}(w_i \mid w_{i-1}) = \frac{C(w_{i-1}, w_i) + 1}{C(w_{i-1}) + |V|}
Note: |V| is the vocabulary size (number of unique word types, NOT total word tokens!)
Intrinsic Evaluation: Perplexity & Cross-Entropy Relation
PPL⁑(W)=P(w1,w2,…,wN)βˆ’1N=∏i=1N1P(wi∣w1iβˆ’1)N=2H(W)\operatorname{PPL}(W) = P(w_1, w_2, \dots, w_N)^{-\frac{1}{N}} = \sqrt[N]{\prod_{i=1}^{N} \frac{1}{P(w_i \mid w_1^{i-1})}} = 2^{H(W)}
Note: Minimizing cross-entropy loss H(W) is strictly equivalent to minimizing Perplexity!
#NLP#Language Models#N-Grams#Perplexity#Laplace Smoothing#Kneser-Ney#GATE DA

Practice Questions on This Chapter (2)

Q1GATE DA NLPMCQMEDIUM
+2M β€’ -0.67

A corpus has vocabulary size ∣V∣=1,000|V| = 1,000. The unigram data occurs C(data)=200C(\text{data}) = 200 times, and the bigram data science occurs C(data, science)=19C(\text{data, science}) = 19 times. Under Laplace (Add-1) Smoothing, what is P(science∣data)P(\text{science} \mid \text{data})?

⚑ Click an option above to verify instantly
Q2GATE DA NLPMCQEASY
+2M β€’ -0.67

A language model achieves a per-token Cross-Entropy of H(W)=5Β bits/tokenH(W) = 5\text{ bits/token} (base-22 log) on a held-out evaluation corpus. What is the Perplexity (PPL\text{PPL}) of the language model?

⚑ Click an option above to verify instantly
Previous TopicPCA Eigen-Directions, K-Means/DBSCAN Clustering & Neural Net BackpropagationNext Topic Lexical & Vector Semantics: TF-IDF, BM25, Word2Vec (Skip-Gram / CBOW) & GloVe