Keep counting
As you eat your half-burned pancakes one morning, you realize that building a better language model is not so different from cooking. If you want a better breakfast, there are three things you can improve: the quality of your ingredients, your skill (how much time you have spent over your recipes, trying them out), and your equipment. They go hand in hand: the best flour in the world means nothing if you have no idea what to do with it, or if all you have is a rusty pan.
The same three things decide how good a language model is. The ingredients are similar to the training data, that is, the web pages we read in the previous chapter to count pairs of symbols. The equipment is similar to the size of the model: roughly, how many numbers we need to estimate to build the model (Figure 1.9). Finally, the skill is how much time we spend studying the data.1 Making all three bigger is called scaling the model, and as we will see, it is how AI got where it is today.
2.1 Scaling our models
Let’s try improving our data first. Since we only collected a small slice of the alien web, it’s conceivable we may have been unlucky in the pages we picked. Imagine the same happening on our own web: if all the pages we collected came from an archive of Shakespeare’s plays, the resulting model would not work very well on today’s news sites!
Figure 2.1 The notebook page for may, counted in Shakespeare’s plays and in news stories from 2020 to 2026 (with dates written out in words).
Look at Figure 2.1:2 in Shakespeare, may is almost always a verb (“it may be so”, “may not”, “I may”), and many different words can follow it. be is the likeliest word, appearing in the texts we looked at a little more than one time in ten. In today’s news, may is instead mostly a month, and more than four times in ten it is followed by a date, “May twenty-first”. If we fill a notebook with Shakespeare’s plays and then use it to predict the next word in a news article, we will do a poor job.
We can check whether in the previous chapter we were unlucky by spending a few more hours browsing other corners of the web and collecting new pairs of symbols for our training data. If the pages we started from were unusual, the probabilities in the notebook should change noticeably as new pages come in; if they were enough, the probabilities should stay put.
Figure 2.2 shows what happens to a few selected symbols as we gather more pages. We consider three examples: how often t is followed by h, how often e ends a word, and how often v is followed by e. Over the first few pages the numbers jump around, especially for v, a symbol we meet only a few times on each page. After about ten pages the two most common pairs move by a point or two at most; the rare one needs a few dozen pages, and then it does not move anymore. We say that the model has settled, or converged: from there on, collecting more data does not improve it. On web pages it has never seen, it guesses the next symbol just as well after 150 pages of reading as after 40.
Figure 2.2 Three cells of the notebook as you read more pages of the alien web. Each line is the probability of one pair of symbols; the training pages grow tenfold at each step along the bottom.
With a small model, then, collecting more data does not benefit us. This was (very roughly) the problem of the earliest language models: computers had little memory and little power, so models had to stay small, and a small model gains little from more data.
2.2 It is a matter of context
In the terminology of language models, the number of symbols the model considers to make a prediction is called the context of the model: our model only looks at a single symbol for a prediction, so it has a context of 1. In order to increase the context, we can consider longer combinations of symbols. For example, we could count all possible triplets of symbols in the text, and then build probabilities for rules such as “after t and h, the next symbol is e with probability 61%” (Figure 2.3). The resulting table in our case would have 27 × 27 × 27 = 19,683 cells: one page for each of the 729 pairs of symbols, and on each page a line for each of the 27 symbols that can follow. This is the trigram (or 3-gram) model we met in the last chapter: it counts three symbols in total, so it looks two symbols back (a context of 2).
Nineteen thousand cells is well past what fits in your pocket notebook; with a hint of sadness you decide to close it and move to the ship’s computer to keep track of the counts instead.3
Figure 2.3 The page for h in the notebook from the last chapter, and the page for t followed by h in the 3-gram model we build now. Knowing one more symbol makes the model better at predicting what comes next.
To check whether the model is indeed better, we can take one of the web pages we used to compute the probabilities. For each position in the text, we use our language model to decide which symbol comes next, and we compare the answer to the real one. The fraction of times the model is correct is called the accuracy of the model.4
Evaluating a language model on the same pages we have used to compute its probabilities will give us an overoptimistic answer, as we show below. For this reason, we generally use an independent set of web pages that were hidden while training. We call this procedure testing the language model. Figure 2.4 evaluates the two models we built with this kind of test. The model from the last chapter guesses the next symbol right about three times in ten, and it does not really matter which pages we test it on: on its training data or on hidden pages, after one page or after a hundred, the answer stays close to 30%. Remember this model required only ten pages to settle, as we saw in Figure 2.2.
Figure 2.4 How often the 2-gram and 3-gram models guess the next symbol right, based on how many pages we have trained them on: on their training data itself (dashed) and on pages initially kept hidden (solid).
The 3-gram model does better, about four times in ten, but only once we have processed enough data. After a single web page it looks brilliant on that very page, right half of the time, while on hidden pages it manages only three times in ten. This is unsurprising: we have 19,683 possible combinations to evaluate and only one web page to fill them with, meaning that most triplets will never appear. Each new web page we add to our training data improves the results a bit: on hidden pages the model stops improving after about twenty-five, and after a hundred the two lines almost meet.
2.3 Never stop scaling
A model with a slightly larger context is clearly better at generating text. But the number of values we need to estimate (what we will call later the parameters of the model) also grows quickly, and so does the amount of data we need to collect and the time we spend counting it. For example, we can keep increasing our context by looking at quadruplets of symbols and build what we call a 4-gram model. Since we need to count all potential combinations of four symbols, however, the number of parameters and the amount of data grow rather quickly. If we decide to go on and build 5-gram, 6-gram, or even 7-gram models, the number of combinations soon becomes unmanageable.
To see how far this goes, the days turn into weeks as you leave the ship’s computer collecting pages from the alien web, gathering about 17,600 of them while you pace the cabin and wait. With all that text, we fill models that look up to six symbols back, from 2-gram up to 7-gram (Figure 2.5). For every new symbol we add to the context the accuracy gets higher, but it takes many more pages to get there: 3-grams settle after about twenty-five pages, 4-grams after about three hundred, and the 6-gram and 7-gram models never settle in this experiment. The tables also stay mostly empty: the one for quadruplets has room for over half a million cells, and only about one in ten gets a count.
Figure 2.5 How often our six n-gram models (looking 1 to 6 symbols back, from 2-gram to 7-gram) guess the next symbol right on hidden pages, after being trained on up to 17,600 pages. A dot marks where each of the first four settles; the 6-gram and 7-gram models are still climbing.
2.4 Scaling laws
Despite having so many numbers to estimate, our counting models are cheap, in the sense that a computer fills one in a few minutes. Back on Earth, existing language models are not: training one of the largest takes months on thousands of computers and costs tens or hundreds of millions of dollars. Before building the next one, its builders want to know how good it will be and how much it will cost. To estimate this, scientists build a few smaller models, measure them, and look for a pattern they can extend to the big one they have not built yet.
Let’s try the same with our six models. For each one we write down three numbers: its size, that is, how many cells it fills (its parameters); how many mistakes it makes on hidden pages; and how many pages we need before it settles. Plotting these numbers runs into a problem: the number of parameters in our models ranges from about six hundred filled cells to almost two million. On an ordinary scale, where every step along the axis adds the same amount, the three smallest models are squeezed together near zero (Figure 2.6, top). We solve this with what is known as a logarithmic scale, where every step along the axis multiplies by ten: 100, 1,000, 10,000, and so on (Figure 2.6, bottom). We have already used one, along the bottom of Figure 2.2.
Figure 2.6 The sizes of our six models (the number of combinations of symbols each model actually records), on an ordinary scale and on a logarithmic one. Each dot is labelled with its n-gram size (from 2-gram to 7-gram).
When we plot both axes on logarithmic scales (Figure 2.7), the models cluster neatly along straight lines. Such a straight line has a useful meaning: every tenfold increase in size cuts the mistakes by the same fraction, irrespective of the size of the model. This suggests that we can extend the line to forecast bigger models before building them (see the box “Straight lines on stretched axes”). A line fitted to measurements like these is called a scaling law: it tells us roughly how much better, and how much more costly, our current model would be if we scaled it up.
A chart where both axes use logarithmic scales is called a log-log plot. Whenever a curve turns into a straight line on a log-log plot, the two quantities follow a power law: one grows as a fixed power of the other (squaring a number is raising it to the power 2), and the slope of the line tells you that power. Multiplying the horizontal axis by ten always multiplies the vertical axis by the same fixed factor, regardless of where you start along the line.
Figure 2.7 A scaling law for our models: their mistakes on hidden pages (left) and the training pages they need before settling (right), against the combinations of symbols each model records (its parameters), on logarithmic scales. The lines are fitted on the four smallest models (filled dots, 2-gram to 5-gram) and extended, dashed, to the two biggest (6-gram and 7-gram).
To show how this works, we can draw the lines through the four smallest models we have and compare the results for the two largest ones. The good news is that the predictions for their mistakes come within about a point of the real ones, meaning that the scaling law works well here. The bad news is that, at this rate, a tenfold increase in parameters removes only about a sixth of the remaining mistakes, and costs more than ten times as many pages. If we aim at being right nine times out of ten, it would take about twelve million years of reading, at a page a minute.
For reference, existing AI models at the time of writing reach a context of millions of symbols! Evidently, just counting will not take us much further. As we will see, neural networks can do better because, instead of keeping a count for every possible combination of symbols, they learn a much smaller set of numbers that also works for combinations they have never seen.
2.5 Before closing
Before moving on from counting models, you return to the driver’s question (Figure 2.8) and test our strongest one so far. Translated into plain English, always taking the most probable symbol gives “of the street and the street and the street”; sampling at random according to the probabilities gives “on the walked before i saw a chair with uneasiness will be to”. This is also unsurprising: six symbols of text are barely a word; of the whole question, the model only sees “pdate” and the space after it. Nobody answers yet.
Figure 2.8 Three weeks later, we try again with our bigger language model: first always taking the most probable symbol, then sampling at random.
That evening, the ship’s computer, which has been testing the air outside since the crash, finally prints its result: breathable. You read the message three times before believing it. Tomorrow you will open the door and take your first walk outside.
- 1951 Shannon asks people to guess a text one letter at a time. The more of it they have read, the better they guess.
- 2007 Google creates n-gram models with up to 300 billion observed word sequences (up to five words long) to translate between languages. The paper’s title: “Large language models in machine translation”.
This last element will be essential when we move to neural networks in the next chapter, but for the counting models we use for now it has no impact, since counting the same pages twice gives exactly the same probabilities.↩︎
We use words instead of letters as it makes the example clearer. As always, data sources for our experiments are listed in the web edition on the Sources page.↩︎
Somehow, ten minutes later you already have an Excel spreadsheet called
Alien_.↩︎language_ model_ v3_ final_ final_ draft.xlsx Computer scientists prefer a related number, the perplexity of the model: roughly, how many symbols the model is hesitating between at each step, so lower is better. It is computed from Shannon’s entropy, the equation in the box of the introduction, as it measures how much the model is perplexed by the text. Using perplexity instead of accuracy would lead to the same conclusions in this chapter, so for now we stick to accuracy.↩︎
Sources
Passages cited in this chapter.
Chapter 3: Moving points is coming soon. Meanwhile, explore the alien web to browse the pages and try cracking the cipher yourself.