Skip to Content

Convert raw text into integer IDs that the model’s embedding layer can index. Subword tokenisation strikes a balance between vocabulary size and sequence length.

Data Flow

text: "unhappiness"

BPE / WordPiece:
      un  happi  ness
      ^   ^      ^
     IDs 1023  876 5421

What it is

Why subword?

Code

from transformers import AutoTokenizer
 
tok = AutoTokenizer.from_pretrained("gpt2")
ids = tok("unhappiness is rare").input_ids
# [10139, 318, 3127, 220, 816]
 
print(tok.decode(ids))
# 'unhappiness is rare'

Pitfalls

Analogy

Breaking a long word into common syllables that fit on a small set of wooden blocks.

Interview tip: Always state: token ID 318 means different things in different models. Candidates who miss this lose credibility fast.

Advertisement