-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathautoencoder.py
191 lines (152 loc) · 6.52 KB
/
autoencoder.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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import GINConv, GCNConv, GraphConv, PNAConv
from torch_geometric.nn import global_add_pool
from ppgn import Powerful
# Decoder
class Decoder(nn.Module):
def __init__(self, latent_dim, hidden_dim, n_layers, n_nodes):
super(Decoder, self).__init__()
self.n_layers = n_layers
self.n_nodes = n_nodes
mlp_layers = [nn.Linear(latent_dim, hidden_dim)] + [nn.Linear(hidden_dim, hidden_dim) for i in range(n_layers-2)]
mlp_layers.append(nn.Linear(hidden_dim, 2*n_nodes*(n_nodes-1)//2))
self.mlp = nn.ModuleList(mlp_layers)
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
def forward(self, x):
for i in range(self.n_layers-1):
x = self.relu(self.mlp[i](x))
x = self.mlp[self.n_layers-1](x)
x = torch.reshape(x, (x.size(0), -1, 2))
x = F.gumbel_softmax(x, tau=1, hard=True)[:,:,0]
adj = torch.zeros(x.size(0), self.n_nodes, self.n_nodes, device=x.device)
idx = torch.triu_indices(self.n_nodes, self.n_nodes, 1)
adj[:,idx[0],idx[1]] = x
adj = adj + torch.transpose(adj, 1, 2)
return adj
class GIN(torch.nn.Module):
def __init__(self, input_dim, hidden_dim, latent_dim, n_layers, dropout=0.2):
super().__init__()
self.dropout = dropout
self.convs = torch.nn.ModuleList()
self.convs.append(GINConv(nn.Sequential(nn.Linear(input_dim, hidden_dim),
nn.LeakyReLU(0.2),
nn.BatchNorm1d(hidden_dim),
nn.Linear(hidden_dim, hidden_dim),
nn.LeakyReLU(0.2))
))
for layer in range(n_layers-1):
self.convs.append(GINConv(nn.Sequential(nn.Linear(hidden_dim, hidden_dim),
nn.LeakyReLU(0.2),
nn.BatchNorm1d(hidden_dim),
nn.Linear(hidden_dim, hidden_dim),
nn.LeakyReLU(0.2))
))
self.bn = nn.BatchNorm1d(hidden_dim)
self.fc = nn.Linear(hidden_dim, latent_dim)
def forward(self, data):
edge_index = data.edge_index
x = data.x
for conv in self.convs:
x = conv(x, edge_index)
x = F.dropout(x, self.dropout, training=self.training)
out = global_add_pool(x, data.batch)
out = self.bn(out)
out = self.fc(out)
return out
class PNA(torch.nn.Module):
def __init__(self, input_dim, hidden_dim, latent_dim, n_layers, dropout=0.2):
super().__init__()
self.dropout = dropout
self.convs = nn.ModuleList()
self.convs.append(PNAConv(input_dim, hidden_dim))
for layer in range(n_layers-1):
self.convs.append(PNAConv(hidden_dim, hidden_dim))
self.bn = nn.BatchNorm1d(hidden_dim)
self.fc = nn.Linear(hidden_dim, latent_dim)
self.relu = nn.ReLU()
def forward(self, data):
edge_index = data.edge_index
x = data.x
for conv in self.convs:
x = self.relu(conv(x, edge_index))
x = F.dropout(x, self.dropout, training=self.training)
out = global_add_pool(x, data.batch)
out = self.bn(out)
out = self.fc(out)
return out
# Autoencoder
class AutoEncoder(nn.Module):
def __init__(self, input_dim, hidden_dim_enc, hidden_dim_dec, latent_dim, n_layers_enc, n_layers_dec, n_max_nodes):
super(AutoEncoder, self).__init__()
self.n_max_nodes = n_max_nodes
self.input_dim = input_dim
self.encoder = GIN(input_dim, hidden_dim_enc, latent_dim, n_layers_enc)
self.decoder = Decoder(latent_dim, hidden_dim_dec, n_layers_dec, n_max_nodes)
def forward(self, data):
x_g = self.encoder(data)
adj = self.decoder(x_g)
return adj
def encode(self, data):
x_g = self.encoder(data)
return x_g
def decode(self, x_g):
adj = self.decoder(x_g)
return adj
def loss_function(self, data):
x_g = self.encoder(data)
adj = self.decoder(x_g)
A = data.A[:,:,:,0]
return F.l1_loss(adj, data.A)
# Variational Autoencoder
class VariationalAutoEncoder(nn.Module):
def __init__(self, input_dim, hidden_dim_enc, hidden_dim_dec, latent_dim, n_layers_enc, n_layers_dec, n_max_nodes):
super(VariationalAutoEncoder, self).__init__()
self.n_max_nodes = n_max_nodes
self.input_dim = input_dim
#self.encoder = GPS(input_dim, hidden_dim_enc, hidden_dim_enc, n_layers_enc)
self.encoder = GIN(input_dim, hidden_dim_enc, hidden_dim_enc, n_layers_enc)
#self.encoder = Powerful(input_dim=input_dim+1, num_layers=n_layers_enc, hidden=hidden_dim_enc, hidden_final=hidden_dim_enc, dropout_prob=0.0, simplified=False)
self.fc_mu = nn.Linear(hidden_dim_enc, latent_dim)
self.fc_logvar = nn.Linear(hidden_dim_enc, latent_dim)
self.decoder = Decoder(latent_dim, hidden_dim_dec, n_layers_dec, n_max_nodes)
def forward(self, data):
x_g = self.encoder(data)
mu = self.fc_mu(x_g)
logvar = self.fc_logvar(x_g)
x_g = self.reparameterize(mu, logvar)
adj = self.decoder(x_g)
return adj
def encode(self, data):
x_g = self.encoder(data)
mu = self.fc_mu(x_g)
logvar = self.fc_logvar(x_g)
x_g = self.reparameterize(mu, logvar)
return x_g
def reparameterize(self, mu, logvar, eps_scale=1.):
if self.training:
std = logvar.mul(0.5).exp_()
eps = torch.randn_like(std) * eps_scale
return eps.mul(std).add_(mu)
else:
return mu
def decode(self, mu, logvar):
x_g = self.reparameterize(mu, logvar)
adj = self.decoder(x_g)
return adj
def decode_mu(self, mu):
adj = self.decoder(mu)
return adj
def loss_function(self, data, beta=0.05):
x_g = self.encoder(data)
mu = self.fc_mu(x_g)
logvar = self.fc_logvar(x_g)
x_g = self.reparameterize(mu, logvar) # concat or sum fully connected layer apo ta feats tou graph
adj = self.decoder(x_g)
#A = data.A[:,:,:,0]
recon = F.l1_loss(adj, data.A, reduction='sum')
kld = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
loss = recon + beta*kld
return loss, recon, kld