Tutorials › Understanding Transformers › How Representations Change Layer by Layer

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:

Q(l) = X(l-1) WQ(l)    K(l) = X(l-1) WK(l)    V(l) = X(l-1) WV(l)

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

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.

StageRepresentation of “bank”Attention might…
Embeddinggeneric and dictionary-like; no context yetnot have run yet
Early layernudged by nearby words like “river” or “muddy”focus on local, lexical clues
Middle layerthe geographic sense is taking holdconnect modifiers and related entities
Late layerhighly contextual, shaped for the prediction taskgather 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.

Persistent vs. temporary. 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.