-
Notifications
You must be signed in to change notification settings - Fork 1
/
VGAE.py
159 lines (131 loc) · 5.51 KB
/
VGAE.py
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import GCN
from torch_geometric.utils import to_dense_adj, negative_sampling
from pygod.detector import DeepDetector
from pygod.nn.decoder import DotProductDecoder
import math
from utils import z_sampling_
class VGAEBase(nn.Module):
def __init__(self,
in_dim,
hid_dim=64,
num_layers=2,
dropout=0.,
act=F.relu,
backbone=GCN,
sigmoid_s=False,
device=None,
**kwargs):
super(VGAEBase, self).__init__()
self.hid_dim = hid_dim
self.device = device
self.enc_layers = backbone(in_channels=in_dim,
hidden_channels=hid_dim,
num_layers=num_layers,
out_channels=hid_dim*2,
act=act,
dropout=dropout,
**kwargs)
self.dec_layers = backbone(in_channels=hid_dim,
hidden_channels=hid_dim,
num_layers=num_layers,
out_channels=in_dim,
act=act,
dropout=dropout,
**kwargs)
self.struc_dec_layers = DotProductDecoder(in_dim=hid_dim,
hid_dim=hid_dim,
num_layers=num_layers-1,
dropout=dropout,
act=act,
sigmoid_s=sigmoid_s,
backbone=backbone,
**kwargs)
def encoder(self, x, edge_index):
ouput = self.enc_layers(x, edge_index)
z_mean, z_log_std = ouput[:,:self.hid_dim], ouput[:,self.hid_dim:]
sampled_z = z_sampling_(z_mean, z_log_std, self.device)
return z_mean, z_log_std, sampled_z
def decoder(self, z, edge_index):
x_ = self.dec_layers(z, edge_index)
adj_ = self.struc_dec_layers(z, edge_index)
return x_, adj_
def forward(self, x, edge_index):
z_mean, z_log_std, z = self.encoder(x, edge_index)
x_rec, adj_rec = self.decoder(z, edge_index)
return z_mean, z_log_std, x_rec, adj_rec
@staticmethod
def process_graph(data, recon_s=False):
"""
Obtain the dense adjacency matrix of the graph.
Parameters
----------
data : torch_geometric.data.Data
Input graph.
recon_s : bool, optional
Reconstruct the structure instead of node feature .
"""
if recon_s:
data.s = to_dense_adj(data.edge_index)[0]
class GAEBase(nn.Module):
def __init__(self,
in_dim,
hid_dim=64,
num_layers=2,
dropout=0.,
act=F.relu,
backbone=GCN,
sigmoid_s=False,
device=None,
**kwargs):
super(GAEBase, self).__init__()
self.hid_dim = hid_dim
self.device = device
self.enc_layers = backbone(in_channels=in_dim,
hidden_channels=hid_dim,
num_layers=num_layers,
out_channels=hid_dim,
act=act,
dropout=dropout,
**kwargs)
self.dec_layers = backbone(in_channels=hid_dim,
hidden_channels=hid_dim,
num_layers=num_layers,
out_channels=in_dim,
act=act,
dropout=dropout,
**kwargs)
self.struc_dec_layers = DotProductDecoder(in_dim=hid_dim,
hid_dim=hid_dim,
num_layers=num_layers-1,
dropout=dropout,
act=act,
sigmoid_s=sigmoid_s,
backbone=backbone,
**kwargs)
def encoder(self, x, edge_index):
z = self.enc_layers(x, edge_index)
return z
def decoder(self, z, edge_index):
x_ = self.dec_layers(z, edge_index)
adj_ = self.struc_dec_layers(z, edge_index)
return x_, adj_
def forward(self, x, edge_index):
z = self.encoder(x, edge_index)
x_rec, adj_rec = self.decoder(z, edge_index)
return x_rec, adj_rec
@staticmethod
def process_graph(data, recon_s=False):
"""
Obtain the dense adjacency matrix of the graph.
Parameters
----------
data : torch_geometric.data.Data
Input graph.
recon_s : bool, optional
Reconstruct the structure instead of node feature .
"""
if recon_s:
data.s = to_dense_adj(data.edge_index)[0]