gnodevae 0.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,561 @@
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from torch_geometric.nn import (GCNConv,
5
+ ChebConv,
6
+ SAGEConv,
7
+ GraphConv,
8
+ TAGConv,
9
+ ARMAConv,
10
+ GATConv,
11
+ TransformerConv,
12
+ SGConv,
13
+ SSGConv)
14
+ from torch.distributions import Normal, kl_divergence
15
+ from typing import Dict, Optional, Tuple, Type, Union, Literal
16
+ from sklearn.neighbors import kneighbors_graph
17
+ import numpy as np
18
+ from .utils import GraphStructureDecoder
19
+
20
+ class BaseGraphNetwork(nn.Module):
21
+ """
22
+ Base class for graph neural networks with various convolution types.
23
+
24
+ Parameters
25
+ ----------
26
+ input_dim : int
27
+ Input feature dimension
28
+ hidden_dim : int
29
+ Hidden layer dimension
30
+ output_dim : int
31
+ Output feature dimension
32
+ conv_layer_type : str
33
+ Type of graph convolutional layer
34
+ hidden_layers : int
35
+ Number of hidden layers
36
+ dropout : float
37
+ Dropout rate
38
+ Cheb_k : int, optional
39
+ Order of Chebyshev polynomials for ChebConv
40
+ alpha : float, optional
41
+ Teleport probability for SSGConv
42
+ """
43
+
44
+ CONV_LAYERS: Dict[str, Type[nn.Module]] = {
45
+ 'GCN': GCNConv,
46
+ 'Cheb': ChebConv,
47
+ 'SAGE': SAGEConv,
48
+ 'Graph': GraphConv,
49
+ 'TAG': TAGConv,
50
+ 'ARMA': ARMAConv,
51
+ 'GAT': GATConv,
52
+ 'Transformer': TransformerConv,
53
+ 'SG': SGConv,
54
+ 'SSG': SSGConv
55
+ }
56
+
57
+ def __init__(
58
+ self,
59
+ input_dim: int,
60
+ hidden_dim: int,
61
+ output_dim: int,
62
+ conv_layer_type: str = 'GAT',
63
+ hidden_layers: int = 2,
64
+ dropout: float = 0.05,
65
+ Cheb_k: int = 1,
66
+ alpha: float = 0.5
67
+ ):
68
+ super().__init__()
69
+ self._validate_conv_type(conv_layer_type)
70
+ self._init_attributes(conv_layer_type, hidden_layers, dropout)
71
+ self._build_network(input_dim, hidden_dim, output_dim, Cheb_k, alpha)
72
+ self.disp = nn.Parameter(torch.randn(output_dim))
73
+ self.apply(self._init_weights)
74
+
75
+ def _validate_conv_type(self, conv_layer_type: str) -> None:
76
+ if conv_layer_type not in self.CONV_LAYERS:
77
+ raise ValueError(f"Unsupported layer type: {conv_layer_type}. Choose from {list(self.CONV_LAYERS.keys())}")
78
+
79
+ def _init_attributes(self, conv_layer_type: str, hidden_layers: int, dropout: float) -> None:
80
+ self.conv_layer_type = conv_layer_type
81
+ self.conv_layer = self.CONV_LAYERS[conv_layer_type]
82
+ self.hidden_layers = hidden_layers
83
+ self.dropout = dropout
84
+ self.convs = nn.ModuleList()
85
+ self.bns = nn.ModuleList()
86
+ self.dropouts = nn.ModuleList()
87
+ self.relu = nn.ReLU()
88
+
89
+ def _create_conv_layer(self, in_dim: int, out_dim: int, Cheb_k: int, alpha: float) -> nn.Module:
90
+ if self.conv_layer_type == 'Transformer':
91
+ return self.conv_layer(in_dim, out_dim, edge_dim=1)
92
+ elif self.conv_layer_type == 'Cheb':
93
+ return self.conv_layer(in_dim, out_dim, Cheb_k)
94
+ elif self.conv_layer_type == 'SSG':
95
+ return self.conv_layer(in_dim, out_dim, alpha=alpha)
96
+ return self.conv_layer(in_dim, out_dim)
97
+
98
+ def _build_network(self, input_dim: int, hidden_dim: int, output_dim: int, Cheb_k: int, alpha: float) -> None:
99
+ # Input layer
100
+ self.convs.append(self._create_conv_layer(input_dim, hidden_dim, Cheb_k, alpha))
101
+ self.bns.append(nn.BatchNorm1d(hidden_dim))
102
+ self.dropouts.append(nn.Dropout(self.dropout))
103
+
104
+ # Hidden layers
105
+ for _ in range(self.hidden_layers - 1):
106
+ self.convs.append(self._create_conv_layer(hidden_dim, hidden_dim, Cheb_k, alpha))
107
+ self.bns.append(nn.BatchNorm1d(hidden_dim))
108
+ self.dropouts.append(nn.Dropout(self.dropout))
109
+
110
+ # Output layer - implemented by subclasses
111
+ self._build_output_layer(hidden_dim, output_dim, Cheb_k, alpha)
112
+
113
+ def _build_output_layer(self, hidden_dim: int, output_dim: int, Cheb_k: int, alpha: float) -> None:
114
+ raise NotImplementedError("Subclasses must implement _build_output_layer")
115
+
116
+ @staticmethod
117
+ def _init_weights(m: nn.Module) -> None:
118
+ if isinstance(m, (nn.Linear, nn.Conv1d)):
119
+ nn.init.xavier_uniform_(m.weight)
120
+ if m.bias is not None:
121
+ nn.init.zeros_(m.bias)
122
+
123
+ def _process_layer(self, x: torch.Tensor, conv: nn.Module, edge_index: torch.Tensor,
124
+ edge_weight: Optional[torch.Tensor]) -> torch.Tensor:
125
+ if isinstance(conv, SAGEConv):
126
+ return conv(x, edge_index)
127
+ elif isinstance(conv, TransformerConv):
128
+ return conv(x, edge_index, edge_weight.view(-1, 1))
129
+ return conv(x, edge_index, edge_weight)
130
+
131
+ def forward(self, x: torch.Tensor, edge_index: torch.Tensor,
132
+ edge_weight: Optional[torch.Tensor] = None,
133
+ use_residual: bool = True) -> Union[torch.Tensor, Tuple[torch.Tensor, ...]]:
134
+ raise NotImplementedError("Subclasses must implement forward")
135
+
136
+
137
+ class GraphEncoder(BaseGraphNetwork):
138
+ """Graph encoder with variational output."""
139
+
140
+ def _build_output_layer(self, hidden_dim: int, latent_dim: int, Cheb_k: int, alpha: float) -> None:
141
+ self.conv_mean = self._create_conv_layer(hidden_dim, latent_dim, Cheb_k, alpha)
142
+ self.conv_logvar = self._create_conv_layer(hidden_dim, latent_dim, Cheb_k, alpha)
143
+
144
+ def forward(self, x: torch.Tensor, edge_index: torch.Tensor,
145
+ edge_weight: Optional[torch.Tensor] = None,
146
+ use_residual: bool = True) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
147
+ residual = None
148
+
149
+ # Process hidden layers
150
+ for i, (conv, bn, dropout) in enumerate(zip(self.convs, self.bns, self.dropouts)):
151
+ x = self._process_layer(x, conv, edge_index, edge_weight)
152
+ x = bn(x)
153
+ x = self.relu(x)
154
+ x = dropout(x)
155
+
156
+ if use_residual and i == 0:
157
+ residual = x
158
+
159
+ if use_residual and residual is not None:
160
+ x = x + residual
161
+
162
+ # Compute variational parameters
163
+ q_m = self._process_layer(x, self.conv_mean, edge_index, edge_weight)
164
+ q_s = self._process_layer(x, self.conv_logvar, edge_index, edge_weight)
165
+
166
+ # Sample from the latent distribution
167
+ std = F.softplus(q_s) + 1e-6
168
+ dist = Normal(q_m, std)
169
+ q_z = dist.rsample()
170
+
171
+ return q_z, q_m, q_s
172
+
173
+
174
+ class GraphDecoder(BaseGraphNetwork):
175
+ """Graph decoder with softmax output."""
176
+
177
+ def _build_output_layer(self, hidden_dim: int, output_dim: int, Cheb_k: int, alpha: float) -> None:
178
+ self.output_conv = self._create_conv_layer(hidden_dim, output_dim, Cheb_k, alpha)
179
+ self.softmax = nn.Softmax(dim=-1)
180
+
181
+ def forward(self, x: torch.Tensor, edge_index: torch.Tensor,
182
+ edge_weight: Optional[torch.Tensor] = None,
183
+ use_residual: bool = True) -> torch.Tensor:
184
+ residual = None
185
+
186
+ # Process hidden layers
187
+ for i, (conv, bn, dropout) in enumerate(zip(self.convs, self.bns, self.dropouts)):
188
+ x = self._process_layer(x, conv, edge_index, edge_weight)
189
+ x = bn(x)
190
+ x = self.relu(x)
191
+ x = dropout(x)
192
+
193
+ if use_residual and i == 0:
194
+ residual = x
195
+
196
+ if use_residual and residual is not None:
197
+ x = x + residual
198
+
199
+ # Apply output layer with softmax
200
+ x = self._process_layer(x, self.output_conv, edge_index, edge_weight)
201
+ return self.softmax(x)
202
+
203
+
204
+ class BaseLinearModel(nn.Module):
205
+ """
206
+ Base linear model for neural network encoders/decoders.
207
+
208
+ Parameters
209
+ ----------
210
+ input_dim : int
211
+ Input feature dimension.
212
+ hidden_dim : int
213
+ Hidden layer dimension.
214
+ output_dim : int
215
+ Output feature dimension.
216
+ hidden_layers : int, optional
217
+ Number of hidden layers, by default 2.
218
+ dropout : float, optional
219
+ Dropout rate, by default 0.0.
220
+ """
221
+ def __init__(
222
+ self,
223
+ input_dim: int,
224
+ hidden_dim: int,
225
+ output_dim: int,
226
+ hidden_layers: int = 2,
227
+ dropout: float = 0.0
228
+ ):
229
+ super().__init__()
230
+ self.input_dim = input_dim
231
+ self.hidden_dim = hidden_dim
232
+ self.output_dim = output_dim
233
+ self.hidden_layers = hidden_layers
234
+
235
+ # Build network layers
236
+ layers = []
237
+ # Input layer
238
+ layers.extend([
239
+ nn.Linear(input_dim, hidden_dim),
240
+ nn.ReLU(),
241
+ nn.Dropout(dropout)
242
+ ])
243
+
244
+ # Hidden layers
245
+ for _ in range(hidden_layers - 1):
246
+ layers.extend([
247
+ nn.Linear(hidden_dim, hidden_dim),
248
+ nn.ReLU(),
249
+ nn.Dropout(dropout)
250
+ ])
251
+
252
+ # Output layer
253
+ layers.append(nn.Linear(hidden_dim, output_dim))
254
+
255
+ self.network = nn.Sequential(*layers)
256
+ self.apply(self._init_weights)
257
+
258
+ def _init_weights(self, module: nn.Module) -> None:
259
+ """Initialize network weights using Xavier uniform initialization."""
260
+ if isinstance(module, nn.Linear):
261
+ nn.init.xavier_uniform_(module.weight)
262
+ if module.bias is not None:
263
+ nn.init.zeros_(module.bias)
264
+
265
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
266
+ """
267
+ Forward pass through the network.
268
+
269
+ Parameters
270
+ ----------
271
+ x : torch.Tensor
272
+ Input tensor of shape (batch_size, input_dim)
273
+
274
+ Returns
275
+ -------
276
+ torch.Tensor
277
+ Output tensor of shape (batch_size, output_dim)
278
+ """
279
+ return self.network(x)
280
+
281
+
282
+ class LinearEncoder(BaseLinearModel):
283
+ """
284
+ Feature encoder network that maps input features to latent space.
285
+
286
+ Parameters
287
+ ----------
288
+ input_dim : int
289
+ Original input feature dimension.
290
+ hidden_dim : int
291
+ Hidden layer dimension.
292
+ latent_dim : int
293
+ Latent space dimension.
294
+ hidden_layers : int, optional
295
+ Number of hidden layers, by default 2.
296
+ dropout : float, optional
297
+ Dropout rate, by default 0.0.
298
+ """
299
+ def __init__(
300
+ self,
301
+ input_dim: int,
302
+ hidden_dim: int,
303
+ latent_dim: int,
304
+ hidden_layers: int = 2,
305
+ dropout: float = 0.0
306
+ ):
307
+ super().__init__(
308
+ input_dim=input_dim,
309
+ hidden_dim=hidden_dim,
310
+ output_dim=hidden_dim,
311
+ hidden_layers=hidden_layers,
312
+ dropout=dropout
313
+ )
314
+ self.mu_layer = nn.Linear(hidden_dim, latent_dim)
315
+ self.logvar_layer = nn.Linear(hidden_dim, latent_dim)
316
+ # Initialize weights for the new layers
317
+ nn.init.xavier_uniform_(self.mu_layer.weight)
318
+ nn.init.zeros_(self.mu_layer.bias)
319
+ nn.init.xavier_uniform_(self.logvar_layer.weight)
320
+ nn.init.zeros_(self.logvar_layer.bias)
321
+
322
+ def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
323
+ """
324
+ Forward pass through the encoder network.
325
+
326
+ Parameters
327
+ ----------
328
+ x : torch.Tensor
329
+ Input tensor of shape (batch_size, input_dim)
330
+
331
+ Returns
332
+ -------
333
+ tuple[torch.Tensor, torch.Tensor, torch.Tensor]
334
+ Tuple containing:
335
+ - z: Sampled latent vector
336
+ - mu: Mean of the latent distribution
337
+ - logvar: Log variance of the latent distribution
338
+ """
339
+ h = self.network(x)
340
+
341
+ q_m = self.mu_layer(h)
342
+ q_s = self.logvar_layer(h)
343
+ std = F.softplus(q_s) + 1e-6
344
+ dist = Normal(q_m, std)
345
+ q_z = dist.rsample()
346
+ return q_z, q_m, q_s
347
+
348
+ class LinearDecoder(BaseLinearModel):
349
+ """
350
+ Feature decoder network that maps latent representations back to feature space.
351
+
352
+ Parameters
353
+ ----------
354
+ input_dim : int
355
+ Original input feature dimension.
356
+ hidden_dim : int
357
+ Hidden layer dimension.
358
+ latent_dim : int
359
+ Latent space dimension.
360
+ hidden_layers : int, optional
361
+ Number of hidden layers, by default 2.
362
+ dropout : float, optional
363
+ Dropout rate, by default 0.0.
364
+ """
365
+ def __init__(
366
+ self,
367
+ input_dim: int,
368
+ hidden_dim: int,
369
+ latent_dim: int,
370
+ hidden_layers: int = 2,
371
+ dropout: float = 0.0
372
+ ):
373
+ super().__init__(
374
+ input_dim=latent_dim,
375
+ hidden_dim=hidden_dim,
376
+ output_dim=input_dim,
377
+ hidden_layers=hidden_layers,
378
+ dropout=dropout
379
+ )
380
+ self.disp = nn.Parameter(torch.randn(input_dim))
381
+ # Add softmax activation for the output layer
382
+ self.network = nn.Sequential(
383
+ self.network,
384
+ nn.Softmax(dim=-1)
385
+ )
386
+
387
+
388
+ class GraphVAE_r(nn.Module):
389
+ """
390
+ Variational Graph Autoencoder (VGAE) with flexible encoder and decoder options.
391
+
392
+ Parameters
393
+ ----------
394
+ input_dim : int
395
+ Input feature dimension.
396
+ hidden_dim : int
397
+ Hidden layer dimension.
398
+ latent_dim : int
399
+ Latent space dimension.
400
+ encoder_type : str
401
+ Type of encoder ('linear' or 'graph')
402
+ graph_type : str, optional
403
+ Type of graph conv layer
404
+ ('GCN', 'Cheb', 'SAGE', 'TAG', 'ARMA', 'GAT', 'Transformer'), by default 'GAT'
405
+ structure_decoder_type : str, optional
406
+ Type of structure decoder ('bilinear', 'inner_product', 'mlp'), by default 'mlp'
407
+ feature_decoder_type : str, optional
408
+ Type of feature decoder ('linear' or 'graph'), by default 'linear'
409
+ hidden_layers : int, optional
410
+ Number of hidden layers, by default 2
411
+ decoder_hidden_dim : int, optional
412
+ Hidden dimension for structure MLP decoder, by default 128
413
+ dropout : float, optional
414
+ Dropout rate, by default 0.05
415
+ use_residual : bool, optional
416
+ Whether to use residual connections, by default True
417
+ Cheb_k : int, optional
418
+ Order of Chebyshev polynomials, by default 1
419
+ alpha : float, optional
420
+ Teleport probability, by default 0.5
421
+ sparse_threshold : int, optional
422
+ Maximum edges per node for structure decoder, by default None
423
+ """
424
+ def __init__(
425
+ self,
426
+ input_dim: int,
427
+ hidden_dim: int,
428
+ latent_dim: int,
429
+ encoder_type: str = 'graph',
430
+ graph_type: str = 'GAT',
431
+ structure_decoder_type: str = 'mlp',
432
+ feature_decoder_type: str = 'linear',
433
+ hidden_layers: int = 2,
434
+ decoder_hidden_dim: int = 128,
435
+ dropout: float = 0.05,
436
+ use_residual: bool = True,
437
+ Cheb_k: int = 1,
438
+ alpha: float = 0.5,
439
+ threshold: float = 0,
440
+ sparse_threshold: Optional[int] = None
441
+ ):
442
+ super().__init__()
443
+
444
+ # Validate encoder type
445
+ if encoder_type not in ['linear', 'graph']:
446
+ raise ValueError("encoder_type must be 'linear' or 'graph'")
447
+
448
+ # Initialize encoder
449
+ if encoder_type == 'linear':
450
+ self.encoder = LinearEncoder(
451
+ input_dim=input_dim,
452
+ hidden_dim=hidden_dim,
453
+ latent_dim=latent_dim,
454
+ hidden_layers=hidden_layers,
455
+ dropout=dropout
456
+ )
457
+ else: # graph
458
+ self.encoder = GraphEncoder(
459
+ input_dim=input_dim,
460
+ hidden_dim=hidden_dim,
461
+ output_dim=latent_dim,
462
+ conv_layer_type=graph_type,
463
+ hidden_layers=hidden_layers,
464
+ dropout=dropout,
465
+ Cheb_k=Cheb_k,
466
+ alpha=alpha
467
+ )
468
+
469
+ # Initialize structure decoder
470
+ self.structure_decoder = GraphStructureDecoder(
471
+ structure_decoder=structure_decoder_type,
472
+ latent_dim=latent_dim,
473
+ hidden_dim=decoder_hidden_dim,
474
+ threshold=threshold,
475
+ sparse_threshold=sparse_threshold,
476
+ symmetric=True,
477
+ add_self_loops=False
478
+ )
479
+
480
+ # Validate and initialize feature decoder
481
+ if feature_decoder_type not in ['linear', 'graph']:
482
+ raise ValueError("feature_decoder_type must be 'linear' or 'graph'")
483
+
484
+ if feature_decoder_type == 'linear':
485
+ self.feature_decoder = LinearDecoder(
486
+ input_dim=input_dim,
487
+ hidden_dim=hidden_dim,
488
+ latent_dim=latent_dim,
489
+ hidden_layers=hidden_layers,
490
+ dropout=dropout
491
+ )
492
+ else: # graph
493
+ self.feature_decoder = GraphDecoder(
494
+ input_dim=latent_dim,
495
+ hidden_dim=hidden_dim,
496
+ output_dim=input_dim,
497
+ conv_layer_type=graph_type,
498
+ hidden_layers=hidden_layers,
499
+ dropout=dropout,
500
+ Cheb_k=Cheb_k,
501
+ alpha=alpha
502
+ )
503
+
504
+ self.encoder_type = encoder_type
505
+ self.feature_decoder_type = feature_decoder_type
506
+ self.use_residual = use_residual
507
+
508
+ def forward(
509
+ self,
510
+ x: torch.Tensor,
511
+ edge_index: Optional[torch.Tensor] = None,
512
+ edge_weight: Optional[torch.Tensor] = None
513
+ ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
514
+ """
515
+ Forward pass of the GraphVAE.
516
+
517
+ Parameters
518
+ ----------
519
+ x : torch.Tensor
520
+ Node features (num_nodes, input_dim)
521
+ edge_index : torch.Tensor, optional
522
+ Edge indices (2, num_edges), required for graph encoder
523
+ edge_weight : torch.Tensor, optional
524
+ Edge weights (num_edges,), required for graph encoder
525
+
526
+ Returns
527
+ -------
528
+ q_z : torch.Tensor
529
+ Sampled latent representation
530
+ q_m : torch.Tensor
531
+ Mean of latent distribution
532
+ q_s : torch.Tensor
533
+ Log variance of latent distribution
534
+ pred_adj : torch.Tensor
535
+ Reconstructed adjacency matrix
536
+ pred_x : torch.Tensor
537
+ Reconstructed node features
538
+ """
539
+ # Encode
540
+ if self.encoder_type == 'linear':
541
+ q_z, q_m, q_s = self.encoder(x)
542
+ else: # graph
543
+ if edge_index is None:
544
+ raise ValueError("edge_index required for graph encoder")
545
+ q_z, q_m, q_s = self.encoder(x, edge_index, edge_weight, self.use_residual)
546
+
547
+ # Decode structure
548
+ pred_a, pred_edge_index, pred_edge_weight = self.structure_decoder(q_z, edge_index)
549
+
550
+ # Decode features
551
+ if self.feature_decoder_type == 'linear':
552
+ pred_x = self.feature_decoder(q_z)
553
+ else: # graph
554
+ pred_x = self.feature_decoder(
555
+ q_z, pred_edge_index, pred_edge_weight
556
+ )
557
+
558
+ return q_z, q_m, q_s, pred_a, pred_x
559
+
560
+
561
+