Learn

Learning preferences

Make learning more comfortable. Changes apply immediately.

Enlarges page content, including diagrams and their labels. You can also use your browser’s zoom.

Choose the colours that are easiest for you to distinguish. Applies to lesson diagrams and lab plots. Labels, shapes, hatching and line patterns also distinguish meanings. Source photographs and raw image brightness stay unchanged; mask-only views are available for image overlays.

ExampleComparisonSample link

Saved in this browser on this device. Preferences are separate from progress backups.

Lesson 1 of 13 in this unit

Understand Neural Networks · ORIGINAL LEARN AI LESSON

Choose the pieces a model predicts.

Fit character, word and subword tokenizers; inspect unknown text and a count-based next-token baseline.

By the end: Compare tokenization tradeoffs, distinguish fitting from encoding, and calculate a smoothed next-token probability without confusing tokens with meanings.

Start with the exampleJump to the experimentRead the explanation
Prerequisites and path options

Why learn this? See how token choices affect sequence length, unknown inputs and the prediction task itself.

01 · TRY

Start with something concrete.

The phrase “green apples sing” uses familiar pieces in an unusual combination. Try it in all three modes. Then try “snow 雪”. Predict which input can be reconstructed from its token IDs.

Try the interactive model
02 · PREDICT
Before you experiment

A word tokenizer maps two unfamiliar words to the same unknown ID. What can the model recover from that ID alone?

A word tokenizer maps two unfamiliar words to the same unknown ID. What can the model recover from that ID alone?
03 · EXPERIMENT

Change one thing. Watch what follows.

Loading this browser experiment…

04 · UNDERSTAND
READ & CONNECT

Understand what changed.

Back to the experiment

A shorter explanation, with the original worked examples available below. Both explain the same experiment and its limits.

Same learning goal in both modes: Compare tokenization tradeoffs, distinguish fitting from encoding, and calculate a smoothed next-token probability without confusing tokens with meanings.

The idea in everyday terms

A tokenizer divides text into pieces and assigns each an integer ID. The ID is a lookup index, not a meaning. Choosing words, characters or smaller pieces changes sequence length and how unfamiliar text is represented. A vocabulary lists the allowed pieces; an unknown marker records text that a particular vocabulary cannot represent directly.

What to watch in the experiment

Use the same text in each mode. Count the pieces, inspect unknown fragments and follow a prediction from token ID to its probability. Consistently renumbering IDs does not change what the tokens mean.

What this does not establish

Per-token loss from different vocabularies describes different prediction units and cannot directly rank model quality. A fitted count model is not a Transformer or evidence of general language understanding.

Open the full worked examples, calculations and limitations

This is the same reference used in Technical mode. Expand it whenever you need the exact numbers, assumptions or further explanation.

Text needs a representation before prediction

A model cannot directly multiply by the sentence “red apples grow”. A tokenizer divides text into pieces and assigns each piece an integer ID. The model can then look up a vector for each ID. An ID is an index, not a meaning. Changing the numbering consistently does not make a word more positive or a sentence more true.

Start with the same probe in every mode. The lab keeps spaces and punctuation explicit, so you can check whether decoding the IDs restores the original text. Your probe is encoded using the fitted vocabulary; it never changes that vocabulary or the count model.

Three useful choices, three different costs

Character mode uses Unicode code points. It can reuse known letters to spell a word never seen during fitting, but sequences can become long. A code point is not always a complete visible character: an accent or emoji sequence can contain several. This teaching tokenizer does not normalize text or fall back to arbitrary bytes, so a code point absent from training becomes unknown.

Word mode keeps word spans, punctuation and whitespace as separate pieces. Familiar sentences can be short, but an unfamiliar whole word becomes an unknown ID even if its individual letters appeared elsewhere. Try “green apples sing”, then “snow”. A novel combination of known words is different from an unknown word.

Subword mode begins with code points and learns frequent adjacent pairs within word spans. It can keep common fragments together while spelling less common words from smaller known pieces. It still cannot represent every possible character in this bounded demonstration.

Production tokenizers differ in their boundary rules, normalization, byte coverage, vocabulary sizes and special markers. Do not treat this small BPE-style implementation as the exact tokenizer of a particular deployed model.

Follow one merge

Suppose the fitting documents are aaaa and aaaa. Each has three adjacent a–a pairs, so the pair count is six. Replacing non-overlapping occurrences from left to right creates aa, aa in each document: four replacements, not six. Overlapping candidate pairs share positions.

The lab repeats this process for at most the selected number of merges. It stops if no pair appears twice. Ties use a fixed string order. Merges never cross a whitespace, punctuation or document boundary. Look at the recorded pairs before interpreting a shortened sequence.

Repetition changes these statistics. Duplicating one document can make its fragments more likely to merge. Fitting a tokenizer on evaluation text also changes the representation using evaluation evidence; it is not the same as simply applying a fixed tokenizer to that text.

Unknown is a loss of information

Two different unknown fragments can receive ID zero. The lab shows their original spelling so you can diagnose the loss, but the count model receives only that shared ID. Its decoder displays a replacement symbol. It cannot reconstruct the missing spelling from zero alone.

A lower token count is therefore not automatically better. Turning a whole paragraph into one unknown token would be short and nearly useless. Compare vocabulary coverage, sequence length, exact reconstruction and the task the representation must support.

Build a baseline before a neural model

A bigram model counts which token follows which other token. It uses only the last token of your prefix, ignoring earlier context. Separate start and end markers keep document boundaries explicit. The end of one document is not followed by the first token of the next during fitting.

For a small worked example, after token A the training data contain B twice and C once. If there are five possible target IDs, add-one smoothing gives B a probability of:

P(B∣A)=2+13+5=38=0.375P(B\mid A)=\frac{2+1}{3+5}=\frac{3}{8}=0.375

Read this as: “the probability of B after A is its observed count plus one, divided by all observations after A plus the number of possible targets.” The vertical bar means given. Smoothing adds one imaginary count to every target so unobserved transitions do not receive zero probability. This is a modeling choice, not new evidence.

In the live lab the displayed counts come from its current fitting documents. The denominator includes every possible next ID except the start marker. The five displayed bars need not add to 100%, because other targets also have probability. Try two prefixes with the same last token and explain why their predictions agree.

Apply the tradeoff to a real task

For a spelling tool, losing a name to an unknown token can defeat the task. For a multilingual application, check which scripts and writing conventions receive good coverage. For a software implementation, test round trips, literal special-marker text, document boundaries and out-of-vocabulary behavior before training a larger model.

Keep a small comparison report: the same input, each mode’s pieces and length, unknown fragments, one checked probability and a limitation. These are guided practice observations, not independent implementation evidence. Token counts or per-token losses from different vocabularies do not directly measure the same prediction task, so avoid ranking model quality from those numbers alone.

Continue to embedding vectors and masked attention to see how neural models use token representations and more context. A count model is fitted from data, but it is not a trained Transformer or evidence of language understanding.

05 · APPLY
Check your understanding

A context has three observed next tokens: B twice and C once. There are five possible target IDs. With add-one smoothing, what probability does B receive?

A context has three observed next tokens: B twice and C once. There are five possible target IDs. With add-one smoothing, what probability does B receive?
06 · CONTINUE

Take this idea with you.

Tokenization chooses what information and prediction units a model receives. Shorter sequences are useful only when the representation still serves the task.

My Learning
Explore related topics and the topic map

How this topic connects

Explore this topic on the interactive map

Use these connections to review a foundation or choose where to go next. You can open any topic without completing the others.

What it builds on

  • Averages and probability

    Combine outcomes with their frequencies or probabilities, keeping the denominator explicit when interpreting this lesson’s evidence.

  • Optional: what an embedding represents

    Compare meanings represented as numbers so you can reason about search results and the information a representation loses.

You are hereChoose the pieces a model predicts

Fit character, word and subword tokenizers; inspect unknown text and a count-based next-token baseline.

What it enables

  • Predict the next token

    Understand what language-model training rewards and why low prediction loss does not prove factual reliability.

  • Prepare a text corpus

    Prevent duplication and split leakage from making a language model appear better than it is.