Attention / Core
verifiedAttention
A model reading a sentence needs to decide, for each word, which other words matter. Attention is that decision made numerically: every position scores every other position, the scores become weights, and the position reads a weighted blend of what it found. Nothing is hard-wired about which words matter — the weights are computed fresh for every input.
Score each query against every key, scale by the square root of the head dimension, softmax the scores into weights, and take the weighted sum of the values. The scaling is not cosmetic: without it the dot products grow with dimension, the softmax saturates, and gradients vanish. Cost is quadratic in sequence length because every position scores every other one.
Attention(Q,K,V) = softmax(QKᵀ / √d sub k ) · V, with Q ∈ ℝ super n×d sub k , K ∈ ℝ super m×d sub k , V ∈ ℝ super m×d sub v . The softmax is row-wise, so each query's weights over the m keys sum to one. The √d sub k divisor keeps the variance of the dot products near one when the entries of Q and K are unit-variance.
8 queries against 8 keys; a brighter cell means more of that query's attention went to that key. Nothing is masked: every position can read every other, itself included.
Each row is a query, each column a key it can attend to; brighter cells carry more of the weight. Drag the sequence length to watch the cost of attending grow with the square of it.
Reviewed by opendroid · 2026-08-04
- arXiv:1409.0473 — Neural Machine Translation by Jointly Learning to Align and Translate
- arXiv:1508.04025 — Effective Approaches to Attention-based Neural Machine Translation
- arXiv:1706.03762 — Attention Is All You Need