liveBook · Manning

Token

As explained in Deep Learning with R, Third Edition

A token is a discrete unit of text processed by a language model, such as a word, subword, character, or punctuation symbol.

A token is a small unit of text produced by splitting an input string. It might be a character, a word, punctuation, or a subword. Splitting is the first step toward turning text into the numbers a model can process. section 14.2

From a sentence to indices

Split “The quick brown fox jumped over the dog.” at word boundaries while keeping punctuation, and the result includes “The”, “quick”, and “.” as separate tokens. A vocabulary assigns each known token an integer index. With a vocabulary containing lowercase “the” but not uppercase “The”, a direct lookup maps “The” to 0; lowercasing before lookup avoids that mismatch. A word absent from the vocabulary still needs an unknown-token representation. section 14.2 section 14.2.1

vocabulary <- c("the", "quick", "brown", "fox", "jumped", "over", "dog", ".")
words <- split_words("The quick brown fox jumped over the lazy dog.")
indices <- match(words, vocabulary, nomatch = 0L)
indices

[1] 0 2 3 4 5 6 1 0 7 8

The lookup makes the numeric step visible: 0 marks tokens missing from this vocabulary, including uppercase “The” and “lazy”. section 14.2

The choice of token size changes what the model must handle:

  • Characters need a small vocabulary but produce long sequences. In the Moby Dick example, 89 character terms cover the full input. section 14.2.1
  • Words produce shorter sequences, but a limited vocabulary leaves uncommon words unmatched. The Moby Dick word example fills a 2,000-term vocabulary without covering every word. section 14.2.1
  • Subwords offer another choice. Byte-pair encoding starts with characters and repeatedly merges frequent adjacent pairs; “o” and “w”, for example, can become one “ow” token. section 14.2.2

Tokens remain the units of work after preprocessing. A language model predicts a token from past tokens, and generated tokens can be fed back in to continue a sequence. In a Transformer, attention uses token embeddings to draw information selectively from elsewhere in the sequence. section 15.6 section 16.6

How Deep Learning with R, Third Edition covers it

How deep it goes
Builds character, word, and byte-pair tokenizers, including vocabulary construction and integer lookup.
Examples
Compares character and word vocabularies on Moby Dick and demonstrates byte-pair merges on short fox sentences.
What it assumes
Starts with input strings and teaches splitting and indexing, while leaving regular-expression instruction outside scope.
What you can do after
Build it from scratch: split text, construct a bounded vocabulary, and map tokens to indices using R.
How it connects
Links token embeddings to attention and next-token language modeling.

Also explained in

Compare how 31 books explain token →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.