How does a language model predict the next word?
A language model predicts the next word given its context. We'll work through a sentence to see how those predictions let us generate text and assign probabilities to complete word sequences. Then we'll estimate the probabilities from word counts and see why the amount of context matters.
What a language model computes
Let's start with a sentence whose last word is missing:
we sat on the bank of the ___
The preceding words give us the context. Several words could complete the sentence, so a language model assigns a probability to each word in its vocabulary. In this example, "river" has the highest probability, followed by alternatives such as "road" and "lake". Together, the probabilities across the vocabulary sum to 1.
For a next word and context , we write the language model's prediction as . Here, the probability assigned to "river" is:
Many language models are autoregressive: they generate text by predicting each new word from the words already generated. We select a word using the predicted probabilities, add it to the context, and repeat. After selecting "river", our context becomes "we sat on the bank of the river", and we can predict what follows.
Autoregressive large language models such as GPT-3 use this process with tokens, which can be whole words or parts of words [1]. They learn to predict the next token from its context during training and use those predictions to generate text.
From one word to a sentence
We can also use next-word predictions to calculate the probability of a complete word sequence. We begin with the probability of the first word, then multiply by the probability of each following word given its context. For the first two words of our sentence:
Continuing through a sequence of words gives the chain rule of probability:
We can write this more compactly:
In our eight-word sentence, each factor predicts one word using all the words before it as context. The first word has no preceding context; by the time we reach "river", the context contains seven words.
This gives us a way to compare possible word sequences. In statistical speech recognition, we combine a sequence's language-model probability with how well it matches the recorded audio [2]. For similar-sounding phrases such as "recognize speech" and "wreck a nice beach", the language model favors the more common phrase.
Estimating the probabilities
To calculate a sentence probability, we need estimates for each next-word probability. One approach, used by count-based language models, is to count occurrences in a collection of text. We count how often word follows context , then divide by the number of times that context appears:
If "of the" appears 1,000,000 times and is followed by "river" 2,000 times, we estimate:
But our sentence requires the probability of "river" after the full context "we sat on the bank of the". That exact sequence may appear only a few times in the training text, or never at all.
With a vocabulary of 50,000 words, there are possible seven-word contexts. A text collection covers only a small fraction of them. If a context never appears, its count is zero and we can't compute the estimate above.
The Markov assumption
We can make counting more practical by limiting the context to the last few words. Under the Markov assumption, we approximate the next-word probability using this shorter context. Keeping just the last word gives:
For our example, we estimate the probability of "river" after "the" rather than after all seven preceding words:
Using one preceding word gives us a bigram model, which counts word pairs. Using two gives us a trigram model; for "river", its context would be "of the".
Substituting the bigram approximation into the chain rule gives:
We now need counts of individual words and word pairs, so common contexts provide many more examples. When predicting "river", though, a bigram model sees only "the"; the clue in "bank" is lost.
Keeping "bank" in the context lets us use that clue, but leaves us with fewer matching examples to count. For a count-based language model, more context is useful only when we have enough training text to estimate the probabilities reliably.
References
- GPT-3: Brown, T. B., et al. (2020). Language Models are Few-Shot Learners. NeurIPS 2020. arXiv:2005.14165 ↩
- Statistical speech recognition: Bahl, L. R., Jelinek, F., & Mercer, R. L. (1983). A Maximum Likelihood Approach to Continuous Speech Recognition. IEEE Transactions on Pattern Analysis and Machine Intelligence 5(2). doi:10.1109/TPAMI.1983.4767370 ↩
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.