All courses

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

▶ Watch this lesson Start the whole course

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.

From one vector to a score for every token

After the last layer, the model holds one vector $r_t$ of $d$ numbers for position $t$ (say $d=768$). To pick what comes next it needs a probability for each of the $V$ tokens (say 50,000). How does a $d$-vector become $V$ scores? And how can the same table that turned tokens into vectors also turn a vector back into token scores?

Score each token by alignment

Give every token $j$ a second vector of length $d$, its **unembedding vector** $u_j$. Token $j$'s score, its **logit**, is the **dot product** $r_t\cdot u_j$. It is large when $r_t$ points the same way as $u_j$ and both are long. All $V$ dot products together are one matrix multiply. Softmax then turns the scores into probabilities. This is the embedding lookup in reverse: instead of fetching one row, compare against every token.

The unembedding formula

$$z_t = r_t W_U,\quad r_t\in\mathbb{R}^{1\times d},\ W_U\in\mathbb{R}^{d\times V},\ z_t\in\mathbb{R}^{1\times V}\ (\text{a length-}V\text{ vector})$$ Column $j$ of $W_U$ is $u_j$, so $z_{t,j}=r_t\cdot u_j$ and $p_\theta(x_{t+1}=j\mid x_{\le t})=\mathrm{softmax}(z_t)_j$. Stack the $r_t$ as rows of $R\in\mathbb{R}^{T\times d}$. Then all positions at once give $Z=RW_U\in\mathbb{R}^{T\times V}$. **Weight tying:** set $W_U=W_E^\top$, so $u_j$ is row $j$ of $W_E$. One table does both jobs, saving $V\cdot d$ parameters. Why this is reasonable: tokens used in similar ways need similar vectors both as inputs and as predictions. The cost is that a token's input vector and output vector are forced to be identical.

Worked example: d=3, V=4

Take $r_t=[1,0,2]$ and $u_0=[1,1,0]$, $u_1=[0,0,1]$, $u_2=[2,-1,0]$, $u_3=[0,1,-1]$. Logits: $z=[1,\,2,\,2,\,-2]$. Subtract the max (2): $[-1,0,0,-4]$. Exponentiate: $[0.368,1,1,0.018]$, which sum to $2.386$. So $p\approx[0.154,0.419,0.419,0.008]$. Tying at $V=50{,}000$, $d=768$ saves $50{,}000\times768=38.4$M parameters.

Tied unembedding in numpy

This computes the logits for every position with one matrix multiply. It checks that one logit is a dot product with a token's embedding row, then applies a stable softmax.
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

$z_t=r_tW_U$: token $j$'s logit is the dot product of the final hidden vector with token $j$'s unembedding vector, and softmax turns the logits into probabilities. With weight tying, $W_U=W_E^\top$: the same table embeds tokens and scores them, saving $V\cdot d$ parameters.

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.