Machine Learning

Attention Mechanism

Attention Mechanism

By Riano · Published · Updated · Report a correction

Attention weights: a large dot product is not a nearby point

A query can put more weight on a key that is farther away on a scatter plot. Riano lets you test why: its scores use dot products, not Euclidean distance. Calculate the scores for four fixed keys, turn them into softmax weights, then vary the query and temperature without changing the keys.

Separate scores, weights and a value mixture

For a query q and keys ki, the demo computes scores si = q · ki and weights wi = exp(si/T) / Σj exp(sj/T), with positive temperature T. Subtracting the largest scaled score before exponentiating gives the same weights while avoiding unnecessarily large exponentials.

In a full attention layer, the weights mix value vectors: output = Σi wivi. Scaled dot-product attention in the Transformer divides scores by √dk. This module instead exposes a generic temperature and visualizes weights; it does not implement learned projection matrices, value-vector outputs, multi-head attention or a trained language model.

Worked example: a query that looks along the x direction

The implemented keys are K1 = (2, 8), K2 = (8, 7), K3 = (3, 3) and K4 = (7, 2). Choose q = (1, 0) and T = 1. The dot products are simply the keys' x coordinates.

  1. The raw scores are [2, 8, 3, 7]. The largest score is 8, belonging to K2.
  2. Subtract 8 before exponentiating: [exp(−6), 1, exp(−5), exp(−1)]. The denominator is approximately 1.377096.
  3. Divide each term by that sum. The weights are approximately [0.18%, 72.62%, 0.49%, 26.71%], totaling 100% up to rounding.

K2's large weight does not mean it is geometrically closest to (1, 0). Its dot product is largest. A key with a large magnitude in the query's direction can outrank a nearby key, and the displayed vectors are not normalized for cosine similarity.

Predictions using the same four keys
QueryTemperatureExpected behavior
(0, 0)1All four weights are exactly 25%
(1, 0)1K2 has the largest weight, about 72.6%
(1, 0)5K2 is still largest; weights become less concentrated
(−1, 0)1K1 becomes largest because −2 is the greatest score

Move the query and inspect a bar

Open Attention Mechanism. Query X and Query Y move the query; Temperature controls score concentration. Hovering over the weight bars reveals percentages.

  1. Set Query X to 0, Query Y to 0 and Temperature to 1. Check that the four bars have equal 25% weights.
  2. Set Query X to 1 while keeping Query Y at 0. Hover the K2 bar and compare its roughly 72.6% weight with the calculation.
  3. Raise Temperature to 5. The ordering stays the same because division by a positive number preserves score order, but the probability differences shrink.
  4. Return Temperature to 1 and set Query X to −1. Compare K1 and K2 and explain why reversing the query reverses which x coordinates score well.

The module's keys are fixed. Moving the cross changes scores against the same reference points; it is not retraining word embeddings. Circles use attention weight as a size cue, so apparent size is also an output, not an independent key attribute.

Weights are relative to the supplied keys

A softmax weight is normalized against the other available scores. Adding a key can change every weight even if a particular dot product stays constant. In exact arithmetic finite scores and positive temperature give strictly positive weights; a displayed 0.0% can be rounding or numerical underflow.

Reducing temperature concentrates probability on the largest scores; equal maxima can share the limiting mass. It does not make the scores more correct. A large attention weight in this toy calculation is not a calibrated probability that a word is correct, a measure of causal importance or evidence that a model understands the text. The scene illustrates one mechanism rather than the complete behavior of a Transformer.

Predict an unchanged result

With the query at (0, 0), lower Temperature from 1 to 0.5. Every score remains zero, so every weight remains 25%. Explain why lowering temperature alone cannot break a tie among equal scores.

Source for scaled dot-product attention

Vaswani and colleagues, Attention Is All You Need, Section 3.2 defines scaled dot-product and multi-head attention. The fixed keys, temperature experiments and numerical weights above are independently calculated for Riano's smaller visualization, whose differences from that architecture are stated explicitly.

Experience it interactively

Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Attention Mechanism module.

Try Attention Mechanism on Riano →

More in Machine Learning