3.5 Transformers and attention
Focusing on what matters
What you will learn
- Explain attention as a mechanism that decides, with weights, how much to draw on each part of the input
- Compute attention weights from queries, keys and values using dot products and the softmax function, with a small example
- Explain what characterizes the Transformer (it relates distant elements directly; its computation grows fast) and how it is used — and what to watch for — in automated driving
Build intuition
Picture a car about to turn right at an intersection (in left-hand traffic, as in Japan, so the turn crosses the oncoming lane). Around it are an oncoming car, a pedestrian on the crosswalk and a car parked at the curb. A human driver deciding “can I turn now?” would look closely at whatever matters most and pay little attention to the rest.
Attention does the same thing by calculation. The element that needs to decide asks every other element “how relevant are you to my decision?”, and gathers their information weighted by relevance. The weights are learned from data, so what to focus on is also decided by training.
The CNN of 3.3 builds up relationships between neighboring pixels with small filters, so relationships between distant things — opposite edges of an image, say — only emerge after many layers. Attention can relate distant elements directly, in a single step, such as a far-off traffic light and a car waiting at the stop line. A model built around attention is a Transformer. It was originally designed for translating text, but is now used widely — for images, video, sensor data, predicting how surrounding vehicles will move, and more.
Check with math
① Queries, keys and values
Attention splits the input into tokens — words in text, small patches in an image, individual vehicles and pedestrians in a scene — and makes three kinds of vector from each:
- Query q: what it is looking for (“which things could block my turn?”)
- Key k: what it is (“I am a crossing pedestrian”)
- Value v: the content it actually passes on (position, speed and other information)
These are made by multiplying each token’s features by learned matrices (3.1). The better a query and a key point in the same direction, the larger their dot product (0.2), and the more closely related they are taken to be.
② Computing the attention weights
Divide the dot product of the query with each key by \(\sqrt{d}\) (d is the vector length), turn the results into weights that add up to 1 with the softmax function (2.4), and average the values with those weights. Let us compute the figure’s example with two-number vectors (d = 2). Take the ego vehicle’s query as q = (1, 2), and the keys as oncoming car (2, 1), pedestrian (1, 2) and parked car (−1, 0).
| Oncoming car | Pedestrian | Parked car | |
|---|---|---|---|
| Dot product q · k | 1×2 + 2×1 = 4 | 1×1 + 2×2 = 5 | 1×(−1) + 2×0 = −1 |
| ÷ √2 | 2.83 | 3.54 | −0.71 |
| Softmax (weights) | 0.33 | 0.66 | 0.01 |
If each value is a single number for “how much this blocks the turn” (oncoming car 0.6, pedestrian 0.9, parked car 0.1), the output is the weighted average \(0.33 \times 0.6 + 0.66 \times 0.9 + 0.01 \times 0.1 \approx 0.79\) — an answer of “quite blocked”, summarized mainly from the pedestrian’s information.
In practice, several such attentions run side by side (multi-head attention), each looking at a different relationship such as distance, speed or type. Because attention itself ignores order, position information is added to the tokens. Stacking many “attention → fully connected layer” blocks, joined by residual connections (3.2), gives a Transformer.
③ Computational cost: comparing every pair
Attention compares every pair of tokens. With n tokens there are n² pairs. Splitting a 1920 × 1080 image into 16 × 16-pixel patches gives about 8,160 tokens, so a single layer compares about 67 million pairs. Doubling the tokens quadruples the computation. Tricks such as limiting how far each token looks, or merging tokens to reduce their number, are therefore used.
Connection to automated driving
- Where it is used: Transformers are used in camera-image perception, in combining images from several cameras into a top-down map (bird’s-eye view), in predicting the interacting motions of surrounding vehicles and pedestrians, and in approaches where a single model goes from sensors to controls. → 4.6 Modular vs. end-to-end, 3.6 Foundation models and generative AI
- Attention weights ≠ reasons for a decision: attention weights look like “this is where it looked”, which makes them tempting as explanations. Research has shown, however, that the places with large weights did not necessarily determine the decision. Treat them with care when using them to explain. → 3.9 Explainability (XAI)
- The danger of what was learned as “barely relevant”: in the example the parked car got a weight of 0.01 — yet a child can dart out from behind a parked car. Relationships that rarely occurred in the training data tend to be learned with small weights. Target situations where something that seems irrelevant becomes important in testing. → 5.6 Scenario-based testing
- Computing time and lots of data: computation grows with the square of the number of tokens, so it must be confirmed that the calculation finishes in time on the vehicle’s computer. Transformers also generally need large amounts of training data.
Summary
- Attention turns the dot products of a query with the keys into weights using the softmax function, and averages the values with those weights — a mechanism that learns how much to draw on each part of the input
- A Transformer stacks layers built around attention and can relate distant elements directly. It is used widely, for text, images, scene prediction and more
- Computation grows with the square of the number of tokens. Attention weights are not necessarily the reasons for a decision, and things learned as “barely relevant” can become important
Quick quiz
Q1. With query q = (1, 0) and keys k₁ = (2, 0) and k₂ = (0, 3), what are the dot products? Skipping the division by √d and applying the softmax function, what are the weights?
The dot products are 2 and 0. The weights are \(e^{2}/(e^{2} + e^{0}) \approx 0.88\) and about 0.12: attention goes to k₁, which points the same way as the query.
Q2. With attention weights (0.7, 0.2, 0.1) and values (10, 20, 30), what is the output?
0.7 × 10 + 0.2 × 20 + 0.1 × 30 = 7 + 4 + 3 = 14.
Q3. With 1,000 tokens, how many pairs does attention compare? And with 2,000 tokens?
1,000 × 1,000 = 1 million pairs; with 2,000 tokens, 4 million — four times as many.
Q4. In the example, the parked car received an attention weight of 0.01. Can the parked car be ignored in the turning decision?
Not safely. Pedestrians can step out from behind a parked car. The weights reflect relationships common in the training data, and rare but dangerous relationships tend to be learned with small weights. Such situations need to be checked in testing.
Further reading
- Next lesson: 3.6 Foundation models and generative AI
- Related: 0.2 Reading math notation (dot products), 2.4 Logistic regression (softmax function), 3.3 CNNs and image recognition
- Glossary: Attention / query, key, value, Transformer, Token, Softmax function
- Video lecture: YouTube (about 5 minutes) (the key points explained with slides and narration; the Japanese article has a Japanese video)
- References
- A. Vaswani et al., “Attention Is All You Need,” NeurIPS, 2017.
- A. Dosovitskiy et al., “An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale,” ICLR, 2021.
- S. Jain, B. C. Wallace, “Attention is not Explanation,” NAACL, 2019.