Transformer Attention Mechanism

How does AI actually "understand" your words?

Transformer Self-Attention Multi-Head

12 min read

Chapter 01

Why Do We Need Attention?

Consider this Chinese sentence:

"小明把苹果给了小红,因为她很饿"

(Xiaoming gave the apple to Xiaohong, because she was hungry)

Who does "she" refer to? You can answer "Xiaohong" without hesitation -- because your brain automatically performed a round of attention focusing: when you see "she", your cognitive system rapidly scans the context and locks onto the most relevant word.

But for traditional word-by-word processing models, this is not so simple. RNNs read sentences sequentially, and by the time they reach "she", the information about "Xiaohong" may have already faded. The revolutionary aspect of the attention mechanism is: it lets the model directly "see" every word in the entire sentence, regardless of distance.

Interactive / Click a word to see attention connections

Click any word above to see how much attention it pays to other words in the sentence

The attention mechanism is like your "focal point" when reading -- when you see "she", your gaze naturally jumps back to find "Xiaohong" in the preceding text. This is an analogy; attention computes weights between representations.
Chapter 02

The Q/K/V Trio

The core of attention lies in three vectors: Query (Q), Key (K), and Value (V). Each word generates all three vectors, and each has its own role.

Imagine walking into a library:

You use Q to match each book's K, find the most relevant ones, and then retrieve the corresponding V to read. It's that simple!

Interactive / Q·K·V Vector Generation Process
Q is your "question", K is each word's "label", and V is the actual "content". When Q and K match up, the corresponding V is retrieved. That's the entire secret of attention.
Chapter 03

How Are Attention Scores Computed?

With Q, K, and V in hand, attention scores are computed using an elegant formula:

Attention(Q, K, V) = softmax( Q · KT / √dk ) × V

Three steps to understand it:

  1. Q · KT -- Compute the "relevance" between every pair of words (higher dot product = more relevant)
  2. ÷ √dk -- Scaling factor to prevent excessively large values that cause softmax gradient vanishing
  3. softmax -- Normalize into a probability distribution (all weights sum to 1)

Finally, use these weights to compute a weighted sum of V, producing a new representation infused with contextual information.

Interactive / Attention Heatmap — Hover to see scores

Hover to see attention weights · Brighter color = higher attention

Attention scores are like your "focus level" during an exam -- you allocate more energy to the important questions. "go" attends to "Beijing" (where to go), "Beijing" attends to "roast duck" (cultural association) -- every pair of words has its own attention score.
Chapter 04

Multi-Head Attention: Understanding from Multiple Angles

A single set of Q/K/V may not be enough -- it's like looking at a problem from only one angle. Transformer uses multiple attention heads (Multi-Head Attention), each learning to focus on different patterns:

Interactive / Switch between attention heads to see patterns
Multi-head attention is like multiple detectives investigating the same case simultaneously -- one focuses on "who did what", another on "where", and another on "why". Head outputs are combined and transformed further; the role of each head requires model-specific analysis.
Chapter 05

Self-Attention: The Full Picture

Now let's put all the pieces together and see how a complete Transformer layer works. Attention aggregates contextual representations, and the feed-forward network transforms them further.

Interactive / Transformer Layer Flow — Click to play

Each layer transforms the preceding output. The 175B version of GPT-3, for example, has 96 layers; depth alone does not explain language performance.

A Transformer layer combines attention and FFN transformations. Residual connections provide additive paths across layers that support information and gradient flow.
Chapter 06

Summary

◆

Attention = Focus

Lets the model "see" the entire sentence without distance limitations, directly establishing connections between any pair of words.

◆

Q/K/V: Clear Division of Labor

Query asks questions, Key provides indices, Value provides content. The three work together to complete information retrieval.

◆

Multi-Head = Multiple Perspectives

Different attention heads learn different patterns -- syntax, semantics, position -- combining to provide multiple representations.

◆

Stacking = Deep Understanding

Stacked Transformer layers transform the representations from previous layers to predict outputs.

"Attention aggregates values using weights derived from queries and keys."

Next article

Next: Model Quantization →