Inside the Transformer: Scaled Dot-Product Attention

Live interactive visualizer for multi-head self-attention mechanisms and query-key routing.
Interactive Attention Graph & Tokens Hover token to isolate attention
Formula: Attention(Q, K, V) = Softmax( (Q · Kᵀ) / √dₖ ) · V
Select or hover over any matrix cell or token to trace vector projections.
Attention Matrix Heatmap [N × N] Queries (Rows) → Keys (Cols)

How Self-Attention Computes Context

In a transformer layer, each input token produces three vectors: Query (Q), Key (K), and Value (V). To compute attention weights, the model evaluates the dot-product similarity between the query of token i and the keys of all tokens j, scales by 1/√dₖ to stabilize gradients, and applies Softmax to generate a normalized probability distribution.

Different attention heads specialize in distinct linguistic patterns: some resolve ambiguous pronouns to their referents, others connect adjectives to adjacent nouns, and others capture broad relational context.