Sub topic 2
Vocabularies and the Out-of-Vocabulary Problem
This chapter assumes Basic Python, dictionaries, loops, frequency counts, and the idea that a token is an input unit for a language model.
What is a vocabulary?
A vocabulary is the fixed collection of tokens that a tokenizer can produce. Each token has an integer ID. A model receives these IDs rather than raw text.
A word-level vocabulary might contain entries such as the, cat, and running. A character-level vocabulary might contain letters, digits, spaces, and punctuation. A subword vocabulary contains reusable pieces such as run, ##ning, or ing.
The vocabulary is usually built from a training corpus. The tokenizer counts or scores candidate units, keeps a selected set, and assigns each unit an ID. During inference, the tokenizer must represent text using only this set.
The out-of-vocabulary problem
An out-of-vocabulary token, or OOV token, is a token that does not appear in the vocabulary. Word-level tokenization makes OOV errors common because language contains many names, spelling variations, technical terms, and newly created words.
For example, a vocabulary trained before a product launch might contain phone but not smartphone. A simple word tokenizer could replace the whole word with an unknown token such as [UNK].
Once the word has become [UNK], the model cannot recover its spelling or internal structure. The same representation may be used for an important name, a rare chemical term, and a typing error.
vocabulary = {"the": 0, "cat": 1, "sat": 2, "[UNK]": 3}
text = "the fox sat"
tokens = [word if word in vocabulary else "[UNK]"
for word in text.split()]
ids = [vocabulary[token] for token in tokens]
print(tokens) # ['the', '[UNK]', 'sat']
print(ids) # [0, 3, 2]Why subwords reduce OOV errors
Subword tokenization breaks an unfamiliar word into smaller known pieces. If the vocabulary contains smart, phone, and smartphone is absent, the tokenizer can use smart and phone.
This gives the model partial information about the word. It also lets the vocabulary represent related forms efficiently:
play,playing, andplayedcan shareplay.un,help, andfulcan representunhelpful.- A rare name can be split into character-like pieces.
A subword vocabulary still needs a fallback for text that cannot be split. Standard BPE and WordPiece systems often use [UNK]. Byte-level methods can represent every byte, so they can avoid unknown tokens, although unusual text may become many tokens.
The vocabulary-size trade-off
A larger vocabulary includes more complete words and longer subwords. This can shorten sequences because fewer tokens are needed. However, it requires a larger embedding table, increases model memory, and may devote entries to rare forms.
A smaller vocabulary has the opposite trade-off. It uses less memory and shares pieces across more words, but sequences become longer and may require more computation.
| Vocabulary choice | Typical benefit | Typical cost |
|---|---|---|
| Small | Better sharing and lower embedding memory | Longer sequences |
| Large | Shorter sequences and more whole-word pieces | More memory and rarer entries |
| Byte-level | No unknown characters | Very long sequences for some text |
Training data matters as much as vocabulary size. A vocabulary trained mostly on English may split another language into many small pieces. A vocabulary trained on one domain may handle its common terms well but fragment terms from another domain.
Exercise
A word-level tokenizer knows data but receives database. What problem can a subword tokenizer avoid, and what trade-off might it introduce?
Show answer
It can split the word into known pieces such as data and base instead of producing [UNK]. The trade-off is that the sequence may contain more tokens than a vocabulary containing the whole word.