Language Models

GATE Relevance: Medium

Prerequisites

🎯 Learning Objectives

  • Define what a language model computes and why it is useful
  • Derive and compute n-gram probabilities from a corpus
  • Compute perplexity and interpret it as a measure of model quality
  • Apply Laplace and Kneser-Ney smoothing to handle zero-count n-grams
  • Understand the transition from n-gram to neural language models

1. What Is a Language Model?

Definition: A language model (LM) assigns a probability P(w₁, w₂, …, wn) to a sequence of words. Equivalently, it estimates the probability of the next word given the preceding context: P(wn | w₁, …, wn−1).

Using the chain rule of probability:

P(w₁, w₂, …, wn) = ∏i=1n P(wi | w₁, …, wi−1)

Applications:

  • Speech recognition: “recognize speech” vs “wreck a nice beach” — the LM assigns higher probability to the meaningful sentence.
  • Machine translation: Choose the more fluent translation.
  • Autocomplete / text generation: Predict the next word given a prefix.
  • Spelling correction: “I went to the [store/stor]” — LM prefers “store”.

2. N-gram Language Models

Estimating P(wi | w₁, …, wi−1) using the full history is impractical (too many possible histories). The Markov assumption limits context to the previous (n−1) words:

P(wi | w₁, …, wi−1) ≈ P(wi | wi−n+1, …, wi−1)

2.1 Unigram Model (n = 1)

P(wi) = count(wi) / N

where N is the total number of tokens in the corpus. Ignores all context.

2.2 Bigram Model (n = 2)

P(wi | wi−1) = count(wi−1, wi) / count(wi−1)

2.3 Trigram Model (n = 3)

P(wi | wi−2, wi−1) = count(wi−2, wi−1, wi) / count(wi−2, wi−1)

Worked Example: Bigram Probabilities

Corpus: “<s> the cat sat on the mat </s>”

Bigram counts:

BigramCount
(<s>, the)1
(the, cat)1
(cat, sat)1
(sat, on)1
(on, the)1
(the, mat)1
(mat, </s>)1

P(cat | the) = count(the, cat) / count(the) = 1/2 = 0.5

P(mat | the) = count(the, mat) / count(the) = 1/2 = 0.5

Note: count(the) = 2 because “the” appears twice in the corpus.

3. Perplexity

Definition: Perplexity (PP) measures how well a language model predicts a test sequence. Lower perplexity = better model.

PP(W) = P(w₁, w₂, …, wN)−1/N

Equivalently, using log probabilities:

log PP(W) = −(1/N) ∑i=1N log₂ P(wi | wi−n+1, …, wi−1)

Intuition: Perplexity is the weighted average branching factor — the average number of equally likely next words the model predicts. A perplexity of 100 means the model is as uncertain as if it had to choose uniformly among 100 options at each step.

Worked Example

Test sequence: “the cat sat” (N = 3 words)

Suppose a bigram model gives: P(the|<s>) = 0.5, P(cat|the) = 0.3, P(sat|cat) = 0.4

P(W) = 0.5 × 0.3 × 0.4 = 0.06

PP = 0.06−1/3 = (1/0.06)1/3 = 16.671/3 ≈ 2.55

💡 GATE Tip

Perplexity questions may appear as numerical answer type (NAT). Remember: PP = exp(−(1/N) ∑ ln P(wi|context)) when using natural log. Be careful with the base of the logarithm!

4. Smoothing Techniques

N-gram models assign zero probability to unseen n-grams, making the entire sequence probability zero. Smoothing redistributes some probability mass to unseen events.

4.1 Laplace (Add-1) Smoothing

Add 1 to every n-gram count:

PLaplace(wi | wi−1) = (count(wi−1, wi) + 1) / (count(wi−1) + |V|)

where |V| is the vocabulary size. This ensures every bigram has a non-zero probability.

Problem: Laplace smoothing shifts too much probability mass to unseen events, significantly distorting the distribution for large vocabularies.

4.2 Add-k Smoothing

Generalize Laplace by adding a fraction k (0 < k < 1) instead of 1:

Padd-k(wi | wi−1) = (count(wi−1, wi) + k) / (count(wi−1) + k·|V|)

4.3 Kneser-Ney Smoothing

Kneser-Ney: Subtracts a fixed discount δ from each non-zero count and distributes the saved probability mass based on the continuation probability — how likely a word is to appear as a novel continuation in different contexts.

PKN(wi | wi−1) = max(count(wi−1, wi) − δ, 0) / count(wi−1) + λ(wi−1) · Pcontinuation(wi)

where Pcontinuation(wi) = |{w: count(w, wi) > 0}| / |{(w, w′): count(w, w′) > 0}|

Why it works: Consider “Francisco”. It has a high unigram count but almost always follows “San”. Its continuation probability is low (appears after only one unique context), so Kneser-Ney correctly assigns it low probability as a novel continuation.

Example: Laplace Smoothing

Given: count(the, cat) = 5, count(the) = 100, |V| = 10,000

MLE: P(cat|the) = 5/100 = 0.05

Laplace: P(cat|the) = (5+1)/(100+10000) = 6/10100 ≈ 0.000594

The Laplace estimate is drastically lower because the |V| term in the denominator dominates. This illustrates why Laplace smoothing is too aggressive for large vocabularies.

5. Neural Language Models

Neural LMs overcome the limitations of n-gram models:

  • No data sparsity: Use continuous word embeddings instead of discrete count tables.
  • Longer context: RNNs and Transformers can capture dependencies far beyond n−1 words.
  • Generalization: Similar words share similar embeddings, enabling generalization to unseen word combinations.

5.1 Bengio et al. (2003) — First Neural LM

Architecture: look up embeddings of previous n−1 words, concatenate, pass through a hidden layer, softmax output over vocabulary.

P(wt | wt−n+1, …, wt−1) = softmax(W · tanh(H · [et−n+1; …; et−1] + b) + d)

5.2 RNN Language Models

Use the hidden state ht to encode the entire preceding context:

ht = f(Wh · ht−1 + Wx · et + b)

P(wt+1 | w₁, …, wt) = softmax(Wo · ht + d)

LSTM and GRU variants address the vanishing gradient problem for long sequences.

5.3 Transformer Language Models

Use self-attention to process all positions in parallel, capturing long-range dependencies without recurrence. This architecture forms the basis of GPT, BERT, and other modern LLMs (covered in upcoming sections).

6. N-gram vs Neural Language Models

FeatureN-gram LMNeural LM
RepresentationDiscrete count tablesContinuous embeddings
Context lengthFixed (n−1 words)Variable (full sequence for Transformers)
Data efficiencyNeeds smoothing for unseen n-gramsGeneralizes via embedding similarity
Computational costLow (table lookups)High (matrix operations, GPU needed)
TrainingCount and normalizeBackpropagation, gradient descent
PerformanceDecent for small tasksState-of-the-art on all benchmarks

7. Summary

  • A language model computes P(w₁, w₂, …, wn) using the chain rule of probability.
  • N-gram models approximate using the Markov assumption: P(wi|context) ≈ P(wi|wi−n+1,…,wi−1).
  • Perplexity = P(W)−1/N: lower is better. Interpretable as average branching factor.
  • Laplace smoothing: add 1 to all counts. Simple but too aggressive.
  • Kneser-Ney: uses continuation probability for better smoothing.
  • Neural LMs (RNN, Transformer) overcome sparsity with continuous representations and variable-length context.