Skip to main content

Relative Anchor Translation: zero-shot embedding space translation via similarity profiles

Project description

RAT — Relative Anchor Translation

PyPI Python License: MIT DOI

Zero-shot embedding space translation using relative distances to shared anchors. No additional training required.

Install

pip install rat-embed            # core (numpy only)
pip install "rat-embed[models]"  # + sentence-transformers for fit()
pip install "rat-embed[dev]"     # + pytest, ruff

Quick Start

import numpy as np
from rat import RATranslator

# 1. Prepare anchor embeddings from both models (K anchors, L2-normalized)
anchor_a = ...  # (K, D_a) from model A
anchor_b = ...  # (K, D_b) from model B

# 2. Fit the translator
translator = RATranslator(kernel="poly").fit_embeddings(anchor_a, anchor_b)

# 3. Transform & retrieve
query_emb = ...  # (N, D_a) from model A
db_emb = ...     # (M, D_b) from model B
results = translator.retrieve(query_emb, db_emb, top_k=10)
# results["indices"]  → (N, 10) nearest neighbor indices
# results["scores"]   → (N, 10) cosine similarity scores

Or transform individually for more control:

q_rel = translator.transform(query_emb, "a")              # query side (no z-score)
d_rel = translator.transform(db_emb, "b")                 # db side (z-score applied)
d_rel = translator.transform(db_emb, "b", role="query")   # override: skip z-score

Note: For cross-family model pairs (e.g., MiniLM → BGE), use normalize="always" when constructing the translator. The default "auto" mode may skip z-score normalization for some models where it would actually help in cross-model scenarios.

Advanced: RATHub (multi-model)

from rat import RATHub

hub = RATHub(kernel="poly")
hub.set_anchors("minilm", anchor_minilm)      # (K, 384)
hub.set_anchors("e5", anchor_e5)              # (K, 1024)
hub.set_anchors("bge", anchor_bge)            # (K, 384)

# Transform from any model
q = hub.transform("minilm", query_emb, role="query")
d = hub.transform("e5", db_emb, role="db")

# Or use retrieve directly
results = hub.retrieve(query_emb, db_emb, "minilm", "e5", top_k=10)

Important: All models must use anchors from the same texts in the same order. Select anchor indices once (e.g., via FPS on one model), then use those indices for every model.

Multi-DB search

Search across multiple databases built with different models:

results = hub.retrieve_multi(
    query_emb,
    databases=[
        (db1_emb, "bge"),       # BGE database
        (db2_emb, "minilm"),    # MiniLM database
        (db3_emb, "e5"),        # E5 database
    ],
    query_model="bge",
    top_k=10,
)
# results["indices"]   → (N, 10) global indices
# results["scores"]    → (N, 10) normalized scores
# results["db_labels"] → (N, 10) which DB each result came from (0, 1, 2)

retrieve_multi uses per-database score normalization internally to make scores comparable across databases. Do not vstack relative representations from different models — their score scales differ.

Paper

See the Zenodo record for the full experiment report.

Experiment reproduction code is in experiments/ (unchanged from the original research).

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rat_embed-0.2.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rat_embed-0.2.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file rat_embed-0.2.0.tar.gz.

File metadata

  • Download URL: rat_embed-0.2.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for rat_embed-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2de0ae2f53a9251c11bbd41d36499058dc68f63aeb04b3be51a55e25af08c02b
MD5 b38d4a7543bae3a1a9ab0965db5664f2
BLAKE2b-256 88d96a794479853627a557b11fa5adf9f2450a03e3b9a4e919a06e5bd038cb97

See more details on using hashes here.

File details

Details for the file rat_embed-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: rat_embed-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for rat_embed-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 293a2420eaf26272771a003cf906864d3ee26c2849ba7cd41e4424324afd24ac
MD5 752de5b3858aba3138473accd5a85b9f
BLAKE2b-256 7706ec4aa2bd8d00eb0ba55e4af4bc5b8f2d781b26c4895eda6a72c40028809e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page