Skip to main content

DoctoSAE

Sparse autoencoders for interpretable and transferable medical retrieval. Reference implementation of the ML4H 2026 paper Explanations that Transfer: Interpreting Medical Retrieval with Sparse AutoEncoders (Diane Bouchacourt and Gaëtan Brison).

DoctoSAE overview

DoctoSAE trains a TopK sparse autoencoder (SAE) on the frozen embeddings of a text encoder, with a reconstruction loss and an InfoNCE loss that keeps queries close to their relevant documents. Documents are then ranked by cosine similarity of codes with at most k active features, and every feature can be described from the texts that activate it: top snippets, TF IDF terms and a topic written by an Interpreter LLM. These descriptions explain single retrievals, and they transfer to corpora the SAE never saw.

Warning: DoctoSAE is in active development and its API may change before version 1.0. It is a research tool: do not use it for diagnosis, treatment or any other clinical decision.

Install

pip install doctosae
pip install "doctosae[llm]"        # optional: LLM descriptions through any OpenAI compatible server
pip install "doctosae[local-llm]"  # optional: local LLM such as google/medgemma-4b-it

DoctoSAE needs Python 3.11 or newer. A GPU is advised for CURE; the small examples run on a laptop CPU.

Quickstart

from doctosae import DoctoSAE, Embedder, Interpreter, Trainer, TrainingConfig, evaluate, load_dataset

dataset = load_dataset("nfcorpus")                                # cure, miriad, r2med_biology, ...
data = Embedder("BAAI/bge-small-en-v1.5").embed_dataset(dataset)  # frozen encoder

sae = DoctoSAE(d_in=data.dim, n_latents=3 * data.dim, k=32, embedder=data.embedder)
sae = Trainer(sae, TrainingConfig(), output_dir="runs/nfcorpus/sae").fit(data).best_model  # paper settings

print(evaluate(None, data, split="test"))  # dense embeddings
print(evaluate(sae, data, split="test"))   # sparse codes: ndcg@10, recall@100, mrr@10

interpreter = Interpreter(sae, data, split="train")
for card in interpreter.describe(interpreter.select_features(n=3)):
    print(card.to_markdown())

The same steps from the command line:

doctosae embed --dataset nfcorpus --model BAAI/bge-small-en-v1.5 --output runs/nfcorpus/embeddings
doctosae train --dataset nfcorpus --embeddings runs/nfcorpus/embeddings --k 32 --output-dir runs/nfcorpus/sae
doctosae evaluate --dataset nfcorpus --embeddings runs/nfcorpus/embeddings --sae runs/nfcorpus/sae
doctosae interpret --dataset nfcorpus --embeddings runs/nfcorpus/embeddings --sae runs/nfcorpus/sae \
    --output runs/nfcorpus/features --n-features 20

What is inside

  • doctosae.load_dataset: CURE, NFCorpus, MIRIAD and the three R2MED subsets, downloaded from the Hugging Face Hub at pinned revisions with the splits of the paper; doctosae.load_beir_folder for your own data.
  • doctosae.Embedder: sentence transformers encoders; queries of the BGE v1.5 models get the retrieval instruction of their model card, the one the paper used for BGE large, and the embeddings record every setting.
  • doctosae.DoctoSAE: the TopK SAE, saved and shared through the Hugging Face Hub (save_pretrained, push_to_hub, from_pretrained, doctosae.load_pretrained).
  • doctosae.Trainer: the paper objective, sampler and checkpoint selection. The defaults are the values the paper holds fixed, and one point of its grid for the InfoNCE weight and temperature, which it selects per k.
  • doctosae.evaluate and doctosae.search: retrieval on sparse codes, NDCG@10 computed as in the paper.
  • doctosae.Interpreter: feature selection, feature cards, Interpreter and Activation Prediction LLMs (OpenAI compatible servers, local Transformers models), and retrieval explanations as in Figure 2 of the paper.
  • doctosae command line: datasets, pretrained, embed, train, evaluate, interpret, explain, push, convert.

Pretrained sparse autoencoders

The checkpoints of the paper, trained on CURE, are in the DoctoSAE collection of the Hugging Face Hub: one repository per encoder, one folder per configuration.

Encoder Alias Repository Checkpoints
BAAI/bge-large-en-v1.5 bge doctolib-lab/doctosae-bge-large-en-v1.5 k64_m3072, k256_m3072
abhinand/MedEmbed-large-v0.1 medembed doctolib-lab/doctosae-medembed-large-v0.1 k64_m3072, k256_m3072
gemini-embedding-001 gemini doctolib-lab/doctosae-gemini-embedding-001 k128_m9216, k256_m9216
from doctosae import load_pretrained

sae = load_pretrained("bge", k=64)    # doctolib-lab/doctosae-bge-large-en-v1.5/k64_m3072
sae = load_pretrained("gemini")       # the smallest k: doctolib-lab/doctosae-gemini-embedding-001/k128_m9216

The smallest k is the configuration the paper interprets, k = 256 the one that retrieves best. doctosae pretrained lists every checkpoint id. Weights trained on CURE are cc-by-nc-4.0 (noncommercial), the license of CURE.

Results from the paper

NDCG@10 (%) with BAAI/bge-large-en-v1.5, SAEs trained on CURE, mean over three seeds (Table 1 of the paper):

BGE CURE NFCorpus MIRIAD R2MED Bioinformatics R2MED Biology R2MED Medical Sciences
original 53.06 38.17 77.24 27.04 12.72 27.78
k = 64 57.07 31.89 68.75 22.50 8.30 22.72
k = 128 59.35 34.48 71.83 27.42 9.76 26.60
k = 256 60.50 35.61 73.85 28.10 10.06 29.05
dense (k = 1024) 61.87 36.62 75.05 27.21 8.37 27.33

Citation

@misc{bouchacourt2026explanationstransferinterpretingmedical,
      title={Explanations that Transfer: Interpreting Medical Retrieval with Sparse AutoEncoders},
      author={Diane Bouchacourt and Gaëtan Brison},
      year={2026},
      eprint={2611.18179},
      archivePrefix={arXiv},
      primaryClass={cs.IR},
      url={https://arxiv.org/abs/2611.18179},
}

License

The code is released under the Apache 2.0 license. Datasets keep their own licenses and are downloaded from the Hugging Face Hub at pinned revisions (CURE is cc-by-nc-4.0, noncommercial). SAE weights trained on CURE are cc-by-nc-4.0.

Metadata

Release files for doctosae 0.1.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 doctosae 0.1.0
File Size Uploaded Provenance Actions
doctosae-0.1.0.tar.gz 193.3 kB Without provenance Details for doctosae-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for doctosae 0.1.0
File Interpreter ABI Platform Actions
doctosae-0.1.0-py3-none-any.whl Python 3 none any Details for doctosae-0.1.0-py3-none-any.whl

Total release size: 329.1 kB

Release files / doctosae-0.1.0.tar.gz

Download URL doctosae-0.1.0.tar.gz
Size 193.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ecf7b17ee12fbc92b57cd3e7532ded7ae4fee563920047fd982d1971d3efc6d0
BLAKE2b-256 checksum
How to use checksums
c8ff1a17873b675c28e6b842b0b1e9fb98f0a9877c424597f4b5105acbf06983
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Release files / doctosae-0.1.0-py3-none-any.whl

Download URL doctosae-0.1.0-py3-none-any.whl
Size 135.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f01d8003d831016fb0ca880b640f0f1bf020919dca80582e80520370bd28b078
BLAKE2b-256 checksum
How to use checksums
f82025085577988de0127494941f41448f74f651fe4e03195842bcae78cc7c03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.5

Security for doctosae 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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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