Lessons › Chapter 10: Sequences and text
Tokenization
Splitting on spaces is a start. But English has hundreds of thousands of words, plus names, typos and new slang. A word-only vocabulary would be huge and still miss things.
Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.
Start the interactive lessonStep by step
The chatbot idea
Customers ask the same questions all day. Maya proposes a support chatbot called Pip. First, text has to become numbers.
Models need numbers, not letters
A neural network only works with numbers. So before a language model can read “unbelievably good”, the text must be chopped up and turned into numbers.
Split into words?
Splitting on spaces is a start. But English has hundreds of thousands of words, plus names, typos and new slang. A word-only vocabulary would be huge and still miss things.
Split rare words into pieces
Modern models split text into tokens: common words stay whole, rarer ones break into familiar pieces like “un” + “believ” + “ably”. Any text at all can be built from a vocabulary of tens of thousands of pieces.
Every piece gets an ID
Each token has a fixed ID number in the vocabulary. The model never sees letters at all, only this list of numbers.
In real life: why chatbots count tokens
Chat assistants measure your message in tokens, not words or letters. Message limits and API prices are counted in tokens too. A rough rule of thumb for English: 100 tokens is about 75 words.
Quick check: Why split rare words into smaller pieces?
- So any word can be handled with a limited vocabulary (correct)
- To make the text longer
- To hide the meaning
Sub-word pieces can spell out any word, including ones never seen in training.
Pseudocode
text = "unbelievably good" words = ["unbelievably", "good"] tokens = ["un", "believ", "ably", " good"] # sub-word pieces ids = [403, 18821, 1867, 1695] # example ID numbers model sees: [403, 18821, 1867, 1695]
Key terms
- vocabulary
- The fixed list of all tokens a model knows, each with its own ID number.
- token
- A small piece of text, like a word or part of a word, that a language model reads.
In the news
Recent models and papers that build on this idea: