How does AI actually "understand" your words?
12 min read
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.
Click any word above to see how much attention it pays to other words in the sentence
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!
With Q, K, and V in hand, attention scores are computed using an elegant formula:
Three steps to understand it:
Finally, use these weights to compute a weighted sum of V, producing a new representation infused with contextual information.
Hover to see attention weights · Brighter color = higher attention
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:
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.
Each layer transforms the preceding output. The 175B version of GPT-3, for example, has 96 layers; depth alone does not explain language performance.
Lets the model "see" the entire sentence without distance limitations, directly establishing connections between any pair of words.
Query asks questions, Key provides indices, Value provides content. The three work together to complete information retrieval.
Different attention heads learn different patterns -- syntax, semantics, position -- combining to provide multiple representations.
Stacked Transformer layers transform the representations from previous layers to predict outputs.
Next article
Next: Model Quantization →