Concepts / Neural Machine Translation of Rare Words with Subword Units

Neural Machine Translation of Rare Words with Subword Units

Common words can stay whole, while rare or unfamiliar words are assembled from smaller pieces that the vocabulary already contains.

  • Programming

Why Rare Words Break Simple Tokenizers

Language models do not read text as whole sentences. They read a sequence of tokens, and the tokenizer decides where one token ends and the next begins. That decision matters more than it first appears. It shapes how much text fits into a model's input, how common and rare words are represented, and how the model handles spellings it has never seen before.

Consider the two obvious designs. If a tokenizer treats every whole word as one token, it needs a very large vocabulary, and any word it did not see during training has no entry at all. If it treats every character as one token, the vocabulary stays small, but sequences become long and individual characters carry little meaning on their own. Subword units sit between these two extremes. Common words can stay whole, while rare or unfamiliar words are assembled from smaller pieces that the vocabulary already contains.

Byte Pair Encoding, usually abbreviated BPE, is one of the tokenization methods used in natural language processing. It is the method behind the tokenizer used to train GPT-2. The name points to its core operation: the tokenizer works on pairs of adjacent symbols and combines frequent pairs into single units. Because the units it builds are pieces of words, BPE belongs to the family of subword tokenization methods.

The method was introduced to machine translation in the 2015 research paper titled Neural Machine Translation of Rare Words with Subword Units by Sennrich et al., available on arXiv. The title states the motivation directly: rare words are the hard case, and subword units are the proposed way to handle them.

Predicting the Most Frequent Pair

BPE starts from a basic set of symbols and looks for the adjacent pair that appears most often. Before reading on, try this on a tiny sequence. The symbols below are a placeholder alphabet, not real text, so you can focus only on the counting rule.

What do you think happens?

In the sequence a b a b a b c, which adjacent pair occurs most often?

  • a b

  • b a

  • b c

Reveal answer
Answer:

a b

Reading left to right, the adjacent pair a b occurs at positions 1-2, 3-4, and 5-6, so it appears 3 times. The pair b a occurs twice (positions 2-3 and 4-5), and the pair b c occurs once (positions 6-7). The most frequent pair is therefore a b, and it is the one a single merge step would combine.

The guess you just made is exactly the decision BPE repeats at every step. The method does not look at meaning. It only counts adjacent pairs, picks the most frequent one, and records the result as a new unit.

One Merge Step, Traced

A BPE tokenizer is easiest to understand as a pipeline with two stages: training and application. During training, the method starts from a basic set of symbols and repeatedly merges the most frequent adjacent pair into a new symbol. Each merge adds one entry to the vocabulary. The merge itself is a simple replacement: every occurrence of the chosen pair in the current sequence is replaced by one unit.

Before

a b a b a b c

Seven single-symbol tokens. The adjacent pair a b occurs 3 times.

After

ab ab ab c

Four tokens. The three a b pairs were each replaced by the single unit ab, and the c was left unchanged.

aabbabaabbcabcmergedmergedmergedkept
After merging the most frequent adjacent pair a b, which positions are replaced and what does the sequence look like?
Worked example

Tracing one merge on a symbol sequence

Starting from the sequence a b a b a b c, apply one BPE merge step and report what changed.

  1. 1
    Count adjacent pairs

    Scan the sequence from left to right. The pair a b appears 3 times, b a appears 2 times, and b c appears 1 time.

  2. 2
    Choose the most frequent pair

    The pair a b has the highest count, so it is the merge candidate. The training rule is to merge the most frequent adjacent pair.

  3. 3
    Record the new unit

    Add the new unit ab to the vocabulary. The vocabulary now holds a, b, c, and ab.

  4. 4
    Replace every occurrence

    Replace each a b pair with ab. The sequence becomes ab ab ab c. Three positions pairs were replaced by three ab units, and the final c has no partner to merge with, so it is unchanged.

Result

The sequence shrank from seven symbols to four, and the vocabulary gained one new unit, ab.

Notice what the merge did not do. It did not decide what ab means. It only recorded that the adjacent pair occurred often enough to be worth a single unit. The meaning of a token emerges from the sequence statistics of the training text, not from any rule about the symbols themselves.

Repeating Merges Builds the Vocabulary

The loop between counting and merging is the heart of the method. Each pass looks at the current sequence of units, finds the pair that occurs most often, and replaces that pair with a single unit. Because the merged unit becomes a building block for later merges, longer units can emerge from shorter ones over many passes. The example here is deliberately small. Real training runs over a large body of text and performs many merges, but the rule at each step is the same.

Round 0a, b, cRound 1adds abRound 2adds ababmerge a bmerge ab ab
On each repeated merge round, which unit is added to the vocabulary, and how does the vocabulary move from characters toward larger subword units?

Round 2 uses the sequence produced by Round 1, ab ab ab c. Counting its adjacent pairs gives ab with ab occurring twice and ab with c occurring once. The pair ab ab is therefore the most frequent, and merging it produces the unit abab. Scanning left to right and merging non-overlapping occurrences turns ab ab ab c into abab ab c. The vocabulary now holds a, b, c, ab, and abab, so a unit that is longer than one character has been built from a unit that was itself built from two characters.

a b3 occurrencesb a2 occurrencesb c1 occurrence
Which adjacent pairs are counted in the sequence a b a b a b c, and how does the highest count determine the next merge?

The key-value view makes the decision rule explicit. Each adjacent pair is a key, and its count is the value. The merge chooses the key with the largest value. Ties are not discussed in the source material, so this article does not assume a tie-breaking rule.

Training Versus Applying the Vocabulary

Training and application are distinct stages. Training builds the vocabulary by running the counting and merging loop over a body of text. Application uses the finished vocabulary to split new text into tokens. Once training is complete, the merge list is fixed, and new input is cut using the units that training produced.

  1. 1
    Training: start from a basic set of symbols, such as individual characters.
  2. 2
    Training: count every adjacent pair in the current sequence.
  3. 3
    Training: merge the most frequent pair into a new unit and add that unit to the vocabulary.
  4. 4
    Training: repeat the count and merge loop until the vocabulary is built.
  5. 5
    Application: take new or rare text that was not in the training data.
  6. 6
    Application: split it into the smallest pieces the vocabulary already holds, so that a rare word becomes a sequence of known subword units.

This is how a rare word is handled. A word that was never seen during training does not need its own entry. It is assembled from smaller pieces that the vocabulary already contains. The source describes this as combining smaller pieces the vocabulary holds, rather than requiring a dedicated token for every possible word.

Mistakes Learners Make with Merges

  • Common mistake

    Assuming the merge chooses the pair with the best meaning

    What it looks like

    Seeing ab appear after one merge and concluding that the method recognized ab as a word.

    Why it fails

    BPE only counts adjacent symbol pairs. It has no notion of meaning, so the most frequent pair is chosen purely by count.

    Do this instead

    Describe a merge as a frequency-driven replacement, not as a semantic discovery.

  • Common mistake

    Merging every pair in the sequence at once

    What it looks like

    Turning a b a b a b c into a single unit in one step.

    Why it fails

    Each step merges only the most frequent adjacent pair. Larger units appear over many rounds, not in one pass.

    Do this instead

    Apply one merge, recount the pairs on the new sequence, and then choose the next merge.

  • Common mistake

    Treating training and application as the same step

    What it looks like

    Expecting the tokenizer to keep adding new merges while it splits a new sentence.

    Why it fails

    Training builds the vocabulary, and application uses the finished vocabulary to split new text.

    Do this instead

    Keep the two stages separate in your mental model: build first, then apply.

  • Common mistake

    Expecting a rare word to need its own token

    Why it fails

    Subword units let rare words be assembled from smaller pieces the vocabulary already holds.

    Do this instead

    Think of a rare word as a sequence of known pieces rather than a single unknown entry.

Trace a Second Merge Yourself

EASY

Start from the symbol sequence a a a b. Count every adjacent pair, identify the most frequent one, and write the sequence that results after one merge step of that pair. Then state how many symbols the sequence now contains and which new unit was added to the vocabulary.

Hints
  • There are three adjacent pairs to count: positions 1-2, 2-3, and 3-4.
  • Count the pair a a first, then the pair a b.
  • After merging a a, the sequence should contain one a a unit and the leftover symbols.

Where to Inspect or Build BPE

The source material points to three concrete resources. Each one lets you move from the concept to real code or to a visual demonstration, and each suits a different goal. Use the concept in this article as the mental model, then turn to those resources to see the same counting and merging loop run on real text.

Best practice

Build the mental model before reading implementations. When you open a real BPE implementation later, look for the same two parts: a training loop that counts pairs and records merges, and an application step that uses the recorded merges to split new text.

Summary of the Merge Loop

  1. 1
    BPE is a tokenization method used in natural language processing, including the tokenizer used to train GPT-2.
  2. 2
    Training repeatedly counts adjacent symbol pairs, merges the most frequent pair into a new unit, and adds that unit to the vocabulary.
  3. 3
    Application uses the finished vocabulary to split new text, so rare words can be built from smaller known subword units.
  4. 4
    Each merge step is a simple replacement, and longer units emerge from shorter ones over many rounds.

At a glance

  • BPE is a subword tokenization method used in natural language processing, including the tokenizer used to train GPT-2.
  • BPE builds its vocabulary by repeatedly merging the most frequent adjacent pair of symbols into a new unit.
  • Training builds the vocabulary, and application uses it to split new text, which lets rare words be assembled from known pieces.
  • A single merge replaces every occurrence of the chosen pair, shrinking the sequence and adding one unit to the vocabulary.

Related concepts

More concepts starting with N