Learn the MachineDansk

Explained with an interactive demo

What is a tokenizer?

A tokenizer is the program that splits text into tokens and gives each token a number before a language model can read the text. The model never sees letters or words, only a list of numbers. Type your own text below and watch it being split.

The demo needs JavaScript. The text below explains what it shows.

Embed this demo on your site

Copy the code into your page. The demo loads from learnthemachine.com, so it stays up to date, and the link under it shows where it comes from.

Why not just letters or words?

You could give every letter its own number. Then the vocabulary is small, but texts become very long, and the model has to learn to put letters together into words by itself. You could also give every word its own number. Then texts are short, but the vocabulary becomes huge, and any word that was not included from the start cannot be read at all.

Modern tokenizers take a middle path: subword tokens. Common words such as “and” and “the” become one token, while rare words are split into smaller pieces the model knows. A new word can always be written with those pieces, so nothing is completely unknown.

How does a tokenizer learn its pieces?

The most widely used method is byte pair encoding, BPE. You start from single characters, count which two neighbouring characters appear together most often, merge them into a new token, and repeat. The slider in the demo sets how many of those merges the tokenizer has learned. At 0 it is a pure character tokenizer. The more merges, the larger the vocabulary and the fewer tokens per text. See BPE step by step.

A tokenizer is trained before the model is trained, and it does not change afterwards. The model learns a vector for every token number in the vocabulary.

Unknown characters and bytes

The demo’s tokenizer starts from the alphabet, the digits and the characters in its training text. Type anything else, such as an emoji, and it becomes [UNK], “unknown”. Older tokenizers had that problem. Most newer tokenizers for language models work on bytes instead: they start from the 256 possible bytes, so any text can be written, including emojis and rare scripts.

Why does it matter to you?

Try real tokenizers on your own text

In the free Tokenization lab you see how real language models’ tokenizers split your text, compare them, and train BPE yourself right in the browser.

Open the Tokenization lab

More explainers