Skip to main content

neembed

Fine-tune pretrained sentence embedding models in non-Euclidean spaces with Geoopt.

Documentation · 日本語

Status: Package version v0.7.0 adds manifold Triplet, MarginMSE, distance-regression, and symmetric MNRL objectives, graded corpus nDCG@K evaluation, and a deterministic objective-comparison workflow while preserving the v0.6 exact-retrieval/mining surface and all v0.4-v0.5 public paths. The API remains intentionally small and may still evolve before a stable 1.0 release.

neembed is a lightweight integration layer between pretrained Sentence Transformer models and manifold-valued representations. It keeps the pretrained encoder intact, optionally projects its Euclidean output, and delegates hyperbolic geometry to Geoopt.

Pretrained Sentence Encoder
        ↓
Euclidean sentence embedding
        ↓
Projection head (optional)
        ↓
Tangent-space representation
        ↓
Geoopt manifold map
        ↓
Non-Euclidean embedding

Why non-Euclidean embeddings?

Hierarchical and tree-like relations can be awkward to represent in a flat Euclidean space. Hyperbolic spaces are a natural fit for rapidly expanding structures such as:

  • taxonomies and concept hierarchies
  • knowledge graphs
  • hierarchical labels
  • tree-like semantic relations

The current API supports the Poincaré ball and Lorentz / Hyperboloid models through the same model, loss, trainer, evaluator, and sentence-model save/load workflow.

Current scope

v0.4 keeps the fixed-curvature v0.3 path backward-compatible:

  • Sentence Transformers as the pretrained encoder backend
  • Poincaré-ball and Lorentz / Hyperboloid embeddings through Geoopt
  • optional lower-dimensional tangent-space projection
  • shared public curvature semantics across the two hyperbolic models
  • geodesic distance and manifold-aware multiple-negatives ranking loss
  • ordinary AdamW fine-tuning for the model-only path
  • encode() / distance(), ManifoldEmbeddingEvaluator, DataLoader interoperability, and local save/load
  • geometry-consistency regressions and a matched Euclidean-vs-Poincaré-vs-Lorentz engineering benchmark

v0.4 also adds:

  • opt-in fixed vs learnable curvature for Poincaré and Lorentz
  • true trainable manifold-valued ManifoldPrototypes
  • ManifoldPrototypeHierarchyLoss for sentence assignments plus parent-child structure
  • an explicit caller-supplied Geoopt Riemannian optimizer path for manifold parameters
  • joint learnable-curvature / prototype training through Geoopt stabilization
  • a compact fixed-vs-learnable structure regression example

v0.5 adds a focused retrieval workflow without introducing a retrieval framework:

  • optional caller-supplied explicit hard negatives while preserving the original (anchors, positives) training contract
  • Recall@K and MRR on the aligned retrieval evaluator
  • model.rank() for small in-memory geodesic reranking
  • nearest-prototype assignment evaluation for learned manifold structure
  • one reproducible v0.5 retrieval regression example

v0.6 extends that workflow to exact caller-owned corpora without adding ANN infrastructure:

  • chunked exact geodesic distance evaluation without materializing the full query-by-corpus distance matrix
  • exact_corpus_search() for exact text-corpus top-k retrieval
  • ManifoldCorpusRetrievalEvaluator with explicit query/corpus IDs and multi-positive Recall@K / MRR
  • deterministic caller-invoked mine_hard_negatives() with positive, self, and additional exclusions
  • a reproducible v0.6 exact-search → evaluation → mining → explicit-negative training regression example

v0.7 expands the retrieval-objective and evaluation surface without changing the v0.4-v0.6 contracts:

  • ManifoldTripletLoss for aligned geodesic margin triplets
  • ManifoldMarginMSELoss for teacher positive-minus-negative margin regression
  • ManifoldDistanceMSELoss for direct geodesic-distance regression
  • opt-in ManifoldSymmetricMultipleNegativesRankingLoss while keeping the original one-directional MNRL unchanged
  • ManifoldGradedCorpusRetrievalEvaluator with nDCG@K while preserving the existing binary/multi-positive evaluator output
  • a deterministic Poincaré objective-comparison example covering MNRL, Triplet, MarginMSE, DistanceMSE, MRR, Recall@K, and nDCG@K

Hierarchy-native retrieval objectives are planned separately for v0.8. New manifold families, ANN/vector-database integration, and distributed retrieval remain outside the v0.7 scope.

A manifold-valued output does not by itself require Riemannian optimization: encoder/projection parameters and learnable curvature are not manifold-valued points. Detailed parameter, optimizer, persistence, and numerical behavior lives in the Learnable structure guide. The end-to-end retrieval composition and the distinction between small in-memory reranking, exact corpus search, and external ANN systems are documented in the Retrieval workflow guide. Objective and graded-evaluation selection for v0.7 is documented in the Retrieval objectives guide.

Installation

pip install neembed-geoopt

The PyPI distribution is named neembed-geoopt; the Python import package remains neembed.

For development:

git clone https://github.com/t-yamsaki/neembed.git
cd neembed
pip install -e ".[dev]"

Quick start

from neembed import (
    ManifoldSentenceTransformer,
    ManifoldMultipleNegativesRankingLoss,
    ManifoldTrainer,
)

model = ManifoldSentenceTransformer(
    "sentence-transformers/all-MiniLM-L6-v2",
    manifold="poincare",
    embedding_dim=64,
    curvature=1.0,
)

loss = ManifoldMultipleNegativesRankingLoss(
    model=model,
    temperature=0.1,
)

trainer = ManifoldTrainer(model=model, loss=loss)

train_batches = [
    (["Shiba Inu", "Siamese cat"], ["dog", "cat"]),
    (["dog", "cat"], ["mammal", "feline"]),
]

trainer.fit(train_batches, epochs=1)

embeddings = model.encode(["Shiba Inu", "dog", "mammal"])
distance = model.distance(embeddings[0], embeddings[1])
print(float(distance))

Each anchor is paired with the positive at the same batch index. Because off-diagonal candidates become in-batch negatives, avoid duplicate positives within one batch. This model-only path keeps the ordinary AdamW behavior even though its outputs lie on a manifold. See the Training guide for the objective and batching details, the Retrieval workflow guide for optional explicit negatives and retrieval evaluation, the Retrieval objectives guide for v0.7 objective/metric selection, and the Learnable structure guide before adding trainable manifold prototypes.

Documentation

The full guide is hosted on Read the Docs:

Examples and validation

Run the main references from the repository root:

python examples/train_poincare.py
python examples/train_lorentz.py
python examples/v04_learnable_structure.py
python examples/v05_retrieval_workflow.py
python examples/v06_exact_retrieval_workflow.py
python examples/v07_objective_comparison.py

License

neembed is released under the MIT License. Third-party dependencies retain their own licenses.

Metadata

Release files for neembed-geoopt 0.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for neembed-geoopt 0.7.0
File Size Uploaded
neembed_geoopt-0.7.0.tar.gz 68.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for neembed-geoopt 0.7.0
File Interpreter ABI Platform
neembed_geoopt-0.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 102.1 kB

Release files / neembed_geoopt-0.7.0.tar.gz

Download URL neembed_geoopt-0.7.0.tar.gz
Size 68.3 kB
Tags Source
SHA-256 checksum
How to use checksums
052919c0286cf3553b1e6993fcb383ce36cc1b451b38c37900810bc4ad43eba6
BLAKE2b-256 checksum
How to use checksums
9d5772069190abd288d463b2284870375903df5058b46f2053cc784925a3a180
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 29, 2026.

Transparency log

Release files / neembed_geoopt-0.7.0-py3-none-any.whl

Download URL neembed_geoopt-0.7.0-py3-none-any.whl
Size 33.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac003437840df798102228f95ed7a16e20b50a1974a86568c966d4307efcfd90
BLAKE2b-256 checksum
How to use checksums
f302396fb5457b96f285f74d6755fca93eb4e4ea9cefa3a8bd377b4cd4f0e259
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 29, 2026.

Transparency log

Release history Release notifications | RSS feed

0.11.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

This release

0.7.0 This release

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page