Logit lens: reading intermediate predictions
Apply the final norm and W_U to the residual stream r^{(ℓ)} at intermediate layers to see how the predicted next token develops through depth. Explain the method's limits and the tuned-lens fix: a small learned linear map per layer applied before unembedding.
Builds on: The residual stream as a shared channel, Unembedding: from hidden state to logits
What the animation says
Where does Paris appear?
Give a model 'The capital of France is', and it answers Paris. But we only see the answer after the last block. Did Paris show up in block three, or block thirty? Inside, the residual stream is a running sum: each block adds its output to it. At the end, a final norm and the unembedding matrix read the next token off that sum. So here's the question: could that same reader be used on the stream right after block two?
The logit lens
Yes: every layer's state has the same d numbers, so the same reader fits. Take the stream at any layer, apply the model's own final norm, then multiply by the unembedding matrix. Softmax turns those logits into a guess: what the model would say if it stopped here. That's the logit lens. It adds no new parameters, and at the last layer it is exactly the model's output. Let's run it on a tiny model: two dimensions, three tokens.
Watching the guess change
The unembedding has one column per token: the, France, and Paris. At layer two the stream is four, minus four. The norm divides by its size, giving one, minus one. Logits: two, minus one, minus two. The lens reads 'the'. At layer six, five, five becomes one, one. Logits zero, one, zero. Now it reads 'France'. At layer twelve the stream is minus six, six. Which token wins?
Direction, not size, and the catch
Paris. Minus six, six normalizes to minus one, one, and the logits are minus two, one, two. Softmax gives Paris about seventy-two percent. Notice the sizes grew: four, five, six. The norm erased that, so only direction decides the reading. But this reader was trained only on the last layer. An early layer might hold Paris in a direction that later blocks still rotate. The lens would read it wrong. So when an early reading looks wrong, it may be the lens, not the model.
Tuned lens: one small map per layer
The tuned lens fixes this: each layer gets its own learned matrix A and bias b, applied before the norm. They start as identity and zero, so it begins as the plain logit lens. Then they're trained, model frozen, to match the final output. Suppose layer six stored Paris turned by a right angle. Its map learns to turn it back: five, five becomes minus five, five, reading Paris. In code, every layer is read at once. Tap the two lines that make this a plain logit lens.
Reuse the model's own reader, early
Logit lens and tuned lens
Watching ' Paris' form
Lens over every layer at once
import numpy as np
rng = np.random.default_rng(0)
L, T, d, V = 4, 3, 8, 10
R = rng.normal(size=(L+1, T, d)).cumsum(0) # stand-in for r^(0..L)
W_U = rng.normal(size=(d, V)); g = np.ones(d) # unembedding, final-norm gain
A = np.stack([np.eye(d)] * (L+1)); b = np.zeros((L+1, 1, d)) # identity = logit lens
def norm_f(x): return g * x / np.sqrt((x**2).mean(-1, keepdims=True) + 1e-6)
Z = norm_f(R @ A + b) @ W_U # [L+1, T, V] logits at every depth
P = np.exp(Z - Z.max(-1, keepdims=True)); P /= P.sum(-1, keepdims=True)
print(P.argmax(-1)) # top token id: rows = layer, cols = position
print(P[:, -1].max(-1).round(2)) # last position's confidence by depth
Remember
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.
Bytewise
All courses