Módulo 5: Distance Metrics Deep Dive
Approximate Nearest Neighbors (ANN)
El problema de escala
# Exact search (brute force):
# - 1M embeddings: ~100ms
# - 10M embeddings: ~1s
# - 100M embeddings: ~10s ❌ Demasiado lento
# Solución: Approximate Nearest Neighbors (ANN)
# Trade-off: Accuracy vs Speed
# - 99% accuracy, 100x speedup ✅
Algoritmos ANN principales
1. HNSW (Hierarchical Navigable Small World)
# Usado por: Pinecone, Weaviate, Qdrant
# Accuracy: ~99%
# Speed: 100-1000x faster
# Memory: 1.5x original size
2. IVF (Inverted File Index)
# Usado por: FAISS
# Divide espacio en clusters
# Búsqueda solo en clusters relevantes
# Speed: 10-100x faster
3. LSH (Locality Sensitive Hashing)
# Hash vectores similares al mismo bucket
# Speed: 50-100x faster
# Accuracy: ~95%
FAISS Example (básico)
import faiss
import numpy as np
# Embeddings
embeddings = np.random.randn(10000, 1536).astype('float32')
query = np.random.randn(1, 1536).astype('float32')
# Index (IVF)
nlist = 100 # Número de clusters
quantizer = faiss.IndexFlatL2(1536)
index = faiss.IndexIVFFlat(quantizer, 1536, nlist)
# Train
index.train(embeddings)
index.add(embeddings)
# Search (top-5)
k = 5
distances, indices = index.search(query, k)
print(f"Top-5 indices: {indices[0]}")
print(f"Distances: {distances[0]}")
Accuracy vs Speed Trade-off
# Exact search:
# - Accuracy: 100%
# - Speed: 1x (baseline)
# HNSW:
# - Accuracy: 99%
# - Speed: 100x
# IVF (nlist=100):
# - Accuracy: 95%
# - Speed: 50x
# IVF (nlist=1000):
# - Accuracy: 98%
# - Speed: 20x
Optimal: HNSW para production (best accuracy/speed balance).
Resumen
- ✅ ANN: 100x speedup con 99% accuracy
- ✅ HNSW: Best algorithm (usado por Pinecone)
- ✅ FAISS: Open-source library
- ✅ Production: ANN es mandatory para >1M vectors
Módulo 5 - Cápsula 06