Spaces:
Paused
Paused
File size: 9,954 Bytes
2fd4f2f | 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 | """
Modular Graph Neural Network (GNN) Architectures for Molecular Property Prediction.
Includes GCN, GAT, GIN, and MPNN (NNConv) with dual global pooling and residual connections.
"""
from typing import Optional, List, Tuple
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import (
GCNConv,
GATConv,
GINConv,
NNConv,
global_mean_pool,
global_add_pool
)
class BaseGNN(nn.Module):
"""
Base class for molecular GNNs.
Implements multi-layer messaging, batch norm, residual connections,
dual pooling ([mean_pool || add_pool]), and task prediction head.
"""
def __init__(
self,
in_channels: int,
hidden_dim: int = 64,
num_layers: int = 3,
num_tasks: int = 1,
dropout: float = 0.2,
residual: bool = True
):
super().__init__()
self.in_channels = in_channels
self.hidden_dim = hidden_dim
self.num_layers = num_layers
self.num_tasks = num_tasks
self.dropout_rate = dropout
self.residual = residual
self.convs = nn.ModuleList()
self.bns = nn.ModuleList()
# Dual pooling produces 2 * hidden_dim features
self.head = nn.Sequential(
nn.Linear(hidden_dim * 2, hidden_dim),
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, num_tasks)
)
def forward(
self,
x: torch.Tensor,
edge_index: torch.Tensor,
edge_attr: Optional[torch.Tensor] = None,
batch: Optional[torch.Tensor] = None
) -> torch.Tensor:
"""Forward pass through GNN backbone, dual pooling, and task head."""
h = self.get_node_representations(x, edge_index, edge_attr)
if batch is None:
batch = torch.zeros(h.size(0), dtype=torch.long, device=h.device)
# Dual pooling: concatenation of mean and sum pooling
mean_p = global_mean_pool(h, batch)
sum_p = global_add_pool(h, batch)
pooled = torch.cat([mean_p, sum_p], dim=1)
return self.head(pooled)
def get_embedding(
self,
x: torch.Tensor,
edge_index: torch.Tensor,
edge_attr: Optional[torch.Tensor] = None,
batch: Optional[torch.Tensor] = None
) -> torch.Tensor:
"""Extract graph-level latent representations (used for t-SNE / analysis)."""
h = self.get_node_representations(x, edge_index, edge_attr)
if batch is None:
batch = torch.zeros(h.size(0), dtype=torch.long, device=h.device)
mean_p = global_mean_pool(h, batch)
sum_p = global_add_pool(h, batch)
return torch.cat([mean_p, sum_p], dim=1)
def get_node_representations(
self,
x: torch.Tensor,
edge_index: torch.Tensor,
edge_attr: Optional[torch.Tensor] = None
) -> torch.Tensor:
"""Subclasses implement convolution message passing over nodes."""
raise NotImplementedError
class GCN(BaseGNN):
"""Graph Convolutional Network (Kipf & Welling)."""
def __init__(
self,
in_channels: int,
hidden_dim: int = 64,
num_layers: int = 3,
num_tasks: int = 1,
dropout: float = 0.2,
residual: bool = True,
**kwargs
):
super().__init__(in_channels, hidden_dim, num_layers, num_tasks, dropout, residual)
for i in range(num_layers):
dim_in = in_channels if i == 0 else hidden_dim
self.convs.append(GCNConv(dim_in, hidden_dim))
self.bns.append(nn.BatchNorm1d(hidden_dim))
def get_node_representations(
self,
x: torch.Tensor,
edge_index: torch.Tensor,
edge_attr: Optional[torch.Tensor] = None
) -> torch.Tensor:
h = x
for i in range(self.num_layers):
h_new = self.convs[i](h, edge_index)
h_new = self.bns[i](h_new)
h_new = F.relu(h_new)
h_new = F.dropout(h_new, p=self.dropout_rate, training=self.training)
if self.residual and h.shape == h_new.shape:
h = h + h_new
else:
h = h_new
return h
class GAT(BaseGNN):
"""Graph Attention Network with Multi-Head Attention (Veličković et al.)."""
def __init__(
self,
in_channels: int,
hidden_dim: int = 64,
num_layers: int = 3,
num_tasks: int = 1,
dropout: float = 0.2,
residual: bool = True,
heads: int = 4,
**kwargs
):
super().__init__(in_channels, hidden_dim, num_layers, num_tasks, dropout, residual)
self.heads = heads
assert hidden_dim % heads == 0, f"hidden_dim ({hidden_dim}) must be divisible by heads ({heads})"
head_dim = hidden_dim // heads
for i in range(num_layers):
dim_in = in_channels if i == 0 else hidden_dim
self.convs.append(GATConv(dim_in, head_dim, heads=heads, concat=True, dropout=dropout))
self.bns.append(nn.BatchNorm1d(hidden_dim))
def get_node_representations(
self,
x: torch.Tensor,
edge_index: torch.Tensor,
edge_attr: Optional[torch.Tensor] = None
) -> torch.Tensor:
h = x
for i in range(self.num_layers):
h_new = self.convs[i](h, edge_index)
h_new = self.bns[i](h_new)
h_new = F.elu(h_new)
h_new = F.dropout(h_new, p=self.dropout_rate, training=self.training)
if self.residual and h.shape == h_new.shape:
h = h + h_new
else:
h = h_new
return h
class GIN(BaseGNN):
"""Graph Isomorphism Network with MLP aggregators (Xu et al.)."""
def __init__(
self,
in_channels: int,
hidden_dim: int = 64,
num_layers: int = 3,
num_tasks: int = 1,
dropout: float = 0.2,
residual: bool = True,
train_eps: bool = True,
**kwargs
):
super().__init__(in_channels, hidden_dim, num_layers, num_tasks, dropout, residual)
for i in range(num_layers):
dim_in = in_channels if i == 0 else hidden_dim
mlp = nn.Sequential(
nn.Linear(dim_in, hidden_dim),
nn.BatchNorm1d(hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim)
)
self.convs.append(GINConv(mlp, train_eps=train_eps))
self.bns.append(nn.BatchNorm1d(hidden_dim))
def get_node_representations(
self,
x: torch.Tensor,
edge_index: torch.Tensor,
edge_attr: Optional[torch.Tensor] = None
) -> torch.Tensor:
h = x
for i in range(self.num_layers):
h_new = self.convs[i](h, edge_index)
h_new = self.bns[i](h_new)
h_new = F.relu(h_new)
h_new = F.dropout(h_new, p=self.dropout_rate, training=self.training)
if self.residual and h.shape == h_new.shape:
h = h + h_new
else:
h = h_new
return h
class MPNN(BaseGNN):
"""
Message Passing Neural Network using Edge Attributes (Gilmer et al.).
Utilizes NNConv where message passing weights are conditioned on edge features.
"""
def __init__(
self,
in_channels: int,
edge_dim: int,
hidden_dim: int = 64,
num_layers: int = 3,
num_tasks: int = 1,
dropout: float = 0.2,
residual: bool = True,
**kwargs
):
super().__init__(in_channels, hidden_dim, num_layers, num_tasks, dropout, residual)
self.edge_dim = edge_dim
self.edge_nns = nn.ModuleList()
for i in range(num_layers):
dim_in = in_channels if i == 0 else hidden_dim
edge_nn = nn.Sequential(
nn.Linear(edge_dim, 32),
nn.ReLU(),
nn.Linear(32, dim_in * hidden_dim)
)
self.edge_nns.append(edge_nn)
self.convs.append(NNConv(dim_in, hidden_dim, edge_nn, aggr='mean'))
self.bns.append(nn.BatchNorm1d(hidden_dim))
def get_node_representations(
self,
x: torch.Tensor,
edge_index: torch.Tensor,
edge_attr: Optional[torch.Tensor] = None
) -> torch.Tensor:
# Fallback for empty edges or missing edge attributes
if edge_attr is None or edge_attr.size(0) == 0:
edge_attr = torch.zeros((edge_index.size(1), self.edge_dim), dtype=torch.float, device=x.device)
h = x
for i in range(self.num_layers):
h_new = self.convs[i](h, edge_index, edge_attr)
h_new = self.bns[i](h_new)
h_new = F.relu(h_new)
h_new = F.dropout(h_new, p=self.dropout_rate, training=self.training)
if self.residual and h.shape == h_new.shape:
h = h + h_new
else:
h = h_new
return h
def build_gnn_model(
model_name: str,
in_channels: int,
edge_dim: int = 10,
num_tasks: int = 1,
hidden_dim: int = 64,
num_layers: int = 3,
dropout: float = 0.2,
residual: bool = True,
**kwargs
) -> BaseGNN:
"""Factory function for creating GNN models."""
name = model_name.lower()
if name == 'gcn':
return GCN(in_channels, hidden_dim, num_layers, num_tasks, dropout, residual, **kwargs)
elif name == 'gat':
return GAT(in_channels, hidden_dim, num_layers, num_tasks, dropout, residual, **kwargs)
elif name == 'gin':
return GIN(in_channels, hidden_dim, num_layers, num_tasks, dropout, residual, **kwargs)
elif name == 'mpnn':
return MPNN(in_channels, edge_dim, hidden_dim, num_layers, num_tasks, dropout, residual, **kwargs)
else:
raise ValueError(f"Unknown GNN architecture '{model_name}'. Choose from: gcn, gat, gin, mpnn")
|