Chances between 0 and 1
A probability is a number from 0 to 1 that says how likely something is. 0 means it never happens, 1 means it always does, 0.25 means one time in four. A list of probabilities for every possible outcome, adding up to exactly 1, is a probability distribution.
This is what a language model produces at each step: a distribution over every token in its vocabulary, often more than 100,000 of them. For a prompt like “The capital of Pakistan is”, the distribution might look like this (illustrative numbers):
All the chances add up to 1. A real model spreads the last 2 % over more than 100,000 other tokens.
To sample from a distribution is to pick one outcome at random, with each outcome’s chance of being picked equal to its probability. Think of a spinning wheel where each token’s slice is as wide as its probability. Spin it, and “Islamabad” comes up 91 times in 100. That is how a chat model chooses its words.
Which of these lists could be a probability distribution?
-
[0.5, 0.5]Answer: Yes
-
[0.7, 0.2, 0.1]Answer: Yes
-
[0.6, 0.6]: adds up to 1.2Answer: No
-
[1.2, −0.2]: one value is negativeAnswer: No
-
[0.25, 0.25, 0.25, 0.25]Answer: Yes
-
[3.1, −1.2, 0.4]: a model’s raw scoresAnswer: No
From scores to chances: softmax
The last layer of a model does not output probabilities directly. It outputs one raw score per token, called a logit. Logits can be any number: 7.3, −2, 0.01. To turn them into a proper distribution, models use a function called softmax.
p(token) = e^score ÷ (sum of e^score over all tokens)Raise e (about 2.718) to the power of each score, then divide each result by the total.
scores [2.0, 1.0, 0.1]Three tokens, say “blue”, “grey” and “green”.
e^scores = [7.39, 2.72, 1.11], total 11.21Every result is positive, whatever the score was.
chances = [0.66, 0.24, 0.10]Divide by the total. They now add up to 1.
Why the strange e to the power? Three good reasons. It makes every chance positive, even for negative scores. It turns differences into ratios: a token that scores 1 point higher is always about 2.7 times as likely, whatever the other scores are. And it keeps the order: the highest score always gets the highest chance.
Quick check
Temperature: confident or adventurous
Before the softmax, the scores can be divided by a number called the temperature. It changes how sharp the distribution is, without changing which token is the favourite.
| Temperature | Scores after dividing | Chances |
|---|---|---|
| 0.5 (cooler) | [4. | [0. |
| 1 (as trained) | [2. | [0. |
| 2 (hotter) | [1. | [0. |
As the temperature falls towards 0, the top token’s chance approaches 1: the model always picks its favourite. This is called greedy decoding.
The same idea, with a real-looking prompt. Lower the temperature and the favourite wins almost every time. Raise it and unlikely words start appearing.
This interactive part needs JavaScript.
Low temperatures suit tasks with one right answer, such as maths, code and facts. Higher temperatures suit brainstorming and stories, where variety helps. Reasoning models are usually not run at 0: their makers recommend temperatures from about 0.6 to 1, because a little variety helps long chains of thought. DeepSeek’s coding-agent results, for example, were measured at temperature 1.0. The full set of controls (top-k, top-p, min-p) has its own lesson in part 6.
Surprise: how a model is graded
Now the key to how language models learn. During training, the model reads real text and, at each position, gives a probability to the token that actually comes next. If it gave that token a high chance, it did well. If it gave it a tiny chance, it was surprised, and that surprise is the loss.
surprise = −log(probability of the right token)log is the natural logarithm. The minus sign makes the result positive.
| Chance given to the right token | Surprise |
|---|---|
| 1 | 0 |
| 0. | 0. |
| 0. | 1. |
| 0. | 2. |
| 0. | 4. |
| 0. | 6. |
Every time the chance falls tenfold, the surprise rises by the same 2.30. Being confidently wrong is punished hard.
The average surprise over all the tokens in the training text is the cross-entropy loss, the number every pretraining run pushes down. Minimising it means one thing: give the text that really exists as high a probability as possible. Everything a model knows, it learned in the service of being less surprised.
Quick check
Reading probability in papers
One more piece of notation, and you can read the opening equation of most language-model papers.
p(x_t | x_<t)“The probability of token t, given all the tokens before it.” The bar | reads as “given”. This is what a language model computes.
p(text) = p(x₁) × p(x₂ | x₁) × p(x₃ | x₁, x₂) × …The chance of a whole text is the chance of each token given the ones before, multiplied together.
loss = −(1/N) × Σ log p(x_t | x_<t)Take logs and the product becomes a sum; Σ means “add up over all N tokens”. This is the cross-entropy loss from the last section.
Remember
- A probability distribution gives every outcome a chance from 0 to 1, adding up to 1. A language model outputs one over its whole vocabulary at every step.
- The model’s raw scores are logits. Softmax (e^score ÷ total) turns them into chances.
- Temperature divides the scores first: lower is more predictable, higher is more varied, near 0 is greedy.
- The training loss is cross-entropy: the average surprise, −log p, of the real next token. Perplexity = e^loss.
- Papers write
p(x_t | x_<t): the chance of the next token given everything before it.
Words to know
- Probability
- A number from 0 to 1 saying how likely something is.
- Distribution
- Chances for every possible outcome, adding up to 1.
- Sampling
- Picking an outcome at random, each with the chance its probability gives it.
- Logit
- A model’s raw score for a token, before softmax. Any number, positive or negative.
- Softmax
- The function that turns scores into a probability distribution: e^score divided by the total.
- Temperature
- A number the scores are divided by before softmax. Low is predictable, high is adventurous.
- Greedy decoding
- Always picking the single most likely token, as if the temperature were 0.
- Cross-entropy loss
- The average surprise of a model at the real next tokens. Pretraining pushes it down.
- Perplexity
- e to the power of the loss: how many equally likely choices the model is as unsure as.
- Calibration
- How well a model’s confidence matches how often it is actually right.
Go to the source
This lesson is built on these papers and documents. Paper links open arxiv.org, a separate site.
-
A Mathematical Theory of Communication (C. E. Shannon)
Where surprise and entropy come from. Section 3 even generates fake English by sampling the next letter and the next word from probabilities: a language model, decades early.
-
Distilling the Knowledge in a Neural Network
Uses softmax temperature to let a small model learn from a big one. Short and clear.
-
Self-Consistency Improves Chain of Thought Reasoning in Language Models
Sample many answers, take a vote. The simplest way to trade more computing for better answers.
-
On Calibration of Modern Neural Networks
Shows that modern networks are often overconfident, and that a single temperature can fix much of it.
Check yourself
5 questions. Tap the answer you think is right.
0 of 5 answered
Lesson complete
Answer every question above to complete this lesson.
Your progress is saved on this device only.
Next lessonBits and floats: how computers store numbers