Suppose the final hidden state is:
and the vocabulary contains only Paris, London, and Rome.
Let:
Logits are dot products:
So:
Softmax gives roughly:
The correct token is Paris.
Cross-entropy gradient¶
For softmax + cross entropy:
with one-hot target:
Thus:
Interpretation: - Paris has negative gradient, so increasing its logit lowers loss. - London and Rome have positive gradients, so increasing theirs raises loss.
Change a weight¶
Since:
we have:
With learning rate \(0.1\):
The new Paris logit becomes:
up from 1.5.
But hidden representations change too¶
The gradient with respect to hidden state is:
That signal propagates into previous Transformer layers.
If an earlier operation was:
then:
and:
So the model can change both its output mapping and the internal representation machinery.
Connect to Q/K/V¶
Since:
gradients can eventually reach:
Thus training can change what queries look for, what keys advertise, and what values carry.
No human labels an attention head “pronoun resolver”; useful behavior emerges because it reduces next-token loss.
Backprop through attention is what teaches retrieval behavior¶
Recall:
If the model should have paid more attention to an earlier token, the loss gradient can alter the query representation, the relevant key representation, the value content, or all three.
Through the chain rule, gradients reach the learned projection matrices:
and the layers that produced their inputs.
That means a high-level behavior such as “when a pronoun appears, retrieve the likely antecedent” can emerge without an explicit pronoun-resolution label. The only requirement is that better retrieval improves prediction loss often enough.
Why gradients are tiny but meaningful¶
A giant model may receive an extremely small change to any one weight in one update. But the same useful statistical regularity appears repeatedly across billions or trillions of tokens.
Training is therefore cumulative:
one example → tiny pressure
millions of related examples → stable feature
This also explains why individual weight values are rarely interpretable. A behavior is generally the result of coordinated changes distributed across many parameters.
Gradient clipping¶
Large training systems sometimes encounter unusually large gradient norms. Gradient clipping limits the magnitude of an update to reduce catastrophic jumps.
Again, optimization is not merely “follow the gradient”; it is engineering a stable trajectory through an enormous parameter space.
Backprop is repeated application of the chain rule¶
If:
then:
A Transformer is just an enormous composition of functions, so autograd repeatedly applies this rule backward through the graph.
The forward pass asks:
Given these weights, what output does the model produce?
Backward asks:
If the final loss moved a tiny amount, which internal quantities and parameters were responsible?
This is why saved activations matter: many local derivatives depend on the exact values seen in the forward pass.
One sequence trains every layer simultaneously¶
A single next-token mistake does not only adjust the vocabulary projection. Its gradient can flow through every layer all the way to embeddings.
So the training objective is global: attention patterns, MLP features, normalization scales, and output mappings are all jointly shaped by the same prediction errors.
Why loss gradients can be dense even when the error looks local¶
The target is one token, but the hidden state producing that token depends on many earlier tokens and many layers. A single prediction error can therefore create nonzero gradients across a huge fraction of the network.
That does not mean every parameter receives an equally useful update. Most individual effects are tiny. But across a large batch, related gradient signals accumulate into a coherent direction.
Backpropagation is therefore best viewed as a mechanism for distributing credit and blame numerically across a computation graph, not as identifying one guilty neuron.