5.1 Training data and batching
What the paper trained on, how it cut the text into tokens, and how it grouped sentences into batches.
First the sentence as our models read it: one token per word or punctuation mark, each with its id, its row number in the vocabulary. A word the models never saw becomes <unk>, id 3. Then byte-pair encoding, learned from our training sentences, starts from single letters and merges the most frequent pair of neighbours, one merge per step. A counter shows the pieces against the characters.
What this section says
The English–German models trained on the standard WMT 2014 data set, about 4.5 million sentence pairs. The text was split with byte-pair encoding into a vocabulary of about 37,000 tokens shared by both languages: frequent words stay whole, rare ones are cut into pieces (the clip above learns such pieces from our sentences). For English–French the paper used a much larger set, 36 million sentences, and a vocabulary of 32,000 word pieces.
Training does not show the model one sentence at a time but a batch of many at once, which the GPU computes together. Every sentence in a batch is filled up with padding to the length of the longest, and that padding is computed for nothing. The paper therefore grouped sentence pairs of about the same length, and sized each batch by tokens, not sentences: about 25,000 English and 25,000 German tokens per batch.
Daniel meets both ideas daily: API prices and context limits are counted in byte-pair tokens, and inference servers batch requests of similar length for the same reason.
In the paper's words
“Sentence pairs were batched together by approximate sequence length.”
“Each training batch contained a set of sentence pairs containing approximately 25000 source tokens and 25000 target tokens.”
Quoted from Vaswani et al., Attention Is All You Need, arXiv:1706.03762v7(opens in a new tab). The figures on this page are redrawn, not copied.
The notation
| Symbol | Meaning | In the paper | Our miniature |
|---|---|---|---|
| \(\text{WMT 2014}\) | the standard translation data sets of 2014 | 4.5 million English–German pairs | our own synthetic pairs: 200,000, about 80,000 different |
| \(\text{vocabulary}\) | the tokens the model knows | about 37,000 byte-pair tokens, shared | 201 whole words and marks, shared |
| \(\text{batch}\) | the sentences trained on together in one step | about 25,000 source and 25,000 target tokens | 128 sentence pairs, drawn at random |
Try it
336 tokens of sentences, 40 tokens of padding.
The first 48 of our held-out English sentences, cut into batches of 8. Each sentence is a row of tokens, each batch padded to its longest sentence; padding is drawn as empty cells. Switch between the order they come in and sorted by length, and watch the padding shrink.
How our miniature differs
Our corpus is synthetic and small, our vocabulary whole words, so the models' tokens stay readable; byte-pair encoding is only demonstrated (the clip). Our training script draws 128 random pairs per step and does not sort by length. On the figure's 48 sentences, sorting cuts the padding from 40 tokens to 8.
Prepared in the Course by
Check yourself
Three questions a reader of this section should now be able to answer. The lab checks your answer against the paper and our models.
The paper's vocabulary
About how many byte-pair tokens did the paper's shared English–German vocabulary have?
Padding saved
On the figure's 48 sentences in batches of 8, how many padding tokens does sorting by length save?
Why sort
Why did the paper batch sentence pairs of about the same length together?