Math Foundations — Lecture 7
Joint probability of a string, chain rule, Markov approximations, count-based MLE, Laplace smoothing, perplexity, neural LM softmax.
Joint probability of a string
A language model is a probability distribution over finite strings:
From the joint we can compute conditionals, sample continuations, and rank candidate sentences.
Chain rule (exact factorisation)
Exact for any joint distribution. The number of distinct histories \(w_1\dots w_{i-1}\) grows exponentially in length, so we cannot store all of them explicitly.
Markov approximation & n-gram models
Truncate the history to the last \(n-1\) tokens:
Special cases:
Bigram approximation in full:
with \(w_0 = \langle s \rangle\) by convention.
Count-based maximum likelihood
For an n-gram model the MLE is the empirical conditional frequency:
For bigrams:
Properties: unbiased on the training corpus, but assigns zero probability to any bigram unseen during training.
Laplace (add-1) and add-k smoothing
Laplace pretends each bigram was seen once before counting:
The \(+|V|\) in the denominator keeps the distribution normalised (one pseudocount for each of the \(|V|\) vocabulary items).
Add-k generalises with a smaller pseudocount \(k \in (0, 1]\) tuned on a validation set:
Kneser-Ney (intuition)
Two ideas: absolute discounting subtracts a constant \(d\) from each observed count, freeing mass for unseen events; continuation probability backs off using the number of distinct contexts a word appears in, not its raw frequency. Words appearing in many contexts (e.g. "York" in many bigrams) are better fallbacks than common but context-narrow words (e.g. "Francisco" mostly follows "San").
Perplexity
Length-normalised inverse joint probability of a test sequence \(W = w_1\dots w_N\):
Equivalently, the exponentiated per-token cross-entropy:
Neural language model (feed-forward sketch)
Given a context window of \(n-1\) tokens, the neural LM computes a distribution over the vocabulary as follows.
Embedding lookup
Each context token is replaced by its column of the embedding matrix \(E\).
Hidden layer
Output (softmax over vocabulary)
Training objective
Maximise log-likelihood (equivalently, minimise cross-entropy per token):
By construction the softmax output is positive and sums to 1, so the model never produces a zero — smoothing is built into the function class. RNN and Transformer LMs replace the hidden-layer construction but keep the same softmax-over-vocab output and cross-entropy objective.