DoctoSAE for gemini-embedding-001

Requires the Google Gemini API. These SAEs take gemini-embedding-001 embeddings as input. That model is proprietary and only available through the paid Gemini API or Vertex AI; no weights are distributed with this repository.

Sparse autoencoders (SAEs) trained on gemini-embedding-001 embeddings for medical retrieval, from the paper Explanations that Transfer: Interpreting Medical Retrieval with Sparse AutoEncoders (ML4H 2026). Code: doctolib-lab/doctosae.

Each SAE maps a 3072 dimensional Gemini embedding to a sparse code where only k of m latents are active. Retrieval is cosine similarity between sparse codes, and every active latent can be read as a medical concept.

Checkpoints

Folder k m When to use it
k128_m9216 128 9216 Interpretability: the configuration used for the Gemini interpretability results in the paper
k256_m9216 256 9216 Retrieval: the best sparse Gemini configuration in domain and on R2MED Medical Sciences

Each folder holds sae.safetensors (weights) and cfg.json (architecture, training hyperparameters, metrics of this checkpoint).

Results

NDCG@10 (%), mean ± 95% CI over 3 seeds, from Table 1 of the paper. The released checkpoints are seed 42; their own CURE test score is in cfg.json.

Model CURE NFCorpus MIRIAD R2MED Bioinformatics R2MED Biology R2MED Medical Sciences
Gemini original 52.77 42.22 84.93 51.35 32.94 52.51
k128_m9216 58.76 ± 0.47 35.19 ± 0.23 79.07 ± 0.45 41.48 ± 2.83 23.26 ± 1.30 44.23 ± 4.85
k256_m9216 59.85 ± 0.45 37.42 ± 1.06 80.49 ± 0.78 44.80 ± 2.66 26.36 ± 1.59 47.86 ± 2.50

The SAEs are trained on CURE only; the five other datasets are never seen in training. Out of domain, no sparse Gemini configuration beats the original embedding.

Usage

import json
import torch
from google import genai
from huggingface_hub import hf_hub_download
from safetensors.torch import load_file

repo, variant = "doctolib-lab/doctosae-gemini-embedding-001", "k128_m9216"
w = load_file(hf_hub_download(repo, f"{variant}/sae.safetensors"))
cfg = json.load(open(hf_hub_download(repo, f"{variant}/cfg.json")))


def sae_encode(x: torch.Tensor) -> torch.Tensor:
    """(n, 3072) Gemini embeddings -> (n, m) sparse codes with k active latents."""
    pre = (x - w["b_pre"]) @ w["encoder.weight"].T + w["encoder.bias"]
    idx = pre.topk(cfg["k"], dim=-1).indices
    return torch.relu(torch.zeros_like(pre).scatter(-1, idx, pre.gather(-1, idx)))


def sae_decode(z: torch.Tensor) -> torch.Tensor:
    return z @ w["decoder.weight"].T + w["b_pre"]


client = genai.Client()  # needs Gemini API or Vertex AI credentials


def embed(texts: list[str]) -> torch.Tensor:
    # Default settings (no task_type, 3072 dimensions), as during training.
    out = client.models.embed_content(model="gemini-embedding-001", contents=texts)
    return torch.tensor([e.values for e in out.embeddings], dtype=torch.float32)


zq = sae_encode(embed(["What are the first line treatments for type 2 diabetes?"]))
zd = sae_encode(embed(["Metformin remains the recommended initial therapy for most adults with type 2 diabetes."]))
print(torch.nn.functional.cosine_similarity(zq, zd))   # retrieval score
print(zq[0].nonzero().squeeze().tolist())              # active latents of the query

Queries and documents are embedded the same way, with the API defaults and no task type.

Training

  • Data: CURE (clinia/CUREv1), English queries, train split (1,400 queries) and the CURE corpus.
  • Objective: MSE reconstruction plus λ · InfoNCE on the sparse codes of query and relevant document pairs; TopK sparsity, ReLU applied after TopK.
  • Hyperparameters: see cfg.json in each folder (λ = 5e‑6 for k=128 and 1e‑5 for k=256; τ = 0.05; lr 5e‑4; batch 512; 100 epochs; checkpoint with the best validation NDCG@10).

Limitations

  • Depends on a proprietary embedding model; if Google changes gemini-embedding-001, these SAEs may no longer match its outputs.
  • English only, trained on scientific literature; not evaluated on clinical notes or patient text.
  • A research artifact for interpretability and retrieval studies, not a medical device and not for clinical decisions.

License

The weights are released under CC BY‑NC 4.0, matching the license of the CURE training data. Use of gemini-embedding-001 is governed by Google's Gemini API terms.

Citation

@inproceedings{bouchacourt2026explanations,
  title     = {Explanations that Transfer: Interpreting Medical Retrieval with Sparse AutoEncoders},
  author    = {Bouchacourt, Diane and Brison, Ga{\"e}tan},
  booktitle = {Proceedings of the Machine Learning for Health (ML4H) Symposium},
  series    = {Proceedings of Machine Learning Research},
  volume    = {346},
  year      = {2026}
}
Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support

Dataset used to train doctolib-lab/doctosae-gemini-embedding-001

Collection including doctolib-lab/doctosae-gemini-embedding-001

Paper for doctolib-lab/doctosae-gemini-embedding-001