NashVec
Optimization of Latent-Space Compression using Game-Theoretic Techniques for Transformer-Based Vector Search
Overview
NashVec is a game-theoretic approach to optimizing vector search by balancing information preservation and retrieval efficiency. By formulating vector compression as a zero-sum game between an encoder (compressor) and retriever, NashVec achieves superior search performance with reduced storage requirements.
Key Features
- 🔬 Game-Theoretic Optimization: Balances reconstruction quality with retrieval performance using a Nash equilibrium approach
- 🎯 Hybrid Search: Combines compressed latent representations with HNSW indexing for fast, accurate retrieval
- 📊 Dual Backend Support: FAISS for baseline comparison and custom HNSW for hybrid search
- 🚀 Production-Ready: Modular design with CLI tools, comprehensive tests, and extensive documentation
- 🔧 Customizable: Adjustable loss weights, dimensions, and hyperparameters for different use cases
Installation
Requirements
- Python 3.8 or higher
- pip
Install from PyPI
pip install nashvec
Install from Source
git clone https://github.com/kushagraagrawal/NashVec.git
cd NashVec
pip install -e .
Install with Test Dependencies
pip install "nashvec[dev]"
Quick Start
Using the Python API
from nashvec import HybridSearcher
# Initialize hybrid searcher with game-theoretic compression
searcher = HybridSearcher(use_hybrid=True)
# Load data and train
searcher.load_and_train(limit=500, epochs=10)
# Search
results = searcher.search("Explain the process of photosynthesis", top_n=5)
for sentence, score in results:
print(f"Score: {score:.4f}\n{sentence}\n")
Using the CLI
# Train a model
nashvec-train --epochs 10 --batch-size 32
# Query the model
nashvec-query "Explain the process of photosynthesis" --top-n 5
# Benchmark performance
nashvec-benchmark --limit 500 --epochs 10
Architecture
Game-Theoretic Framework
NashVec implements a two-player game:
- Encoder (Compressor): Minimizes reconstruction error to preserve information
- Retriever: Maximizes search efficiency by clustering similar items in latent space
The game-theoretic loss function:
L_total = L_reconstruction + λ × L_triplet
Where:
L_reconstruction: MSE between original and reconstructed embeddingsL_triplet: Triplet loss ensuring similar items are close in latent spaceλ: Balance parameter (typically 0.5)
Component Overview
┌─────────────────────────────────────────────────────────────┐
│ Transformer Embeddings │
│ (Sentence-BERT, 384-dim) │
└────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Game-Theoretic Autoencoder │
│ ┌──────────────┐ ┌──────────────┐ │
│ │ Encoder │─────▶│ Latent │ │
│ │ (compress) │ │ Space │ │
│ └──────────────┘ │ (128-dim) │ │
│ └──────┬───────┘ │
│ │ │ │ │
│ │ │ ▼ │
│ │ │ Decoder │
│ │ │ (reconstruct) │
│ ▼ ▼ ▼ │
│ Triplet Loss + MSE Loss │
└────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ HNSW Index (Fast Retrieval) │
│ + │
│ Re-ranking (Original Space) │
└─────────────────────────────────────────────────────────────┘
Documentation
Core Modules
nashvec.data
Dataset loading and preprocessing:
from nashvec.data import load_alpaca_data, load_custom_dataset
# Load Alpaca dataset
instructions = load_alpaca_data(limit=500)
# Load custom dataset
texts = load_custom_dataset("data.csv", text_column="text")
nashvec.embedding
Sentence embedding generation:
from nashvec.embedding import SentenceEmbedder
embedder = SentenceEmbedder(model_name="all-MiniLM-L6-v2")
embeddings = embedder.encode_batch(texts, batch_size=32)
nashvec.autoencoder
Game-theoretic autoencoder:
from nashvec.autoencoder import GameTheoreticAutoencoder, build_encoder_decoder
encoder, decoder = build_encoder_decoder(input_dim=384, latent_dim=128)
model = GameTheoreticAutoencoder(encoder, decoder, lambda_retrieval=0.5)
model.compile(optimizer='adam', ae_loss_fn='mse')
nashvec.search
High-level search interface:
from nashvec.search import HybridSearcher, compute_utility
searcher = HybridSearcher(use_hybrid=True)
results = searcher.search("query text", top_n=5)
utility = compute_utility(accuracy=0.95, query_time=0.01)
Configuration
from nashvec.utils import NashVecConfig
config = NashVecConfig(
latent_dim=128,
epochs=10,
lambda_retrieval=0.5,
margin=0.2
)
Examples
Example 1: Basic Search
from nashvec import HybridSearcher
# Create searcher
searcher = HybridSearcher(use_hybrid=True)
# Train on dataset
searcher.load_and_train(limit=500, epochs=10)
# Query
results = searcher.search("How does photosynthesis work?", top_n=3)
for i, (text, score) in enumerate(results, 1):
print(f"{i}. [{score:.4f}] {text}")
Example 2: Evaluation with Metrics
from nashvec import HybridSearcher
searcher = HybridSearcher(use_hybrid=True)
searcher.load_and_train(limit=500, epochs=10)
# Get evaluation metrics
metrics = searcher.evaluate("query text", top_n=5)
print(f"Query Time: {metrics['query_time']:.4f}s")
print(f"Avg Similarity: {metrics['avg_similarity']:.4f}")
print(f"Utility: {metrics['utility']:.4f}")
Example 3: Comparison Study
from nashvec import HybridSearcher
queries = [
"Explain machine learning",
"What is Python?",
"Describe neural networks"
]
# Test hybrid system
hybrid_searcher = HybridSearcher(use_hybrid=True)
hybrid_searcher.load_and_train(limit=500, epochs=10)
# Test baseline
faiss_searcher = HybridSearcher(use_hybrid=False)
faiss_searcher.load_and_train(limit=500)
for query in queries:
hybrid_results = hybrid_searcher.evaluate(query)
faiss_results = faiss_searcher.evaluate(query)
if hybrid_results['utility'] > faiss_results['utility']:
print(f"✓ Hybrid wins for '{query}'")
else:
print(f"✓ Baseline wins for '{query}'")
Running Examples
# Run the demo
python examples/demo_search.py
# Run tests
pytest tests/
# Run specific test
pytest tests/test_autoencoder.py
API Reference
See full API documentation for detailed information about all modules, classes, and functions.
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Citation
If you use NashVec in your research, please cite:
@misc{agrawal2025optimizationlatentspacecompressionusing,
title={Optimization of Latent-Space Compression using Game-Theoretic Techniques for Transformer-Based Vector Search},
author={Kushagra Agrawal and Nisharg Nargund and Oishani Banerjee},
year={2025},
eprint={2508.18877},
archivePrefix={arXiv},
primaryClass={cs.IR},
url={https://arxiv.org/abs/2508.18877},
}
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Sentence Transformers for embedding generation
- Facebook AI Research for FAISS
- Yury Malkov for HNSW algorithm
- Hugging Face for datasets
Support
For issues, questions, or contributions:
NashVec - Game-Theoretic Vector Search for the Modern Era 🔬🚀
Metadata
Release files for nashvec 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| nashvec-0.1.0.tar.gz | 23.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nashvec-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 44.4 kB
Release files / nashvec-0.1.0.tar.gz
| Download URL | nashvec-0.1.0.tar.gz |
|---|---|
| Size | 23.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2758e53b88d9b29ed45608b88f45686eb4d27f6789a8424d09e0b59680b9567d
|
|
BLAKE2b-256 checksum How to use checksums |
641bbc683ab1fb03619c7b76f92f0ec7eb35b3c6794fcd882e274633a85c9927
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.10
|
Release files / nashvec-0.1.0-py3-none-any.whl
| Download URL | nashvec-0.1.0-py3-none-any.whl |
|---|---|
| Size | 21.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9d14d3312f227310de8ab7b31385cb287a38cdcf6cd05c2dfb18acde93801de3
|
|
BLAKE2b-256 checksum How to use checksums |
000eedb08f962ca4255bc5f4abc93021a2e0ffa10f6ae0b921cf784bb0a25f30
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.10
|
Security for nashvec 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.