-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_seq.py
More file actions
328 lines (288 loc) · 12.3 KB
/
model_seq.py
File metadata and controls
328 lines (288 loc) · 12.3 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import numpy as np
import torch
import torch.nn.functional as F
from torch.nn import BatchNorm1d, Dropout, TransformerEncoder, TransformerEncoderLayer, LSTM
from torch_geometric.nn import global_mean_pool
from rnaglib.utils.misc import tonumpy
class SequenceModel(torch.nn.Module):
@classmethod
def from_task(cls,
task,
num_node_features=None,
num_classes=None,
graph_level=None,
multi_label=None,
**model_args):
if num_node_features is None:
num_node_features = task.metadata["num_node_features"]
if num_classes is None:
num_classes = task.metadata["num_classes"]
if graph_level is None:
graph_level = task.metadata["graph_level"]
if multi_label is None:
multi_label = task.metadata["multi_label"]
activation = 'softmax' if num_classes > 2 else 'sigmoid'
return cls(
num_node_features=num_node_features,
num_classes=num_classes,
graph_level=graph_level,
multi_label=multi_label,
final_activation=activation,
**model_args
)
def __init__(
self,
num_node_features,
num_classes,
graph_level=False,
num_layers=2,
hidden_channels=128,
dropout_rate=0.5,
multi_label=False,
final_activation="sigmoid",
num_heads=8,
use_bilstm=False, # New parameter to toggle BiLSTM
device=None
):
super().__init__()
self.num_node_features = num_node_features
self.num_classes = num_classes
self.graph_level = graph_level
self.num_layers = num_layers
self.hidden_channels = hidden_channels
self.dropout_rate = dropout_rate
self.multi_label = multi_label
self.num_heads = num_heads
self.use_bilstm = use_bilstm # Store BiLSTM flag
# Input embedding layer
self.input_embedding = torch.nn.Linear(num_node_features, hidden_channels)
# Positional encoding
self.pos_encoder = PositionalEncoding(hidden_channels, dropout_rate)
if self.use_bilstm:
# Bidirectional LSTM encoder
self.lstm = LSTM(
input_size=hidden_channels,
hidden_size=hidden_channels // 2, # Divide by 2 to account for bidirectional output
num_layers=num_layers,
batch_first=False, # Input: [seq_len, batch_size, hidden_channels]
bidirectional=True,
dropout=dropout_rate if num_layers > 1 else 0
)
else:
# Transformer encoder
encoder_layer = TransformerEncoderLayer(
d_model=hidden_channels,
nhead=num_heads,
dim_feedforward=hidden_channels * 4,
dropout=dropout_rate,
activation="relu"
)
self.transformer_encoder = TransformerEncoder(encoder_layer, num_layers=num_layers)
# Batch normalization and dropout
self.bn = BatchNorm1d(hidden_channels)
self.dropout = Dropout(dropout_rate)
# Final activation
if final_activation == "sigmoid":
self.final_activation = torch.nn.Sigmoid()
elif final_activation == "softmax":
self.final_activation = torch.nn.Softmax(dim=1)
else:
self.final_activation = torch.nn.Identity()
# Output layer
if self.multi_label:
self.final_linear = torch.nn.Linear(hidden_channels, num_classes)
self.criterion = torch.nn.BCEWithLogitsLoss()
self.final_activation = torch.nn.Identity()
elif num_classes == 2:
self.final_linear = torch.nn.Linear(hidden_channels, 1)
self.criterion = torch.nn.BCEWithLogitsLoss()
self.final_activation = torch.nn.Identity()
else:
self.final_linear = torch.nn.Linear(hidden_channels, num_classes)
self.criterion = torch.nn.CrossEntropyLoss()
if final_activation == "sigmoid":
self.final_activation = torch.nn.Sigmoid()
elif final_activation == "softmax":
self.final_activation = torch.nn.Softmax(dim=1)
else:
self.final_activation = torch.nn.Identity()
self.optimizer = None
if device is None:
if torch.cuda.is_available():
self.device = "cuda"
elif torch.backends.mps.is_available():
self.device = "mps"
else:
self.device = "cpu"
else:
self.device = device
self.configure_training()
def forward(self, data):
x, batch, chain_index = data.x, data.batch, data.chain_index
# Embed residue types
x = self.input_embedding(x) # [num_nodes, hidden_channels]
# Reshape for sequence model: [seq_len, batch_size, hidden_channels]
seq_lengths = torch.bincount(batch)
max_seq_len = seq_lengths.max().item()
batch_size = seq_lengths.size(0)
# Pad sequences to max length
x_padded = torch.zeros(max_seq_len, batch_size, self.hidden_channels, device=x.device)
mask = torch.ones(max_seq_len, batch_size, device=x.device, dtype=torch.bool)
chain_index_padded = torch.zeros(max_seq_len, batch_size, dtype=torch.long, device=x.device)
node_idx = 0
for b in range(batch_size):
seq_len = seq_lengths[b]
x_padded[:seq_len, b] = x[node_idx:node_idx + seq_len]
chain_index_padded[:seq_len, b] = chain_index[node_idx:node_idx + seq_len]
mask[:seq_len, b] = False # False for valid positions
node_idx += seq_len
# Apply positional encoding
x_padded = self.pos_encoder(x_padded, chain_index_padded)
if self.use_bilstm:
# BiLSTM forward pass
# Pack padded sequence for LSTM efficiency
packed_input = torch.nn.utils.rnn.pack_padded_sequence(
x_padded, seq_lengths.cpu(), enforce_sorted=False
)
packed_output, (hn, cn) = self.lstm(packed_input)
x_transformed, _ = torch.nn.utils.rnn.pad_packed_sequence(
packed_output, total_length=max_seq_len
) # [seq_len, batch_size, hidden_channels]
else:
# Transformer forward pass
x_transformed = self.transformer_encoder(x_padded, mask=None)
# Reshape back to [num_nodes, hidden_channels]
x_out = []
for b in range(batch_size):
seq_len = seq_lengths[b]
x_out.append(x_transformed[:seq_len, b])
x = torch.cat(x_out, dim=0)
# Apply batch norm and dropout
x = self.bn(x)
x = F.relu(x)
x = self.dropout(x)
# Pool for graph-level tasks
if self.graph_level:
x = global_mean_pool(x, batch)
# Final linear layer and activation
x = self.final_linear(x)
x = self.final_activation(x)
return x
def configure_training(self, learning_rate=0.001):
self.to(self.device)
self.criterion = self.criterion.to(self.device)
self.optimizer = torch.optim.Adam(self.parameters(), lr=learning_rate)
def compute_loss(self, out, target):
if self.multi_label:
target = target.float()
elif self.num_classes == 2:
out = out.flatten()
loss = self.criterion(out, target)
return loss
def train_model(self, task, epochs=500):
if self.optimizer is None:
self.configure_training()
if self.num_classes == 2:
neg_count = float(task.metadata["class_distribution"]["0"])
pos_count = float(task.metadata["class_distribution"]["1"])
pos_weight = torch.tensor(np.sqrt(neg_count / pos_count)).to(self.device)
self.criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight)
for epoch in range(epochs):
self.train()
epoch_loss = 0
num_batches = 0
for batch in task.train_dataloader:
graph = batch["sequence"].to(self.device)
self.optimizer.zero_grad()
out = self(graph)
loss = self.compute_loss(out, graph.y)
loss.backward()
self.optimizer.step()
epoch_loss += loss.item()
num_batches += 1
if epoch % 10 == 0:
val_metrics = self.evaluate(task, split="val")
print(
f"Epoch {epoch}: train_loss = {epoch_loss / num_batches:.4f}, val_loss = {val_metrics['loss']:.4f}",
)
def inference(self, loader) -> tuple:
self.eval()
all_probs = []
all_preds = []
all_labels = []
total_loss = 0
with torch.no_grad():
for batch in loader:
graph = batch["sequence"]
graph = graph.to(self.device)
out = self(graph)
labels = graph.y
loss = self.compute_loss(out, labels)
total_loss += loss.item()
preds = (out > 0).float() if (self.multi_label or self.num_classes == 2) else out.argmax(dim=1)
probs = out
probs = tonumpy(probs)
preds = tonumpy(preds)
labels = tonumpy(labels)
if not self.graph_level:
cumulative_sizes = tuple(tonumpy(graph.ptr))
probs = [
probs[start:end]
for start, end in zip(cumulative_sizes[:-1], cumulative_sizes[1:], strict=False)
]
preds = [
preds[start:end]
for start, end in zip(cumulative_sizes[:-1], cumulative_sizes[1:], strict=False)
]
labels = [
labels[start:end]
for start, end in zip(cumulative_sizes[:-1], cumulative_sizes[1:], strict=False)
]
all_probs.extend(probs)
all_preds.extend(preds)
all_labels.extend(labels)
if self.graph_level:
all_probs = np.stack(all_probs)
all_preds = np.stack(all_preds)
all_labels = np.stack(all_labels)
mean_loss = total_loss / len(loader)
return mean_loss, all_preds, all_probs, all_labels
def get_dataloader(self, task, split="test"):
if split == "test":
dataloader = task.test_dataloader
elif split == "val":
dataloader = task.val_dataloader
else:
dataloader = task.train_dataloader
return dataloader
def evaluate(self, task, split="test"):
dataloader = self.get_dataloader(task=task, split=split)
mean_loss, all_preds, all_probs, all_labels = self.inference(loader=dataloader)
metrics = task.compute_metrics(all_preds, all_probs, all_labels)
metrics["loss"] = mean_loss
return metrics
class PositionalEncoding(torch.nn.Module):
def __init__(self, d_model, dropout=0.1, max_len=5000):
super().__init__()
self.dropout = torch.nn.Dropout(p=dropout)
self.d_model = d_model
self.max_len = max_len
position = torch.arange(max_len).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2) * (-np.log(10000.0) / d_model))
pe = torch.zeros(max_len, d_model)
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
self.register_buffer('pe', pe)
def forward(self, x, chain_index=None):
seq_len, batch_size, _ = x.shape
if chain_index is not None:
positions = torch.zeros(seq_len, batch_size, device=x.device, dtype=torch.long)
for b in range(batch_size):
chain_ids = chain_index[:, b]
for chain_id in chain_ids.unique():
mask = (chain_ids == chain_id) & (chain_ids != -1)
positions[mask, b] = torch.arange(mask.sum(), device=x.device)
else:
positions = torch.arange(seq_len, device=x.device).unsqueeze(1).expand(-1, batch_size)
x = x + self.pe[positions, :].to(x.device)
return self.dropout(x)