Skip to content
AI360XpertBeta

The Logit Lens

Peek at a model's half-finished thinking. Run any middle layer through its final word decoder and read which word is winning so far.

The logit lens reuses the final word decoder on a middle layer, turning one hidden state into readable next-word odds that sharpen layer by layer
The logit lens reuses the final word decoder on a middle layer, turning one hidden state into readable next-word odds that sharpen layer by layer

Why Does This Exist?

A transformer with 32 layers makes you wait until layer 32 to hear its answer. Everything before that is a stack of vectors no human can read, so when the model gets a fact wrong you cannot tell whether it never knew the answer or lost it late. Probing classifiers can translate those middle layers, but each probe needs its own training, its own data, and its own caveats.

The logit lens skips all that with a trick hiding in plain sight. The model's final step is a decoder that turns the last layer's vector into word scores, and nothing stops you from pointing that same decoder at layer 8 or layer 14 instead. One matrix you already have, applied early, gives you a running transcript of the model's best guess at every depth.

This page covers that readout: the one equation, a hand-computed example, and where the picture lies to you. The training objective that produces those scores lives in next-token prediction. When you want proof of which layer caused an answer instead of just watching, see activation patching.

The one idea you need first. A logit is an unnormalized score, one per word in the vocabulary, before probabilities enter the picture. If three words score [2,1,0][2, 1, 0], softmax turns them into shares of 1, roughly [0.67,0.24,0.09][0.67, 0.24, 0.09]. The lens is just this scoring step, replayed before the final layer.

Think of It Like This

Tasting the soup before it is done

You are cooking a soup and you keep stealing spoonfuls as it simmers. The first taste is mostly raw onion. Halfway through, tomato takes the lead. By the end, basil wins and stays winning. Each spoonful is the same act, tasting, applied to an unfinished pot, and the sequence tells you when the dish made up its mind.

The pot is the hidden state at some layer. The spoon is the final decoder, borrowed early. The taste is the current frontrunner word.

Where it stops: soup is meant to be tasted, but hidden states were never meant to meet that decoder early. A spoonful of half-cooked flour tastes like nothing much, and a middle layer can read as gibberish for the same reason. That says nothing about the model being lost. It says you used the spoon before its time.

How It Actually Works

A transformer carries one vector per position through its layers, widening nothing and narrowing nothing: every layer's hidden state has the same width dd. Call the state at layer ll by the name hlh_l, a list of dd numbers. Call the model's final decoder the unembedding matrix WUW_U, a table with one row per vocabulary word, each row dd numbers long, learned to translate vectors into word scores. Because every hlh_l has width dd, WUW_U fits every layer, not just the last.

The lens is a single replay of the final step. Take hlh_l, run it through the model's final normalization (the same LayerNorm or RMSNorm the last layer uses, with its learned weights), multiply by WUW_U, and turn the scores into probabilities:

zl=WU norm(hl),pl=softmax(zl)z_l = W_U \, \mathrm{norm}(h_l), \qquad p_l = \mathrm{softmax}(z_l)

In plain words: zlz_l (the logits at layer ll) holds one score per word, how strongly layer ll votes for each word, and plp_l turns those scores into shares of 1 so layers can be compared. The usual pattern is refinement, not decision: some middle layer already ranks the right word first, and the remaining layers widen its lead while they polish the wording around it.

Worked example

Vocabulary of three words, hidden width of two numbers, small enough to check by hand. Let the unembedding rows be Paris [2,−1][2, -1], London [1,1][1, 1], Apple [−1,2][-1, 2]. At layer 8 the hidden state is h8=[1.0,0.5]h_8 = [1.0, 0.5]. To keep the arithmetic visible the normalization here is the identity; the pitfalls section covers what breaks when you skip the real one.

Score each word with a dot product, row against hidden state:

  • Paris: 2×1.0+(−1)×0.5=1.52 \times 1.0 + (-1) \times 0.5 = 1.5
  • London: 1×1.0+1×0.5=1.51 \times 1.0 + 1 \times 0.5 = 1.5
  • Apple: (−1)×1.0+2×0.5=0.0(-1) \times 1.0 + 2 \times 0.5 = 0.0

So z8=[1.5,1.5,0.0]z_8 = [1.5, 1.5, 0.0]. Softmax exponentiates and divides by the total: e1.5≈4.48e^{1.5} \approx 4.48 twice, e0=1e^{0} = 1, total ≈9.96\approx 9.96. That gives p8≈[0.45,0.45,0.10]p_8 \approx [0.45, 0.45, 0.10]. Paris and London tie. The model knows the answer is a capital but has not picked between them.

At layer 14 the state has moved to h14=[1.5,0.0]h_{14} = [1.5, 0.0]:

  • Paris: 2×1.5+(−1)×0.0=3.02 \times 1.5 + (-1) \times 0.0 = 3.0
  • London: 1×1.5+1×0.0=1.51 \times 1.5 + 1 \times 0.0 = 1.5
  • Apple: (−1)×1.5+2×0.0=−1.5(-1) \times 1.5 + 2 \times 0.0 = -1.5

Now z14=[3.0,1.5,−1.5]z_{14} = [3.0, 1.5, -1.5]. Subtract the max (3.0) before exponentiating, the standard safety shift: [0,−1.5,−4.5][0, -1.5, -4.5] becomes [1,0.223,0.011][1, 0.223, 0.011], total ≈1.234\approx 1.234. So p14≈[0.81,0.18,0.01]p_{14} \approx [0.81, 0.18, 0.01]. Same decoder, same words, but Paris now holds four-fifths of the probability. That sharpening from a tie to 0.810.81 is exactly what the diagram draws.

What the trajectory tells you, and what it costs

Read every layer and you will usually see three phases: early noise, a middle stretch where the right answer climbs to the top, and a late stretch where its share grows while the wording gets polished. The lens is cheap, one matrix multiply per layer with weights you already have, and it never changes the model's behavior because it only reads.

But it is a readout, not an explanation. It assumes middle layers speak the same numerical dialect as the last layer, which mostly holds because each layer adds a small update to a shared residual stream instead of rewriting it. When a layer rotates or rescales that stream, the decoder misreads it, and early layers often look like noise for this reason rather than any real confusion. For why features tangle this way, see circuits and superposition.

And the lens shows correlation, never causation. A layer voting for Paris does not prove Paris came from there. When you need proof, reach for activation patching. When you need cleaner features than raw vocabulary words, see sparse autoencoders.

Code

The point of this snippet is the replay itself: one function that turns any hidden state into word odds through a decoder matrix, run on both worked-example states.

import numpy as np

def softmax(z: np.ndarray) -> np.ndarray:    e = np.exp(z - z.max())  # shift by the max so exp cannot overflow    return e / e.sum()

def lens_probs(h: np.ndarray, w: np.ndarray) -> np.ndarray:    return softmax(w @ h)  # one score per vocab row, then shares of 1

w = np.array([[2.0, -1.0], [1.0, 1.0], [-1.0, 2.0]])  # Paris, London, Appleprint(lens_probs(np.array([1.0, 0.5]), w).round(3).tolist())  # -> [0.45, 0.45, 0.1]print(lens_probs(np.array([1.5, 0.0]), w).round(3).tolist())  # -> [0.81, 0.181, 0.009]

Both lines match the hand computation above: the tie at layer 8, then Paris at 0.810.81 after the state shifts. Swap in any layer's real hidden state with the model's own decoder weights and you get the full trajectory.

Watch Out For

Skipping the final normalization

The lens needs the model's final normalization with its learned weights applied before the decoder, not the raw hidden state. Skip it and every layer decodes to near-uniform mush: the symptom is a trajectory that looks dead flat, with even late layers refusing to commit, which reads like a confused model.

The cause is scale mismatch. The decoder was trained on normalized vectors, so feeding it unnormalized ones is feeding it inputs from a range it never saw. Always route through the final norm first, exactly as the last layer does. If the picture is still mushy everywhere, that is a real signal that this layer's dialect differs, not a bug in your code.

Trusting one layer's frontrunner

You catch Paris leading at layer 14 of 32 and declare the model decided. Then layer 27 flips to something else after folding in a negation or qualifier you forgot sat at the end of the prompt. The symptom is a confident claim about "where the model answers" that the next input quietly contradicts.

One layer's vote is a snapshot, not a verdict. Read the whole trajectory, and never steer the model or claim causation from a single layer. When the answer matters, confirm with a causal check in activation patching: swap the state and watch whether the answer follows.

The Quick Version

  • The logit lens points the model's own final word decoder at a middle layer, so you can read that layer's current best guess.
  • Every layer's hidden state has the same width, which is the only reason one decoder fits them all.
  • The math replays the last step early: normalize the hidden state, multiply by the unembedding matrix, softmax the scores.
  • A tie that sharpens, 0.45/0.45 early and 0.81 late, means the model settled early and spent the rest on confidence.
  • Early gibberish usually means the decoder misreads that layer's dialect, not that the model is confused.
  • A layer voting for a word never proves the word came from there. Patch the state to prove causation.