Skip to the content

fatehi.devx2Learning

Deep learning

Byte-pair encoding

Builds a vocabulary of word pieces by starting from characters and merging the most frequent neighbouring pair, again and again.

Words are too many, letters too small

A vocabulary of whole words is never complete: names, numbers, rare and new words keep appearing, and each unknown word would become one “unknown” token. A vocabulary of characters is complete but makes sequences long and leaves the network to learn every word from its letters. Byte-pair encoding (BPE) sits between the two: frequent words stay whole, rare ones are written in pieces, and any text can be written at all.

Learning the merges

Start from a corpus with every word cut into characters. Count every pair of neighbouring pieces, each word counted as often as it occurs. Merge the most frequent pair into a new piece everywhere, add it to the vocabulary, and count again. Repeat until the vocabulary has the size wanted; the list of merges, in order, is the result.

A small example: the words low (5 times), lower (2), newest (6) and widest (3). The pair e s occurs 6 + 3 = 9 times, more than any other (ties go to the pair seen first), so it merges into es; then es t (9), l o (7), lo w (7), n e (6), ne w (6). After six merges low is one piece, newest is new + est, and widest is w i d + est.

The example above, merge by merge. Each step joins the most frequent pair of neighbouring pieces everywhere it occurs.

To cut a new text, apply the merges in the order they were learned. A word never seen, such as lowest, is still written, as low + est. The method comes from data compression; Sennrich, Haddow and Birch brought it to translation in 2016. Variants work on bytes instead of characters, so that no character is ever unknown.

Test yourself

  1. In the example, why is e s merged first?

    It occurs 9 times (6 in newest, 3 in widest), as often as s t and more than any other pair; ties go to the pair seen first.

  2. How is a word the merges never saw written?

    In smaller pieces: apply the learned merges in order, and whatever cannot be merged stays as single characters (or bytes). lowest becomes low + est.

  3. What did the shared English–German vocabulary allow the paper to do?

    Use one embedding matrix for the encoder, the decoder and the output layer.

Where it leads

Kept in this browser only, never sent.

Paths through it