Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Tokenization Methods Compared

Hello! Welcome to the first lesson in our module on "Applied Transformers with HuggingFace."

In our last module, we established a strong theoretical foundation by exploring the three main families of Transformer architectures: encoder-only (like BERT), decoder-only (like GPT), and the full encoder-decoder (like T5). We learned that these models are incredibly powerful, but they share a common prerequisite: they don't understand text in its raw form. They operate on numbers.

This brings us to the crucial first step in any modern NLP pipeline: tokenization. This is the process of converting a piece of text into a sequence of numbers that a model can understand. Today, our goal is to describe the differences between word, character, and subword tokenization methods, using Byte-Pair Encoding (BPE) as our primary example. Understanding this process is fundamental to working with and fine-tuning the models we'll be exploring soon.

The Need for Tokenization: From Text to Numbers

Before a model can process text, we must convert it into a numerical format. This involves two main steps:

  1. Tokenizing: Splitting the raw text into smaller units called "tokens".
  2. Encoding: Mapping each token to a unique integer ID from a pre-defined vocabulary.

The challenge lies in the first step: how should we split the text? There are several strategies, each with significant trade-offs. Let's start by examining the two most intuitive but extreme approaches.

How tokenizers work in AI models: A beginner-friendly guide

This article from Nebius provides a clear, beginner-friendly introduction to the main tokenization methods. It will help you understand the pros and cons of the simplest approaches.

Please read the sections titled 'Word tokenization' and 'Character tokenization'. As you read, focus on the core limitations of each method, particularly regarding vocabulary size and the length of the resulting token sequences.

As you've just read, we face a dilemma:

  • Word Tokenization: This method is simple and preserves the meaning of whole words. However, it leads to a massive vocabulary. Think about it: "run", "runs", "running", and "ran" would all be separate tokens. More importantly, this approach struggles with words it has never seen before, known as out-of-vocabulary (OOV) words.
  • Character Tokenization: This method solves the OOV problem and keeps the vocabulary extremely small (just the set of all characters). However, it breaks words into their atomic parts, losing semantic meaning. It also creates very long token sequences (e.g., "Transformer" becomes 11 tokens), which increases the computational load and makes it harder for the model to learn relationships between words.

Neither extreme is ideal for the large, complex models we use today. We need a compromise that balances vocabulary size, sequence length, and the ability to handle any word.

The Middle Ground: Subword Tokenization

This is where subword tokenization comes in. It's the standard for virtually all modern language models, including the BERT and GPT families we discussed.

The core principle is:

  • Commonly used words should remain as single, whole tokens (e.g., "the", "is", "and").
  • Rare or complex words should be broken down into smaller, meaningful sub-units (e.g., "tokenization" might become ["token", "ization"]).

This hybrid approach gives us the best of both worlds:

  • It keeps the vocabulary size manageable.
  • It can represent any word by breaking it down into known subwords, effectively eliminating the OOV problem.
  • It preserves more semantic meaning than character tokenization by keeping common words intact and learning meaningful affixes (like un-, -ly, -ed).

But how do we find these "meaningful" subwords? Several algorithms can learn a subword vocabulary from a large corpus of text. The most famous is Byte-Pair Encoding (BPE).

How Byte-Pair Encoding (BPE) Works

BPE is a data compression algorithm that was adapted for tokenization. It's an iterative process that merges the most frequent pairs of characters or tokens.

Let's watch a video that provides a clear, step-by-step demonstration of the BPE algorithm.

Tokenization and Byte Pair Encoding

This video from Serrano.Academy gives a fantastic, intuitive explanation of how the BPE algorithm builds a vocabulary by iteratively merging the most common pairs of tokens.

Please watch from 02:23 to 06:11. The first part (02:23 - 05:06) walks through a concrete example of building a vocabulary. The second part (05:06 - 06:11) shows the practical benefit of this method for handling grammatical variations in unknown words.

To reinforce this, let's read another explanation from the Hugging Face documentation, which is the company behind the transformers library we will be using.

Summary of the tokenizers

The Hugging Face documentation provides a concise, text-based explanation of BPE with a similar example. Reading this will help solidify your understanding of the merge process.

Read the section 'Byte-Pair Encoding (BPE)' up to (but not including) the subsection 'Byte-level BPE'. Notice how the vocabulary is built up by identifying the most frequent pair at each step.

BPE is not the only subword algorithm. Others like WordPiece (used by BERT) and Unigram (used by T5) exist. They follow a similar spirit but use different criteria for merging or splitting tokens. For instance, while BPE merges the most frequent pair, WordPiece merges the pair that maximizes the likelihood of the training data. For now, understanding BPE is sufficient to grasp the core concept of subword tokenization.

Putting It All Together: A Comparison

We've covered the three major strategies for tokenization. This mind map provides a great visual overview of the landscape.

Tokenization Methods Mind Map
This mind map categorizes tokenization into three primary branches: Word-Level, Character-Level, and Subword. It shows how methods like BPE and WordPiece fit into the Subword category, which is the dominant approach in modern NLP.

To crystallize the differences, let's turn back to the Nebius article, which contains a very useful summary table.

How tokenizers work in AI models: A beginner-friendly guide

This final reading summarizes the key trade-offs between the three tokenization methods in a convenient table.

Scroll down to the table under the 'Subword tokenization' section. Review the advantages, limitations, and typical use cases for each method.

Conclusion

In this lesson, we dissected the critical process of tokenization, the bridge between human language and machine understanding.

Key Takeaways:

  • Tokenization is the process of breaking text into smaller units (tokens) and converting them into numerical IDs.
  • Word Tokenization is intuitive but suffers from huge vocabularies and an inability to handle out-of-vocabulary (OOV) words.
  • Character Tokenization solves the OOV problem with a tiny vocabulary but creates long, semantically weak sequences that are computationally expensive.
  • Subword Tokenization (e.g., BPE, WordPiece) is the modern standard. It balances vocabulary size and sequence length by keeping common words whole and breaking rare words into meaningful pieces, effectively handling any text thrown at it.

Preview of the Next Lesson

Now that we understand how text is prepared, we can start working with it. In the next lesson, we'll take our first dive into the core mechanism of the Transformer: attention. You will learn to compute scaled dot-product attention step-by-step in PyTorch on a small example. This will be our first hands-on calculation and will bring the theory we've been discussing to life.

Can't find a good explanation? Sign up and we'll make it for you

Sign up