YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
polygene_large
Checkpoint checkpoint-1700000 of the Polygene model, its tokenizer (tokenizer.pkl) and the code needed to load it (disease_geometry/, load.py).
Tokenizer
GeneTokenizer turns one AnnData cell (a single row, var_names as Ensembl IDs) into two aligned sequences, input_ids (token values) and token_type_ids (token identities):
assay cell_type development_stage disease sex tissue gene_a gene_b ...
input_ids <phenotype vocabulary tokens ..................... > bin_k bin_k ...
type_ids 1 2 3 4 5 6 7 + a 7 + b ...
- Phenotypes: one token per phenotype type in
tokenizer.phenotype_types, read fromcell.obs. A missing or"unknown"value becomes[mask]; development stage is coarsened into 5 year bins. - Genes: the cell's genes ordered by
tokenizer.hvg(top 50,000). Only expressed genes are included (sparse). Expression is binned with edges[0, 1, 2, 3, 4, 5, 6]into tokensbin_0...bin_6. The gene's identity is itstoken_type_id,7 + gene index. - The sequence is truncated to 2,000 tokens.
vocab_size= 946 (special, phenotype and bin tokens),type_vocab_size= 50,007.
Forward pass
- Embedding:
token_value_embedding(input_ids) + token_type_embedding(token_type_ids), then LayerNorm and dropout. There are no positional embeddings. - Encoder: DistilBERT transformer, 6 layers, dim 240, 6 heads, FFN 480.
- Latent: every output position is L2 normalised onto the unit sphere.
- Prediction head: tied to the token value embedding matrix;
logits = latent @ token_value_embedding.T / temperatureover the whole vocabulary at every position. - Loss (when
labelsis given): masked language modelling cross entropy over phenotype and gene positions.
Outputs (MaskedLMOutput): logits (B, S, 946), hidden_states = normalised latent (B, S, 240).
Running it
Requires a Hugging Face login with access to this repo and the packages in requirements.txt (transformers 4.44, huggingface_hub < 1.0).
from load import load
model, tokenizer = load() # downloads Louis-Alexandre/polygene_large
# model, tokenizer = load("/local/copy") # or an already downloaded copy
import torch, anndata
adata = anndata.read_h5ad("cells.h5ad")
input_ids, token_type_ids, _ = tokenizer(adata[0])
number_of_phenotypes = len(tokenizer.phenotype_types)
input_ids[:number_of_phenotypes] = tokenizer.convert_tokens_to_ids(tokenizer.mask_token) # mask phenotypes to predict them
with torch.inference_mode():
output = model(input_ids=input_ids[None], token_type_ids=token_type_ids[None], attention_mask=torch.ones_like(input_ids)[None])
allowed = torch.zeros(number_of_phenotypes, tokenizer.vocab_size, dtype=torch.bool)
for index, phenotype in enumerate(tokenizer.phenotype_types):
allowed[index, tokenizer.convert_tokens_to_ids(tokenizer.phenotype_vocabulary[phenotype])] = True
phenotype_logits = output.logits[0, :number_of_phenotypes].masked_fill(~allowed, -torch.inf)
id_to_token = tokenizer.flattened_tokens
predicted = {phenotype: id_to_token[i] for phenotype, i in zip(tokenizer.phenotype_types, phenotype_logits.argmax(-1).tolist())}
latent = output.hidden_states[0] # (S, 240), one vector per token
For batches, pad with disease_geometry.model.train_utils.collate_fn_wrapper(tokenizer).
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐ Ask for provider support