Skip to content
AI360XpertBeta
Core ML

Visual explainer

Attention Mechanism

Follow a query through key matching, scaled scores, normalized shares and a value blend, then inspect masks and indistinguishable keys.

Different queries can revisit separate source positions.
Different queries can revisit separate source positions.

Attention creates a query-specific blend of stored values. Each output can revisit individual source positions instead of receiving only one fixed summary. The arrows show schematic source access, not hard selection of one token; the weighted blend follows below.

Bahdanau's encoder-decoder attention addressed the difficulty of compressing a whole source sentence into one fixed context. Attention can coexist with recurrence. Access does not guarantee perfect recall, and no vector literally overflows.

Matching and Contents

Keys choose the matching scores while values supply the contents.
Keys choose the matching scores while values supply the contents.

A query describes what to match; a key supplies matching features; a value supplies the contents to combine. Left paths score keys; right paths combine values from the same labelled positions. Path widths are schematic, not probabilities.

Use illustrative keys A = [2,0,0,0], B = [1,1,0,0], C = [0,2,0,0]; values A = [1,0], B = [0,1], C = [0,0]. Query q1 is [2,0,0,0]. Keys have four coordinates and values have two. These abstract features are chosen for arithmetic, not measured word meanings. Learned key and value projections can differ even when they start from the same token.

Read Horizontal Components

A horizontal query reads the keys through their horizontal components.
A horizontal query reads the keys through their horizontal components.

This is key space: the first two key coordinates are plotted, and the trailing two are zero. The horizontal query has length two, so its dot product doubles each key's horizontal coordinate. A, B and C receive raw dots 4, 2, 0: for B, 2×1 + 0×1 + 0×0 + 0×0 = 2. The dashed segment projects B onto the horizontal coordinate; it is not an attention path.

The horizontal scale is 180 drawing units per feature unit and the vertical scale is 100. The query arrow is translated 30 drawing units upward for clearance, keeping its 360-unit length and direction. Do not infer angles or distances from these unequal axis scales. The later value plane is a different space.

Scale the Gaps

Dividing by two halves the score gaps in this fixture.
Dividing by two halves the score gaps in this fixture.

Both rulers share a zero-to-four score scale. Each dashed connector links a key's raw dot to its scaled score. Scaled dot-product attention divides by the square root of key width: four components give divisor √4 = 2 and scores [2,1,0].

Under independent, zero-mean, unit-variance query and key components, dot-product variance grows with key width; this divisor counters that growth. It does not guarantee broad weights or correct matching. The picture shows this fixture's halved gaps.

Normalize One Unit

Softmax distributes one unit of weight across the three keys.
Softmax distributes one unit of weight across the three keys.

Softmax exponentiates scores and divides by their shared sum. Exponentials are approximately [7.389056,2.718282,1]; their sum is 11.107338. The shares are [0.665240956,0.244728471,0.090030573]. The strip is one unit, partitioned in those proportions. Rounded printed values can differ slightly from an exact total of one.

These are weights, not the output vector. Finite unmasked real-number scores produce positive weights. Floating-point underflow can round a sufficiently small numerical weight to zero.

Blend the Values

The attention output lies at the weighted blend of the values.
The attention output lies at the weighted blend of the values.

Now plot the two-dimensional values. C is the origin [0,0], A is [1,0], B is [0,1]; horizontal position is channel one, vertical height is channel two. The diamond is 0.665240956×[1,0] + 0.244728471×[0,1] + 0.090030573×[0,0] = [0.665240956,0.244728471].

The triangle encloses all convex combinations of these values. Nonnegative weights summing to one keep this pre-dropout weighted sum inside it. This claim does not apply to an entire Transformer block after dropout, output projection, residuals or feed-forward transformations. The value-plane scale is 360 drawing units horizontally and 220 vertically per feature unit; it stays fixed in the next comparison and final failure.

Change Only the Query

A new query moves the output while the values stay fixed.
A new query moves the output while the values stay fixed.

Keep every key and value. Query q2 = [0,2,0,0] gives scaled scores [0,1,2], reversing outer weights to [0.090030573,0.244728471,0.665240956]. Its diamond becomes [0.090030573,0.244728471]. The original q1 output is the outlined circle.

The output moves left by 0.575210383 feature units while channel two stays unchanged. The joining segment compares queries; it is not an optimization update or learned trajectory. C's weight pulls toward the zero value, affecting the output even though C contributes no nonzero coordinate.

Mask Before Normalizing

Masking C redistributes the weight over A and B.
Masking C redistributes the weight over A and B.

Start with q2's open strip. The cross and stripes mark excluded C. Add negative infinity to C's score before softmax, leaving allowed scores [0,1]. Exponentiate to [1,2.718282], sum to 3.718282, then divide: new weights are [0.268941421,0.731058579,0]. Both strips still total one, so A and B expand.

Deleting C's old weight would leave a total near 0.335, not a normalized row. An unmasked zero value still consumes probability mass, unlike an excluded key. At least one key must remain allowed here; a fully masked row needs explicit implementation handling because ordinary normalization has no valid denominator.

One Row per Query

Each query gets its own normalized row and output vector.
Each query gets its own normalized row and output vector.

Rows are q1, q2 and a fresh q3 = [1,1,0,0]; columns A/B/C are source keys. Every small track spans zero to one. Left tracks show weights; right tracks show two output coordinates. These are different groups, not a five-column normalization.

Q3 has raw dots [2,2,2] and scaled scores [1,1,1], so its weights and output coordinates are all one third. Complete output rows are [0.665240956,0.244728471], [0.090030573,0.244728471], and [0.333333333,0.333333333]. Softmax normalizes across keys within each query row, never across the entire matrix.

In self-attention, queries, keys and values originate from one sequence; in cross-attention, queries come from a different sequence than keys and values. More positions offer more choices but cost more matching work; mask positions when the task forbids access. The Transformer visual places attention inside a complete block; foundation models apply cross-attention across vision and language in Vision-Language-Action Models.

Where It Breaks

Identical keys cannot receive different weights for the same query.
Identical keys cannot receive different weights for the same query.

Change the keys explicitly: A and C both become [2,0,0,0], while B becomes [0,0,0,0]. Keep q1 and every value. Scores are [2,0,2], producing weights [0.468310531,0.063378938,0.468310531]. The diamond lands at [0.468310531,0.063378938] on the unchanged value plane. The upper tag describes equal keys, not equal values. The dashed horizontal guide shows the first-channel difference from A, not a full distance; the desired point also differs vertically.

Suppose the task requires A's value [1,0], marked by the ring. A and C offer identical matching features, so this query cannot give A more weight than C even though their values differ. Making tied scores more confident cannot create the missing distinction. The blend fails this stated retrieval target, not every possible task. Learning distinguishable keys or supplying additional information can change that condition.

The Quick Version

  • Each query can revisit separately stored source positions.
  • Keys supply matching features; values supply contents.
  • Dot products compare query and key components.
  • Divide dot products by the square root of key width.
  • Softmax shares one unit across each query's allowed keys.
  • Attention outputs a weighted blend of value vectors.
  • Changing queries can move outputs while values stay fixed.
  • Masks exclude keys before remaining weights are normalized.
  • Each query produces its own weight row and output.
  • Identical keys cannot receive different weights for one query.