Self-Attention

Self-Attention


Self-attention is the mechanism that computes how much attention each token in a sequence should pay to every other token. The transformer architecture works on this principle. Each token rebuilds its own representation as a weighted mixture of the others.

The calculation runs on three vectors. Query, key and value vectors are produced for every token. One token's query is multiplied against the keys of the others, the resulting scores are normalised, and the value vectors are summed using those scores. The token ends up pulling information from whichever tokens are relevant to it. Multi-head attention performs the same operation several times in parallel so different kinds of relationship can be captured in separate heads.

What makes it matter is that it works regardless of distance. In recurrent networks the link between two words weakened as the gap grew. With self-attention the connection between a word at the start of a sentence and one at the end is made in a single step.

A concrete case: in "the shoes I ordered arrived but the size is too small", working out what "size" belongs to requires the model to put weight on "shoes". Inspecting the attention weights shows that link actually being formed.

The cost is the size of the computation. Because every token is compared against every other, the operation count grows with the square of sequence length, which becomes a serious load on long sequences.

From generative AI strategy to custom agent development and retrieval architectures, we help you scale AI responsibly.
Discuss your AI project