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.
GNODEVAE/trainer.py ADDED
@@ -0,0 +1,397 @@
1
+ from .mixin import scviMixin, adjMixin
2
+ from .module import GraphVAE, iGraphVAE
3
+ from .module_refined import GraphVAE_r
4
+ import torch
5
+ import torch.nn.functional as F
6
+ from torch_geometric.data import Data
7
+ import numpy as np
8
+ from typing import List, Tuple, Optional
9
+
10
+ class _BaseTrainer(scviMixin, adjMixin):
11
+ """Base trainer class with shared logic."""
12
+ def __init__(self, model, lr, beta, graph, device):
13
+ self.gvae = model.to(device)
14
+ self.opt = torch.optim.Adam(self.gvae.parameters(), lr=lr)
15
+ self.beta = beta
16
+ self.graph = graph
17
+ self.loss = []
18
+ self.device = device
19
+
20
+ def take_latent(self, cd: Data) -> np.ndarray:
21
+ """
22
+ Extract the latent representation from the GraphVAE model.
23
+
24
+ Parameters
25
+ ----------
26
+ cd : torch_geometric.data.Data
27
+ Input graph data containing node features (`x`), edge indices (`edge_index`),
28
+ and edge attributes (`edge_attr`).
29
+
30
+ Returns
31
+ -------
32
+ np.ndarray
33
+ Latent representation of the input data, shape (num_nodes, latent_dim).
34
+ """
35
+ states = cd.x
36
+ edge_index = cd.edge_index
37
+ edge_weight = cd.edge_attr
38
+ res = self.gvae(states, edge_index, edge_weight)
39
+ q_z = res[0]
40
+ return q_z.detach().cpu().numpy()
41
+
42
+ def _kl_loss(self, q_m, q_s):
43
+ p_m = torch.zeros_like(q_m)
44
+ p_s = torch.zeros_like(q_s)
45
+ return self.beta * self._normal_kl(q_m, q_s, p_m, p_s).sum(-1).mean()
46
+
47
+ def _adj_loss(self, adj, pred_a):
48
+ return self.graph * F.binary_cross_entropy_with_logits(pred_a, adj)
49
+
50
+ def _optimize(self, loss):
51
+ self.opt.zero_grad()
52
+ loss.backward()
53
+ self.opt.step()
54
+
55
+ class Trainer_r(_BaseTrainer):
56
+ """
57
+ Trainer class for training the GraphVAE_r model.
58
+
59
+ This class combines the functionality of `scviMixin` and `adjMixin` to train
60
+ a Graph Variational Autoencoder (GraphVAE) model. It handles the forward pass,
61
+ loss computation, and optimization steps.
62
+
63
+ Parameters
64
+ ----------
65
+ input_dim : int
66
+ Input feature dimension.
67
+ hidden_dim : int
68
+ Hidden layer dimension.
69
+ latent_dim : int
70
+ Latent space dimension.
71
+ encoder_type : str
72
+ Type of encoder ('linear' or 'graph')
73
+ graph_type : str, optional
74
+ Type of graph conv layer
75
+ ('GCN', 'Cheb', 'SAGE', 'TAG', 'ARMA', 'GAT', 'Transformer'), by default 'GAT'
76
+ structure_decoder_type : str, optional
77
+ Type of structure decoder ('bilinear', 'inner_product', 'mlp'), by default 'mlp'
78
+ feature_decoder_type : str, optional
79
+ Type of feature decoder ('linear' or 'graph'), by default 'linear'
80
+ hidden_layers : int, optional
81
+ Number of hidden layers, by default 2
82
+ decoder_hidden_dim : int, optional
83
+ Hidden dimension for structure MLP decoder, by default 128
84
+ dropout : float, optional
85
+ Dropout rate, by default 0.05
86
+ use_residual : bool, optional
87
+ Whether to use residual connections, by default True
88
+ Cheb_k : int, optional
89
+ Order of Chebyshev polynomials, by default 1
90
+ alpha : float, optional
91
+ Teleport probability, by default 0.5
92
+ sparse_threshold : int, optional
93
+ Maximum edges per node for structure decoder, by default None
94
+ lr : float
95
+ Learning rate for the optimizer.
96
+ beta : float
97
+ Weight for the KL divergence term in the loss function.
98
+ graph : float
99
+ Weight for the graph reconstruction loss.
100
+ device : torch.device
101
+ Device to run the model on (e.g., 'cpu' or 'cuda').
102
+
103
+ Attributes
104
+ ----------
105
+ gvae : GraphVAE_r
106
+ The GraphVAE_r model instance.
107
+ opt : torch.optim.Adam
108
+ Optimizer for training the model.
109
+ beta : float
110
+ Weight for the KL divergence term in the loss function.
111
+ graph : float
112
+ Weight for the graph reconstruction loss.
113
+ loss : List[Tuple[float, float, float]]
114
+ List of loss values (reconstruction loss, KL divergence, BCE loss) for each training step.
115
+ device : torch.device
116
+ Device to run the model on.
117
+ """
118
+
119
+ def __init__(
120
+ self,
121
+ input_dim: int,
122
+ hidden_dim: int,
123
+ latent_dim: int,
124
+ encoder_type: str = 'graph',
125
+ graph_type: str = 'GAT',
126
+ structure_decoder_type: str = 'mlp',
127
+ feature_decoder_type: str = 'linear',
128
+ hidden_layers: int = 2,
129
+ decoder_hidden_dim: int = 128,
130
+ dropout: float = 0.05,
131
+ use_residual: bool = True,
132
+ Cheb_k: int = 1,
133
+ alpha: float = 0.5,
134
+ threshold: float = 0,
135
+ sparse_threshold: Optional[int] = None,
136
+ lr: float = 1e-4,
137
+ beta: float = 1,
138
+ graph: float = 1,
139
+ device: torch.device = torch.device('cuda'),
140
+ ):
141
+
142
+ model = GraphVAE_r(
143
+ input_dim,
144
+ hidden_dim,
145
+ latent_dim,
146
+ encoder_type,
147
+ graph_type,
148
+ structure_decoder_type,
149
+ feature_decoder_type,
150
+ hidden_layers,
151
+ decoder_hidden_dim,
152
+ dropout,
153
+ use_residual,
154
+ Cheb_k,
155
+ alpha,
156
+ threshold,
157
+ sparse_threshold
158
+ )
159
+ super().__init__(model, lr, beta, graph, device)
160
+
161
+ def update(
162
+ self,
163
+ cd: Data
164
+ ) -> None:
165
+ """
166
+ Perform a single training step for the GraphVAE model.
167
+
168
+ This method computes the reconstruction loss, KL divergence, and graph
169
+ reconstruction loss, and updates the model parameters using backpropagation.
170
+
171
+ Parameters
172
+ ----------
173
+ cd : torch_geometric.data.Data
174
+ Input graph data containing node features (`x`), edge indices (`edge_index`),
175
+ and edge attributes (`edge_attr`).
176
+
177
+ Returns
178
+ -------
179
+ None
180
+ """
181
+ states = cd.x
182
+ edge_index = cd.edge_index
183
+ edge_weight = cd.edge_attr
184
+
185
+ q_z, q_m, q_s, pred_a, pred_x = self.gvae(states, edge_index, edge_weight)
186
+
187
+ l = states.sum(-1).view(-1, 1)
188
+ recon_loss = self._recon_loss(l, states, pred_x)
189
+ kl_loss = self._kl_loss(q_m, q_s)
190
+
191
+ num_nodes = states.size(0)
192
+ adj = self._build_adj(edge_index, num_nodes, edge_weight).to_dense()
193
+ adj_loss = self._adj_loss(adj, pred_a)
194
+
195
+ loss = recon_loss + kl_loss + adj_loss
196
+ self.loss.append((recon_loss.item(), kl_loss.item(), adj_loss.item()))
197
+ self._optimize(loss)
198
+
199
+ def _recon_loss(
200
+ self,
201
+ l,
202
+ states,
203
+ pred_x
204
+ ):
205
+ pred_x = pred_x * l
206
+ disp = torch.exp(self.gvae.feature_decoder.disp)
207
+ recon_loss = -self._log_nb(states, pred_x, disp).sum(-1).mean()
208
+ return recon_loss
209
+
210
+ class Trainer(_BaseTrainer):
211
+ """
212
+ Trainer class for training the GraphVAE model.
213
+
214
+ This class combines the functionality of `scviMixin` and `adjMixin` to train
215
+ a Graph Variational Autoencoder (GraphVAE) model. It handles the forward pass,
216
+ loss computation, and optimization steps.
217
+
218
+ Parameters
219
+ ----------
220
+ interpretable : bool
221
+ Use the iVGAE model.
222
+ input_dim : int
223
+ Dimensionality of the input features.
224
+ hidden_dim : int
225
+ Dimensionality of the hidden layers in the encoder and decoder.
226
+ latent_dim : int
227
+ Dimensionality of the latent space.
228
+ idim : int, optional
229
+ Dimensionality of the interpreted space.
230
+ encoder_type : str
231
+ Type of encoder to use.
232
+ encoder_hidden_layers : int
233
+ Number of hidden layers in the encoder.
234
+ decoder_type : str
235
+ Type of decoder to use.
236
+ decoder_hidden_dim : int
237
+ Dimensionality of the hidden layers in the decoder.
238
+ feature_decoder_hidden_layers : int
239
+ Number of hidden layers in the feature decoder.
240
+ dropout : float
241
+ Dropout rate for the model.
242
+ use_residual : bool, optional
243
+ Whether to use residual connections, by default True.
244
+ Cheb_k : int, optional
245
+ The order of Chebyshev polynomials for ChebConv, by default None.
246
+ alpha : float, optional
247
+ Teleport probability, by default 0.5.
248
+ lr : float
249
+ Learning rate for the optimizer.
250
+ beta : float
251
+ Weight for the KL divergence term in the loss function.
252
+ graph : float
253
+ Weight for the graph reconstruction loss.
254
+ device : torch.device
255
+ Device to run the model on (e.g., 'cpu' or 'cuda').
256
+
257
+ Attributes
258
+ ----------
259
+ gvae : GraphVAE
260
+ The GraphVAE model instance.
261
+ opt : torch.optim.Adam
262
+ Optimizer for training the model.
263
+ beta : float
264
+ Weight for the KL divergence term in the loss function.
265
+ graph : float
266
+ Weight for the graph reconstruction loss.
267
+ loss : List[Tuple[float, float, float]]
268
+ List of loss values (reconstruction loss, KL divergence, BCE loss) for each training step.
269
+ device : torch.device
270
+ Device to run the model on.
271
+ """
272
+
273
+ def __init__(
274
+ self,
275
+ interpretable: bool,
276
+ input_dim: int,
277
+ hidden_dim: int,
278
+ latent_dim: int,
279
+ idim: Optional[int],
280
+ encoder_type: str,
281
+ encoder_hidden_layers: int,
282
+ decoder_type: str,
283
+ decoder_hidden_dim: int,
284
+ feature_decoder_hidden_layers: int,
285
+ dropout: float,
286
+ use_residual: bool,
287
+ Cheb_k: Optional[int],
288
+ alpha: Optional[float],
289
+ lr: float,
290
+ beta: float,
291
+ graph: float,
292
+ device: torch.device,
293
+ ):
294
+ self.interpretable = interpretable
295
+ if interpretable:
296
+ model = iGraphVAE(
297
+ input_dim,
298
+ hidden_dim,
299
+ latent_dim,
300
+ idim,
301
+ encoder_type,
302
+ encoder_hidden_layers,
303
+ decoder_type,
304
+ decoder_hidden_dim,
305
+ feature_decoder_hidden_layers,
306
+ dropout,
307
+ use_residual,
308
+ Cheb_k,
309
+ alpha
310
+ )
311
+ else:
312
+ model = GraphVAE(
313
+ input_dim,
314
+ hidden_dim,
315
+ latent_dim,
316
+ encoder_type,
317
+ encoder_hidden_layers,
318
+ decoder_type,
319
+ decoder_hidden_dim,
320
+ feature_decoder_hidden_layers,
321
+ dropout,
322
+ use_residual,
323
+ Cheb_k,
324
+ alpha
325
+ )
326
+ super().__init__(model, lr, beta, graph, device)
327
+
328
+ def update(
329
+ self,
330
+ cd: Data
331
+ ) -> None:
332
+ """
333
+ Perform a single training step for the GraphVAE model.
334
+
335
+ This method computes the reconstruction loss, KL divergence, and graph
336
+ reconstruction loss, and updates the model parameters using backpropagation.
337
+
338
+ Parameters
339
+ ----------
340
+ cd : torch_geometric.data.Data
341
+ Input graph data containing node features (`x`), edge indices (`edge_index`),
342
+ and edge attributes (`edge_attr`).
343
+
344
+ Returns
345
+ -------
346
+ None
347
+ """
348
+ states = cd.x
349
+ edge_index = cd.edge_index
350
+ edge_weight = cd.edge_attr
351
+
352
+ if self.interpretable:
353
+ q_z, q_m, q_s, pred_a, pred_x, i_q_z, pred_ia, pred_ix = self.gvae(states, edge_index, edge_weight)
354
+ l = states.sum(-1).view(-1, 1)
355
+ recon_loss = self._recon_loss(l, states, pred_x)
356
+ irecon_loss = self._recon_loss(l, states, pred_ix)
357
+
358
+ kl_loss = self._kl_loss(q_m, q_s)
359
+
360
+ num_nodes = states.size(0)
361
+ adj = self._build_adj(edge_index, num_nodes, edge_weight).to_dense()
362
+ adj_loss = self._adj_loss(adj, pred_a)
363
+ iadj_loss = self._adj_loss(adj, pred_ia)
364
+
365
+ loss = recon_loss + kl_loss + adj_loss + irecon_loss + iadj_loss
366
+ self.loss.append((recon_loss.item(),
367
+ kl_loss.item(),
368
+ adj_loss.item(),
369
+ irecon_loss.item(),
370
+ iadj_loss.item()
371
+ ))
372
+
373
+ else:
374
+ q_z, q_m, q_s, pred_a, pred_x = self.gvae(states, edge_index, edge_weight)
375
+ l = states.sum(-1).view(-1, 1)
376
+ recon_loss = self._recon_loss(l, states, pred_x)
377
+ kl_loss = self._kl_loss(q_m, q_s)
378
+
379
+ num_nodes = states.size(0)
380
+ adj = self._build_adj(edge_index, num_nodes, edge_weight).to_dense()
381
+ adj_loss = self._adj_loss(adj, pred_a)
382
+
383
+ loss = recon_loss + kl_loss + adj_loss
384
+ self.loss.append((recon_loss.item(), kl_loss.item(), adj_loss.item()))
385
+
386
+ self._optimize(loss)
387
+
388
+ def _recon_loss(
389
+ self,
390
+ l,
391
+ states,
392
+ pred_x
393
+ ):
394
+ pred_x = pred_x * l
395
+ disp = torch.exp(self.gvae.x_decoder.disp)
396
+ recon_loss = -self._log_nb(states, pred_x, disp).sum(-1).mean()
397
+ return recon_loss