Attention step by step
- Lesson 17
- 60 min
- Module 5: Represent sequences and meaning
Lesson 17 of 23 · Deep Learning and Transformers ExplainedAttention step by step
1. What a neural network is
2. Tensors and PyTorch
3. How neural networks learn
4. Diagnose training problems
5. Represent sequences and meaning
6. Assemble a transformer
7. Use pretrained models
8. Under the hood: the mathematicsOptional
What you will be able to do
Build attention by hand, with every number visible, so that you can follow what a transformer computes. Then read the real attention weights of the course’s tiny transformer on Larkfield tickets, and see what they explain and what they do not.
NotebookAttention by hand and on a real ticket, in a notebook
The notebook computes the worked example with PyTorch, checks every shape, and does the module practice. Then it trains a small transformer for a few epochs, about 2 minutes on a free Colab CPU. It reads the attention weights on one ticket. About 35 minutes.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- Embeddings: one learned row of numbers for each token, from Tokens and embeddings.
- Why a word needs the other words of the ticket, from Why word order and context matter.
- Softmax, which turns scores into probabilities that add up to 1, from Loss.
- Matrix multiplication and its shape rule, (a, b) @ (b, c) → (a, c), from Tensors and shapes.
- The dot product of two vectors, from Similarity and distance in the mathematics course.
What you need on your computer
- A browser and a calculator. The notebook opens in Colab or Kaggle with PyTorch already installed.
Listening in a meeting
Section titled “Listening in a meeting”You join a meeting with one question in mind: “When does the new stock arrive?” Five colleagues speak. You listen closely to the person from the warehouse and a little to the buyer. You hardly listen to the person who talks about the office party. Afterwards, your answer is mostly what the warehouse person said, with a little from the buyer.
You did three things. You had a question. You judged how well each speaker’s topic matched it. And you took what they said, weighted by that match. Attention does the same, for every word of a ticket at once.
The problem: which words should “damaged” read?
Section titled “The problem: which words should “damaged” read?”The previous lesson ended with a need. In “The lamp was damaged on arrival, not after a month of use”, the word “damaged” needs “arrival”, not “on”. In T-64813, “charged” matters more than “refunded”. The useful words can be anywhere in the ticket, and they change from ticket to ticket.
A fixed window, such as word pairs, always reads the same neighbours. An average reads every word equally. Neither can choose. We want each word to choose its own sources, by what they say, with weights that add up to 1.
The idea: queries, keys and values
Section titled “The idea: queries, keys and values”Each word makes three vectors from its embedding, with three learned weight matrices:
- A query: what this word is looking for. In the meeting, your question.
- A key: what this word offers, like the label on a folder. The topic of each speaker.
- A value: what this word passes on to a word that reads it. What each speaker says.
To update one word, compare its query with the key of every word, itself included. A good match gives a high score. Softmax turns the scores into weights that add up to 1. The word’s new vector is the weighted average of all the values.
This is attention. The weights say where each word “pays attention”, which gives the method its name. The reading word is often called the query word; this lesson calls it the reading word. Every word reads at the same time, each with its own query.
Worked example: three words, two numbers each
Section titled “Worked example: three words, two numbers each”Take three words, “damaged on arrival”, and only 2 numbers per vector. The numbers below are made up so that you can follow every step. In a real network, they come from the embeddings and the learned matrices.
| word | query q | key k | value v |
|---|---|---|---|
| damaged | (0.5, 1.5) | (1, 0) | (2, 0) |
| on | (0, 0) | (0, 0.5) | (0, 0) |
| arrival | (1, 0) | (0, 2) | (0, 1) |
Follow the reading word “damaged”. Its query is (0.5, 1.5).
- Score: the dot product of the query with each key. damaged: 0.5 × 1 + 1.5 × 0 = 0.5. on: 0.5 × 0 + 1.5 × 0.5 = 0.75. arrival: 0.5 × 0 + 1.5 × 2 = 3.0.
- Divide by √d, where d is the number of numbers in a key: √2 = 1.414. The scores become 0.354, 0.530 and 2.121.
- Softmax. e^0.354 = 1.424, e^0.530 = 1.699, e^2.121 = 8.342; together 11.466. Divide each by the total: the weights are 0.124, 0.148 and 0.728. They add up to 1.
- Weighted sum of the values. 0.124 × (2, 0) + 0.148 × (0, 0) + 0.728 × (0, 1) = (0.248, 0.728).
The new vector of “damaged” is mostly the value of “arrival”. After attention, “damaged” carries information about when the damage happened. The diagram shows the four steps.
Text description of the diagram
The reading word is damaged, with the query (0.5, 1.5). There are three columns, one for each word: damaged, on and arrival. Their keys are (1, 0), (0, 0.5) and (0, 2). Step 1, the score is the dot product of the query with each key: 0.5, 0.75 and 3.0. Step 2, each score is divided by the square root of 2, 1.414: 0.354, 0.530 and 2.121. Step 3, softmax turns them into weights that add up to 1: 0.124, 0.148 and 0.728. The values of the three words are (2, 0), (0, 0) and (0, 1). Each value is multiplied by its weight: (0.248, 0), (0, 0) and (0, 0.728). Step 4, these are added: the output is (0.248, 0.728). The arrow from arrival is the thickest, because its weight is the largest.
Why divide by √d? A dot product adds up d products, so with more numbers per vector the scores get larger. Large scores make softmax extreme: one weight close to 1, all others close to 0, and the network almost stops learning which words to read. Dividing by √d keeps the scores in a similar range for any d. The tiny transformer of this course uses d = 16, so it divides by 4.
The other two words read too. “on” has the query (0, 0): every score is 0, so the weights are equal, 0.333 each. A word with no question listens to everyone equally. “arrival” has the query (1, 0), which matches the key of “damaged”: its weights are 0.503, 0.248 and 0.248.
All words at once, with matrices
Section titled “All words at once, with matrices”Put the queries of all three words in one matrix Q, one row per word, and the keys in K and the values in V. Then one matrix product computes every score at once: Q @ Kᵀ. Here Kᵀ is K turned on its side, so that each query meets each key. Softmax works along each row, so that every row adds up to 1.
import mathimport torchfrom torch import nn
Q = torch.tensor([[0.5, 1.5], [0.0, 0.0], [1.0, 0.0]]) # one query per word: damaged, on, arrivalK = torch.tensor([[1.0, 0.0], [0.0, 0.5], [0.0, 2.0]]) # one key per wordV = torch.tensor([[2.0, 0.0], [0.0, 0.0], [0.0, 1.0]]) # one value per wordscores = Q @ K.T # (3, 2) @ (2, 3) -> (3, 3)weights = torch.softmax(scores / math.sqrt(2), dim=1) # every row adds up to 1out = weights @ V # (3, 3) @ (3, 2) -> (3, 2)print(scores)print(weights.round(decimals=3))print(out.round(decimals=3))print(torch.allclose(out, nn.functional.scaled_dot_product_attention(Q, K, V)))The output:
tensor([[0.5000, 0.7500, 3.0000], [0.0000, 0.0000, 0.0000], [1.0000, 0.0000, 0.0000]])tensor([[0.1240, 0.1480, 0.7280], [0.3330, 0.3330, 0.3330], [0.5030, 0.2480, 0.2480]])tensor([[0.2480, 0.7280], [0.6670, 0.3330], [1.0070, 0.2480]])TrueThe first row of each tensor is the hand calculation for “damaged”. dim=1 tells softmax to work along each row. The last line compares the result with PyTorch’s own function, scaled_dot_product_attention: the same numbers. Written as one formula, the whole calculation is:
The shapes
Section titled “The shapes”Read the shapes, as always. With n words and d numbers per vector:
| Step | Shape in the example | Shape in general |
|---|---|---|
| Q, K, V | (3, 2) each | (n, d) each |
| scores Q @ Kᵀ | (3, 2) @ (2, 3) → (3, 3) | (n, n) |
| weights, after softmax | (3, 3), each row adds up to 1 | (n, n) |
| output, weights @ V | (3, 3) @ (3, 2) → (3, 2) | (n, d) |
Two things to see. First, attention gives back one new vector per word, of the same size: words in, words out. Second, the weights form an n × n square, one weight for every pair of words. A ticket of 120 tokens has 14,400 weights. Doubling the length gives four times as many weights, so attention gets expensive for long texts.
The course’s tiny transformer has 64 numbers per token. It splits them into 4 heads of 16 numbers each. Each head has its own query, key and value matrices, so it can look for something different. Think of four listeners in the meeting, each with their own question. Each head divides by √16 = 4. The four outputs of 16 numbers are joined again into 64. The model has 2 blocks, each with its own 4 heads. Module 6 builds the whole block. Here is one block’s attention on a real ticket of 30 tokens. You do not have the file tiny_transformer.pt yet: Transformer blocks trains the model and saves it. For now, read the code and its output. The notebook trains a small version itself.
from ticketnet import TEAMS, TinyTransformer
tf = TinyTransformer(len(vocab))tf.load_state_dict(torch.load("tiny_transformer.pt")) # the course's trained modeltf.eval()row = next(r for r in valid_rows if r["ticket_id"] == "T-65052")ids = torch.tensor([vocab.encode(row["text"])])block = tf.blocks.layers[0]attn = block.self_attnprint(attn.embed_dim, "numbers per token,", attn.num_heads, "heads of", attn.head_dim)with torch.no_grad(): h = tf.embed(ids) + tf.position(torch.arange(ids.shape[1])) x = block.norm1(h) _, w = attn(x, x, x, need_weights=True, average_attn_weights=False)print("tokens", ids.shape, "-> vectors", h.shape, "-> weights", w.shape)print("each row adds up to 1:", torch.allclose(w.sum(-1), torch.ones(1, 4, ids.shape[1])))The output:
64 numbers per token, 4 heads of 16tokens torch.Size([1, 30]) -> vectors torch.Size([1, 30, 64]) -> weights torch.Size([1, 4, 30, 30])each row adds up to 1: TrueThe weights have the shape (batch, heads, tokens, tokens): for each of the 4 heads, a 30 × 30 square. The model also adds a position vector to each token’s embedding, so that word order can affect the result; Module 6 explains it. norm1 is the layer normalization that the block applies before attention, also explained in Module 6. PyTorch’s attention layer does not return its weights unless you ask, so the code calls it again with need_weights=True.
The real attention weights on Larkfield tickets
Section titled “The real attention weights on Larkfield tickets”The course’s tiny transformer was trained on the 4,696 training tickets for 20 epochs. It routes 94.3% of the validation tickets right. The explorer shows its real attention weights on six validation tickets. Pick a ticket, a block and a head, then click any word to make it the reading word. The words of the ticket turn darker where the reading word gives more weight. “Whole matrix” shows every reading word at once.
Explore
Read the attention weights of the tiny transformer
Predict first: Ticket T-65052 starts 'Not a delivery issue'. In block 2, which words will 'not' read most: its neighbours 'a delivery issue', or words further away? Will the four heads agree?
Click a word of the ticket to make it the reading word. Or use the buttons:
Reading word: not (word 1 of 30). Darker words get more of its attention.
- Grace's team
- payment
- Tiny transformer
- payment, over 99.9%
- Bag of words
- payment
- Transformer, words shuffled
- payment, over 99.9%
In block 2, head 1, not gives 0.50 of its attention to twice and 0.07 to “'”. The other heads of this block read charge, remove, remove most.
- darker: a larger attention weight (relative to the largest one in view)
| Word | Bar | Weight |
|---|---|---|
| twice (word 18) | 0.50 | |
| “'” (word 7) | 0.07 | |
| duplicate (word 28) | 0.05 | |
| card (word 15) | 0.05 | |
| for (word 19) | 0.04 | |
| the other 25 words | 0.29 |
The 30 weights add up to 1.00.
Most attention received in block 2 (all heads, all reading words): remove, twice, charge. Words whose removal weakens the answer most: charged, charge, remove.
Text description of the explorer
The explorer shows one validation ticket at a time, written as a row of word buttons. At the start, the ticket is T-65052: “Not a delivery issue and I’m keeping the leaf blower. My card was charged twice for the same order. Can you remove the duplicate charge?” Grace’s team is payment, and the tiny transformer says payment with a probability over 99.9%. The reading word is “not”, in block 2, head 1. Its five largest weights are: twice 0.50, the apostrophe 0.07, duplicate 0.05, and smaller weights for the rest. In the other heads of block 2, “not” reads charge (0.55, head 2), remove (0.50, head 3) and remove (0.48, head 4): all four heads look at the words of the payment request at the end, not at “delivery” next to it. Controls: a list of six tickets (a negation, two topics, two boundary tickets, a plain ticket, and a negation that the transformer routes wrongly); block 1 or 2; head 1, 2, 3, 4 or the average of the four; the view, one word or the whole matrix. The readout shows Grace’s team, the transformer’s answer, the bag-of-words network’s answer, and the transformer’s answer when the words of the ticket are shuffled. A line below compares the words that receive the most attention in block 2 with the words whose removal weakens the answer most. For T-65052 these are remove, twice and charge, and charged, charge and remove.
In T-65052, all four heads of block 2 send “not” to the end of the ticket: “twice”, “charge”, “remove”. None of them reads “delivery”, the word right after “not”. The weights come from what the words say, not from where they are. That is the behaviour the previous lesson asked for.
What attention weights show, and what they do not
Section titled “What attention weights show, and what they do not”It is tempting to read the heatmap as “the model routed this ticket to payment because of these words”. Be careful. The weights show something real, but much less than that.
What they show: for one head in one block, where each word took its information from. That is a fact about the computation, and it is useful when you check a model or look for a bug.
What they do not show:
- There is not one heatmap. This small model has 2 blocks × 4 heads = 8 weight matrices per ticket, and they often disagree. In T-65052, block 2, the word “card” gives 0.95 of its weight to “twice” in head 1 and 0.77 to “remove” in head 4. In heads 2 and 3 it spreads its weight out. Larger models have hundreds of heads.
- Weights are not reasons. A simple test: hide one word at a time (replace it with
<unk>) and see how much the answer weakens. Take the word that receives the most attention in block 2, and the word whose removal weakens the answer most. On the 600 validation tickets, they are the same word in only 73 tickets (12.2%). In T-64917, a boundary ticket, the most attention goes to “?” and “.”, but hiding “unpacked” weakens the delivery answer most. - Attention is one step of many. After attention, each block has a feed-forward layer and adds the result to its input. Then the second block, an average over the words, and the output layer follow. The value vectors and all those layers decide what a weight of 0.5 actually passes on.
- This model barely uses order. When the words of every validation ticket are shuffled, it still routes 93.3% right, only 1 point less than with the right order. Its attention patterns look meaningful, but most of its decisions could be made from the words alone.
- Tidy weights can come with a wrong answer. In T-65198 (“This isn’t about a refund. The courier left my bed linen set with a neighbour …”), the transformer says warranty (80.4%), which is wrong. With the words shuffled, it says delivery (99.7%), which is right. Its weights in the right order look just as orderly as in the other tickets.
And remember the overall result. After 20 epochs, this transformer routes 94.3% of the validation tickets right, the bag-of-words network 97.3% (98.3% at its best epoch) and the linear model 98.8%. About 4,700 tickets are too few to learn a language from nothing. Attention is the right idea for text whose meaning depends on order and context. A model also needs enough data to learn it, or a start from a pretrained model.
When to use attention, and what it costs
Section titled “When to use attention, and what it costs”Use attention when the meaning of an input depends on how its parts relate to each other, such as the words of a sentence. It reads any distance in one step. It also computes all the words at the same time, which suits modern hardware. It is the core of every transformer, from this tiny model to the large language models.
Its costs:
- Length squared. Every word reads every word, so the weights grow with n × n: 14,400 per head and block for a 120-token ticket.
- Data. The query, key and value matrices are learned. With little data, as here, they learn less than a simple count already knows.
- No order by itself. Attention compares contents, not positions. Without the position vectors of Module 6, this model would give exactly the same answer for shuffled words.
Guided practice: read the explorer
Section titled “Guided practice: read the explorer”- In the explorer, keep ticket T-65052, block 2, head 1, and the reading word “not”. Write down its largest weight.
- Click the heads 2, 3 and 4 in turn. For each head, write the word that “not” reads most.
- Click “average”. Note the largest weight in the average of the four heads.
- Switch to block 1. Does “not” read the same words?
- Read the line below the explorer: which words receive the most attention, and which words weaken the answer most when hidden?
Show the expected answers
- Head 1: “twice”, 0.50.
- Head 2: “charge”, 0.55. Head 3: “remove”, 0.50. Head 4: “remove”, 0.48. Every head reads a word of the payment request, but not the same word.
- In the average of the four heads, the largest weight is “remove”, 0.26. It is smaller than in any single head, because each head prefers a different word.
- No. In block 1, “not” spreads its weight more. Head 1 reads “for” most (0.27) and head 4 reads “charge” (0.56). Head 3 has no weight above 0.16. The two blocks do different work.
- Most attention: remove, twice, charge. Hiding hurts most: charged, charge, remove. For this ticket the two lists overlap well. Over all 600 validation tickets, the top words agree in only 12.2% of the tickets.
Your turn: a ticket with two topics
Section titled “Your turn: a ticket with two topics”Choose ticket T-65234: “My password reset worked fine after I wrote in last week, thanks. Now I’d like to send back the cushion covers, how do I start?” Grace’s team is returns. Before you click, predict: in block 2, head 4, which word will “back” read most? Then check, and compare with the hide-one-word line.
Show the answer
“back” gives 0.90 of its weight to “thanks”, a word of the finished password topic, not to “send”, its partner. Yet the model routes the ticket to returns, and hiding “send” (then “back”) weakens that answer most. A large weight on “thanks” does not mean that “thanks” decided the team. In block 1, head 2, “back” reads mostly itself (0.92). Each head and block computes something different, and only all of them together, with the layers after them, give the answer.
Common mistake
An attention heatmap presented as the reason for a decision
Symptom: Omar wants to show Grace why the router chose payment for T-65052. He takes the heatmap of block 2, head 1, and writes: “The model chose payment because it focused on ‘twice’.” Grace asks the same about T-64917, and the heatmap says the model “focused” on “?” and “.”.
Cause: one head of one block shows where one word took its information from. It is not a list of reasons. The other 7 heads show other words. Attention is followed by more layers. The most attended word is the word whose removal weakens the answer most in only 12.2% of the validation tickets.
Fix: to say which inputs influence an answer, change the input and measure the output. Hide one word at a time, or swap a word (for example “arrival” for “use”). Then compare the probabilities. Use attention weights to look inside the computation, not to explain a decision. And say what the test cannot show: words can also matter together, which one-at-a-time hiding misses.
Knowledge check
Section titled “Knowledge check”Check your understanding
In your own words: what do the query, the key and the value of a word do in attention?
Show the answer
The query says what the reading word is looking for. The key says what each word offers, like a label. The dot product of the query with each key gives a score: a good match gives a high score. Softmax turns the scores into weights that add up to 1. The value is what each word passes on: the reading word’s new vector is the weighted average of the values.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Predict: a reading word's three scores are 2, 2 and 2. What are its attention weights, and what is its output if the values are (3, 0), (0, 3) and (0, 0)?
Show the answer
Equal scores give equal weights after dividing by √d and softmax: 1/3 each, 0.333. The output is the plain average of the values: (3 + 0 + 0) ÷ 3 = 1 and (0 + 3 + 0) ÷ 3 = 1, so (1, 1). When all scores are equal, attention is the same as the average-embedding network’s average.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Module practice
Section titled “Module practice”The module practice joins the three lessons. Trace one attention calculation by hand and check every dimension.
Worked example
Section titled “Worked example”You did it above: the reading word “damaged”, scores 0.5, 0.75 and 3.0; divided by √2: 0.354, 0.530 and 2.121; weights 0.124, 0.148 and 0.728; output (0.248, 0.728). The shapes were Q, K and V (3, 2), the weights (3, 3), the output (3, 2).
Your task
Section titled “Your task”Three words, two numbers each. The numbers are made up.
| word | query q | key k | value v |
|---|---|---|---|
| word 1 | (1, 0) | (2, 0) | (1, 0) |
| word 2 | (0, 1) | (0, 2) | (0, 1) |
| word 3 | (1, 1) | (1, 1) | (1, 1) |
- For the reading word word 1, compute the three scores, the scaled scores (÷ √2), the softmax weights and the output.
- Write the shape of Q, of Q @ Kᵀ, of the weights and of the output.
- Predict the weights of word 3 without computing every step. Explain why.
- The tiny transformer reads a batch of 8 tickets, padded to 50 tokens, with 4 heads. Give the shape of its attention weights, and the shape of one block’s output.
Show the model answer
1. Word 1. Scores: q · k = 1 × 2 + 0 × 0 = 2, 1 × 0 + 0 × 2 = 0, 1 × 1 + 0 × 1 = 1. Scaled: 2 ÷ 1.414 = 1.414, 0, 1 ÷ 1.414 = 0.707. Softmax: e^1.414 = 4.113, e^0 = 1, e^0.707 = 2.028; total 7.141. Weights: 0.576, 0.140, 0.284 (they add up to 1). Output: 0.576 × (1, 0) + 0.140 × (0, 1) + 0.284 × (1, 1) = (0.860, 0.424).
2. Shapes. Q is (3, 2). Q @ Kᵀ is (3, 2) @ (2, 3) → (3, 3). The weights are (3, 3), and every row adds up to 1. The output is (3, 3) @ (3, 2) → (3, 2): one new vector of 2 numbers for each word.
3. Word 3. Its query (1, 1) gives the scores 2, 2 and 2: the same for every key. So its weights are equal, 0.333 each, and its output is the average of the values, (0.667, 0.667).
4. The tiny transformer. The weights are (batch, heads, tokens, tokens) = (8, 4, 50, 50). A block gives back one vector of 64 numbers per token: (8, 50, 64), the same shape as its input.
The notebook checks all four answers with PyTorch. A complete answer shows every intermediate number, and checks that each row of weights adds up to 1.
Module check
Section titled “Module check”Check your understanding
Retrieval: what is the shape of the attention weights for one head and a ticket of n tokens, and what must every row of them add up to?
Show the answer
One head gives an n × n matrix: one row for each reading word, one column for each word that it reads. Softmax works along each row, so every row adds up to 1. With a batch and several heads, the shape is (batch, heads, n, n).
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Explain or predict: Tomás shuffles the words of every validation ticket. The bag-of-words network routes 98.0% right (97.3% before, both after 20 epochs). The tiny transformer drops from 94.3% to 93.3%. What does each result tell you?
Show the answer
The bag-of-words network cannot notice the shuffle: the counts are the same, so its input and its answers are the same. (Its 97.3% becomes 98.0% only because the course cuts tickets after 120 tokens: in a few long tickets, the shuffle changes which words are kept. Without the cut, it is 98.0% in both orders.) The transformer can see order (it has position vectors and attention), but losing the order costs it only 1 point. So on these tickets it barely uses the order; it decides mostly from the words. Attention gives a model the ability to use order and context, but the model uses it only as far as the data teaches it to.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Completion check: for ticket T-65052 ('Not a delivery issue … My card was charged twice … Can you remove the duplicate charge?'), explain which inputs influence the payment answer, without treating attention as a complete explanation.
Show the answer
Measure influence by changing the input. When one word at a time is hidden, the payment answer weakens most without “charged”, then “charge” and “remove”. These words influence the answer. The attention weights agree for this ticket. In block 2, the most attended words are “remove”, “twice” and “charge”. Every head sends “not” to the payment request.
But the weights are not the explanation. They come from 8 heads in 2 blocks that often disagree, and more layers follow them. Over all 600 validation tickets, the most attended word is the most influential word in only 12.2% of the tickets. Hiding one word at a time also misses words that matter only together. So a careful statement is: “Hiding ‘charged’ or ‘charge’ weakens the payment answer most; the attention weights are consistent with this, but they do not prove it.”
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
Module 5 recap
- Text becomes tokens and token IDs; each ID picks a learned row from the embedding table: (batch, tokens) → (batch, tokens, 64). Embeddings learn “used alike for this task”, partly meaningful and partly noise.
- A bag of words and an average of embeddings forget the order. Two made-up tickets with the same words get the same input, so one of them is always routed wrongly. N-grams help a little and cost a lot.
- Attention: each word compares its query with every key and divides the scores by √d. Softmax turns the scores into weights, and the output is the weighted average of the values. Shapes: (n, d) in, (n, n) weights, (n, d) out.
- The tiny transformer’s real weights show where each word took its information from, in 8 heads that disagree. They are not reasons: the most attended word is the most influential one in only 12.2% of the validation tickets. This model barely uses order (shuffled words: −1 point) and routes fewer tickets right than the bag of words. Larkfield keeps its linear router.
Reset: this module installs nothing on your computer. To start an explorer again, reload the page. If you used a notebook, restart it: in Colab, choose Runtime › Restart session; in Kaggle, Run › Restart & clear cell outputs. Then run all cells again. The notebooks save their files only in the folder ticket-router; delete that folder to start completely fresh.
Next, Module 6 assembles a whole transformer block around attention: several heads, a feed-forward layer, residual connections and normalization. Then it trains the tiny transformer and compares it honestly with the bag of words.
Next: Transformer blocks