Understanding Transformers · Part 11 of 16
How Representations Change Layer by Layer
A token builds a fresh query, key, and value at every layer, out of whatever it understands at that point.
The intuition: the token's understanding keeps updating
A token's Query, Key, and Value are not computed once and reused up the stack. Every layer receives the current hidden representation X and builds fresh Q, K, and V from it, using that layer's own learned matrices:
The superscript l marks the layer. Both the input representation and the projection matrices differ from layer to layer, so the resulting Q, K, and V differ too. (Most current models apply a layer norm to X before the projections; the formulas leave it out.)
What changes, and why
- X changes because each attention and FFN sublayer adds its output to it (the residual stream, Part 4).
- Q changes because it is computed from a different
Xwith a different matrix, so the query can depend on more context. - K changes for the same reason: what a token advertises is computed from its current representation.
- V changes likewise, so what a token offers others can carry more context.
WQ, WK, and WV are separate learned parameters in every layer and in every head.
import numpy as np
# one token's representation, layer 0
X = np.array([[1., 0.]])
for layer in range(3):
Wq = np.random.randn(2, 2)
Wk = np.random.randn(2, 2)
Wv = np.random.randn(2, 2)
# fresh Q, K, V every layer, from the *current* X
Q, K, V = X @ Wq, X @ Wk, X @ Wv
X = X + 0.1 * V # toy "update": X keeps evolving
# Q, K, V are recomputed each pass; only X carries over. (Attention is
# omitted: Q and K go unused, so the update uses V alone.)
The word “bank,” layer by layer
The table below is an illustration of the general pattern, not a measurement of any model. Probing studies of BERT found that part-of-speech and syntactic information is easier to read out from earlier and middle layers and more semantic information from later ones, with considerable overlap (Tenney et al., 2019). Layers do not map cleanly onto single functions, and individual models differ.
| Stage | Representation of “bank” | Attention might… |
|---|---|---|
| Embedding | generic and dictionary-like; no context yet | not have run yet |
| Early layer | nudged by nearby words like “river” or “muddy” | focus on local, lexical clues |
| Middle layer | the geographic sense is taking hold | connect modifiers and related entities |
| Late layer | highly contextual, shaped for the prediction task | gather what's needed to pick the next token |
The vector starts as the embedding of “bank” and accumulates context as sublayers add to it. In a decoder-only model the last layers are also pushed toward predicting the next token: decoding intermediate vectors through the output matrix (the “logit lens,” nostalgebraist, 2020) shows predictions that usually sharpen with depth in GPT-2-style models.
X is the evolving state of each token, handed from one layer to the next. Q, K, and V are temporary views of that state, built for one attention operation in one layer and then discarded.