Quant Memo
Core

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 qq (what this position is looking for), a key kk (what this position offers to others, as a label to be matched against), and a value vv (the actual content this position contributes if chosen). For position ii, its new representation is:

Attention(qi,K,V)=jsoftmax(qikjd)vj\text{Attention}(q_i, K, V) = \sum_j \text{softmax}\left(\frac{q_i \cdot k_j}{\sqrt{d}}\right) v_j

In words: for query ii, compare it against every key kjk_j using a dot product (a similarity score — high when the two vectors point in a similar direction), scale by d\sqrt{d} (a fixed constant that keeps the scores from growing too large as the vector dimension dd grows, which would otherwise make the next step too extreme), then pass all the scores for position ii 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 ii should attend to each position jj. The output is a weighted average of every position's value vector, weighted by how relevant each one is to query ii.

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": q=(1,0)q = (1, 0). Keys: k1=(1,0)k_1 = (1, 0) ("cloud revenue"), k2=(0,1)k_2 = (0, 1) ("last year"), k3=(0.8,0.2)k_3 = (0.8, 0.2) ("grew").

Dot products: qk1=1(1)+0(0)=1q \cdot k_1 = 1(1)+0(0) = 1. qk2=1(0)+0(1)=0q \cdot k_2 = 1(0)+0(1) = 0. qk3=1(0.8)+0(0.2)=0.8q \cdot k_3 = 1(0.8)+0(0.2) = 0.8.

Scale by d=21.41\sqrt{d} = \sqrt{2} \approx 1.41: scores are (0.71,0,0.57)(0.71, 0, 0.57).

Softmax: exponentiate, e0.712.03e^{0.71}\approx 2.03, e0=1e^{0}=1, e0.571.77e^{0.57}\approx 1.77; sum 4.80\approx 4.80. Weights: (0.42,0.21,0.37)(0.42, 0.21, 0.37).

"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 (0.42,0.21,0.37)(0.42, 0.21, 0.37) and 1-dimensional values for simplicity: v1=5.0v_1 = 5.0 ("cloud revenue," a strong, specific concept), v2=1.0v_2 = 1.0 ("last year," a weak time marker), v3=3.0v_3 = 3.0 ("grew," a moderate action concept).

output=0.42(5.0)+0.21(1.0)+0.37(3.0)=2.10+0.21+1.11=3.42\text{output} = 0.42(5.0) + 0.21(1.0) + 0.37(3.0) = 2.10 + 0.21 + 1.11 = 3.42

"It"'s new representation, 3.423.42, 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.

Matrix explorer
dashed = before · solid = after
det 1.25trace 2.50λ 1.81, 0.69area scales by 1.25×

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.

cloud rev. grew last year it thicker line = higher attention weight
The query token "it" attends directly to every other token in one step; line thickness shows the learned weight, with "cloud revenue" and "grew" dominating.

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 t1t-1 before computing step tt), 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 TT positions takes O(T2)O(T^2) 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.

Related concepts

Practice in interviews

Further reading

  • Vaswani et al., Attention Is All You Need
  • Alammar, The Illustrated Transformer
ShareTwitterLinkedIn