What is a transformer?
A transformer is a type of neural network designed for sequences like sentences. Instead of reading one word at a time, it can look at the whole sentence at once.
The original transformer has an encoder and a decoder. Modern LLMs, like GPT, are mostly decoder-only: they read a prompt and generate the next token, one at a time.
Key idea: transformers can process all tokens in parallel, which makes them fast to train. Self-attention is what gives them long-range memory.
Query, key, value
Attention works like a lookup system. For each word, the model creates three vectors:
Each word asks a question, and every word offers a key. The more a key matches the query, the more of that word's value gets included in the output.
These vectors are learned during training. The model figures out which kinds of queries and keys are useful for understanding language.
Self-attention
For each position in the sentence, the model:
- Computes a query vector from that position.
- Compares the query to every key vector using a dot product.
- Turns those similarity scores into probabilities with softmax.
- Takes a weighted average of all the value vectors using those probabilities.
The result is a new representation of the word that blends in information from the words it found most relevant.
Example: in the sentence "The cat sat on the mat because it was tired," when the model processes the word "it," attention should point strongly back to "cat." That is how pronouns get resolved.
Multi-head attention
One attention pass might look for one kind of relationship. But language has many kinds: grammar, meaning, references, tone, facts. So transformers run attention multiple times in parallel, each with its own set of learned weights. These are called attention heads.
Each head can specialize. One head might learn subject-verb agreement, another might learn that pronouns refer to nouns, and another might learn which words usually appear close together.
Key idea: multi-head attention gives the model many different lenses to view the same sentence at the same time.
Attention heatmap demo
Each word below has a tiny vector in our demo. Click a word to see how strongly it "attends" to every other word. The higher the score, the more influence that word has on the selected word's meaning.
Try this: click the word "it." If the attention system is working well, it should pay extra attention to "cat."
Self-attention in Python
Here is a tiny self-attention function. It takes simple vectors for each word, turns the dot products into probabilities with a softmax, and blends the value vectors using those weights.
This is the heart of the transformer. The weights decide which other words each word borrows meaning from. In a real LLM the vectors are much longer and there are separate query, key, and value matrices to learn, but the loop is identical: dot product, softmax, weighted average.
Check yourself
Why is it useful for every word in a sentence to look at every other word at the same time? What kind of context would a model miss if it only looked at the previous word?
Your answer:
Sample answer: Looking at all words at once lets the model resolve references, understand long-range relationships, and figure out which words modify which. If it only saw the previous word, it would miss things like pronouns referring to subjects from earlier in the sentence.
Quick quiz
1. What does self-attention let a model do?
2. In attention, what do Query, Key, and Value represent?
3. Why are transformers faster to train than older sequence models?
Keep learning about LLMs
Pre-training makes a model a good mimic. RLHF teaches it to be helpful, harmless, and honest. Next, learn how human feedback turns raw predictions into chatbot responses.
Part of the “LLMs Under the Hood” tutorial series.