No history yet

Tokenization Basics

Breaking Down Language

Large language models seem to understand language like we do, but their process is very different. Computers don't read words; they process numbers. The first, crucial step in turning human language into something a machine can analyze is called tokenization.

Tokenization is, generally, an early step in the NLP process, a step which splits longer strings of text into smaller pieces, or tokens.

Think of it like chopping vegetables before cooking. You take a whole sentence or paragraph and slice it into smaller, more manageable units. These units are called tokens.

Token

noun

A sequence of characters that is treated as a single unit for processing. A token can be a word, a part of a word, or even just a single character.

A simple approach is to split text by spaces. The sentence "I am learning" becomes three tokens: I, am, learning. But what about the word "unhappily"? Breaking it down further into un, happi, and ly could help a model understand its structure and meaning. This is where more advanced methods come in.

Smarter Slicing

Modern language models use subword tokenization algorithms. These methods break words into smaller, meaningful parts. This approach is powerful because it can handle new or rare words by breaking them into familiar pieces. It also keeps the vocabulary of the model from becoming enormous.

Two of the most common algorithms are Byte Pair Encoding (BPE) and WordPiece.

Byte Pair Encoding (BPE) starts by treating every individual character in the text as a token. It then iteratively finds the most frequent pair of adjacent tokens and merges them into a new, single token. This process repeats until the vocabulary reaches a desired size.

Imagine our training text is "new newest newer".

  1. The initial tokens are the individual characters: n, e, w, s, t, r.
  2. The pair e and w appears most often. So, we merge them to create a new token: ew.
  3. Now, the most frequent pair might be n and ew. We merge them to create new.

This continues for a set number of steps. After training, a word like "newer" would be tokenized into new and er.

WordPiece is very similar to BPE but makes its merge decisions slightly differently. Instead of picking the most frequent pair, it picks the pair that, when merged, increases the likelihood of the training data the most. It essentially asks, "Which merge makes the text data I was trained on most probable?"

Models like Google's BERT use WordPiece, while GPT models use BPE. The end result is similar: a flexible vocabulary of subwords that can represent any text.

Why Tokens Matter

How a text is tokenized directly impacts an LLM's performance and efficiency. Since models have limits on how many tokens they can process at once (the context window), the way text is broken down matters.

A sentence tokenized into 10 tokens uses less of the model's capacity than one tokenized into 20 tokens. This is why you often see token counts mentioned when using LLMs. Every word, every space, and every punctuation mark gets converted into one or more tokens that count against that limit.

Lesson image

Furthermore, the tokenization process influences how a model "understands" relationships between words. If "tokenization" and "tokenize" are broken into token + ization and token + ize, the model can more easily learn the connection between them. This helps the model generalize better to new words it hasn't seen before.

Understanding this foundational step is the key to working effectively with any large language model. It's the bridge between our language and the model's numerical world.

Quiz Questions 1/5

What is the primary purpose of tokenization in the context of large language models?

Quiz Questions 2/5

Subword tokenization is a crucial technique for modern LLMs because it allows them to handle words they've never seen before during training.