liveBook · Manning

Token type IDs

As explained in Hugging Face in Action

Token type IDs are integer labels that identify which input segment each token belongs to.

Token type IDs are integer labels that identify which input segment each token belongs to. Hugging Face tokenization adds them alongside input_ids and attention_mask in the processed dataset. section 5.2.2

from transformers import AutoTokenizer

dataset = load_dataset(dataset_id)
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
tokenized_dataset = dataset.map(
    lambda examples:
        tokenizer(examples['text'],
                  truncation = True,
                  padding = 'max_length'),
    batched = True)

Run the tokenizer mapping to produce tokenized dataset fields, including token_type_ids, with truncation and fixed-length padding. section 5.2.2

Tokenized dataset output

After tokenization, each dataset split exposes token_type_ids as a feature alongside text, label, input_ids, and attention_mask.

How Hugging Face in Action covers it

How deep it goes
Defines the field and places it in tokenizer output, but does not explain its values or segment-pair processing.
Examples
Shows token_type_ids as a feature in train, test, and unsupervised dataset splits after mapping a tokenizer.
What it assumes
Assumes familiarity with datasets, tokenizers, and transformer model inputs.
What you can do after
Evaluate tokenization output by inspecting token_type_ids alongside the other model input fields.
How it connects
Connects tokenization with input IDs, attention masks, padding, BERT, and Hugging Face Transformers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hugging Face in Action. The section links point to the parts of the book it draws on.