Lensa ML
Lensa ML

How Self-Attention Works

Step 1 of 6

What Is Self-Attention?

Each word attends to the whole sentence

self-attention — same sentence attends to itselfThe20%cat22%sat16%on10%the19%mat13%ThecatsatonthematKeysQueryfull self-attention matrix (row = query)

Query

"cat"

Attends Most To

"cat"

Top Weight

22%

"cat" attends most to itself (22%). In self-attention, each word in a sentence computes how relevant every other word is to its own meaning — including itself.

Step 1 of 6: What Is Self-Attention?

Each word attends to the whole sentence

self-attention — same sentence attends to itselfThe20%cat22%sat16%on10%the19%mat13%ThecatsatonthematKeysQueryfull self-attention matrix (row = query)

Query

"cat"

Attends Most To

"cat"

Top Weight

22%

"cat" attends most to itself (22%). In self-attention, each word in a sentence computes how relevant every other word is to its own meaning — including itself.

Step 2 of 6: Query, Key, Value

Projecting input into Q, K, V matrices

creating the query, key, and value vectorsInput MatrixThecatsatonthemat0.80.20.50.30.90.1-0.50.70.4-0.8-0.40.60.70.10.40.2-0.60.8W_Q0.6-0.30.10.40.8-0.20.10.50.7Query Matrix0.60.20.40.60.7-0.10.00.90.1-0.60.20.40.50.10.3-0.0-0.10.7×=all three projectionsQueryKeyValue

Projection

Query

Input Shape

6 × 3

Query Shape

6 × 3

The Query matrix is computed by multiplying the input embeddings by W_Q. Each row becomes a query vector — it asks "what am I looking for?"

Step 3 of 6: Q·K Dot Product

Computing similarity between queries and keys

computing Q·K dot product for "cat"Q("cat")[0.55, 0.68, -0.08]K("The")(0.55)(0.69) + (0.68)(0.24) + (-0.08)(0.14)= 0.53K("cat")(0.55)(0.15) + (0.68)(0.85) + (-0.08)(0.33)= 0.63K("sat")(0.55)(-0.30) + (0.68)(0.45) + (-0.08)(0.67)= 0.09K("on")(0.55)(-0.34) + (0.68)(-0.64) + (-0.08)(0.44)= -0.66K("the")(0.55)(0.60) + (0.68)(0.15) + (-0.08)(0.07)= 0.43K("mat")(0.55)(0.44) + (0.68)(-0.66) + (-0.08)(0.18)= -0.22

Query

"cat"

Highest Score

0.63

Best Match

"cat"

Score("cat", "cat") = 0.63 is the highest. The attention score is the dot product Q·K — multiply each dimension pairwise and sum. Higher scores mean the query and key are more aligned.

Step 4 of 6: Attention Score

Scale, softmax, and attention weights

attention score pipeline for Q("cat")ThecatsatonthematQ·K0.530.630.09-0.660.43-0.22÷ √3 = 1.73scaled0.310.370.05-0.380.25-0.13softmaxweight20.3%21.5%15.7%10.2%19.1%13.1%Σ = 1.00 (weights sum to 1)

Query

"cat"

1/√d_k

0.58

Top Weight

21.5%

Attends To

"cat"

Raw Q·K scores are divided by √d_k = 1.73 to prevent large values from pushing softmax into extremes. Then softmax converts scores into a probability distribution — "cat" puts 21.5% of its attention on "cat".

Step 5 of 6: Causal Self-Attention

Masking future tokens in decoders

bidirectional self-attention — all tokens visibleKeysThecatsatonthematQueriesThecatsatonthemat0.3019%0.2118%0.0916%-0.0813%0.2418%0.1316%0.3120%0.3722%0.0516%-0.3810%0.2519%-0.1313%0.1417%0.4724%0.2720%-0.3211%0.0917%-0.3311%-0.1714%0.1419%0.3222%0.1419%-0.1614%-0.1913%0.2419%0.1417%0.0616%-0.0414%0.1918%0.1317%0.0215%0.0616%0.2419%0.2419%0.0015%0.1217%Click a row to select query. Top: scaled score, bottom: attention weight.

Query

"sat"

Visible Tokens

6

Top Weight

24.2%

In bidirectional self-attention, "sat" attends to all 6 tokens equally. This is used in encoders (like BERT) where the full context is available. Compare with causal masking to see the difference.

Step 6 of 6: Full Pipeline

End-to-end attention in one view

Input Embedding"cat" → [0.3, 0.9, 0.1]Linear ProjectionsQ[0.55, 0.68, -0.08]K[0.15, 0.85, 0.33]V[0.41, -0.15, 0.83]Q · KᵀThe0.53cat0.63sat0.09on-0.66the0.43mat-0.22÷ √d_k (1.73)scaled0.310.370.05-0.380.25-0.13softmaxweights20.3%21.5%15.7%10.2%19.1%13.1%× V → OutputΣ (weight × V) for each dimension20% × V("The")22% × V("cat")16% × V("sat")10% × V("on")19% × V("the")13% × V("mat")Output("cat")[0.14, 0.17, 0.45]Attention(Q, K, V) = softmax(Q·Kᵀ / √d_k) · V

Query

"cat"

Attends to

"cat"

Output

[0.14, 0.17, 0.45]

This is the full attention pipeline for "cat": embed → project to Q,K,V → compute Q·K scores → scale by 1/√3 → softmax → weighted sum of V vectors. The output [0.14, 0.17, 0.45] is a context-aware representation that blends information from all words, weighted by relevance.