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)

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.1.1.tar.gz (14.5 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.1.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rat_embed-0.1.1.tar.gz
  • Upload date:
  • Size: 14.5 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.1.1.tar.gz
Algorithm Hash digest
SHA256 77c484a903680db46405e1c1ad6a40acab8cbe5aafd8f966ddac16059790a133
MD5 99349574e47df1a4863359ccddd2d884
BLAKE2b-256 899e185fdf6fe435f99ce2f8fbe1b6bd35efe8042ecb78c0c539cfc805645c5c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rat_embed-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 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.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 45e61611fc8cd9ab32d0aeee2176478f7d5d15d0ff906a8921bb4776c767a32d
MD5 8ee09fcaec20062c40daabcf76298f3d
BLAKE2b-256 e4b5067d0dafdc6d9b1bd5d7876326a33e317acf0e21da16dacefe051524ce1b

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