liveBook · Manning

Tokenizer

As explained in AI-Powered Search

A tokenizer is a text-processing component that segments text into tokens according to a model's tokenization scheme.

A tokenizer converts text into the token sequence expected by a language model. For Transformer encoders, it splits text into vocabulary-defined word pieces before encoding produces one vector per token. section 13.3.1

Figure 13.4 The Transformer encoder
figure

Follow the Transformer encoder pipeline from tokenization through contextual vector representations. section 13.3.1

A tokenizer applies the model's predefined tokenization scheme:

  • Splits text into word pieces represented in the model vocabulary. section 13.3.1
  • Represents “It’s” as three pieces: “it”, an apostrophe, and “s”. section 13.3.1
  • Adds special pieces such as [CLS] and [SEP] to mark sentence boundaries. section 13.3.1
  • Produces token values that the Transformer accepts as input rather than raw words. section 14.3.1

Tokenization also shapes long inputs for training. A sliding window divides a context into overlapping token lists, while truncation limits each list and padding makes every tensor the same length. section 14.3.1

Question-answering tokenization

Load a tokenizer trained for roberta-base, tokenize each question and context pair, and create tensors with a maximum of 384 tokens. Consecutive windows overlap by 128 tokens; padding fills the final window when necessary. section 14.3.1

Listing 14.9 Tokenizing our training set
# This function adapted from:
# https://github.com/huggingface/notebooks/blob/master/examples/
#question_answering.ipynb
# Copyright 2001, Hugging Face. Apache 2.0 Licensed.
from datasets import load_from_disk
from transformers import RobertaTokenizerFast

file = "data/question-answering/question-answering-training-set"
datadict = datasets.load_from_disk(file) #1
tokenizer = from_pretrained("roberta-base") #2
...



def tokenize_dataset(examples):
  maximum_tokens = 384  #3
  document_overlap = 128  #4
  pad_on_right = tokenizer.padding_side == "right" #5
  tokenized_examples = tokenizer(  #6
    examples["question" if pad_on_right  #6
                        else "context"],  #6
    examples["context" if pad_on_right  #6
                       else "question"],  #6
    truncation="only_second" if pad_on_right  #6
                             else "only_first",  #6
    max_length=maximum_tokens,  #6
    stride=document_overlap,  #6
    return_overflowing_tokens=True,  #6
    return_offsets_mapping=True,  #6
    padding="max_length"  #6
  ) 
  ...  #7
  return tokenized_examples

tokenized_datasets = datadict.map(  #8
  tokenize_dataset,  #8
  batched=True,  #8
  remove_columns=datadict["train"].column_names)  #8

Inspect the training-set tokenization setup, including tokenizer loading, maximum length, overlap, padding direction, and paired inputs. section 14.3.1

How AI-Powered Search covers it

How deep it goes
Explains word-piece segmentation, special tokens, sliding windows, overlap, truncation, and padding without detailing tokenizer internals.
Examples
Uses “It’s” word pieces, [CLS] and [SEP], and 384-token question-context windows overlapping by 128 tokens.
What it assumes
Assumes familiarity with Transformer models and tensors while establishing word pieces, vocabularies, token values, and padding.
What you can do after
Apply tokenization with a pretrained RoBERTa tokenizer to prepare fixed-shape inputs for model training and evaluation.
How it connects
Connects tokenizers to Transformers, BERT, RoBERTa, embeddings, dense vectors, semantic search, and question answering.

Also explained in

Compare how 16 books explain tokenizer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Search. The section links point to the parts of the book it draws on.