Tokenizing a transaction #Tokenization#Embeddings#Interactive
How our TFM handles reading one transaction → How it’s built
Drag the amount, change the merchant, and watch each field become a token and then a learned vector.
Models read integers. Each field gets its own rule:
- Amount: log-spaced buckets, so $5 and $6 share a token but $5 and $500 do not.
- Categories (merchant type, entry mode, hour): a direct lookup.
- High-cardinality ids (merchant id): hashed into a fixed number of buckets, which bounds the vocabulary and handles unseen merchants.
Each token id then selects a row of a learned embedding table: the coloured bars.
Transcript
You'd think a model sees four dollars fifty. It sees bin twenty-three. So does four sixty, for another coffee, in real public data. Five dollars is not. It lands in bin twenty-six. What it can tell apart was settled before the model trained on a single payment.
Like ChatGPT with words, a Transformer reads payments only as whole numbers, called tokens. Today: how one payment becomes them. By the end, you'll know three choices that decide what a model can still tell apart: how to cut, how to name, when to learn. Here's why it matters. Two amounts that share a token look the same, in that field, to every later layer of the model. We start in the lab, with one fictional cardholder's morning coffee. Each row is one field. The coffee's amount, four dollars fifty, has its rule, its raw value, and its token: thirty-two. The bars come later. Along the bottom is the model's whole input for the coffee: one token per field. The dollar sign, the café and the clock are gone. Choice one: how to cut the amount. Prices are continuous, so this lab cuts one dollar to ten thousand into two hundred log-spaced buckets.
Four fifty: bucket thirty-two. Four sixty: still thirty-two. Five dollars: thirty-four. A new bucket. The token moves in steps, not smoothly: each log bucket is about five percent wider than the one before. Log spacing judges money in ratios: a dollar matters on a coffee, not on a laptop. Small prices get fine buckets, big ones wide ones. Choice two: how to name. A merchant category is a lookup: its own id in a vocabulary, the fixed list of names a field allows. The coffee's category, restaurants, is token fifty-seven, in a vocabulary of three hundred. The id is a name, not a quantity. Hour of day works the same way: the coffee's eight a.m. is token eight, in a vocabulary of twenty-four. Entry mode has four values, and chip is token zero. Small, closed lists are the easy case.
The hard case is the merchant. Millions exist and new ones open daily, so a table with a row for each would never stop growing. So the lab hashes the id: a fixed formula scrambles it into one of four thousand and ninety-six buckets. The vocabulary never grows. The same id always lands in the same bucket, and a brand-new merchant gets a token on day one. The price: two merchants can collide.
Now the bars. Each token picks one row, its embedding: learnt numbers that stand for the token. The rows form the embedding table. Here the hour turns to three in the morning and the channel to online, so those two fields select different rows, and their bars change.
Back on the real bins, the lab's rule sits beneath: thirty-two, thirty-two, thirty-four. Both rules merge the coffees and split off five dollars. The big numbers come from our real tokenizer, run on Sparkov: a public dataset of 1.85 million card payments. Its bins are not log-spaced but equal-frequency: each of its two hundred bins holds about the same share of past payments, one in two hundred. So bins are narrow where payments crowd. Bin twenty-three runs from four forty-seven to four sixty-four: seventeen cents wide. Between four sixty and five dollars sit two whole bins, about eleven thousand past payments. Small, everyday prices get the finest detail. The reason: every bin gets about the same number of examples to learn from. Two hundred of its two hundred and one slots are used. Slot zero is kept for a missing amount.
Now a real row, not Maya: a Sparkov payment of four dollars seven, food and dining, at 12:44 on New Year's Day, 2019. The amount becomes twenty, its equal-frequency bin. Food and dining becomes eight: the vocabulary is sorted by count, and it was eighth most common. The time becomes six: our model reads the hour in two-hour blocks, and noon to two is block six. Tuesday becomes one; Monday is zero. Then eight more fields: twelve tokens per payment. Most compare it with history, like amount against the merchant's average, or time since the last payment. Notice what is missing: no merchant id. This model sees a merchant only through numbers about it.
Choice three: when to learn. The bins and the category vocabulary were fitted only on the 1,070,966 payments before 21 March 2020. The 781,428 later payments were never seen while fitting. They are held back to validate, up to 21 June 2020, and then to test. Our leak check tests it: rebuild the tokens without the later rows; the earlier ones must not change. On a sample of cards, they didn't.
Here's where a TFM parts from a language model. An LLM cuts text into subwords, drawn from one shared vocabulary. A TFM gives every field its own rule and its own vocabulary. In our model, every rule is fitted only on the past. In practice: check which amounts your bins merge, size each vocabulary to its field, and fit every rule before the test period begins. To recap: every field becomes a token by its own rule, cut, named and learnt from the past, and each token picks an embedding row.
But four dollars alone says little. At a café it is ordinary; at a hotel it would be strange. So why does the amount care about the merchant? That's the next episode.
Chapters
Watch first
In code (PyTorch)
import torch, torch.nn as nn
# one embedding table per field
vocab = {"amount": 200, "mcc": 300, "hour": 24, "entry": 4, "merchant": 4096}
emb = nn.ModuleDict({f: nn.Embedding(n, 64) for f, n in vocab.items()})
tokens = {"amount": torch.tensor([137]), "mcc": torch.tensor([88]),
"hour": torch.tensor([14]), "entry": torch.tensor([2]),
"merchant": torch.tensor([3021])}
vectors = torch.stack([emb[f](t) for f, t in tokens.items()], dim=1) # [1, 5, 64]