ML//transformer//attention
Attention is the layer of a transformer that performs **context routing**, letting encoded information from one token's vector flow into another's while every token looks at every other token in parallel, and it is what lets a model use the context of a sequence (in *the bank by the river*, attention is what lets *river* pull the vector of *bank* toward its riverside sense). Stripped of its name it is a weighted average: kernel regression (average stored values, each weighted by how similar its key is to the query) with the kernel learned. It replaced the sequential bottleneck of RNNs with parallel computation.
Attention is the layer of a transformer that performs context routing, letting encoded information from one token's vector flow into another's while every token looks at every other token in parallel, and it is what lets a model use the context of a sequence (in the bank by the river, attention is what lets river pull the vector of bank toward its riverside sense). Stripped of its name it is a weighted average: kernel regression (average stored values, each weighted by how similar its key is to the query) with the kernel learned. It replaced the sequential bottleneck of RNNs with parallel computation.
out(q)=∑i=1nαi vi,αi=exp(qTki/d)∑j=1nexp(qTkj/d)\operatorname{out}(q)=\sum_{i=1}^{n}\alpha_i\,v_i,\qquad \alpha_i=\frac{\exp\left(q^{\mathsf T}k_i/\sqrt d\right)}{\sum_{j=1}^{n}\exp\left(q^{\mathsf T}k_j/\sqrt d\right)}out(q)=i=1∑nαivi,αi=∑j=1nexp(qTkj/d)exp(qTki/d)
A query qqq is compared with nnn keys kik_iki, each paired with a value viv_ivi: the query asks a question, the keys answer how relevant they are, and the values carry the content. The weights αi\alpha_iαi are positive and sum to 1 (softmax), and dividing by d\sqrt dd, the dimension of the vectors, keeps the dot products from growing with dimension and saturating the softmax (dot product attention). The learned part is in the projections q=WQxq=W_Qxq=WQx, ki=WKxik_i=W_Kx_iki=WKxi, vi=WVxiv_i=W_Vx_ivi=WVxi (Q, K, V), trained by backpropagation: the network learns what similar means for its task. With keys of equal norm, −∥q−ki∥2=2qTki+const-\lVert q-k_i\rVert^2=2q^{\mathsf T}k_i+\text{const}−∥q−ki∥2=2qTki+const, so the softmax of dot products is exactly a Gaussian kernel, and attention is a soft k-nearest neighbors with a learned metric.
max weight0.163 entropy3.35 bits effective points10.2 of 24 prediction−0.30 true f(x)−0.43 A query at x = 3.2 averages the 24 samples with softmax weights of a gaussian similarity (h = 0.62). The largest weight is 0.163 and the weights spread over about 10.2 effective points, giving a prediction of −0.30 where the true function is −0.43.
Drag the query and watch the weights and the prediction. Lower the width: the weights pile onto the nearest sample and the prediction turns into steps, a kNN with k=1k=1k=1; raise it and every sample weighs the same, the prediction flattening toward the global mean. Below, each row of the sentence's matrix is one word sharing its attention among the others, and each row sums to 1.
Attention is kernel regression with a learned kernel.
It resembles the weighting of a Kalman filter, a convex combination of what it holds, but no model of uncertainty stands behind its weights, so they say nothing about how far to trust the result (weighted averaging).
The spread of the weights, the attention entropy, says how many elements a query really consults: a low temperature concentrates them, a high one flattens them, the bias-variance trade on one knob.
In self-attention queries, keys and values come from the same sequence, so the weights form an n×nn\times nn×n matrix and the cost is O(n2)O(n^2)O(n2) in sequence length, the fundamental scaling constraint: doubling the context means four times the memory and the compute. Multi-head attention runs several learned kernels in parallel.
Attention existed before the 2017 paper. The key contribution of Attention Is All You Need was that attention was all you needed, with no recurrence; self-attention specifically was novel.
Attention is roughly a third of what a transformer needs: it handles context, and the other two thirds are the MLP storing facts. Its weights are not a reliable explanation of a decision, and when the relevant past is known (the same day last week, the last shift change) a feature in a simple model does the same job.