-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmodel.py
More file actions
148 lines (122 loc) · 6.23 KB
/
model.py
File metadata and controls
148 lines (122 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import math
import torch
from torch import nn
import torch.nn.functional as F
from transformers import EsmModel
from config import config
from huggingface_hub import PyTorchModelHubMixin
class ESMwrap(nn.Module, PyTorchModelHubMixin):
def __init__(self, esm2_select, model_select):
super().__init__()
# Load the ESM2 model
self.esm2 = EsmModel.from_pretrained(config[esm2_select]['model_id'])
self.freeze_esm = config[model_select]['freeze_esm']
# Freeze self.esm2 parameters if freeze_esm is True
if self.freeze_esm:
for param in self.esm2.parameters():
param.requires_grad = False
self.esm2.eval() # Set to evaluation mode
# Randomize self.esm2 parameters if randomize_esm is True
if config[model_select]['randomize_esm']:
self.randomize_model(self.esm2)
# dimensions of input and output
embed_dim = config[esm2_select]['embed_dim']
res_out_dim = config[esm2_select]['res_out_dim']
atten_dim = config[esm2_select]['atten_dim']
pair_out_dim = config[esm2_select]['pair_out_dim']
# Residue-level prediction layer
self.res_pred_nn = nn.Sequential(
nn.Linear(embed_dim, embed_dim),
nn.GELU(),
nn.LayerNorm(embed_dim),
nn.Dropout(config['training']['dropout']), # Apply dropout after LayerNorm
nn.Linear(embed_dim, res_out_dim)
)
# transform res embedding for Pairwise prediction
self.res_transform_nn = nn.Sequential(
nn.Linear(embed_dim, embed_dim),
nn.GELU(),
nn.LayerNorm(embed_dim),
nn.Dropout(config['training']['dropout']), # Apply dropout after LayerNorm
nn.Linear(embed_dim, embed_dim*2)
)
# Pairwise prediction layer
self.pair_middle_linear = nn.Linear(embed_dim*2, atten_dim)
self.pair_pred_linear = nn.Linear(atten_dim + atten_dim, pair_out_dim)
# Activation functions
self.gelu = nn.GELU()
self.softplus = nn.Softplus(beta=1.0, threshold=2.0)
self.sigmoid = nn.Sigmoid()
self.softmax = nn.Softmax(dim=-1)
# Feature indices from config
self.res_feature_idx = config['training']['res_feature_idx']
self.pair_feature_idx = config['training']['pair_feature_idx']
# Initialize biases to zero
self._init_bias_zero()
def randomize_model(self, model):
""" Randomize the parameters of the given model. """
for module_ in model.named_modules():
if isinstance(module_[1], (torch.nn.Linear, torch.nn.Embedding)):
if hasattr(module_[1], 'bias') and module_[1].bias is not None:
module_[1].bias.data.zero_()
if hasattr(module_[1], 'weight'):
if 'query' in module_[0] or 'key' in module_[0] or 'value' in module_[0]:
module_[1].weight = nn.init.xavier_uniform_(module_[1].weight, gain=1 / math.sqrt(2))
else:
module_[1].weight = nn.init.xavier_uniform_(module_[1].weight)
elif isinstance(module_[1], nn.LayerNorm):
if hasattr(module_[1], 'bias'):
module_[1].bias.data.zero_()
if hasattr(module_[1], 'weight'):
module_[1].weight.data.fill_(1.0)
elif isinstance(module_[1], nn.Dropout):
module_[1].p = config['training']['dropout']
def _init_bias_zero(self):
""" Set all biases in the model (excluding esm2) to zero. """
for name, module in self.named_modules():
if "esm2" not in name and isinstance(module, nn.Linear) and module.bias is not None:
torch.nn.init.zeros_(module.bias)
def forward(self, inputs, return_res_emb=False, return_attention_map=False, return_res_pred=True, return_pair_pred=True):
output = {}
# ESM forward pass, Ensure no gradients are stored for frozen ESM2
if self.freeze_esm:
with torch.no_grad():
esm_output = self.esm2(**inputs, output_attentions=True)
else:
esm_output = self.esm2(**inputs, output_attentions=True)
res_emb = esm_output['last_hidden_state']
pair_atten = torch.cat(esm_output['attentions'], dim=1).permute(0, 2, 3, 1)
if return_res_emb:
output['res_emb'] = res_emb
if return_attention_map:
output['attention_map'] = pair_atten
# Residue-level prediction
if return_res_pred:
res_pred = self.res_pred_nn(res_emb)
for feature in self.res_feature_idx:
if feature == 'rmsf_nor':
# Normalized RMSF (max = 1)
output[feature] = self.sigmoid(res_pred[:, :, self.res_feature_idx[feature]])
elif feature in ['ss', 'chi', 'phi', 'psi']:
# Secondary structure, chi, phi, psi sum up to 1
output[feature] = self.softmax(res_pred[:, :, self.res_feature_idx[feature]])
else:
# All other features are non-negative
output[feature] = self.softplus(res_pred[:, :, self.res_feature_idx[feature]])
# Pairwise transformation
s = self.res_transform_nn(res_emb)
q, k = s.chunk(2, dim=-1)
prod = q[:, None, :, :] * k[:, :, None, :]
diff = q[:, None, :, :] - k[:, :, None, :]
pair_middle = self.gelu(self.pair_middle_linear(torch.cat([prod, diff], dim=-1)))
# Pairwise prediction
if return_pair_pred:
pair_pred = self.pair_pred_linear(torch.cat([pair_middle, pair_atten], dim=-1))
for feature in self.pair_feature_idx:
if feature in ['corr', 'nma_pair1', 'nma_pair2', 'nma_pair3']:
# Co-movement and NMA co-movement correlations: range [-1, 1]
output[feature] = self.sigmoid(pair_pred[:, :, :, self.pair_feature_idx[feature]]) * 2 - 1.0
else:
# All interaction features are non-negative
output[feature] = self.softplus(pair_pred[:, :, :, self.pair_feature_idx[feature]])
return output