Every AI tool that you have used this year, from ChatGPT to speech-to-text model transcribing your voice memos, runs on the same idea: attention. Most of these explanations jump straight into the queries, keys, values, and multi-head this-and-that without telling you why anyone needed this in the first place. So lets do it another way around: start off with the problem, and let the solution show up on its own.
The Problem: Which Words Matter?
Read this sentence: The deer didn't cross the street because it was too tired.
What does "it" refer to? The animal, obviously. You figured that out instantly because your brain connected the "it" to the "animal" and completely disregarded the "street."
Now change one word: The deer didn't cross the street because it was too wide.
Suddenly, "it" means the "street." It is the same sentence structure, just a completely different link. A plain neural network, like the ones from my previous post, has no good way to do this. It takes a fix set of inputs and runs the same weighted sums on them every time. Older sequence models (RNNs) tried to fix this by reading one word at a time as they carried the "memory" forward, but that memory got blurry over long sentences, and reading slow by slow is time-taking.
What we actually want is simple: let every word look at every other word and decide which ones matter for understanding it. And this is attention.
Attention in One Sentence
Attention is the weighted average, where the model learns the weights based on what's relevant.
That is basically the whole idea. You already know what a weighted average is. The only new part is that the weights are not fixed. They are computed on the fly, differently for every word from every sentence. When the model processes the "it," it assigns the weight to the animal (or "street," depending on the context) and tiny weight to the surrounding words in the sentence. Then's it blends the information from those words together, mostly from the important ones.
Queries, Keys, and Values: The Intuition
To compute those weights, each word gets turned into three distinct vectors:
Query (Q): "What am I looking for?"
Key (K): "What do I contain?"
Value (V): "What information do I hand over if you pick me?"
Think of it like searching a library. Your query is what you type into the search bar. Every book has a key, its title and its tags. The search compares your query against every key to see which books match the best. Then you actually read the value, the content of the best-matching books.
Now, for the word "it":
Its query asks something like "Who is referring to me when I am a pronoun?"
Every other word's key advertises what it is ("I'm a noun, an animal, a subject...").
The query-key match scores are high for "animal" and low for "the."
The model grabs a blend of the values, mostly from animal."
All three vectors come from the same word embedding, multiplied by three different learned weigth matrices. Those matrices are what training actually adjusts.
The Math (It's Shorter Than you Think)
Step one is measuring how well each query matches each keky using a dot product. A big dot product means the vectors point in similar directions, which means "good match":
score = QK^T
Step two is scaling those scores down so they don't get huge (big numbers make softmax too extreme):
(QK^T)/sqrt(dk) --> where d_k is the key vectors size.
Step three is running the scores through softmax, the same function from my neural networks post, to turn them into weights between 0 and 1 that sum to 1:
weights = softmax(step two)
Step four is using those weights to take a weighted average of the values:
Attention(Q, K, V) = (step three)V
That one line is the heart of every transformer. Dot product, softmax, weighted average. There is nothing you haven't already seen!
Attention in 10 Lines of Code
Here's the entire mechanism in NumPy:
Each row of weights shows how much one taken attends to every token in the sequence, and each row sums to 1. In a real transformer, Q, K, and V come from separate learned projections of X. Here, I'm skipping that to keep the core idea highlighted.
Multi-Head Attention, Many Perspectives at Once
One attention calculation captures one kind of relationship. But language has many at once: grammar, meaning, who-did-what, and what-refers-to-what.
So transformers run multiple attention "heads" in parallel, each with its own Q, K, and V matrices. One head might learn to track pronouns, another might focus on adjacent words, and another might pick up on verb-subject relationships. Their outputs get concatenated and mixed back together.
It's like asking several pepole to read the same sentence, each paying attention to something different, and then combining their notes.
Building a Full Transformer
Attention is the star, but a transformer block has a few supporting pieces:
Positional Encoding: attention itself has no sense of word order, so we add position information to each embedding. Otherwise "dog bites man" and "man bites dog" would look identical.
Multi-head Attention: the mechanism above.
Feed-forward Network: a small neural network applied to each token individually after attention.
Residual Connections and Layer Normalization: shortcuts and stabilizers that keep training smooth in deep stacks.
Stack that block as many times as you want, and you have finally got yourself a transformer. Train it with the same loop as always: forward pass, cross-entropy loss, backpropagation, gradient descent.
Where This Shows up in Real AI
LLMs such as ChatGPT and Claude: transformers trained to predict the next token. The same attention mechanism lets them keep track of context across thousands of words.
Image Models: vision transformers chop images into patches and dtreat them like words in a sentence.
Different inputs, but the same engine.
The One-Sentence Summary
Attention lets every piece of an input look at every other piece and learn how much to listen to each one, which is just a softmax-weighted average, and stacking that idea into transformers is what powers modern AI.

Top comments (0)