Notes

Subword Tokenization

Ask a model to read text and something has to decide what counts as one unit. Whole words run out the instant someone types a surname or a typo; single characters never run out, but they stretch every sentence into a sequence too long to learn from comfortably. Subword tokenization is the settlement between the two.

Pieces learned from a corpus
The vocabulary is not written by hand — it is learned. A trainer scans a large body of text, keeps the fragments that recur often enough to deserve an entry, and stops at a vocabulary size you choose. Frequent words survive whole, rare ones arrive in parts, and no string is unrepresentable. The schemes differ in how the pieces are picked:

  • Byte-Pair Encoding repeatedly merges the commonest adjacent pair; its byte-level form does this over raw bytes, so any character at all can be encoded.
  • WordPiece merges the pair that most improves the likelihood of the training text, not simply the most frequent one.
  • Unigram language-model tokenisation starts from a large candidate set and prunes it, scoring whole segmentations by probability.
  • SentencePiece trains these directly on raw text with no prior word splitting — which is what makes Japanese or Thai tractable.

What rides on it
It retires the UNK token, so an open vocabulary stops being a hole in the model, and it lets walk, walked and walking share material the embeddings can generalise over. It also fixes sequence length: text that fragments badly costs more compute and more of a fixed context window. The failures are quiet ones — specialist jargon the trainer never saw is shredded into single letters, character offsets stop lining up with the spans an extraction task must return, and two models built on different vocabularies cannot share weights.

Subword tokenization splits text into frequently occurring word fragments drawn from a fixed, data-learned vocabulary, so any string — including a word never seen in training — can be encoded without an UNK token. Schemes such as Byte-Pair Encoding, WordPiece and unigram language-model tokenisation differ in whether pieces are merged greedily by frequency or scored probabilistically. The choice fixes vocabulary size and sequence length, bounding both model cost and what later layers can distinguish.

Imagine reading a word you have never met — defibrillator, perhaps. You do not stop dead. You break it into chunks that look familiar and carry on.

Subword tokenization gives software the same trick. Before it learns anything about language, the system reads a huge amount of text and makes itself a list of useful word fragments. Everyday words stay in one piece. Unusual ones — a brand name, a medical term, a misspelling — get chopped into smaller parts it already knows. So the software is never stuck in front of a new word, and it does not need an impossibly long dictionary to cope with the words people actually write.