liveBook · Manning

Tokenizer

As explained in Learn Generative AI with PyTorch

A text-processing component that splits text into tokens and converts text to a discrete token sequence for modeling.

A tokenizer splits text into model-ready tokens, then maps those tokens to discrete indexes. A pretrained subword tokenizer handles multilingual text more efficiently than a custom word-level tokenizer. section 10.1.1

A practical tokenization pipeline:

  • Load a pretrained XLM tokenizer through Hugging Face Transformers. section 10.1.1
  • Split sentences into complete words, punctuation, or subwords such as “fr” and “ench,” using </w> to mark word boundaries. section 10.1.1
  • Add BOS and EOS markers, count token frequencies, retain the 50,000 most common entries, and reserve indexes for PAD and UNK. section 10.1.1
  • Convert tokens to indexes for modeling, then reverse the mapping and remove boundary markers to reconstruct readable text. section 10.1.1

English–French tokenization

“I don't speak French.” becomes ['i</w>', 'don</w>', "'t</w>", 'speak</w>', 'fr', 'ench</w>', '.</w>']; “Je ne parle pas français.” becomes ['je</w>', 'ne</w>', 'parle</w>', 'pas</w>', 'franc', 'ais</w>', '.</w>']. section 10.1.1

import pandas as pd
  
df=pd.read_csv("files/en2fr.csv")                                #1
num_examples=len(df)                                             #2
print(f"there are {num_examples} examples in the training data")
print(df.iloc[30856]["en"])                                      #3
print(df.iloc[30856]["fr"])                                      #4

Load the translation dataset and inspect paired English and French phrases before tokenization. section 10.1.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains subword splitting, boundary markers, vocabulary construction, index mapping, and reconstruction.
Examples
Uses English–French sentence pairs, subword outputs, special tokens, frequency counts, indexes, and reconstructed phrases.
What you can do after
Apply it via Hugging Face Transformers and custom dictionaries to convert multilingual sentences into model indexes.
How it connects
Connects tokenization with Transformers, machine translation, subword tokenization, vocabulary, and word-level tokenization.

Also explained in

Compare how 16 books explain tokenizer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.