Introduction
Strip away the scale and a large language model does one thing: given the tokens so far, it predicts the next one. Writing an essay is just doing that again and again, feeding each new token back in. This lesson looks at that single step closely: the scores a model produces, how they become probabilities, how training measures mistakes, and how to tell a good language model from a poor one.
How it works
For the current context, the model outputs a logit for every token in its vocabulary. Logits can be any numbers; softmax turns them into probabilities:
P(token i) = e^(zᵢ) / Σⱼ e^(zⱼ)
Higher logits get higher probabilities, all are positive and they add up to 1. Subtracting the largest logit from all of them first does not change the result, but stops e^z overflowing for big scores.
Training compares the prediction with the token that actually came next. The cross-entropy loss is
loss = −ln P(actual next token)
If the model gave the right token probability 1, the loss is 0; probability 0.5 costs 0.69; probability 0.01 costs 4.6. Training adjusts the model's weights to lower the average loss over billions of positions in its training text. To predict well, the model is forced to learn spelling, grammar, facts and reasoning patterns, because all of them help guess what comes next.
Perplexity summarizes the average loss: perplexity = e^(average loss). A perplexity of 2 means the model is, on average, as uncertain as choosing between 2 equally likely tokens; a perfect model would have perplexity 1. It is the standard measure of a language model on held-out text.
Generation is autoregressive: predict a distribution, choose a token (next lesson), append it to the context, and predict again.
Example
import math
vocab = ["cat", "dog", "sat", "the", "."]
# A made-up model's scores (logits) for the next token after each word
logits = {
"the": [2.0, 1.5, -1.0, -2.0, -1.0],
"cat": [-1.0, -1.5, 2.5, -0.5, 0.5],
"sat": [-2.0, -2.0, -2.0, 0.5, 1.5],
}
def softmax(scores):
top = max(scores)
exps = [round(math.exp(s - top), 12) for s in scores] # rounded, so every computer agrees
total = sum(exps)
return [e / total for e in exps]
print("after 'the':", {w: round(p, 3) for w, p in zip(vocab, softmax(logits["the"]))})
sentence = ["the", "cat", "sat", "."]
losses = []
for current, actual in zip(sentence, sentence[1:]):
p = softmax(logits[current])[vocab.index(actual)]
losses.append(round(-math.log(p), 12))
print(f"P({actual!r} | {current!r}) = {p:.3f}, loss {losses[-1]:.3f}")
average = sum(losses) / len(losses)
print(f"average loss {average:.3f}, perplexity {round(math.exp(average), 12):.2f}")Line by line
logitsstands in for a trained model: for each current word, one score per vocabulary token.softmaxsubtracts the largest score, exponentiates and divides by the total.- After "the", the model expects "cat" or "dog" much more than anything else.
- For each step of the sentence, the loss is −ln of the probability given to the word that really came next.
- The average loss and the perplexity summarize how well this model predicts the whole sentence.
Expected result
after 'the': {'cat': 0.58, 'dog': 0.352, 'sat': 0.029, 'the': 0.011, '.': 0.029}
P('cat' | 'the') = 0.580, loss 0.545
P('sat' | 'cat') = 0.811, loss 0.210
P('.' | 'sat') = 0.686, loss 0.377
average loss 0.377, perplexity 1.46
In the real world
Pretraining a large language model is exactly this loop on trillions of tokens: compute logits for every position, compute the cross-entropy loss, and adjust billions of weights by gradient descent. Model reports quote the loss or perplexity on held-out text, and the probabilities themselves are used to rank autocomplete suggestions, to detect unusual text and to estimate how confident a model is.
Common mistakes
- Treating logits as probabilities. They must go through softmax first.
- Computing e^z on large scores without subtracting the maximum, which overflows.
- Comparing perplexities across different tokenizers; they are only comparable with the same vocabulary.
- Reading a low loss as truthfulness. It measures how well the model predicts text, not whether the text is true.
Best practices
- Always use the numerically stable softmax.
- Evaluate loss and perplexity on text the model was not trained on.
- Look at the probabilities of the top few tokens to see how confident a prediction is.
Summary
A language model outputs a logit for every token, and softmax turns logits into next-token probabilities. Training minimizes the cross-entropy loss, −ln of the probability of the true next token, over huge amounts of text. Perplexity, e to the average loss, measures how surprised a model is by real text. Generation repeats the prediction step autoregressively.
Quick check
Key takeaways
- A language model outputs one logit per vocabulary token; softmax turns them into next-token probabilities.
- Training lowers the cross-entropy loss: −ln P(actual next token).
- Lower perplexity means the model is less surprised by real text.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.