The Self-Attention Mechanism
Self-attention lets every position in a sequence directly look at every other position and decide how much to weight each one, replacing the step-by-step relay of a recurrent network with a single, parallel, all-to-all comparison.
Prerequisites: Recurrent Neural Networks, Linear Algebra for Quants
An RNN reading a long report has to compress everything it has read so far into one running summary, and by page 40 details from page 2 have often faded or been overwritten. Self-attention takes a completely different approach: instead of a relay of running summaries, every word gets to look back — directly, in one step — at every other word in the document and decide for itself which ones matter most for understanding it right now. No compression bottleneck, no forgetting through a long chain of updates.
Think of a research analyst reading a earnings call transcript. When they hit the sentence "it grew significantly," they don't recall a compressed summary of everything said before — they scan back and directly notice which earlier sentence "it" refers to ("cloud revenue," three sentences up) and weight that connection heavily while ignoring most of the rest. Self-attention formalizes exactly that: for each word, compute a relevance score against every other word, turn those scores into weights, and build a new representation as a weighted blend.
The mechanics, one symbol at a time
Every input position (a word, a time step, a bar of price data) is turned into three vectors by three learned weight matrices: a query (what this position is looking for), a key (what this position offers to others, as a label to be matched against), and a value (the actual content this position contributes if chosen). For position , its new representation is:
In words: for query , compare it against every key using a dot product (a similarity score — high when the two vectors point in a similar direction), scale by (a fixed constant that keeps the scores from growing too large as the vector dimension grows, which would otherwise make the next step too extreme), then pass all the scores for position through softmax — a function that turns any list of numbers into positive weights summing to 1, favoring the largest inputs. Those weights say how much position should attend to each position . The output is a weighted average of every position's value vector, weighted by how relevant each one is to query .
Worked example 1: attention scores for one query, three positions
Three tokens with 2-dimensional keys and query already computed (numbers simplified for hand computation). Query for token "it": . Keys: ("cloud revenue"), ("last year"), ("grew").
Dot products: . . .
Scale by : scores are .
Softmax: exponentiate, , , ; sum . Weights: .
"It" ends up attending most heavily (42%) to "cloud revenue," some (37%) to "grew," and least (21%) to "last year" — a sensible allocation for resolving what "it" refers to, produced entirely by dot-product similarity, with no hand-coded grammar rule.
Worked example 2: the output as a weighted blend
Using the same weights and 1-dimensional values for simplicity: ("cloud revenue," a strong, specific concept), ("last year," a weak time marker), ("grew," a moderate action concept).
"It"'s new representation, , is now a blend dominated by "cloud revenue" and "grew," with "last year" barely contributing — the model has effectively resolved the reference by mixing in the content of the words it matters most, in a single computation involving every position at once, not a step-by-step relay.
The query-key dot product is, geometrically, exactly the operation this explorer visualizes one level down: vectors are compared by how aligned they are after being placed in a learned space, and that alignment — not raw word order — is what attention scores measure.
What this means in practice
Self-attention is the core operation inside transformers, which now underlie most state-of-the-art sequence models — language models, and increasingly time-series and order-book models in finance. Its two big practical advantages over recurrence: every position can be processed in parallel (no waiting for step before computing step ), and the path length between any two positions is exactly one step, regardless of how far apart they are, which avoids the vanishing-gradient-over-many-steps problem recurrent networks face. The cost is quadratic: computing attention scores between every pair of positions takes work and memory, which is why very long sequences (say, years of daily bars, or full limit-order-book histories) need specialized, sparse, or windowed variants of attention to stay tractable.
Self-attention computes, for every position, a weighted blend of every other position's content, with the weights coming from a learned similarity between queries and keys — giving direct, parallel access to the whole sequence instead of a step-by-step relay.
The classic confusion: assuming attention weights are a causal explanation of "what the model is thinking," or "which feature mattered." A high attention weight only says two representations were similar in a learned query/key space at that layer — it is not proof of causal importance to the final prediction, and multiple studies have found high-attention tokens can be swapped or removed without changing the output, which is exactly why Attention Is Not Explanation is its own separate warning worth knowing.
Practice in interviews
Further reading
- Vaswani et al., Attention Is All You Need
- Alammar, The Illustrated Transformer