Skip to main content

1. What is Tokenization?

Tokenization is the process of breaking text into smaller units called tokens so that a machine-learning model can process the text. Example:
The model then converts these tokens into numerical IDs.

2. Why Do We Need Tokenization?

Neural networks work with numbers, not raw text. For example:
cannot directly be given to a neural network. Instead:
The IDs are then converted into vectors called embeddings.

3. Types of Tokenization

Common approaches include:
  1. Word-level tokenization
  2. Character-level tokenization
  3. Subword tokenization
For modern Transformer models, subword tokenization is especially important. Two popular subword algorithms are:
  • BPE — Byte Pair Encoding
  • WordPiece

4. BPE — Byte Pair Encoding

BPE breaks words into smaller pieces and learns which pieces frequently occur together.

Basic idea

Suppose the training data contains:
Initially, words can be viewed as characters:
BPE looks for frequent combinations and merges them. For example:
It can eventually learn subwords such as:
So:
could be represented approximately as:
The exact tokens depend on the tokenizer’s learned vocabulary.

5. Why BPE is Useful

Consider a rare word:
If the complete word isn’t in the vocabulary, a subword tokenizer can break it into smaller pieces. For example:
or potentially:
This means the model doesn’t need a separate vocabulary entry for every possible word.

Main advantages

  • Handles rare words
  • Handles new words
  • Keeps vocabulary manageable
  • Captures meaningful subwords

6. WordPiece

WordPiece is another subword tokenization algorithm. It is famously used with BERT-style models. Example:
could be tokenized as:
The ## means that ing is a continuation of the previous token. Another example:
could become:
The exact result depends on the tokenizer vocabulary.

7. BPE vs WordPiece

Simple way to remember:

8. BPE Tokenizer Code

We can use Hugging Face transformers. Install:
Example using a GPT-2 tokenizer:
Possible output:
The Ġ is how this tokenizer represents a space before a token.

9. Convert BPE Tokens to IDs

Possible output:
The exact IDs depend on the tokenizer vocabulary.

10. WordPiece Tokenizer Code

For WordPiece, we can use the BERT tokenizer.
Possible output:
Try a word that may be split into subwords:
Possible output:
For another word, you may see:
The exact split depends on the pretrained vocabulary.

11. Convert WordPiece Tokens to IDs


12. Tokenizer encode()

Instead of manually converting tokens to IDs, we can use encode().
BERT adds special tokens by default. Conceptually:
The special tokens help the model understand the structure of the input.

13. Better Way: tokenizer()

In modern Hugging Face code, it is common to directly call the tokenizer.
You can also inspect individual components:

14. What is an Attention Mask?

When sentences have different lengths, padding is often added. Example:
After padding:
The attention mask tells the model which positions are real tokens.
Example:

15. What are Embeddings?

An embedding is a numerical representation of text. Instead of representing:
only as an ID:
we represent it as a vector:
The vector contains information learned by the model. Similar concepts generally have embeddings that are closer together in the embedding space.

16. Sentence Embeddings

A sentence embedding represents an entire sentence using a single vector. Example:
becomes something like:
Another sentence:
also becomes a vector. If the meanings are similar, their vectors should be relatively close.

17. Sentence Transformers

Sentence Transformers is a Python library designed to generate useful embeddings for sentences, paragraphs, and other text. Install it:
Import it:
Load a pretrained model:

18. Generate Sentence Embeddings

Possible output:
This means:
So:

19. View the Embedding

Possible output:
The exact values will be different depending on the model and library version.

20. Sentence Similarity

One major application of sentence embeddings is semantic similarity. For example:
These sentences have related meanings. But:
have very different meanings. We can measure this using cosine similarity.

21. Cosine Similarity Code

Install scikit-learn if necessary:
Code:
Possible output:
The exact numbers will vary. Interpretation:

22. Compare Two Sentences Directly

You can also compare only two sentences.
Possible output:
Again, the exact score depends on the model.

23. Complete Example

This combines tokenization + sentence embeddings.

24. Tokenization vs Embeddings

These two concepts are related but different.

Tokenization

Converts text into smaller pieces.

Sentence Embedding

Converts the meaning of the entire sentence into a vector.

25. Complete NLP Pipeline

The overall process can be remembered as:

26. Practical Applications

Search by meaning rather than exact keywords.

2. Duplicate Detection

Can be identified as semantically similar.

3. Recommendation Systems

Find similar:
  • Products
  • Articles
  • Questions
  • Documents

4. Document Clustering

Convert documents into embeddings and group similar documents.

5. Question Matching

Find which stored question is most similar to a user’s question.

27. Key Points to Remember

Quick Revision