Unit 13: Attention
How the big models work.
Unit 13 of 13 in AI and machine learning for kids. Its 4 lessons are Meaning as Numbers, Paying Attention, What a Transformer Is and Using AI Well — below is everything each one explains, and a question or two from it to try.
Every sample on this page is plain Python with no libraries, run before it shipped, and prints exactly what it says it prints.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
🧭 Meaning as Numbers
A name tag is not a meaning
In unit 7, cat became 0 and mat became 1. Those numbers say nothing about cats. A model given them has to learn everything about the word from scratch.
So instead, give every word a small list of numbers. Here the three columns mean "is it an animal", "can it move" and "is it small" — and now the numbers say something.
Python
words = {
"dog": [1, 1, 0],
"cat": [1, 1, 1],
"ball": [0, 1, 1],
"tree": [0, 0, 0],
}
for word in sorted(words):
print(word, words[word])
It prints
ball [0, 1, 1] cat [1, 1, 1] dog [1, 1, 0] tree [0, 0, 0]
Multiply and add to compare
Pair the lists up, multiply each pair, add the results. That is the dot product, and it is big when two words agree on the things they are both strong in.
You have used it in every unit since 3 — a neuron's weighted sum is a dot product of its inputs with its weights.
Python
def dot(a, b):
total = 0
for i in range(len(a)):
total = total + a[i] * b[i]
return total
cat = [1, 1, 1]
dog = [1, 1, 0]
tree = [0, 0, 0]
print(dot(cat, dog))
print(dot(cat, tree))
It prints
2 0
Nobody labels the columns
Real word lists are hundreds or thousands of numbers long, and they are learned — by gradient descent, from text, exactly like every other weight in this track. Nobody decides what column 47 means, and mostly nobody can say afterwards.
What comes out is still a map: words used in similar places end up with similar numbers. These lists are called embeddings.
Try it yourself
Two words have a large dot product. What does that suggest?
- They are spelled alike
- They are used in similar ways, so their numbers point the same way
- They appear next to each other
- They have the same length
What does this print?
Python
def dot(a, b):
total = 0
for i in range(len(a)):
total = total + a[i] * b[i]
return total
print(dot([2, 0, 1], [3, 5, 4]))
Answer them in the app
👀 Paying Attention
"The dog chased the ball because it was fast"
What is "it"? You knew instantly: the dog. To work that out, something has to look back over the earlier words and decide which ones matter here — a lot of weight on "dog", almost none on "the".
That is attention, and it is the idea that made modern language models work.
Score, then share out
The word doing the looking sends out a query — here, "something that is an animal". Score it against every word with a dot product, and the scores say how well each one fits.
But scores can be any size at all, and we need shares that add up to 1.
Python
def dot(a, b):
total = 0
for i in range(len(a)):
total = total + a[i] * b[i]
return total
query = [2, 0, 0]
words = {"dog": [1, 1, 0], "ball": [0, 1, 1], "tree": [0, 0, 0]}
for word in ["dog", "ball", "tree"]:
print(word, dot(query, words[word]))
It prints
dog 2 ball 0 tree 0
Softmax turns scores into shares
Raise e to the power of each score, then divide each one by the total. Every answer comes out between 0 and 1, they always add to exactly 1, and the biggest score keeps the biggest share without the others dropping to nothing.
Python
import math
def softmax(scores):
tops = []
for s in scores:
tops.append(math.exp(s))
total = sum(tops)
shares = []
for t in tops:
shares.append(t / total)
return shares
weights = softmax([2, 0, 0])
print([round(w, 3) for w in weights])
print(round(sum(weights), 6))
It prints
[0.787, 0.107, 0.107] 1.0
Then blend the words in those proportions
Multiply each word's numbers by its share and add them all up. The result is 79% dog — a new list of numbers standing for "what *it* refers to here".
That blended list is what the next layer reads. The model has replaced a vague word with mostly-the-dog.
Python
weights = [0.787, 0.107, 0.107]
values = [[1, 1, 0], [0, 1, 1], [0, 0, 0]]
blend = [0, 0, 0]
for i in range(len(values)):
for j in range(3):
blend[j] = blend[j] + weights[i] * values[i][j]
print([round(b, 2) for b in blend])
It prints
[0.79, 0.89, 0.11]
And the queries are learned too
Nobody wrote "look for an animal". The query, and the numbers each word offers up to be matched against it, all come out of weights trained by gradient descent — the same loop from unit 6, on an enormous amount of text.
A real model does this many times over in parallel, so one attention step can follow the grammar while another follows the subject.
Try it yourself
Why put the scores through softmax instead of using them directly?
- To make them positive shares that add up to 1
- To make them bigger
- To sort them
- To remove the smallest one
Attention lets a model do what the bigram model of unit 7 could not:
- Spell correctly
- Look back at any earlier word and decide how much it matters
- Run faster
- Learn without training data
Answer them in the app
🏛️ What a Transformer Is
It is a stack of things you have already built
A transformer block is two parts:
1. an attention step — every token looks at the earlier tokens and blends in what matters, which is unit 8;
2. an ordinary layer of neurons on the result, which is unit 5.
Stack that block dozens of times, feed tokens in at the bottom, and read a guess at the next token off the top. That is the architecture behind every chatbot you have used.
And it is trained the way you trained XOR
Show it a stretch of real text with the next token hidden. It guesses. Compare the guess with the token that actually came next, work the blame backwards through every layer, nudge every weight downhill.
That is unit 6, unchanged, run over an amount of text no person could read in a lifetime. The G, P and T in GPT stand for generative (it writes), pre-trained (that huge reading happened first), transformer (this stack).
Only the amount is different
Your XOR network held 9 numbers. A small handwriting network holds about eighty thousand. The large language models hold hundreds of billions, and take thousands of computers weeks to train.
But every one of those numbers is a weight, every layer is multiply-add-squash, and the training is gradient descent. Nothing in the list of ideas is missing from this track.
Python
def count(layers):
total = 0
for i in range(1, len(layers)):
total = total + layers[i] * (layers[i - 1] + 1)
return total
print("xor network:", count([2, 2, 1]))
print("handwriting:", count([784, 100, 10]))
It prints
xor network: 9 handwriting: 79510
Try it yourself
What is a transformer block made of?
- A database and a search engine
- An attention step and a layer of neurons
- Rules written by experts
- One very large neuron
What is a language model actually trained to do?
- Answer questions correctly
- Predict the next token of real text, over and over
- Store facts it is given
- Copy the internet
Answer them in the app
🏆 Using AI Well
Look how far you have come
A rule that ran out. A threshold found by searching. Neighbours and distance. A tree of questions you could read. Two ways of being wrong, and a price on each. One neuron, its weights fixed by its own mistakes. A valley, and the slope down it. A hidden layer that solved what one neuron could not. Blame, travelling backwards. Filters sliding over a picture. Groups nobody labelled. A robot paid to find treasure. Words as numbers, and attention deciding what matters.
There is no other ingredient. Everything else is more of these, and more data.
So: it can be confidently wrong
You saw this yourself in unit 6. The network was perfect on every example it had seen and answered 1.0 — completely certain — on the one it had not.
A model is not being careless when it does this, and it cannot tell that it is doing it. If an answer matters, check it somewhere else. That is not distrust of AI, it is how the thing works.
It learns what it is shown, including the unfair parts
Train the fruit model on a basket where every apple happens to be red, and it learns that red means apple. It was never told that. It has no way to know the basket was unusual.
Now put people in place of fruit, and a decision like who gets an interview in place of apple. A model trained on records of who was hired before will repeat whoever was picked before, including the unfair parts, and it will do it in a confident voice with no reason attached. This is called bias, and it comes from the data, not from the maths.
And what you type goes somewhere
A chatbot runs on somebody else's computers, and what you send is usually kept. So keep your address, your school, your phone number, your friends' names and anything private out of it — the same rule as anywhere else online.
If a machine ever says something that worries or upsets you, close it and tell a grown-up you trust. It is a program guessing at the next word. It is not a person, and it is never in charge.
You built the whole ladder
Most people meet AI as magic in a box. You have written every rung: the score, the search, the weights, the slope, the layers, the blame, the tokens, the attention.
None of it was magic. It was arithmetic, repeated, on examples somebody chose — and now you are one of the people who knows which part to ask about.
Try it yourself
An AI gives you an answer for your homework, in a very confident tone. What should you do?
- Copy it — it sounded sure
- Check it somewhere else before trusting it
- Ask the same question again
- Assume it is wrong
A model that decides things unfairly. Where did the unfairness most likely come from?
- The examples it was trained on
- The learning rate
- The programming language
- Its own opinions
Answer them in the app