Tokenwerk · The LLM textbook

Chapter 11 · II · From numbers to language · 7 minutes

Language as a probability problem

“The cat sleeps on the …” sofa? roof? sea? A language model doesn't decide; it hands out percentages. Here you learn how raw numbers turn into clean probabilities.

Several continuations can fit

Text is now a series of tokens. The model's job: for the text so far, estimate which token fits next.

After “The cat sleeps on the”, quite a few things would work. We play it through with a tiny vocabulary of three words: “sofa”, “roof” and “sea”. A real model has tens of thousands of candidates, but the principle is the same.

Instead of committing to one answer, the model gives each option a probability: a number between 0 and 1. 0.25 means 25 percent. And because exactly one of the words has to come next, they always add up to 100 percent.

A distribution splits up the cake

Let's say the model gives the sofa 60 percent, the roof 30 and the sea 10. Together, 100 percent. Such a split is called a probability distribution. Picture a cake that gets divided among all the candidates.

This is the model's estimate, not a guarantee. Different training data, different estimate. And “sofa is likely” doesn't mean “the sentence is true”; maybe the cat sleeps on the roof after all. Language models estimate what typically comes next, not what is correct. That, by the way, is the core reason why they sometimes tell convincing falsehoods.

The text so far counts

The percentages of course depend on the text so far. After “We take the boat out to the”, “sea” would have much better odds than after our cat sentence. The text already read is called the context or prefix: whatever is already in front.

Probabilities in shorthand

For “probability of sofa after this text” you write in short: P(sofa∣text so far)P(\text{sofa}\mid\text{text so far}). The P stands for probability. The vertical bar means “given”, or simply “after this context”. It is not a division sign!

From raw value to percentage

At the end, a neural network first spits out ordinary numbers, for example 2, 1 and 0. These raw values are called scores or, at this spot in the network, logits. They may be negative and don't have to add up to 1; think of them as points in a rating.

To turn them into probabilities there is a rule called softmax. It does two things: whoever has more points gets more percent. And at the end, all of them add up to exactly 100 percent.

Softmax on three numbers

For the scores [2,1,0] it takes three steps:

  1. Turn each score into a positive number with the exponential function (explained in a moment). That gives about [7.389; 2.718; 1].
  2. Add these numbers up: 7.389+2.718+1≈11.107.
  3. Divide each number by the sum.
Option Score Positive conversion Divided by the sum
sofa 2 7.389 about 0.665 = 66.5 %
roof 1 2.718 about 0.245 = 24.5 %
sea 0 1 about 0.090 = 9.0 %

And there is the distribution. (Because of rounding, the shown percentages may add up to 99.9 instead of 100; unrounded it fits exactly.)

What does the exponential function do?

The rule from step 1 is called exp. Just treat it like a calculator key: exp(0)=1, exp(1)≈2.718 and exp(2)≈7.389.

Only two properties matter: the result is always positive, even for negative scores. And more in means more out. That is exactly what we need: percentages can't be negative, and a better score should get more percent. Side effect: because exp grows fast, differences get amplified, and the favorite gets a solid lead.

Now read the formula

pi=exp⁡(zi)∑jexp⁡(zj).p_i=\frac{\exp(z_i)}{\sum_j\exp(z_j)}.
Part of the formula In plain words
i Number of the option we are looking at
ziz_i Its raw score, for example the score 2 for sofa
exp⁡(zi)\exp(z_i) The positive conversion of that score
j in the sum sign Running number through all options
∑jexp⁡(zj)\sum_j\exp(z_j) Sum of all positively converted scores
pip_i The probability we want for this one option

In words: “converted value of this one option, divided by the sum of all converted values.” For sofa: 7.389/11.107≈0.665. Exactly the three steps from above.

When one wins, the others lose

If you raise only the score for sofa, its slice of the cake grows. At the same time roof and sea shrink, even though their scores haven't changed at all. Of course: the cake stays the same size, so there is less left for the others.

That is why softmax always answers the question “Which one token comes next?”, not many independent yes/no questions.

Whole sentences: multiply step by step

How likely is a whole sequence? Simple: multiply the probabilities of the individual steps. If the first token gets 0.5 and the second (after the first) 0.2, the two-token sequence has probability 0.5×0.2=0.1.

This is the decisive trick of the whole book: instead of “How good is this whole text?”, the model only ever asks “How well does the next token fit?”, position by position. One huge task becomes many small ones. And that is exactly what you experienced in the guessing game in chapter 1.

The sequence formula

For a sequence of length T you write:

P(x1,…,xT)=∏t=1TP(xt∣x<t).P(x_1,\ldots,x_T)=\prod_{t=1}^{T}P(x_t\mid x_{<t}).

Read step by step: xtx_t is the token at position t, x<tx_{<t} are all tokens before it. The product sign Π means: multiply the conditional probabilities from position 1 to T.

This is called the chain rule of probability. Same name and same idea of putting things together as in backpropagation, but a different rule.

In the experiment

In the experiment you can move the three scores. There is also a temperature slider: each score is divided by this value before softmax. A low temperature makes the distribution spiky; the favorite takes almost everything. A high temperature makes it flat; everyone gets about the same. You may know this from chatbots: higher temperature means more creative, but also wilder answers. More on this in the chapter on generating.

The displayed entropy measures how spread out or concentrated the percentages are: small when almost everything sits on one candidate, large when everything is evenly spread. You don't need the formula.

One more detail for programmers: with very large scores, the code first subtracts the largest score from all of them. That changes nothing about the result but prevents huge intermediate numbers.

Key points

  • A language model outputs a probability for every possible next token. Together they add up to 100 percent.
  • Softmax turns arbitrary raw values (logits) into such probabilities: exp, add up, divide.
  • A whole text is rated token by token. Likely doesn't mean true.