Subword Models in NLP (BPE & WordPiece)

Nlp tutorial · PySpark.in


Subword Models are advanced tokenization techniques in Natural Language Processing (NLP) used to divide words into smaller meaningful units called subwords. These methods are extremely important in modern NLP systems because they help handle: 

  • Unknown words  

  • Rare words  

  • Morphologically rich languages  

  • Large vocabularies  

Subword tokenization is widely used in: 

  • BERT  

  • GPT  

  • Transformer models  

  • Machine Translation  

  • Large Language Models (LLMs)  

The two most important subword tokenization methods are: 

  1. BPE (Byte Pair Encoding)  

  1. WordPiece  

These techniques form the backbone of modern transformer-based NLP architectures. 

Need for Subword Models 

Traditional word-level tokenization has several problems. 

Example: 

"unhappiness" 

If this word is not present in the vocabulary: 

  • Word2Vec cannot process it  

  • GloVe cannot process it  

This creates the: 

Out-of-Vocabulary (OOV) Problem 

Subword models solve this issue by splitting words into smaller components. 

Example 

Word: 

"unhappiness" 

Subwords: 

["un", "happi", "ness"] 

Now the model can understand the word using known subwords. 

Byte Pair Encoding (BPE) 

BPE (Byte Pair Encoding) is one of the most widely used subword tokenization algorithms. 

Originally developed for data compression, BPE was later adapted for NLP. The algorithm attempts to maximize compression by merging frequent subword units. 

 

BPE works by: 

  1. Starting with character-level tokens  

  1. Repeatedly merging frequent character pairs  

  1. Building larger subwords iteratively 

Example of BPE 

Suppose the corpus contains: 

low 
lowest 
newer 
wider 

Initial tokens: 

l o w 
l o w e s t 
n e w e r 
w i d e r 

Frequent pairs are merged gradually. 

Example merges: 

l + o → lo 
 
lo + w → low 

Final tokens may become: 

["low", "est", "new", "er"] 

Working Principle of BPE 

Steps: 

  1. Split words into characters  

  1. Count frequent adjacent pairs  

  1. Merge most frequent pair  

  1. Repeat until vocabulary size reached 

WordPiece Tokenization 

WordPiece is another popular subword tokenization algorithm. 

It is primarily used in: 

  • BERT  

  • ALBERT  

  • DistilBERT  

WordPiece is similar to BPE but uses probability-based merging rather than simple frequency counts. 

 

Example of WordPiece 

Word: 

"playing" 

Tokenization: 

["play", "##ing"] 

The prefix: 

"##" 

indicates continuation of a word. 

Working Principle of WordPiece 

Steps: 

  1. Initialize character vocabulary  

  1. Evaluate token probabilities  

  1. Merge subwords maximizing likelihood  

  1. Generate optimal subword vocabulary 

WordPiece maximizes language modeling probability. 

The likelihood formula is: 

 

 

Example of WordPiece Tokenization 

Sentence: 

"Transformers are powerful" 

Possible tokens: 

["transform", "##ers", "are", "powerful"] 

Why Subword Models are Important in Transformers 

Transformer models use fixed vocabularies. 

Without subword tokenization: 

  • Vocabulary becomes huge  

  • Unknown words increase  

  • Training becomes inefficient  

Subword models: 

  • Reduce vocabulary size  

  • Improve generalization  

  • Enable multilingual NLP 

Practical Implementation Using BPE 

Install Libraries 

``` 

Hide  

pip install tokenizers 

``` 

Example: 

``` 

#Import library 

from tokenizers import Tokenizer from tokenizers.models import BPE from tokenizers.pre_tokenizers import Whitespace from tokenizers.trainers import BpeTrainer 

Sample dataset 

sentences = [ 

"Natural Language Processing is powerful", 
 
"Transformers improve NLP systems", 
 
"Deep Learning helps AI" 
 

] 

Create tokenizer 

tokenizer = Tokenizer(BPE()) 

Pre-tokenization 

tokenizer.pre_tokenizer = Whitespace() 

Trainer 

trainer = BpeTrainer( 

vocab_size=50, 
 
special_tokens=["[UNK]"] 
 

) 

#Train tokenizer 

tokenizer.train_from_iterator( 

sentences, 
 
trainer=trainer 
 

) 

#Encode sentence 

output = tokenizer.encode( 

"Transformers are powerful" 
 

) 

#Print tokens 

print("Tokens:\n") 

print(output.tokens) 

 

``` 

Practical Implementation Using WordPiece 

Install Libraries 

``` 

hide 

pip install transformers 

``` 

Example: 

``` 

#Import tokenizer 

from transformers import BertTokenizer 

#Load tokenizer 

tokenizer = BertTokenizer.from_pretrained( 

'bert-base-uncased' 
 

) 

#Tokenize sentence 

tokens = tokenizer.tokenize( 

"Transformers are amazing" 
 

) 

#Display tokens 

print("WordPiece Tokens:\n") 

print(tokens) 

 

``` 

Conclusion 

Subword models such as BPE and WordPiece represent one of the most important innovations in modern Natural Language Processing. These techniques solve critical vocabulary limitations by splitting words into smaller semantic units, enabling efficient handling of rare and unknown words. BPE provides simple frequency-based tokenization widely used in GPT architectures, while WordPiece offers probabilistic segmentation optimized for transformer models such as BERT. Subword tokenization has become an essential component of modern large language models and advanced NLP systems. 

 

More Nlp tutorials

All tutorials · Try the free PySpark compiler · Practice challenges