Sparse Embeddings in NLP (BM25 & SPLADE)

Nlp tutorial · PySpark.in


Sparse Embeddings are advanced text representation techniques used in Natural Language Processing (NLP) and Information Retrieval (IR). Unlike dense embeddings, sparse embeddings represent text using high-dimensional vectors where most values are zero. 

Sparse embeddings are highly effective for: 

  • Search Engines  

  • Information Retrieval  

  • Document Ranking  

  • Semantic Search  

  • Question Answering  

  • Retrieval-Augmented Generation (RAG)  

The two most important sparse retrieval methods are: 

  1. BM25 (Best Matching 25)  

  1. SPLADE (Sparse Lexical and Expansion Model)  

These methods are widely used in modern AI retrieval systems and search architectures. 

Sparse embeddings are vector representations where: 

  • Most vector elements are zero  

  • Only important terms receive non-zero weights  

Example: 

Vocabulary: 

["AI", "machine", "learning", "football"] 

Sentence: 

"AI learning" 

Sparse vector: 

[1,0,1,0] 

Only relevant terms are activated. 

Why Sparse Embeddings are Important 

Sparse retrieval methods are extremely useful because: 

  1. They preserve keyword matching  

  1. They are interpretable  

  1. They work efficiently in search systems  

  1. They scale well for large document collections  

Modern AI systems often combine: 

  • Dense embeddings  

  • Sparse embeddings  

for hybrid retrieval systems. 

BM25 (Best Matching 25) 

BM25 is one of the most important ranking algorithms used in search engines. 

It improves traditional TF-IDF ranking by considering: 

  • Term frequency  

  • Inverse document frequency  

  • Document length normalization  

BM25 is widely used in: 

  • Google-like search systems  

  • Elasticsearch  

  • Information retrieval systems 

Working Principle of BM25 

BM25 ranks documents according to their relevance to a query. 

Steps: 

  1. Analyze query terms  

  1. Calculate term importance  

  1. Normalize document length  

  1. Compute relevance score  

  1. Rank documents 

 

BM25 Formula 

 

Components of BM25 

Component 

Purpose 

TF (Term Frequency) 

Word occurrence importance 

IDF 

Rare word importance 

Length Normalization 

Penalizes long documents 

Example of BM25 

Query: 

"machine learning" 

Documents: 

Document 

Score 

“Machine learning improves AI” 

High 

“Football match today” 

Low 

Relevant documents receive higher scores. 

SPLADE (Sparse Lexical and Expansion Model) 

SPLADE is a modern sparse retrieval model that combines: 

  • Sparse embeddings  

  • Transformer architectures  

  • Semantic expansion  

Unlike BM25, SPLADE uses neural networks to generate sparse semantic representations. 

Main Idea of SPLADE 

SPLADE expands queries and documents using transformer-based lexical weighting. 

This allows: 

  • Better semantic retrieval  

  • Sparse interpretable vectors  

  • Improved search accuracy 

Working Principle of SPLADE 

Steps: 

  1. Input query/document  

  1. Transformer generates token importance  

  1. Sparse vectors created  

  1. Important terms activated  

  1. Retrieval performed using sparse matching 

SPLADE Objective 

SPLADE uses transformer-based activation functions. 

Sparse activation formula: 

 

Sparse Retrieval in Modern AI Systems 

Modern Retrieval-Augmented Generation (RAG) systems use: 

  • Sparse retrieval  

  • Dense retrieval  

  • Hybrid retrieval  

Sparse embeddings improve: 

  • Keyword matching  

  • Explainability  

  • Search efficiency 

Hybrid Retrieval Example 

Query: 

"AI healthcare applications" 

BM25 retrieves: 

  • Exact keyword matches  

Dense retrieval retrieves: 

  • Semantic matches  

Hybrid retrieval combines both. 

Practical Implementation Using BM25 

Install Libraries 

``` 

Hide 

pip install rank-bm25 

``` 

Exmaple: 

``` 

#Import library 

from rank_bm25 import BM25Okapi 

#Sample documents 

documents = [ 

"machine learning improves ai", 
 
"football is a popular sport", 
 
"deep learning powers modern ai", 
 
"healthcare uses artificial intelligence" 
 

] 

#Tokenize documents 

tokenized_docs = [ 

doc.split(" ") 
 
for doc in documents 
 

] 

#Create BM25 model 

bm25 = BM25Okapi(tokenized_docs) 

#Query 

query = "machine learning ai".split(" ") 

Calculate scores 

scores = bm25.get_scores(query) 

#Display results 

print("BM25 Scores:\n") 

print(scores) 

``` 

Practical Implementation Using SPLADE 

Install Libraries 

``` 

pip install transformers torch 

``` 

Exmaple: 

``` 

#Import libraries 

from transformers import AutoTokenizer, AutoModel import torch 

#Load tokenizer and model 

tokenizer = AutoTokenizer.from_pretrained( 

"distilbert-base-uncased" 
 

) 

model = AutoModel.from_pretrained( 

"distilbert-base-uncased" 
 

) 

Sample text 

text = "Artificial Intelligence improves healthcare" 

#Tokenize input 

inputs = tokenizer( 

text, 
 
return_tensors="pt" 
 

) 

#Generate embeddings 

outputs = model(**inputs) 

#Last hidden states 

embeddings = outputs.last_hidden_state 

#Print shape 

print("Embedding Shape:\n") 

print(embeddings.shape) 

 

``` 

Sparse Retrieval in RAG Systems 

Retrieval-Augmented Generation systems retrieve relevant documents before generating responses. 

Sparse retrieval improves: 

  • Accuracy  

  • Explainability  

  • Retrieval precision  

This is widely used in: 

  • ChatGPT-style systems  

  • Enterprise AI search  

  • AI assistants 

Conclusion 

Sparse embeddings are highly important for modern information retrieval and semantic search systems. Techniques such as BM25 provide efficient lexical ranking using statistical relevance scoring, while advanced neural sparse models such as SPLADE combine transformer-based semantic understanding with sparse retrieval efficiency. These methods play a crucial role in search engines, enterprise retrieval systems, Retrieval-Augmented Generation (RAG), and modern AI-powered information access systems. 

 

More Nlp tutorials

All tutorials · Try the free PySpark compiler · Practice challenges