Semantic Search
Semantic search retrieves documents based on meaning rather than keyword matching. It uses dense vector representations and efficient similarity search algorithms.
Sentence Embeddings
Sentence embeddings encode text into fixed-size dense vectors that capture semantic meaning.
from sentence_transformers import SentenceTransformer
import numpy as np
# Load pre-trained model
model = SentenceTransformer('all-MiniLM-L6-v2')
# Encode documents
documents = [
"Machine learning is a subset of artificial intelligence.",
"Deep learning uses neural networks with many layers.",
"Natural language processing deals with text and speech.",
"Computer vision analyzes images and videos.",
]
# Generate embeddings
embeddings = model.encode(documents)
print(f"Embedding shape: {embeddings.shape}") # (4, 384)
# Compute similarity
query = "What is AI?"
query_embedding = model.encode([query])
similarities = np.dot(embeddings, query_embedding.T).flatten()
ranked_indices = np.argsort(similarities)[::-1]
for idx in ranked_indices:
print(f"Score: {similarities[idx]:.4f} | {documents[idx]}")
Cosine Similarity
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
# Batch computation
def batch_cosine_similarity(query_emb, doc_embs):
# Normalize
query_norm = query_emb / np.linalg.norm(query_emb, axis=1, keepdims=True)
doc_norm = doc_embs / np.linalg.norm(doc_embs, axis=1, keepdims=True)
return np.dot(doc_norm, query_norm.T)
FAISS (Facebook AI Similarity Search)
FAISS provides efficient similarity search and clustering of dense vectors.
import faiss
import numpy as np
# Generate random embeddings for demonstration
dimension = 384
num_documents = 100000
np.random.seed(42)
documents = np.random.random((num_documents, dimension)).astype('float32')
# Build FAISS index
# Option 1: Flat index (exact search)
index_flat = faiss.IndexFlatL2(dimension)
index_flat.add(documents)
# Option 2: IVF index (approximate search)
nlist = 100 # Number of clusters
quantizer = faiss.IndexFlatL2(dimension)
index_ivf = faiss.IndexIVFFlat(quantizer, dimension, nlist)
index_ivf.train(documents)
index_ivf.add(documents)
# Option 3: HNSW index (graph-based)
index_hnsw = faiss.IndexHNSWFlat(dimension, 32)
index_hnsw.hnsw.efConstruction = 64
index_hnsw.add(documents)
# Search
query = np.random.random((1, dimension)).astype('float32')
k = 5 # Number of nearest neighbors
# Flat search (exact)
distances, indices = index_flat.search(query, k)
print(f"Flat results: {indices[0]}, distances: {distances[0]}")
# IVF search (approximate)
index_ivf.nprobe = 10 # Number of clusters to search
distances, indices = index_ivf.search(query, k)
print(f"IVF results: {indices[0]}, distances: {distances[0]}")
# HNSW search
distances, indices = index_hnsw.search(query, k)
print(f"HNSW results: {indices[0]}, distances: {distances[0]}")
Index Comparison
| Index Type | Build Time | Search Time | Memory | Accuracy |
|---|---|---|---|---|
| Flat (L2) | O(1) | O(n) | High | 100% |
| IVF | O(n) | O(n/k) | Medium | ~95% |
| HNSW | O(n log n) | O(log n) | High | ~98% |
| PQ | O(n) | O(n/k) | Low | ~90% |
| IVF-PQ | O(n) | O(n/k²) | Low | ~92% |
FAISS Index Selection
def select_index(num_vectors, dimension, speed_priority=True):
if num_vectors < 10000:
return "IndexFlatL2" # Exact search
elif num_vectors < 1000000:
if speed_priority:
return "IndexHNSWFlat" # Fast, good accuracy
else:
return "IndexIVFFlat" # Balanced
else:
if speed_priority:
return "IndexIVFPQ" # Memory efficient
else:
return "IndexHNSWFlat" # Good accuracy
# Example usage
index_type = select_index(500000, 768, speed_priority=True)
print(f"Recommended index: {index_type}")
Vector Databases
| Database | Type | Scaling | Features |
|---|---|---|---|
| FAISS | Library | Single machine | Fast, GPU support |
| Milvus | Distributed | Horizontal | Production-ready |
| Pinecone | Cloud | Managed | Easy to use |
| Weaviate | Self-hosted | Horizontal | GraphQL API |
| Qdrant | Self-hosted | Horizontal | Filtering |
| ChromaDB | Embedded | Single machine | Lightweight |
# Example with ChromaDB
import chromadb
client = chromadb.Client()
collection = client.create_collection("documents")
# Add documents
documents = [
"Machine learning algorithms learn patterns from data.",
"Deep neural networks have multiple hidden layers.",
"Natural language processing understands human language.",
]
ids = ["doc1", "doc2", "doc3"]
collection.add(
documents=documents,
ids=ids,
metadatas=[{"source": "textbook"} for _ in documents]
)
# Query
results = collection.query(
query_texts=["What is deep learning?"],
n_results=2
)
print(results["documents"])
Evaluation Metrics
| Metric | Description | Formula |
|---|---|---|
| Recall@k | Relevant docs in top-k | |relevant ∩ top-k| / |relevant| |
| MRR | Mean Reciprocal Rank | 1/Q Σ 1/rank_i |
| NDCG | Normalized Discounted Cumulative Gain | DCG / IDCG |
| MAP | Mean Average Precision | 1/Q Σ AP_i |
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type — Always start by categorizing the problem
- Incorrect formula application — Verify the formula applies to your specific case
- Unit errors — Always check units are consistent throughout
- Sign errors — Pay careful attention to positive and negative values
- Rounding too early — Keep full precision until the final answer
- Forgetting constraints — Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic — Build on the foundation laid here
- Next Topic — See how this concept extends to more advanced material
- Practice Problems — Additional exercises with solutions
- Video Explanation — Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions