hackforla / hackforla/data-science
Text Analysis Tutorial: Semantic Search & Similarity: An Intermediate, End-to-End Analysis Tutorial
- Dominant language
- Jupyter Notebook
- Stars
- 33
- Forks
- 22
- PR merge metrics
- No merged PRs in 30d
Description
# Title & Overview
**Template:** *Semantic Search & Similarity: An Intermediate, End-to-End Analysis Tutorial*
**Overview (≤2 sentences):** Learners will implement semantic similarity and search pipelines, comparing dense embeddings (Sentence-Transformers) with classical vector spaces (TF-IDF, spaCy vectors). It is intermediate because it introduces approximate nearest neighbor (ANN) search, slice-based robustness evaluation, and reproducibility concerns with vector indexes.
# Purpose
The value-add is understanding when and why to use **semantic embeddings vs sparse vectors**, how to evaluate similarity tasks beyond accuracy, and how to deploy reproducible ANN search pipelines. Learners will gain experience in slice analysis, error inspection, and ANN trade-offs.
# Prerequisites
* Skills: Python, Git, pandas, ML basics.
* NLP: embeddings, cosine similarity, semantic similarity metrics.
* Tooling: pandas, scikit-learn, Sentence-Transformers, spaCy, Hugging Face Datasets, FAISS, MLflow, FastAPI.
# Setup Instructions
* Environment: Conda/Poetry (Python 3.11), deterministic seeds.
* Install: pandas, scikit-learn, Sentence-Transformers, spaCy, Hugging Face Transformers + Datasets, FAISS, MLflow, FastAPI.
* Datasets:
* **Small:** STS-Benchmark (semantic similarity scoring).
* **Medium:** Tatoeba or SNLI for cross-lingual/pairwise similarity.
* Repo layout:
```
tutorials/t8-semantic-search/
├─ notebooks/
├─ src/
│ ├─ vectorizers.py
│ ├─ embeddings.py
│ ├─ search.py
│ └─ config.yaml
├─ data/README.md
├─ reports/
└─ tests/
```
# Core Concepts
* **Sparse vs dense similarity:** TF-IDF cosine vs transformer embeddings.
* **Sentence embeddings:** capture semantic meaning, robust to paraphrase.
* **ANN search:** FAISS indexing for scalable retrieval.
* **Evaluation:** correlation (Spearman/Pearson) with gold similarity, retrieval precision\@k.
* **Error slicing:** robustness to synonyms, typos, and cross-lingual queries.
# Step-by-Step Walkthrough
1. **Data intake & splits:** load STS-Benchmark and Tatoeba/SNLI; reproducible splits.
2. **Classical similarity:** TF-IDF + cosine similarity; spaCy word vectors.
3. **Dense embeddings:** Sentence-Transformers (e.g., `all-MiniLM-L6-v2`) with byte-level BPE tokenizer.
4. **ANN search:** FAISS index for dense embeddings; compare exact vs approximate retrieval.
5. **Evaluation:**
* Similarity: Pearson/Spearman correlation with human labels.
* Retrieval: precision\@k, recall\@k, MRR.
6. **Error analysis:** hard pairs (paraphrases missed by TF-IDF), language drift, short vs long queries.
7. **Reporting:** metrics tables, embedding visualizations, error examples → `reports/t8-semantic-search.md`.
8. *(Optional)* Serve: FastAPI endpoint for semantic search with ANN backend and schema validation.
# Hands-On Exercises
* Ablations: TF-IDF vs spaCy vs Sentence-Transformers; FAISS exact vs approximate.
* Robustness: typos, synonyms, multilingual queries; compare retrieval drop.
* Slice analysis: short vs long sentences, rare vocabulary.
* Stretch: hybrid retrieval (BM25 + embeddings).
# Common Pitfalls & Troubleshooting
* **High-dimensional embeddings:** memory issues; mitigate with PCA or quantization.
* **ANN drift:** FAISS index must be rebuilt when embeddings change.
* **Metrics misuse:** accuracy is uninformative — use correlation or ranking metrics.
* **Tokenizer drift:** must log tokenizer artifacts for reproducibility.
* **OOM:** large datasets with dense embeddings; shard or batch.
# Best Practices
* Log vocab sizes, embedding configs, and FAISS index metadata in MLflow.
* Compare sparse and dense under identical splits.
* Unit tests: cosine similarity outputs reproducible under fixed embeddings.
* Guardrails: schema validation in serving, enforce max query length.
* Keep **Baseline → Dense Embeddings → ANN** progression.
# Reflection & Discussion Prompts
* When do sparse methods outperform dense embeddings?
* How do ANN approximations trade accuracy for speed?
* What real-world risks come from low recall in semantic search?
# Next Steps / Advanced Extensions
* Experiment with cross-encoder models for reranking.
* Explore hybrid retrieval pipelines (BM25 + embeddings).
* Add multilingual embeddings (LaBSE, mUSE).
* Lightweight monitoring: log drift in embedding distributions.
# Glossary / Key Terms
Semantic similarity, cosine similarity, TF-IDF, [Sentence-Transformers](https://www.sbert.net/), FAISS, ANN, precision\@k, recall\@k, MRR.
# Additional Resources
* [Sentence-Transformers](https://www.sbert.net/)
* [[Hugging Face Transformers](https://huggingface.co/docs/transformers)](https://huggingface.co/docs/transformers)
* [[Hugging Face Datasets](https://huggingface.co/datasets)](https://huggingface.co/datasets)
* [[FAISS](https://faiss.ai/)](https://faiss.ai/)
* [[MLflow](https://mlflow.org/)](https://mlflow.org/)
* [[FastAPI](https://fastapi.tiangolo.com/)](https://fastapi.tiangolo.com/)
# Contributors
Author(s): TBD
Reviewer(s): TBD
Maintainer(s): TBD
Date updated: 2025-09-20
Dataset licenses: STS-Benchmark (permissive), Tatoeba (CC BY).
# Issues Referenced
Epic: HfLA Text Analysis Tutorials (T0–T14).
This sub-issue: **T8: Semantic Search & Similarity**.
---
Contributor guide
Assessment
This issue has not been assessed yet.