Unembedding: from hidden state to logits
Compute z_t = r_t W_U ∈ R^V, where r_t is the final hidden vector at position t. Each logit is then a dot product with that token's unembedding vector; apply softmax to get probabilities. Explain weight tying (W_U = W_Eᵀ) and its parameter savings.
Builds on: Token embeddings as a lookup table, Softmax, logits and the log-sum-exp trick
What the animation says
One vector, 50,000 scores
After the last layer, the model holds one vector for the current position: seven hundred sixty-eight numbers. To choose the next token, it needs a score for every token in the vocabulary, about fifty thousand of them. So how do seven hundred sixty-eight numbers become fifty thousand scores? And here's the twist: the same table that turned tokens into vectors can run in reverse to do it.
Score each token by alignment
Give every token a second vector, the same length as the hidden vector. We call it its unembedding vector. A token's score, its logit, is a dot product: multiply matching entries, then add them up. Small case: three numbers per vector, four tokens. The hidden vector is one, zero, two. Token zero gives one plus zero plus zero, so logit one. Tokens one and two both score two. Scores are big when the vectors point the same way. Your turn: token three is zero, one, minus one. What's its logit?
All tokens in one multiply
Minus two. Token three points away from the hidden vector, so it scores low. Now stand the four token vectors up as columns, side by side. That's the unembedding matrix. One row times this matrix does all four dot products at once. Out comes a row of four logits. Stack every position's hidden vector as rows, and one multiply scores every position against every token. Next, softmax turns these logits into probabilities. What happens to the minus two?
Softmax: logits to probabilities
Small, but positive. Softmax never drops a token. First subtract the largest logit, two. That keeps the numbers safe and doesn't change the result. Exponentiate each one: about point three seven, one, one, and point zero two. They add up to about two point three nine. Divide by that sum: point one five, point four two, point four two, and just under point zero one. Softmax acts on the logits, not on the hidden vector. And don't just divide logits by their sum: you'd get a negative probability.
Weight tying: one table, two jobs
The unembedding matrix has one column per token. The embedding table has one row per token. It's the same shape, flipped. So many models just reuse it: the unembedding is the embedding table, transposed. That's weight tying. At fifty thousand tokens and seven hundred sixty-eight dimensions, that saves thirty-eight point four million parameters. It works because tokens used alike need similar vectors going in and coming out. The cost is that each token's input and output vectors must be identical. Tap the line that ties the two tables.
Score each token by alignment
The unembedding formula
Worked example: d=3, V=4
Tied unembedding in numpy
import numpy as np
V, d, T = 6, 4, 3
rng = np.random.default_rng(0)
W_E = rng.normal(size=(V, d)) # embedding table [V, d]
W_U = W_E.T # tied unembedding [d, V]
R = rng.normal(size=(T, d)) # final hidden vectors, one row per position
Z = R @ W_U # logits [T, V]
assert np.allclose(Z[1, 2], R[1] @ W_E[2]) # logit = dot with token's vector
P = np.exp(Z - Z.max(axis=1, keepdims=True))
P /= P.sum(axis=1, keepdims=True)
print(Z.shape, P.sum(axis=1)) # (3, 6) [1. 1. 1.]
Remember this
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