liveBook · Manning

Token

As explained in Hugging Face in Action

A token is a unit of text that a machine learning model processes as a single input element.

A token is a chunk of text that a machine learning model processes as one input unit. Depending on the tokenizer, it may represent a word, punctuation mark, subword, character, or other linguistic element. section 3.1.1 section 7.1

Inspect BERT tokens with Hugging Face

from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased") input_text = "What is unhappiness?" tokens = tokenizer.tokenize(input_text, return_tensors="pt") print(f"{tokens = }") # ['what', 'is', 'un', '##ha', '##pp', '##iness', '?'] section 3.1.1

Common tokenization strategies include:

  • Word tokenization splits text into words using whitespace or punctuation. section 3.1.1
  • Subword tokenization splits words into units such as prefixes, suffixes, or roots, supporting varied vocabularies and linguistic forms. section 3.1.1
  • Character-level tokenization splits text into individual letters, digits, and punctuation marks, but can reduce higher-level interpretability. section 3.1.1
  • Tokenization can also convert text into numerical IDs required by most NLP models. section 5.2
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")  #1
input_text = "What is unhappiness?"
tokens = tokenizer.tokenize(input_text, return_tensors="pt")  #2

print(f"{tokens = }")

Use the listing to inspect how a pretrained BERT tokenizer converts an input sentence into model-ready token units. section 3.1.1

How Hugging Face in Action covers it

How deep it goes
Explains word, subword, and character strategies, then demonstrates BERT token output without deriving tokenizer internals.
Examples
Uses “I love cats,” “unhappiness,” “Hello,” and a BERT tokenizer listing to illustrate token boundaries.
What it assumes
Assumes basic NLP and machine-learning context while defining tokenization strategies and model representation directly.
What you can do after
Apply it via Hugging Face Transformers to tokenize text and inspect the resulting model input units.
How it connects
Connects tokens with tokenization, token embeddings, positional encodings, self-attention, transformers, and large language models.

Also explained in

Compare how 31 books explain token →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hugging Face in Action. The section links point to the parts of the book it draws on.