Attention & Transformers
Attention & Transformers — Attention lets each token in a sequence build its representation as a weighted blend of the other tokens, where the weights come from the similarity of learned query and key vectors. It is the core mechanism of the transformer, the architecture behind modern large language models.
Attention is the idea that made large language models possible. Give it a handful of words and watch each one decide how much to draw from every other — the weights light up as arcs and a heatmap, computed live. Then crank the temperature and watch attention flatten until the model can no longer tell which words matter.
The idea in plain words
Attention answers a simple question for every word in a sequence: which of the other words should I pay attention to? Each word compares itself against the others and builds a new representation by blending in the ones it finds most relevant. In the phrase “the animal didn’t cross the street because it was tired,” attention is what lets it look back to animal rather than street.
Mechanically, every token produces three vectors — a query, a key, and a value. A token’s query is compared with every key by a dot product; the scores are turned into weights with a softmax, and the token’s output is the weighted sum of all the values. Stack this operation into many layers and heads and you get the transformer — the architecture behind modern large language models. Here the tokens are the word embeddings you already met.
Now, the math
Scaled dot-product attention is one compact formula:
- the query, key, and value matrices — one row per token.
- all pairwise similarity scores between queries and keys.
- a scaling factor that keeps the scores from growing with dimension.
- turns each row of scores into attention weights that sum to one.
▸ Show the derivation
Dividing by matters: in high dimensions dot products grow large, which would push the softmax into a near one-hot spike and starve the gradients. The scaling keeps attention smooth and trainable. This demo uses the token embeddings themselves as Q, K, and V (self-similarity attention), so a word attends to the words closest in meaning; real transformers learn separate projections for Q, K, and V, and run several attention “heads” in parallel.
Now Break It
Try this: Raise the temperature and the softmax flattens — every word attends to every other word equally, so attention carries no information.
Control: Temperature slider (raise it high)
What happens: Attention has flattened! At high temperature the softmax becomes uniform — every word attends to every word equally, and the layer can no longer focus on what is relevant.
Where attention & transformers is used
Attention is the engine of the transformer, and transformers now dominate machine learning. They power large language models like the GPT and Claude families, machine translation, code assistants, and text summarization; the same mechanism drives vision transformers for image recognition, protein-structure models like AlphaFold, speech recognition, and multimodal systems that tie images and text together. Attention replaced recurrent networks for most sequence tasks because it looks at all positions at once, so it both captures long-range relationships and trains efficiently on parallel hardware.
A frequent misconception is that attention “understands” language. It only computes weighted averages based on learned similarity; the apparent understanding emerges from stacking many layers over enormous data. Another pitfall is forgetting the cost: because every token attends to every other, computation grows with the square of the sequence length, which is why long-context models need special efficiency tricks. Finally, self-attention alone is order-blind — it treats a sentence as a bag of tokens — so transformers add positional information separately; without it, “dog bites man” and “man bites dog” would look identical.
Frequently asked questions
What is attention in a neural network?
What are queries, keys, and values?
What is self-attention?
Why divide by the square root of d in attention?
What is a transformer?
How is attention different from an RNN?
Written & reviewed by the ML Visualization team · Last updated .