AI4deeperScience commited on
Commit
64fcdda
·
verified ·
1 Parent(s): 00f7a8e

Upload src/models.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/models.py +424 -0
src/models.py ADDED
@@ -0,0 +1,424 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Graph Neural Network models for retention time prediction.
3
+ Includes GCN, GIN, GAT, and ensemble models.
4
+ """
5
+
6
+ import torch
7
+ import torch.nn as nn
8
+ import torch.nn.functional as F
9
+ from torch_geometric.nn import (
10
+ GCNConv, GINConv, GATConv, GINEConv, GlobalAttention,
11
+ global_mean_pool, global_max_pool, global_add_pool
12
+ )
13
+ from torch_geometric.nn.norm import BatchNorm, GraphNorm, LayerNorm
14
+ from typing import Optional, List, Dict, Sequence, Any
15
+ import numpy as np
16
+
17
+
18
+ class GraphConvModel(nn.Module):
19
+ """Base Graph Convolutional Network model."""
20
+
21
+ def __init__(self, input_dim: int, hidden_dim: int = 128, output_dim: int = 1,
22
+ num_layers: int = 3, dropout: float = 0.2,
23
+ num_labs: int = 23, lab_embed_dim: int = 16):
24
+ super().__init__()
25
+
26
+ self.input_dim = input_dim
27
+ self.hidden_dim = hidden_dim
28
+ self.num_layers = num_layers
29
+ self.dropout = dropout
30
+
31
+ # Lab embedding
32
+ self.lab_embedding = nn.Embedding(num_labs, lab_embed_dim)
33
+
34
+ # Graph convolution layers
35
+ self.convs = nn.ModuleList()
36
+ self.batch_norms = nn.ModuleList()
37
+
38
+ # First layer
39
+ self.convs.append(GCNConv(input_dim, hidden_dim))
40
+ self.batch_norms.append(GraphNorm(hidden_dim))
41
+
42
+ # Hidden layers
43
+ for _ in range(num_layers - 1):
44
+ self.convs.append(GCNConv(hidden_dim, hidden_dim))
45
+ self.batch_norms.append(GraphNorm(hidden_dim))
46
+
47
+ # Global pooling
48
+ self.global_pool = global_mean_pool
49
+
50
+ # Final prediction layers
51
+ final_input_dim = hidden_dim + lab_embed_dim
52
+ self.predictor = nn.Sequential(
53
+ nn.Linear(final_input_dim, hidden_dim // 2),
54
+ nn.ReLU(),
55
+ nn.Dropout(dropout),
56
+ nn.Linear(hidden_dim // 2, hidden_dim // 4),
57
+ nn.ReLU(),
58
+ nn.Dropout(dropout),
59
+ nn.Linear(hidden_dim // 4, output_dim)
60
+ )
61
+
62
+ def forward(self, x, edge_index, batch, lab_feature, edge_attr=None):
63
+ # Graph convolutions
64
+ for conv, norm in zip(self.convs, self.batch_norms):
65
+ # GCN expects edge_weight, not edge_attr for individual edge features
66
+ edge_weight = None
67
+ if edge_attr is not None and edge_attr.size(1) == 1:
68
+ edge_weight = edge_attr.squeeze(-1)
69
+
70
+ x = conv(x, edge_index, edge_weight=edge_weight)
71
+ x = norm(x, batch)
72
+ x = F.relu(x)
73
+ x = F.dropout(x, p=self.dropout, training=self.training)
74
+
75
+ # Global pooling
76
+ graph_repr = self.global_pool(x, batch)
77
+
78
+ # Lab embedding - handle different input shapes
79
+ if lab_feature.dim() == 1:
80
+ lab_embed = self.lab_embedding(lab_feature)
81
+ else:
82
+ lab_embed = self.lab_embedding(lab_feature.squeeze(-1))
83
+
84
+ # Concatenate graph and lab features
85
+ combined = torch.cat([graph_repr, lab_embed], dim=1)
86
+
87
+ # Final prediction
88
+ output = self.predictor(combined)
89
+ return output.squeeze()
90
+
91
+
92
+ class GATModel(nn.Module):
93
+ """Graph Attention Network (GAT) model."""
94
+
95
+ def __init__(self, input_dim: int, hidden_dim: int = 128, output_dim: int = 1,
96
+ num_layers: int = 3, dropout: float = 0.2, num_heads: int = 4,
97
+ num_labs: int = 23, lab_embed_dim: int = 16):
98
+ super().__init__()
99
+
100
+ self.input_dim = input_dim
101
+ self.hidden_dim = hidden_dim
102
+ self.num_layers = num_layers
103
+ self.dropout = dropout
104
+ self.num_heads = num_heads
105
+
106
+ # Lab embedding
107
+ self.lab_embedding = nn.Embedding(num_labs, lab_embed_dim)
108
+
109
+ # GAT layers
110
+ self.convs = nn.ModuleList()
111
+ self.batch_norms = nn.ModuleList()
112
+
113
+ # Calculate dimensions properly for multi-head attention
114
+ head_dim = hidden_dim // num_heads
115
+
116
+ # First layer: input -> hidden_dim (via multi-head)
117
+ self.convs.append(GATConv(input_dim, head_dim, heads=num_heads, dropout=dropout, concat=True))
118
+ self.batch_norms.append(GraphNorm(hidden_dim)) # head_dim * num_heads = hidden_dim
119
+
120
+ # Hidden layers: hidden_dim -> hidden_dim (via multi-head)
121
+ for _ in range(num_layers - 2):
122
+ self.convs.append(GATConv(hidden_dim, head_dim, heads=num_heads, dropout=dropout, concat=True))
123
+ self.batch_norms.append(GraphNorm(hidden_dim))
124
+
125
+ # Last layer: hidden_dim -> hidden_dim (single head)
126
+ if num_layers > 1:
127
+ self.convs.append(GATConv(hidden_dim, hidden_dim, heads=1, dropout=dropout, concat=False))
128
+ self.batch_norms.append(GraphNorm(hidden_dim))
129
+
130
+ # Global pooling
131
+ self.global_pool = global_mean_pool
132
+
133
+ # Final prediction layers
134
+ final_input_dim = hidden_dim + lab_embed_dim
135
+ self.predictor = nn.Sequential(
136
+ nn.Linear(final_input_dim, hidden_dim // 2),
137
+ nn.ReLU(),
138
+ nn.Dropout(dropout),
139
+ nn.Linear(hidden_dim // 2, hidden_dim // 4),
140
+ nn.ReLU(),
141
+ nn.Dropout(dropout),
142
+ nn.Linear(hidden_dim // 4, output_dim)
143
+ )
144
+
145
+ def forward(self, x, edge_index, batch, lab_feature, edge_attr=None):
146
+ # GAT convolutions
147
+ for i, (conv, norm) in enumerate(zip(self.convs, self.batch_norms)):
148
+ x = conv(x, edge_index)
149
+ x = norm(x, batch)
150
+ if i < len(self.convs) - 1: # No ReLU on last layer
151
+ x = F.relu(x)
152
+ x = F.dropout(x, p=self.dropout, training=self.training)
153
+
154
+ # Global pooling
155
+ graph_repr = self.global_pool(x, batch)
156
+
157
+ # Lab embedding - handle different input shapes
158
+ if lab_feature.dim() == 1:
159
+ lab_embed = self.lab_embedding(lab_feature)
160
+ else:
161
+ lab_embed = self.lab_embedding(lab_feature.squeeze(-1))
162
+
163
+ # Concatenate graph and lab features
164
+ combined = torch.cat([graph_repr, lab_embed], dim=1)
165
+
166
+ # Final prediction
167
+ output = self.predictor(combined)
168
+ return output.squeeze()
169
+
170
+
171
+ class MoleculeMPNNModel(nn.Module):
172
+ """Edge-aware message passing network tailored for molecular graphs."""
173
+
174
+ def __init__(
175
+ self,
176
+ input_dim: int,
177
+ hidden_dim: int = 256,
178
+ output_dim: int = 1,
179
+ num_layers: int = 4,
180
+ dropout: float = 0.2,
181
+ edge_dim: int = 4,
182
+ num_labs: int = 23,
183
+ lab_embed_dim: int = 16,
184
+ use_batch_norm: bool = True,
185
+ ):
186
+ super().__init__()
187
+
188
+ self.hidden_dim = hidden_dim
189
+ self.num_layers = num_layers
190
+ self.dropout = dropout
191
+ self.use_batch_norm = use_batch_norm
192
+
193
+ self.input_proj = nn.Linear(input_dim, hidden_dim)
194
+ self.edge_encoder = nn.Linear(edge_dim, hidden_dim) if edge_dim > 0 else None
195
+ self.lab_embedding = nn.Embedding(num_labs, lab_embed_dim)
196
+
197
+ self.convs = nn.ModuleList()
198
+ self.norms = nn.ModuleList()
199
+
200
+ for _ in range(num_layers):
201
+ mlp = nn.Sequential(
202
+ nn.Linear(hidden_dim, hidden_dim),
203
+ nn.ReLU(),
204
+ nn.Linear(hidden_dim, hidden_dim),
205
+ )
206
+ self.convs.append(GINEConv(mlp, train_eps=True))
207
+ if use_batch_norm:
208
+ self.norms.append(BatchNorm(hidden_dim))
209
+ else:
210
+ self.norms.append(GraphNorm(hidden_dim))
211
+
212
+ pooled_dim = hidden_dim * 2 # mean + max pooling
213
+ final_input_dim = pooled_dim + lab_embed_dim
214
+
215
+ self.predictor = nn.Sequential(
216
+ nn.Linear(final_input_dim, hidden_dim),
217
+ nn.ReLU(),
218
+ nn.Dropout(dropout),
219
+ nn.Linear(hidden_dim, hidden_dim // 2),
220
+ nn.ReLU(),
221
+ nn.Dropout(dropout),
222
+ nn.Linear(hidden_dim // 2, output_dim),
223
+ )
224
+
225
+ def forward(self, x, edge_index, batch, lab_feature, edge_attr=None):
226
+ x = self.input_proj(x)
227
+ encoded_edge_attr = edge_attr
228
+ if edge_attr is not None and self.edge_encoder is not None:
229
+ encoded_edge_attr = self.edge_encoder(edge_attr)
230
+
231
+ for conv, norm in zip(self.convs, self.norms):
232
+ x = conv(x, edge_index, encoded_edge_attr)
233
+ if isinstance(norm, BatchNorm):
234
+ x = norm(x)
235
+ else:
236
+ x = norm(x, batch)
237
+ x = F.relu(x)
238
+ x = F.dropout(x, p=self.dropout, training=self.training)
239
+
240
+ mean_pool = global_mean_pool(x, batch)
241
+ max_pool = global_max_pool(x, batch)
242
+ graph_repr = torch.cat([mean_pool, max_pool], dim=1)
243
+
244
+ if lab_feature.dim() == 1:
245
+ lab_embed = self.lab_embedding(lab_feature)
246
+ else:
247
+ lab_embed = self.lab_embedding(lab_feature.squeeze(-1))
248
+
249
+ combined = torch.cat([graph_repr, lab_embed], dim=1)
250
+ output = self.predictor(combined)
251
+ return output.squeeze()
252
+
253
+
254
+ # Backwards-compatible alias for previous naming
255
+ GINEModel = MoleculeMPNNModel
256
+
257
+
258
+ class HybridModel(nn.Module):
259
+ """Hybrid model combining graph features with molecular descriptors."""
260
+
261
+ def __init__(
262
+ self,
263
+ *,
264
+ graph_model_class,
265
+ descriptor_dim: int,
266
+ graph_model_kwargs: Optional[Dict[str, Any]] = None,
267
+ graph_feature_dim: Optional[int] = None,
268
+ descriptor_hidden_dims: Optional[Sequence[int]] = None,
269
+ final_hidden_dims: Optional[Sequence[int]] = None,
270
+ dropout: float = 0.2,
271
+ use_batch_norm: bool = True,
272
+ output_dim: int = 1,
273
+ ) -> None:
274
+ super().__init__()
275
+
276
+ graph_model_kwargs = dict(graph_model_kwargs or {})
277
+
278
+ if graph_feature_dim is None:
279
+ graph_feature_dim = graph_model_kwargs.get("hidden_dim")
280
+ if graph_feature_dim is None:
281
+ graph_feature_dim = 128
282
+
283
+ graph_model_kwargs.setdefault("hidden_dim", graph_feature_dim)
284
+ graph_model_kwargs.setdefault("output_dim", graph_feature_dim)
285
+
286
+ self.graph_model = graph_model_class(**graph_model_kwargs)
287
+ self.graph_feature_dim = graph_feature_dim
288
+ self.dropout = dropout
289
+ self.use_batch_norm = use_batch_norm
290
+
291
+ if descriptor_hidden_dims is None or len(descriptor_hidden_dims) == 0:
292
+ self.descriptor_net = nn.Identity()
293
+ self.descriptor_output_dim = descriptor_dim
294
+ else:
295
+ descriptor_layers: List[nn.Module] = []
296
+ in_dim = descriptor_dim
297
+ for hidden_dim in descriptor_hidden_dims:
298
+ descriptor_layers.append(nn.Linear(in_dim, hidden_dim))
299
+ if use_batch_norm:
300
+ descriptor_layers.append(nn.BatchNorm1d(hidden_dim))
301
+ descriptor_layers.append(nn.ReLU())
302
+ if dropout > 0:
303
+ descriptor_layers.append(nn.Dropout(dropout))
304
+ in_dim = hidden_dim
305
+ self.descriptor_net = nn.Sequential(*descriptor_layers)
306
+ self.descriptor_output_dim = in_dim
307
+
308
+ if final_hidden_dims is None or len(final_hidden_dims) == 0:
309
+ final_hidden_dims = [max(graph_feature_dim // 2, 1)]
310
+
311
+ final_layers: List[nn.Module] = []
312
+ in_dim = self.graph_feature_dim + self.descriptor_output_dim
313
+ for hidden_dim in final_hidden_dims:
314
+ final_layers.append(nn.Linear(in_dim, hidden_dim))
315
+ if use_batch_norm:
316
+ final_layers.append(nn.BatchNorm1d(hidden_dim))
317
+ final_layers.append(nn.ReLU())
318
+ if dropout > 0:
319
+ final_layers.append(nn.Dropout(dropout))
320
+ in_dim = hidden_dim
321
+ final_layers.append(nn.Linear(in_dim, output_dim))
322
+
323
+ self.final_predictor = nn.Sequential(*final_layers)
324
+
325
+ def forward(self, x, edge_index, batch, lab_feature, descriptors, edge_attr=None):
326
+ graph_features = self.graph_model(
327
+ x,
328
+ edge_index,
329
+ batch,
330
+ lab_feature,
331
+ edge_attr,
332
+ )
333
+ if graph_features.dim() == 1:
334
+ graph_features = graph_features.unsqueeze(0)
335
+
336
+ if descriptors.dim() == 1:
337
+ descriptors = descriptors.unsqueeze(0)
338
+
339
+ desc_features = self.descriptor_net(descriptors)
340
+ if isinstance(self.descriptor_net, nn.Identity):
341
+ desc_features = descriptors
342
+
343
+ combined = torch.cat([graph_features, desc_features], dim=1)
344
+ output = self.final_predictor(combined)
345
+
346
+ return output.squeeze(-1)
347
+
348
+
349
+ def create_model(model_type: str, input_dim: int, num_labs: int = 23, **kwargs):
350
+ """Factory function to create models."""
351
+
352
+ models = {
353
+ 'gcn': GraphConvModel,
354
+ 'gin': GINEModel,
355
+ 'gat': GATModel,
356
+ 'mpnn': MoleculeMPNNModel,
357
+ }
358
+
359
+ if model_type not in models:
360
+ raise ValueError(f"Unknown model type: {model_type}")
361
+
362
+ return models[model_type](input_dim=input_dim, num_labs=num_labs, **kwargs)
363
+
364
+
365
+ class EarlyStopping:
366
+ """Early stopping utility."""
367
+
368
+ def __init__(self, patience: int = 10, min_delta: float = 0.0, restore_best_weights: bool = True):
369
+ self.patience = patience
370
+ self.min_delta = min_delta
371
+ self.restore_best_weights = restore_best_weights
372
+ self.best_loss = None
373
+ self.counter = 0
374
+ self.best_weights = None
375
+
376
+ def __call__(self, val_loss: float, model: nn.Module) -> bool:
377
+ if self.best_loss is None:
378
+ self.best_loss = val_loss
379
+ self.save_checkpoint(model)
380
+ elif val_loss < self.best_loss - self.min_delta:
381
+ self.best_loss = val_loss
382
+ self.counter = 0
383
+ self.save_checkpoint(model)
384
+ else:
385
+ self.counter += 1
386
+
387
+ if self.counter >= self.patience:
388
+ if self.restore_best_weights and self.best_weights is not None:
389
+ model.load_state_dict(self.best_weights)
390
+ return True
391
+ return False
392
+
393
+ def save_checkpoint(self, model: nn.Module):
394
+ """Save model weights."""
395
+ self.best_weights = model.state_dict().copy()
396
+
397
+
398
+ if __name__ == "__main__":
399
+ # Test model creation
400
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
401
+
402
+ # Create sample data
403
+ batch_size = 32
404
+ input_dim = 7 # Number of atom features
405
+ num_nodes = 20
406
+ num_edges = 40
407
+
408
+ x = torch.randn(num_nodes, input_dim)
409
+ edge_index = torch.randint(0, num_nodes, (2, num_edges))
410
+ batch = torch.zeros(num_nodes, dtype=torch.long)
411
+ lab_feature = torch.randint(0, 23, (1,))
412
+
413
+ # Test different models
414
+ models = ['gcn', 'gin', 'gat']
415
+
416
+ for model_type in models:
417
+ print(f"\nTesting {model_type.upper()} model:")
418
+ model = create_model(model_type, input_dim=input_dim, hidden_dim=64)
419
+ model.eval()
420
+
421
+ with torch.no_grad():
422
+ output = model(x, edge_index, batch, lab_feature)
423
+ print(f"Output shape: {output.shape}")
424
+ print(f"Output value: {output.item():.4f}")