Which past events does the model look at? #Attention#Causalmask#Interactive
How our TFM handles attention over the history → How it’s built
Click any purchase and watch attention arcs reach back through the history — never forward.
Sequence attentionTransformer foundations
Each arc is an attention weight from the selected purchase to an earlier one; thicker means more weight. The weights here are illustrative (same category and recency count most) to show the mechanics.
The arcs only go backwards because of the causal mask: when scoring an event, the model may not see the future.
Transcript
How does ChatGPT decide which earlier words matter? Same trick, here for payments. These bars score each earlier payment on one card. A new payment lands: nine hundred dollars of electronics, at three oh seven a.m., online, from a new device. The model has only the card's past to go on. Which earlier payments should it weigh most? Here is the twist: nothing in her past looks like this payment. So where does the weight go? By the end, you'll know how a new payment asks a question of every earlier one, and how the answers come back as one view. The cardholder is made up; call her Maya. Every weight here comes from a small hand-built model on her payments.
This trick is called attention: deciding which earlier payments matter for this one. It works like a library. The new payment writes a request slip, the question it asks. That slip is its query. Every earlier payment carries a label on its spine, what it answers to. That label is its key. What you take home is the books' contents, called values. The best-matching labels give the most.
Start with an ordinary payment: her forty-one-dollar grocery on Wednesday evening. Its slip asks: who is like me? Here the slip and each label are short lists of numbers: the kind of merchant, online or not, and the size of the amount. Earlier groceries light up. Her Saturday grocery and her Monday evening grocery take about twenty-two percent each.
Now the nine hundred dollars. Each bar is a match score: how closely its slip and that label agree. The travel booking matches best. Then her late-night digital goods, then the rent. Where the two lists point the same way, the score grows. Where they disagree, they cancel out. Next, every score is scaled down by the same amount, so no single match runs away, as the film on softmax showed. Then softmax turns the scores into shares of one hundred percent. The travel booking takes twenty-five.
Now each earlier payment's contents flow into the new payment, in proportion to its share. That mix is added to the payment itself. It keeps what it was, and takes in what it looked at. In a trained model, query, key and value are three learnt projections: each made by weights learnt from data. Ours are set by hand. And no share is ever exactly zero. A payment that matters simply takes more.
One question is rarely enough. So attention asks several at once, each through its own head, with its own slips and labels. Ours has two. Head one asks about kind: the arcs above the tape. Head two asks about the time of day: the arcs below. Back on the Wednesday grocery, the kind head splits its weight between the two earlier groceries. The time-of-day head goes mostly to one: Monday's six p.m. grocery, with seventy-one percent. These attention heads are questions inside one layer, not the task heads on top, one per product, from the film on what a TFM is.
Back to the nine hundred dollars. There is no electronics anywhere in her history. Nothing looks like it. So the kind head falls back on its nearest kin: the online travel booking first, the late-night digital goods next. The time-of-day head spreads out too: her two early fuels and the eleven p.m. digital goods. When nothing is close, the weight spreads thin. This payment comes back in the film on the embedding map.
Now a longer history: sixty-four payments, her two weeks repeated back in time, so each habit shows up several times. Equal payments get equal weight, so the bars count them together, by kind. Her four travel bookings take twenty-four point five percent: more than all seventeen groceries together, at twenty. Every copy counts, but a closer match wins by far: one travel booking takes five times a grocery's share. Recency is missing here. Real models can learn to favour recent payments, the subject of the film on time.
In our TFM, each payment arrives as one vector, a list of numbers made by field attention, the film on reading one payment. Four sequence layers each run this question and answer over the payment and the ones before it, with two heads per layer. Here a TFM parts from a language model. An LLM reads back over the words of one text; a TFM, over one card’s payments.
To recap: each payment asks a question, earlier payments answer, and the best matches count most. And the nine hundred dollars? Nothing matched, so its weight spread thin, with the travel booking first. Now take Tuesday’s fuel, on her tape. Every payment after it is greyed. When the model trains on this payment, what stops it reading the ones after it, and why would peeking ruin it? That's next.
Sources
- Attention Is All You Need — Vaswani, Shazeer, Parmar, Uszkoreit, Jones, Gomez, Kaiser, Polosukhin, NeurIPS 2017
Chapters
Watch first
In code (PyTorch)
import torch, torch.nn.functional as F
T, d = 10, 64
q = k = v = torch.randn(1, T, d)
mask = torch.ones(T, T).tril().bool() # causal: no looking ahead
scores = (q @ k.transpose(-1, -2)) / d ** 0.5
scores = scores.masked_fill(~mask, float("-inf"))
weights = F.softmax(scores, dim=-1) # each row sums to 1
out = weights @ v