Foundations

Attention as geometry

Attention is dot products and a softmax. Drag the query and key vectors with your hands and feel it: alignment wins weight, and vector length is secretly a temperature.

Drag the black query arrow, or any of the coloured keys.

outputcatdogcartaxqueryATTENTION WEIGHTScat56.7%q·k = 2.98dog39.2%q·k = 2.46car3.2%q·k = -1.07tax0.9%q·k = -2.89
1.49
Query norm
1.21bits of 2
Sharpness
cat 57%
Top weight

The weight on each key is the softmax of its dot product with the query. Alignment matters and so does length: drag a key further out and it wins attention without the angle changing.

Every explanation of attention eventually shows the formula, and the formula is honest but mute. It does not tell you what changing anything feels like. This page makes the vectors physical objects: drag them and the weights respond at every frame.

Three things are worth discovering with your hands rather than reading. First, the obvious one: swing the query toward a key and that key wins weight, because the dot product rewards alignment. Second, the one people miss: drag the query further from the origin without changing its angle. Every dot product grows in proportion, the softmax sharpens, and attention concentrates. Query magnitude is an inverse temperature, and nothing in the usual diagram tells you that. Third, the asymmetric counterpart: lengthening a single key raises only that key's score, so a key can win attention by being long rather than by being aligned, which is one reason real networks need normalisation layers to keep magnitudes from quietly deciding everything.

The dashed output line is the part most diagrams omit. Attention does not choose a key, it blends values in proportion to the weights, and here the values are the keys themselves, so the output visibly swings between vectors as you drag. When the weights flatten, it retreats toward the average of everything, which is what attention degenerating into a blur actually means.

The divide-by-root-d in the formula exists precisely because of the magnitude effect you just felt. In high dimensions, dot products of random vectors grow with the square root of the dimension, so without the correction the softmax would start saturated. What you produce here by dragging the query outward is the exact disease that scaling factor treats.

The honest limit: this plane is two-dimensional and the values are the keys themselves, both chosen so the geometry stays visible. Real attention runs the same equations at d of 64 or 128 with separate value vectors, where nothing changes except that you can no longer watch it happen.

The maths

Scaled dot-product attention
wi  =  exp ⁣(qki/d)jexp ⁣(qkj/d),o  =  iwiviw_i \;=\; \frac{\exp\!\big(\mathbf{q} \cdot \mathbf{k}_i / \sqrt{d}\big)}{\sum_j \exp\!\big(\mathbf{q} \cdot \mathbf{k}_j / \sqrt{d}\big)}, \qquad \mathbf{o} \;=\; \sum_i w_i \, \mathbf{v}_i

The bars are w, the dashed arrow is o. Here d = 2 and the values are the keys themselves, so the output is a visible blend of the arrows you are dragging.

Magnitude is temperature
qcq        qkic(qki)\mathbf{q} \to c\,\mathbf{q} \;\;\Rightarrow\;\; \mathbf{q} \cdot \mathbf{k}_i \to c\,(\mathbf{q} \cdot \mathbf{k}_i)

Scaling the query multiplies every score by the same c, which is identical to dividing softmax temperature by c. Dragging the query outward is cooling; dragging it toward the origin is heating.

Why the root-d correction exists
E[(qk)2]  =  dfor independent zero-mean, unit-variance components\mathbb{E}\big[(\mathbf{q} \cdot \mathbf{k})^2\big] \;=\; d \quad \text{for independent zero-mean, unit-variance components}

Random high-dimensional vectors have dot products that grow like root d, so without dividing by it the softmax would begin life saturated at exactly the sharp extreme you can produce here by stretching the query.

Related terms

More visualisations

Building with language models?

These explainers come out of the work. If you want the same thinking applied to your own system, that is what I do.

See how I can help