Field attention: reading one receipt #Attention#Interactive
How our TFM handles reading one transaction → How it’s built
Six details of one purchase attend to each other. Change the purchase and watch which details light each other up.
Field attention runs inside one transaction: each field (amount, merchant, time, country, channel, device) decides how much to look at every other field.
Why it matters: a field's meaning depends on the others. $900 is ordinary at an electronics store at 2 pm and alarming in a gift-card purchase at 3 am. Attention lets the model learn these combinations instead of you hand-crafting them as features.
The weights in this lab come from a small illustrative rule (unusual values attend to each other); a real model learns them from data.
Transcript
Three oh seven a.m. A nine-hundred-dollar payment, from a device never seen before. Electronics, bought online, on one card. Before approving it, the bank must judge: cardholder or thief? At two in the afternoon, our lab reads the same payment differently. The tool is self-attention, the heart of every Transformer, ChatGPT's included. Here it runs across one payment's details. You’ll see every detail of a payment weigh every other, and why nine hundred dollars reads differently at two p.m. and three a.m. The card is a fictional cardholder's; call her Maya. Every weight in this film comes from the lab's formula, not a trained model. This builds on the episode on tokens. It ended on a question this film answers: why does the amount care about the merchant?
Here is that payment as six fields: amount, merchant, time, country, channel and device. Each is one token. Field attention lets every field score every other field, itself included. Six fields make thirty-six scores, filling this grid. No field is told in advance which others matter. The scores decide, payment by payment. Each row is one field doing the looking. Each cell is how closely it looks. On the left, a thicker link is a closer look.
Take the amount's row. Its six scores fly up and become shares of one bar, and the shares always add up to one hundred percent. So a closer look at one field leaves less for the others. The rule that does this sharing is called softmax. Softmax has its own episode, next. Today we only need what it gives: one row, one hundred percent.
First, an ordinary payment: Maya's grocery, thirty-eight dollars twenty, at six in the evening, chip, on her usual device. Nothing stands out, so the amount spreads its look thinly. No field gets more than twenty-five percent of it.
Now the nine hundred dollars, at two in the afternoon. Electronics, online, on a new device. The amount's row changes shape. It gives the new device forty-one percent, and the merchant thirty-five. Half the answer to last episode’s question. Nine hundred dollars alone says little. Next to the store, and the device, it starts to mean something.
Now one change: the clock moves to three oh seven in the morning. Same amount, same store, same device. The amount's look swings to the clock. The hour takes forty-eight percent, and the merchant falls from thirty-five to nineteen. The amount itself never changed. Its context did, and attention weights are computed from the context.
The looking goes both ways. The hour's own row gives the amount fifty-two percent. The lab's formula is built so that unusual details find each other. A trained model may learn this; we show no trained weights.
What does the amount do with its look? It adds a weighted mix of what it looked at to what it already was. Forty-eight parts hour, twenty-two device, nineteen merchant, and a little of the rest, in the shares this row gave them. So the amount token that leaves this layer carries a share of the clock, the device and the store. It no longer travels alone.
That was the lab. Our real design, in our Sparkov config, gives each payment twelve field tokens. Five of the twelve carry the merchant: its category, how busy it has been lately, how often this card uses it, and two ratios. One ratio is the amount divided by the merchant's average amount. So the amount arrives already read against its merchant. That ratio is a design choice: it hands the model the comparison ready-made, before attention starts. That is the full answer to last episode’s question. And there is no merchant-name field at all: only its category and numbers about it.
Over those twelve tokens, our design runs two field-attention layers: every field with every field, twice. There is no position and no mask. A payment's fields arrive together, so in these layers their order changes nothing. Each field still knows which field it is, because each has its own embedding table from last episode. Then the twelve fold into one vector per payment, and that vector joins the card's history for the layers that follow. Here a TFM parts from a language model. An LLM's attention runs along words in order; a TFM's first runs across one payment's fields. IBM's TabFormer paper uses the same two levels: a field transformer for each row, then a second transformer across the rows. How a field asks and answers, with queries and keys, comes in the episode on past purchases.
To recap: each field scores every other, shares fill one hundred percent, and at three oh seven the amount looks hardest at the clock. These are the amount's raw scores at three oh seven, before sharing. The hour's is four point oh two; the merchant's, three point oh eight. So how do raw scores become shares of one hundred percent, and what does softmax's one dial do? That's the next episode.
Chapters
Watch first
In code (PyTorch)
import torch, torch.nn as nn
F, d = 6, 64 # 6 fields per transaction
fields = torch.randn(1, F, d) # one transaction, one vector per field
attn = nn.MultiheadAttention(d, num_heads=4, batch_first=True)
mixed, weights = attn(fields, fields, fields) # weights: [1, 6, 6]
event_vec = mixed.mean(dim=1) # pool the fields into one event vector