liveBook · Manning

Tokenization

As explained in Grokking AI Algorithms, Second Edition

Tokenization is the process of breaking text into a sequence of discrete units for computational language processing.

Tokenization breaks text into smaller units called tokens so a language model can process it. Tokens may be words, character pieces, or punctuation rather than complete words. section 11.9

A practical tokenization workflow moves through these steps:

  • Split training text into initial pieces, such as individual characters, and append an end-of-word marker like </w> so words remain distinguishable. section 11.9
  • Count adjacent token pairs, called bigrams, then repeatedly merge the most frequent pair to form larger reusable tokens. section 11.9
  • Assign each vocabulary token an integer ID, turning tokenized text into a numerical token stream for model training. section 11.10
  • Divide the stream into batches that fit the model's context window before training or inference. section 11.10

From words to a token stream

The sentence “Alice was beginning to get very tired of sitting” can initially be represented as word tokens. A word such as unbelievable may instead become un, believ, able, allowing related pieces to recur across words. With a character-based procedure, frequent pairs are merged step by step; the resulting vocabulary can include pieces such as in, ing, ers, th, and punctuation. Those tokens receive IDs and become a numerical sequence, which is then sliced into training batches. section 11.9 section 11.10

Figure 11.19 Tokenized training data as a token stream
11_19

Visualizes tokenized training data as a stream of token IDs. section 11.10

def batch_tokens(token_stream, context_window_size):
   batches = []

   start_index = 0
   while True:
       end_index = start_index + context_window_size     #A

       if end_index > len(token_stream):      #B
           break

       batch = token_stream[start_index:end_index]      #C
       batches.append(batch)

       start_index += 1      #D

   return batches

Shows Python-style sliding-window batching that extracts sequences from a continuous token stream. section 11.10

How Grokking AI Algorithms, Second Edition covers it

How deep it goes
Explains character initialization, end-of-word markers, bigram counts, repeated BPE merges, vocabulary IDs, and batching.
Examples
Uses Alice's sentence, the word unbelievable, character markers, bigram tables, token-ID listings, token streams, and Python batching code.
What it assumes
Assumes basic text handling and introduces from zero why language models need smaller units instead of intuitive human-readable words.
What you can do after
Build it from scratch by merging frequent character pairs, assigning token IDs, and slicing token streams into training batches.
How it connects
Connects tokenization with vocabulary, vectorization, byte-pair encoding, bigrams, token streams, context windows, and batches.

Also explained in

Compare how 25 books explain tokenization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Algorithms, Second Edition. The section links point to the parts of the book it draws on.