Concepts / Building a Vocabulary from Training Text

Building a Vocabulary from Training Text

A tokenizer's vocabulary can be extended with special context tokens that carry meanings ordinary words cannot express.

  • Programming

Text the Vocabulary Never Saw

Every tokenizer works from a fixed vocabulary, which is a table that assigns each known token an integer ID. The tokenizer looks up each piece of incoming text in that table and emits the matching ID. The difficulty is that real text is open-ended. A new product name, a rare dialect form, or a misspelling can appear at the exact moment the tokenizer is being used, even if it never appeared in the material the vocabulary was built from.

What do you think happens?

The tokenizer reads a word that is not in its vocabulary. What should it emit in the token sequence?

  • Nothing, and it should drop the word silently

  • The ID of the closest similar word in the vocabulary

  • A special token that stands in for an unknown word

Reveal answer
Answer:

A special token that stands in for an unknown word

The source describes the <|unk|> token as the tokenizer's fallback. When a word is not part of the vocabulary, the tokenizer emits an <|unk|> token in its place and keeps going, rather than stopping or discarding the text.

This article shows how a vocabulary can be extended with special context tokens. These tokens handle unfamiliar words and also make clear where one unrelated piece of text stops and another begins.

Where Special Tokens Live in the Vocabulary

An ordinary vocabulary is built from the words and fragments found in the training data, and each one receives an ID. A special context token is an additional entry that does not come from the text itself. Its job is to carry a meaning that ordinary words cannot express, such as 'this word was unknown' or 'a new document starts here'.

Adding a special token means giving it its own ID, placed alongside the existing ones. Once that is done, the tokenizer can emit it exactly like any other token. The source illustrates this with a vocabulary that is extended by two entries: 783 is assigned to the unknown-word token and 784 to the document-boundary token. Those particular numbers depend on how many entries the existing vocabulary already holds, so the lesson to keep is that the special tokens are ordinary vocabulary members once they have been added.

ordinary wordfrom training data<|unk|>783<|endoftext|>784
How does each vocabulary entry, including <|unk|> and <|endoftext|>, map to a unique integer ID that the model actually sees?

The Fallback Rule for Unknown Words

The first special token, <|unk|>, is the tokenizer's fallback for unfamiliar words. The source describes the rule directly: the tokenizer is modified so that when it encounters a word that is not part of the vocabulary, it emits an <|unk|> token instead. The tokenizer does not stop, and it does not throw the text away at that point. It substitutes the marker and continues with the next word.

Next wordIn vocabulary?Emit the word's IDknown wordContinue tokenizingEmit <|unk|> IDunknown wordyesno
When a word is not in the vocabulary, what branch does the tokenizer take, and which ID ends up in the token sequence instead?

The marker matters even though it hides the original word. The <|unk|> token occupies one position in the token sequence, so the model still receives a signal that something was there. The original word, however, cannot be recovered from the token itself. The token records the event of an unknown word, not its spelling.

Marking Boundaries Between Documents

The second special token, <|endoftext|>, solves a different problem. When a GPT-like language model is trained on many independent documents or books, those texts are usually concatenated into one long stream of tokens. Without any marker, the model would see one sentence flow straight into an unrelated one. The source notes that it is common to insert an <|endoftext|> token before each document that follows a previous text source. The token tells the model that the surrounding material, though joined for training, does not belong to one continuous narrative.

The source's illustration uses four independent passages: a sports result, a fairy tale, a financial report, and a romantic moment. They are placed in sequence, and each passage after the first is preceded by an <|endoftext|> token. The result is one concatenated stream. The source caption describes these tokens as markers that signal the start or end of a segment. The same token can be read as the close of one document and the opening of the next, so the boundary is visible from either side.

Preparing a Mixed Batch Step by Step

Document 1independent textJoined stream<|endoftext|> before eachlater documentTokenizeunknown words become<|unk|>Token IDssequence for trainingDocument 2independent textseparator
What happens step by step as separate documents are joined, separated by <|endoftext|>, and converted into token IDs for training?
Worked example

Joining Two Unrelated Documents

Two independent documents are prepared for training: doc A reads 'the cat sat' and doc B reads 'stocks rose'. The vocabulary contains the, cat, and sat, but not stocks or rose. Trace how the batch is prepared.

  1. 1
    Start with document A

    Its words are the, cat, and sat. All three are in the vocabulary, so each emits its own ID.

  2. 2
    Insert the boundary

    Because doc B follows another text source, an <|endoftext|> token is placed before it. This tells the model that doc B does not continue doc A.

  3. 3
    Tokenize document B

    The word stocks is not in the vocabulary, so the tokenizer emits <|unk|> in its place and continues. The word rose is also missing, so it too becomes <|unk|>.

  4. 4
    Read the final sequence

    The stream holds the IDs for the, cat, sat, then the <|endoftext|> ID, then two <|unk|> IDs. The boundary is visible in the middle, and the unknown positions are marked but their spellings are not kept.

Result

A single token sequence in which the <|endoftext|> token separates the two unrelated documents and <|unk|> stands in for each word the vocabulary did not contain.

Mistakes Beginners Make with Special Tokens

  • Common mistake

    Thinking <|unk|> stores the original unknown word

    What it looks like

    Reading the sequence ... <|unk|> ... and assuming the spelling of the missing word is still recoverable from it.

    Why it fails

    The token only marks that an unknown word occurred at that position. The source states that the word cannot be recovered from the token itself.

    Do this instead

    Treat <|unk|> as a marker of an event, not a copy of the word.

  • Common mistake

    Assuming special tokens are not part of the vocabulary

    What it looks like

    Believing <|endoftext|> is a rule written into the code rather than a table entry.

    Why it fails

    Each special token must be given its own ID so the tokenizer can emit it like any other vocabulary entry.

    Do this instead

    Think of <|unk|> and <|endoftext|> as ordinary entries with IDs that were added deliberately.

  • Common mistake

    Joining documents without a boundary

    Why it fails

    Without a marker, the model sees one sentence flow straight into an unrelated one.

    Do this instead

    Insert an <|endoftext|> token before each document that follows a previous text source.

Trace a Batch Yourself

MEDIUM

Three independent passages are joined for training: passage 1 is a financial report, passage 2 is a short fairy tale, and passage 3 contains a brand-new product name that is not in the vocabulary. Write out the order of the stream, marking where each <|endoftext|> goes and where each <|unk|> appears.

Hints
  • Start with passage 1 and do not put an <|endoftext|> before it.
  • Each passage after the first is preceded by an <|endoftext|> token.
  • Any word missing from the vocabulary, including the new product name, becomes <|unk|>.

Key Takeaways

  • A tokenizer's fixed vocabulary can be extended with special context tokens that carry meanings ordinary words cannot express.
  • The <|unk|> token replaces any word that is not in the vocabulary, so tokenization can continue, but the original spelling is not recoverable from it.
  • The <|endoftext|> token separates unrelated text sources, and is commonly inserted before each document that follows a previous text source.
  • Each special token is given its own integer ID, so the tokenizer emits it like any other vocabulary entry.

Related concepts

More concepts starting with B