Views
No views yet
Rostlab/prot_t5_xl_uniref50 protein language model, trained with Low-Rank Adaptation (LoRA) using a hybrid objective that combines standard Masked Language Modeling (MLM) with a contrastive loss.transformers and peft libraries. The LoRA adapters live inside this repo under runs/protrans_XL_Full_lora_envhog_ContraMLM_v1_1/lora_adapters, so make sure to pass subfolder when loading.1import torch
2from transformers import T5Tokenizer, T5ForConditionalGeneration
3from peft import PeftModel
4
5device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6base_model_name = "Rostlab/prot_t5_xl_uniref50"
7adapter_repo = "Amin-Saeidi/PhageContraMLM"
8adapter_subfolder = "runs/protrans_XL_Full_lora_envhog_ContraMLM_v1_1/lora_adapters"
9
10# 1. Load tokenizer and base model
11tokenizer = T5Tokenizer.from_pretrained(base_model_name, do_lower_case=False)
12model = T5ForConditionalGeneration.from_pretrained(base_model_name, torch_dtype=torch.float16)
13
14# 2. Attach LoRA adapters and merge
15model = PeftModel.from_pretrained(model, adapter_repo, subfolder=adapter_subfolder)
16model = model.merge_and_unload().to(device).eval()
17
18# 3. Prepare sequence (space-separated, rare amino acids replaced)
19seq = "M A K K L K I L L L A A S L V S L S P S V F A"
20inputs = tokenizer(seq, return_tensors="pt").to(device)
21
22# 4. Extract mean-pooled embeddings
23with torch.no_grad():
24 outputs = model.encoder(**inputs)
25 hidden = outputs.last_hidden_state
26 mask = inputs.attention_mask.unsqueeze(-1).to(hidden.dtype)
27 pooled_embedding = (hidden * mask).sum(dim=1) / mask.sum(dim=1).clamp(min=1.0)
28
29print(pooled_embedding.shape)conda create -n phagecontramlm_env python=3.11.5
conda activate phagecontramlm_envsudo apt-get install git-lfs, for Windows either use git bash or get git-lfs from here. Then:git lfs install
git clone https://huggingface.co/Amin-Saeidi/PhageContraMLMcd PhageContraMLM
pip install -r requirements.txtpython src/train.pysrc/train.py for the available config flags (data paths, LoRA rank/alpha, loss weighting) before launching a run.envhog_phrog2 dataset.src/, data/, and runs/ directories.src/train.py
Main training loop. Implements a custom PairGraphCollator that samples positive protein pairs on-the-fly and builds a dynamic adjacency matrix for the contrastive loss, combined with a curriculum-aware MLM objective.src/produce_test_data_embeddings.py
High-throughput script for generating mean-pooled encoder embeddings. Loads the base ProtT5 model, attaches the best LoRA adapters from your checkpoints, and processes raw FASTA/CSV sequences in batches, saving results as .pkl and .csv files.src/eval_EmbeddingSpace.py
Generates publication-quality plots analyzing the embedding space:src/eval_PhrogRetrieval.py
Zero-shot functional retrieval benchmarking using hnswlib (Hierarchical Navigable Small World graphs):torch==2.6.0
transformers==4.37.2
peft==0.10.0
pandas==3.0.1
numpy==2.3.5
matplotlib==3.10.8
seaborn==0.13.2
scikit-learn==1.8.0
hnswlib==0.8.0
safetensors==0.7.0
sentencepiece==0.2.0