/ module 04 · Foundations
Embeddings & Attention
After tokenizing, every token ID is swapped for a long list of numbers: its embedding. Words with similar meaning end up close together. Then attention lets each token look at the others and borrow the context it needs, which is how 'it' finds out it means 'the cat'.
- Explain how embeddings turn meaning into distance and direction
- Use cosine similarity and the king − man + woman example
- Describe what one attention head does for a token like 'it'
Try this first: Click two dots on the map, then try a vector arithmetic button.
concept
Meaning becomes geometry
An embedding is a learned vector. During training, words used in similar contexts get pushed toward similar vectors, so distance and direction start to mean something. The famous example: the arrow from man to woman points the same way as king to queen.
Real models use hundreds or thousands of dimensions. The lab below squeezes a small vocabulary into two so you can see it.
Next click sets word A.
mechanism
Attention: tokens talking to tokens
An embedding alone does not know its context: the vector for bank is the same in "river bank" and "bank account". Self-attention fixes this. Each token asks a question (query), every token offers a label (key), and the match scores, after a softmax, decide how much of each token's information (value) to mix in.
A model has many heads per layer and dozens of layers, and each head learns its own pattern. The three toy heads below are hand-written to mimic patterns researchers actually find.
| query ↓ key → | The | cat | sat | on | the | mat | because | it | was | tired |
|---|---|---|---|---|---|---|---|---|---|---|
| The | 100 | |||||||||
| cat | 95 | |||||||||
| sat | 91 | |||||||||
| on | 87 | |||||||||
| the | 83 | |||||||||
| mat | 80 | |||||||||
| because | 77 | |||||||||
| it | 77 | |||||||||
| was | 72 | |||||||||
| tired | 69 |
Try: switch to Head 2 and read the "it" row. Then change "cat" to "dog" or replace "it" with "she". Turn off the causal mask to see what an encoder would allow.
Check yourself
Tick each one you could explain to a friend. If you can't yet, scroll back up and replay the lab.