COMP5046 — Natural Language Processing

Lecture 7: Models — Language Models

P(w1..N) · n-gram Markov approximation · count-based MLE · smoothing · perplexity · neural LMs · tasks framed as LM.

How to use: trace each formula’s estimator and what it predicts · close the page and answer Recall lines.

Definition

What is a language model?

RecallWhat is the single object an LM defines, and what task does it directly enable?

Approximation

N-gram language models & the Markov assumption

Bigram approximation of P(University of Sydney) P(University|The) P(of|University) P(Sydney|of) Product → joint approximation
Bigram model: replace each P(wi | history) with P(wi | wi-1); multiply across positions.

RecallWhy is increasing \(n\) not always better? Name the two failure modes.

Estimator

Count-based maximum likelihood

RecallWrite the bigram MLE in words and identify the two ways it can produce a problematic 0.

Boundaries

Sequence start, end, and log-probabilities

RecallTwo structural fixes for the ends of sequences, and one numerical fix during scoring.

Zero counts

Smoothing: Laplace, add-k, Kneser-Ney

RecallWrite add-1 smoothing for a bigram and explain in one sentence what Kneser-Ney measures that add-1 ignores.

Intrinsic metric

Perplexity

RecallState perplexity two ways (probability and cross-entropy). Which direction is better — lower or higher?

From counts to vectors

Neural language models

tokens \(w_{i-n+1..i-1}\)
embed lookup \(E\)
hidden \(h\)
linear \(Wh+b\)
softmax over \(|V|\)
\(P(w_i \mid \text{context})\)

RecallWalk a single token through a neural LM: which layer produces the distribution and via what nonlinearity?

Applications

Tasks framed as language modelling

RecallGive the conditional probability that translation, speech recognition, and completion each maximise.

Lab

Workshop & materials

Quick practice

A bigram language model approximates \(P(w_i \mid w_1, \dots, w_{i-1})\) by:
\(P(w_i)\) — ignore all context
\(P(w_i \mid w_{i-2}, w_{i-1})\) — last two tokens
\(P(w_i \mid w_{i-1})\) — only the previous token
\(P(w_i \mid w_{i+1})\) — only the following token
The main reason to apply Laplace (add-1) smoothing is to:
Speed up training
Avoid assigning zero probability to bigrams unseen in the training data
Lower the perplexity on training data
Remove the need for an <end> token
Which expression for perplexity is correct?
\(\text{PP}(W) = P(w_1\dots w_N)^{-1/N} = \exp(\text{cross-entropy})\)
\(\text{PP}(W) = P(w_1\dots w_N) \cdot N\)
\(\text{PP}(W) = \log P(w_1\dots w_N)\)
\(\text{PP}(W) = 1 - P(w_1\dots w_N)\)

Tutorial

Worked problems

Problem 1 — Bigram MLE by hand

Corpus: <s> I am Sam </s>, <s> Sam I am </s>, <s> I do not like green eggs and ham </s>.

Compute \(P(\text{I}\mid\langle s\rangle)\), \(P(\text{Sam}\mid\text{am})\), and \(P(\text{am}\mid\text{Sam})\) using count-based MLE.

Approach: count bigrams (history, word) then divide by Count(history). Answers: 2/3, 1/2, 1/2.

Problem 2 — Add-1 smoothing on an unseen bigram

Vocabulary \(|V| = 1000\). In training, Count(the) = 8000 and Count(the, quokka) = 0.

Compute the Laplace-smoothed probability \(P(\text{quokka}\mid\text{the})\) and compare to the unsmoothed MLE.

Approach: \(P_\text{Lap}=(0+1)/(8000+1000)=1/9000 \approx 1.11\times 10^{-4}\) vs. MLE \(=0\). Smoothing rescues the joint from collapsing to zero.

Problem 3 — Perplexity comparison

Test sequence has 2 tokens; LMA assigns each token probability \(\tfrac{1}{10}\); LMB assigns each token probability \(\tfrac{1}{2}\).

Compute \(\text{PP}_A\) and \(\text{PP}_B\); which model is better?

Approach: \(\text{PP}_A = ((0.1)(0.1))^{-1/2}=10\); \(\text{PP}_B = ((0.5)(0.5))^{-1/2}=2\). Lower is better, so LMB is better — it concentrates probability mass on the observed tokens.

Chapter quizzes

Retrieval practice for this lecture.

Open Quiz Hub Chapter flashcards