Learn the MachineDansk

Explained with an interactive demo

What are logits?

When a language model predicts the next token, its last layer does not output probabilities. It outputs one raw score for every token in its vocabulary: the logits. Softmax turns them into probabilities. Move the logits below and see what changes, and what doesn’t.

The demo needs JavaScript. The text below explains what it shows.

Embed this demo on your site

Copy the code into your page. The demo loads from learnthemachine.com, so it stays up to date, and the link under it shows where it comes from.

One number per token

The model’s last layer turns its internal vector for the last position into one number for every token in the vocabulary. GPT-2 has a vocabulary of 50,257 tokens, so it produces 50,257 logits for every position. Each logit is the dot product of the model’s final hidden vector with that token’s own output vector (the unembedding), in some models plus a bias. A logit can be any real number: negative, zero or large.

Softmax: from scores to probabilities

Softmax raises e to the power of each logit and divides by the sum: pi = ezi / Σj ezj. The results are positive and add up to 100%. Because of the exponential, a token whose logit is 1 higher gets e ≈ 2.7 times the probability; 2.5 higher gives about 12 times.

Only the differences between logits matter. Add the same number to every logit and the probabilities stay exactly the same: try the +2 button. A single logit therefore says nothing on its own. “Paris has a logit of 6” only means something compared with the other tokens.

Logit difference

The difference between two logits is the logarithm of the ratio between their probabilities: zParis − zRome = ln(pParis / pRome). In the example the difference is 2.5, so Paris is about 12 times as likely as Rome, whatever happens to the other tokens. That is why interpretability research often measures the logit difference between a right and a wrong answer: it isolates exactly the comparison you care about.

A token’s probability can still rise while its logit falls, if other tokens’ logits fall even more. Softmax always compares all tokens.

Log-probabilities and the loss

Taking the logarithm of softmax gives log-probabilities: each logit minus the same normalising term, the log of the sum. This is what language models are trained on. The loss for one position is cross-entropy: −ln of the probability the model gave the correct token. At 86% the loss is 0.16; if the correct token gets only 1%, the loss is 4.6.

The gradient of that loss with respect to the logits is simple: the probability minus 1 for the correct token, and the probability itself for every other token. A training step therefore pushes the correct token’s logit up and the others down, most of all those that took the most probability. Press “Take one training step” to see it.

Logits and temperature

Temperature divides every logit by the same number before softmax. Unlike adding a number, that does change the probabilities: the differences shrink or grow. Read more in Temperature and top-p.

Logits inside the model: the logit lens

The logit lens applies the model’s unembedding to the hidden vector after an intermediate layer, to see which tokens the model already favours partway through. It is a rough tool: the intermediate layers were not trained to be read that way, and the results vary between models.

Where does the name come from?

In statistics, the logit is the logarithm of the odds, ln(p / (1 − p)), the inverse of the logistic function used in logistic regression. In deep learning the word is used more loosely for the unnormalised scores that go into a softmax (or a sigmoid). With two classes, the difference between the two logits is exactly the log-odds.

Read the logits of a real language model

In the Open language model lab you run Pythia-70M, an open language model, on your own computer, read its logits for “The capital of France is”, and measure how the logit difference between Paris and Rome changes when you move one layer’s activation over from another prompt. The lab requires full access.

See the Open language model labStart free with Tokenization

More explainers