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. It's an elegant "understanding loop" -- first look at context (attention), then think deeply (feed-forward network), continuously deepening comprehension.
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!
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 for comprehensive understanding.
Multiple Transformer layers stacked together, each deepening understanding on top of the previous, giving rise to remarkable capabilities.
Next article
Next: Model Quantization →