C.W.K.
Stream
Lesson 03 of 07 · published

Tokenizers

~8 min · keras-nlp

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Why the tokenizer is not an afterthought

A model never sees text — it sees integers. The tokenizer is the bridge: it chops a string into sub-word pieces and maps each to an ID from a fixed vocabulary the model was trained on. That last clause is the whole story. The vocabulary is baked into the weights, so the tokenizer and the model are a married pair — swap one and the IDs point at the wrong embeddings, and you get fluent-looking garbage with no error to warn you.

Three algorithms, one job

Different model families tokenize differently: WordPieceTokenizer (BERT), BytePairTokenizer / BPE (GPT family), and SentencePieceTokenizer (T5, Llama, Gemma). They all solve the same problem — represent any string, including words the vocabulary has never seen, by falling back to smaller pieces — they just split differently. In modern KerasHub these run pure-Python with no TensorFlow dependency, so a tokenizer is no longer a reason to drag the whole TF stack into a torch or JAX project.

The practical upshot (see the Code section): you almost never construct a tokenizer by hand. You load a Preprocessor.from_preset(), and the matched tokenizer rides along inside it. Building one from a raw vocabulary= is for the rare case where you're training a brand-new vocab from scratch.

Code

Build from a raw vocab vs. load the matched tokenizer from a preset·python
# Tokenizers are included with model presets
tokenizer = keras_hub.tokenizers.WordPieceTokenizer(
    vocabulary=vocab_data,
)

# Or use the tokenizer that comes with a preset
preprocessor = keras_hub.models.BertPreprocessor.from_preset(
    "bert_base_en",
    sequence_length=128,
)

External links

Exercise

Get BERT's tokenizer. Tokenize 'Hello, world! 안녕!'. Print the token IDs and the decoded strings. Note how it handles Korean (BERT-base-en likely splits into bytes).

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.