§12.4 Natural language processing
Natural language processing (NLP) is the use of human languages by a computer — machine translation, question answering, and more. Most NLP rests on language models: a language model language model A model defining a probability distribution over sequences of tokens (words, characters, or bytes) in a natural language. defined in ch. 12 — open in glossary defines a probability distribution over sequences of tokens (words, characters, or bytes). Generic neural techniques apply, but scale demands specialization for sequential data — and, because the vocabulary is huge, word-based models must operate on an extremely high-dimensional, sparse discrete space.
§12.4.1 n-grams
The earliest successful language models are n-grams n-gram A fixed-length sequence of n tokens. An n-gram language model defines P(sequence) as a product of conditionals P(xₜ | previous n−1 tokens), estimated by counting; unigram/bigram/trigram for n=1/2/3. defined in ch. 12 — open in glossary — a fixed-length sequence of tokens. An n-gram model defines the probability of a sequence as a product of conditionals, each on the previous tokens:
The n-gram decomposition (eq 12.5)
| chain rule of probability: the sequence probability is a product of one-step predictions | product | |
| each token conditions on ONLY the previous n−1 tokens — the n-gram (Markov) assumption | conditional | |
| the opening tokens, handled by a smaller-n model | boundary |
Training is just counting. Usually you keep both an -gram and an -gram model, so a conditional is a ratio of two stored counts — e.g. for the trigram “THE DOG RAN AWAY”:
But counting is brittle. Pick a context and watch what happens when it’s unseen — then turn on smoothing:
Maximum-likelihood counts: only words seen after "the cat" get probability (pink); every other word gets exactly 0. On a huge vocabulary most n-grams are unseen, so raw counts are brittle — hence smoothing.
The zero-count catastrophe — an unseen tuple giving zero (or undefined) probability — forces every n-gram model to use smoothing smoothing (language models) Techniques that shift probability mass from observed to unobserved-but-similar token tuples, so an n-gram model does not assign zero probability (and −∞ log-likelihood) to sequences unseen in training; includes add-mass, back-off, and mixtures. defined in ch. 12 — open in glossary (add-mass, back-off to lower-order models, or mixtures). Deeper still, n-grams suffer the curse of dimensionality: there are possible n-grams, so most never occur in training. An n-gram model is really a local, nearest-neighbor predictor (§5.11.2) — and in one-hot space every pair of words is equidistant, so only literal repeats of a context help. Class-based models cluster words to share strength, but lose much information.
§12.4.2 Neural language models
Neural language models neural language model A language model that overcomes the curse of dimensionality by learning distributed word embeddings, letting it share statistical strength between a word and other similar words (Bengio 2001). defined in ch. 12 — open in glossary beat the curse of dimensionality with a distributed representation — a word embedding word embedding A learned distributed representation that maps each word (originally a one-hot vector, all words equidistant) to a point in a lower-dimensional feature space where words appearing in similar contexts are close — enabling generalization across semantically similar words. defined in ch. 12 — open in glossary for each word. Unlike a class-based model, they recognize that two words are similar without collapsing them into one class. Click a word to see its neighbors, and toggle to the one-hot baseline to feel the difference:
Nearest neighbours of cat: dog, kitten, puppy — all in the same semantic neighbourhood. Because cat and its neighbours share learned features, a sentence about one informs predictions about the others, letting the model generalize to exponentially many similar sentences.
Why embeddings generalize exponentially
If dog and cat map to representations sharing many attributes, then a sentence containing cat informs the model’s predictions for sentences containing dog, and vice versa. Because there are many such shared attributes, each training sentence transfers information to an exponentially large number of semantically related sentences — directly countering the curse of dimensionality, which demands generalizing to a number of sentences exponential in their length. (Other domains have embeddings too — a CNN’s hidden layer is an “image embedding” — but NLP practitioners prize them most, because language doesn’t start in a real-valued vector space.)
Next: the flip side of a large vocabulary — the expensive softmax output layer, and the tricks that tame it — high-dimensional outputs and combining with n-grams.
Check yourself — n-grams and neural language models
1.What is a language model?
2.How does an n-gram language model work?
3.In the NGramLab, choosing an unseen context gives an undefined distribution. What is this problem and how is it addressed?
4.Why are classical n-gram models vulnerable to the curse of dimensionality?
5.How do neural language models overcome the curse of dimensionality? (See the WordEmbeddingLab.)
6.Why does a distributed (embedded) representation generalize so well?