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. Transformer gives AI the same ability.
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". In the end, all the clues are combined for a comprehensive understanding.
Chapter 05

Self-Attention: The Full Picture

Now let's put all the pieces together and see how a complete Transformer layer works. It's an elegant "understanding loop" -- first look at context (attention), then think deeply (feed-forward network), continuously deepening comprehension.

Interactive / Transformer Layer Flow — Click to play

Each layer continues to deepen understanding based on the output of the previous layer. GPT-3 has 96 layers, meaning it goes through 96 such "understanding loops" -- no wonder it can comprehend such complex language!

A single Transformer layer is like one "understanding loop" -- first look at context (attention), then think (FFN), continuously deepening comprehension. Residual connections ensure information isn't lost in deep networks.
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 for comprehensive understanding.

Stacking = Deep Understanding

Multiple Transformer layers stacked together, each deepening understanding on top of the previous, giving rise to remarkable capabilities.

"Attention allows AI to stop reading word by word, and instead 'grasp the key points' just like humans do."

Next article

Next: Model Quantization