Neural language models
A language model predicts the next word from its context , the words that came before it. Counting models estimate by looking up how often followed that exact context, and every word is a separate symbol with nothing in common with any other word. A neural language model replaces the count table with a feed-forward network that reads each context word as a vector of learned numbers. We'll follow one prediction through such a network, the word after "bank of the" in the sentence "we sat on the bank of the river", and see why words used in similar ways end up sharing what the model learns.
Counts don't transfer between contexts
An n-gram model takes the context to be the last words. With three context words, it estimates the probability of "river" by counting:
Each exact context gets its own row of counts. If the training text contains "bank of the river" but never "shore of the river", the model has no count for the second context, even though "shore" and "bank" are used in nearly the same way. Smoothing and backoff give the unseen context some probability by falling back to shorter contexts, such as "of the", which throws away the most informative word.
The space of contexts is also far too large to fill. With a vocabulary of 100,000 words there are possible sequences of ten words [1], so almost every long context in new text is one the training data never contained.
Each word becomes a learned vector
The neural language model of Bengio and colleagues [1] gives every word in the vocabulary a vector of real numbers, its word embedding. The embeddings are the rows of a matrix with rows and columns, and reading a word means looking up its row . If is the one-hot vector with a 1 in the position of word , the lookup is an ordinary matrix product:
So the embedding table is simply the first layer of the network, a linear layer whose weights are the word vectors. The rows start out random. Nothing about "bank" is written in by hand; its numbers come only from training. Representing a concept as a pattern over many learned features, rather than as one symbol, is called a distributed representation [2].
The context becomes one input vector
For the context we look up three rows and place them side by side:
With numbers per word, has numbers. Each position keeps its own slot, so the network still knows which word came first. The same matrix supplies every position, so "the" has the same vector wherever it appears.
A hidden layer combines the context
The input vector goes through one hidden layer with the hyperbolic tangent as its nonlinearity:
has the same S shape as the sigmoid but outputs values between and . Because sees all three word vectors at once, a hidden unit can respond to combinations, such as a noun followed by "of the", that no single word signals on its own.
Softmax scores every word in the vocabulary
The output layer produces one score per vocabulary word, and softmax turns the scores into the next-word distribution:
has rows, so has entries. To see the arithmetic, we can shrink the vocabulary to five words. With scores for river, for stream, for lake, for road and for sat, softmax gives river , stream , lake , road and sat . The five probabilities sum to 1, and the ordering of the scores is kept.
Training moves the embeddings too
The model is trained on running text. At each position the correct next word is known, and the loss is its negative log-probability. For our example, where "river" received :
Backpropagation sends the gradient of through and as in any feed-forward network, and then one step further, into . Since is just three rows of , those three rows get updated as well. The embeddings, the hidden layer and the output layer are all fitted together to make the training text likely.
Similar words end up with similar vectors
Suppose "bank" and "shore" both show up before words like "of the river" and "of the lake" in the training text. Every time, the gradient nudges each word's vector toward values that make those same continuations likely, so the two vectors drift toward each other. Words that appear in similar contexts end up with similar embeddings.
Now take the context "shore of the", which may never have appeared in training. Its input vector is close to the one for "bank of the", so the hidden layer responds in nearly the same way and "river" again gets a high probability. The count table had no row for this context. The network generalizes from one context to another through the similarity of their word vectors.
The network also needs far fewer numbers than the space of contexts. Its parameters are the embeddings, the hidden weights and biases, and the output weights and biases:
With , , three context words and hidden units, that is parameters. A table of 4-grams over the same vocabulary has possible entries. Adding a fourth context word adds weights to , rather than multiplying the number of contexts by .
Better predictions at a high cost
The 2003 experiments measured perplexity, the geometric mean of over the words of the test text, where lower means the model was less surprised. On the Brown corpus, about 1.2 million words of varied English, the best n-gram model scored 312. That model was class-based: it grouped words into 500 classes and shared counts within each class [3], an earlier way of letting similar words pool statistics. The networks on their own reached 268, and averaging a network's probabilities with a simple trigram model's brought the test perplexity down to 252. On the larger AP News corpus, about 14 million words, the network's advantage was about 8%. On Brown, extending the context from two words to four helped the network but not the n-gram models.
The cost sits in the output layer. Every prediction computes a score for every word in the vocabulary before softmax can normalize, and every training example sends gradients back through all of those scores. On AP News, five passes over the data took about three weeks on 40 CPUs [1].
The window is still fixed
The network reads exactly words. Predicting "river" from "bank of the" ignores "we sat on", the words that tell us this is a bank someone can sit on. A longer window adds another block of weights to for every extra position and still cuts the sentence off somewhere, and the embeddings, the part that transfers between contexts, come out only as a by-product of training this slow full-vocabulary predictor.
References
- Bengio, Y., et al. (2003). A neural probabilistic language model. Journal of Machine Learning Research 3. jmlr.org ↩
- Hinton, G. E. (1986). Learning distributed representations of concepts. Proceedings of the Eighth Annual Conference of the Cognitive Science Society. ↩
- Brown, P. F., et al. (1992). Class-based n-gram models of natural language. Computational Linguistics 18(4). aclanthology.org ↩
Want to keep up with AI? Follow us on X (opens in a new tab) and Instagram (opens in a new tab), or check out our shirts.