bindevaluator module with esm embedding guidance
Browse files
models/bindevaluator_emb_guidance.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pdb
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
import pytorch_lightning as pl
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
from .bindevaluator_modules import *
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class BindEvaluator(pl.LightningModule):
|
| 11 |
+
def __init__(self, n_layers, d_model, d_hidden, n_head,
|
| 12 |
+
d_k, d_v, d_inner, dropout=0.2,
|
| 13 |
+
learning_rate=0.00001, max_epochs=15, kl_weight=1):
|
| 14 |
+
super(BindEvaluator, self).__init__()
|
| 15 |
+
self.esm_model = EsmModel.from_pretrained("facebook/esm2_t33_650M_UR50D")
|
| 16 |
+
self.esm_model = self.esm_model.to('cuda:0')
|
| 17 |
+
|
| 18 |
+
for param in self.esm_model.parameters():
|
| 19 |
+
param.requires_grad = False
|
| 20 |
+
|
| 21 |
+
self.repeated_module = RepeatedModule3(n_layers, d_model, d_hidden,
|
| 22 |
+
n_head, d_k, d_v, d_inner, dropout=dropout)
|
| 23 |
+
|
| 24 |
+
self.final_attention_layer = MultiHeadAttentionSequence(n_head, d_model,
|
| 25 |
+
d_k, d_v, dropout=dropout)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
self.final_ffn = FFN(d_model, d_inner, dropout=dropout)
|
| 29 |
+
self.output_projection_prot = nn.Linear(d_model, 1)
|
| 30 |
+
self.learning_rate = learning_rate
|
| 31 |
+
self.max_epochs = max_epochs
|
| 32 |
+
self.kl_weight = kl_weight
|
| 33 |
+
|
| 34 |
+
self.classification_threshold = nn.Parameter(torch.tensor(0.5))
|
| 35 |
+
self.historical_memory = 0.9
|
| 36 |
+
self.class_weights = torch.tensor([3.000471363174231, 0.5999811490272925])
|
| 37 |
+
|
| 38 |
+
def forward(self, binder_tokens, target_tokens):
|
| 39 |
+
peptide_sequence = self.esm_model(**binder_tokens).last_hidden_state
|
| 40 |
+
protein_sequence = self.esm_model(**target_tokens).last_hidden_state
|
| 41 |
+
|
| 42 |
+
prot_enc, sequence_enc, sequence_attention_list, prot_attention_list, \
|
| 43 |
+
seq_prot_attention_list, seq_prot_attention_list = self.repeated_module(peptide_sequence,
|
| 44 |
+
protein_sequence)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
prot_enc, final_prot_seq_attention = self.final_attention_layer(prot_enc, sequence_enc, sequence_enc)
|
| 48 |
+
|
| 49 |
+
prot_enc = self.final_ffn(prot_enc)
|
| 50 |
+
|
| 51 |
+
prot_enc = self.output_projection_prot(prot_enc)
|
| 52 |
+
|
| 53 |
+
return prot_enc, peptide_sequence
|
| 54 |
+
|
| 55 |
+
def get_probs(self, xt, target_sequence, original_binder_emb):
|
| 56 |
+
'''
|
| 57 |
+
Inputs:
|
| 58 |
+
- xt: Shape (bsz*seq_len*vocab_size, seq_len)
|
| 59 |
+
- target_sequence: Shape (bsz*seq_len*vocab_size, tgt_len)
|
| 60 |
+
'''
|
| 61 |
+
binder_attention_mask = torch.ones_like(xt)
|
| 62 |
+
target_attention_mask = torch.ones_like(target_sequence)
|
| 63 |
+
|
| 64 |
+
binder_attention_mask[:, 0] = binder_attention_mask[:, -1] = 0
|
| 65 |
+
target_attention_mask[:, 0] = target_attention_mask[:, -1] = 0
|
| 66 |
+
|
| 67 |
+
binder_tokens = {'input_ids': xt, 'attention_mask': binder_attention_mask.to(xt.device)}
|
| 68 |
+
target_tokens = {'input_ids': target_sequence, 'attention_mask': target_attention_mask.to(target_sequence.device)}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# start = time.time()
|
| 72 |
+
logits, peptide_embedding = self.forward(binder_tokens, target_tokens)
|
| 73 |
+
peptide_embedding_avg = torch.mean(peptide_embedding, dim=1)
|
| 74 |
+
|
| 75 |
+
similarity_scores = F.cosine_similarity(
|
| 76 |
+
peptide_embedding_avg, # Shape: (396, 1280)
|
| 77 |
+
original_binder_emb, # Shape: (1, 1280)
|
| 78 |
+
dim=1 # Compute similarity along the embedding dimension
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
logits = logits.squeeze(-1)
|
| 84 |
+
|
| 85 |
+
# print(f"Time: {time.time() - start} seconds")
|
| 86 |
+
|
| 87 |
+
logits[:, 0] = logits[:, -1] = -100 # float('-inf')
|
| 88 |
+
log_probs = F.softmax(logits, dim=-1)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
return log_probs, similarity_scores # shape (bsz*seq_len*vocab_size, tgt_len)
|