-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodel.py
361 lines (307 loc) · 14.8 KB
/
model.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from re import T
import torch
import dgl
import torch.nn.functional as F
import pyro
import copy
import numpy as np
class Node_Generator(torch.nn.Module):
def __init__(self, in_feats, h_feats, discrete_feat):
super(Node_Generator, self).__init__()
self.conv1 = dgl.nn.GraphConv(in_feats, h_feats, norm='both')
self.conv2 = dgl.nn.GraphConv(h_feats, h_feats, norm='both')
self.discrete_feat = discrete_feat
if discrete_feat:
self.generator = torch.nn.Linear(3 * h_feats, in_feats)
else:
self.generator = torch.nn.Linear(3 * h_feats, 2*in_feats)
self.activation = torch.nn.LeakyReLU()
self.in_feats = in_feats
def forward(self, g, node_index, temperature=0.1):
h = self.activation(self.conv1(g, g.ndata['feat']))
h = self.activation(self.conv2(g, h))
h = torch.cat((h.sum(0), h.max(0).values, h[node_index]))
feature_dist = self.generator(h)
if self.discrete_feat:
feature_dist = torch.sigmoid(feature_dist)
dist = BernoulliStraightThrough(probs=feature_dist)
feat = dist.rsample()
log_prob = dist.log_prob(feat).mean()
feat = F.normalize(feat, dim=0)
return feat, feat.mean(), log_prob
else:
mu = feature_dist[:self.in_feats]
sigma = torch.abs(feature_dist[self.in_feats:])
dist = torch.distributions.Normal(mu, sigma)
feat = dist.sample()
log_prob = dist.log_prob(feat).mean()
return feat, [mu, sigma], log_prob
class Edge_Sampler(torch.nn.Module):
def __init__(self, in_feats, h_feats, alpha_n=1000000):
super(Edge_Sampler, self).__init__()
self.conv1 = dgl.nn.GraphConv(in_feats, h_feats, norm='both')
self.conv2 = dgl.nn.GraphConv(h_feats, h_feats, norm='both')
self.regressor = torch.nn.Linear(in_feats + h_feats * 2, 1)
self.activation = torch.nn.LeakyReLU()
self.alpha_n = alpha_n
def forward(self, g, node_index, edge_set):
num_candidate_dst = g.number_of_nodes() - 1 - len(edge_set)
mask = [[i for i in range(g.number_of_nodes() - 1) if i not in edge_set]]
# if khop == 0:
# num_candidate_dst = g.number_of_nodes() - 1 - len(edge_set)
# mask = [[i for i in range(g.number_of_nodes() - 1) if i not in edge_set]]
# else:
# candidates = dgl.khop_in_subgraph(g, node_index, k=khop)[0].ndata[dgl.NID]
# if len(candidates) - len(edge_set) - 1 <= 0:
# if g.number_of_nodes() < 10000:
# candidates = np.arange(g.number_of_nodes() - 1)
# else:
# candidates = np.random.permutation(np.arange(g.number_of_nodes() - 1))[:10000]
# candidates = [c.item() for c in candidates if c.item() not in edge_set]
# else:
# candidates = [c.item() for c in candidates if c.item() not in edge_set][:-1]
# num_candidate_dst = len(candidates)
# mask = candidates
node_neighbors = g.predecessors(node_index)[:-1] # [:-1] excludes the connection to the target node
node_neighbors_mask = torch.zeros(g.number_of_nodes() - 1)
node_neighbors_mask[node_neighbors] = self.alpha_n
node_neighbors_mask = node_neighbors_mask[mask].to(g.device)
sampled_feature = g.ndata['feat'][-1].expand((num_candidate_dst, -1))
h = self.activation(self.conv1(g, g.ndata['feat']))
h = self.activation(self.conv2(g, h))
target_node_embedding = h[node_index].expand((num_candidate_dst, -1))
candidate_dst_h = h[mask]
# if khop != 0:
# g_ = dgl.node_subgraph(g, mask + [node_index.item()])
# h = self.activation(self.conv1(g_, g_.ndata['feat']))
# h = self.activation(self.conv2(g_, h))
# target_node_embedding = h[-1].expand((num_candidate_dst, -1))
# candidate_dst_h = h[:-1]
# else:
# h = self.activation(self.conv1(g, g.ndata['feat']))
# h = self.activation(self.conv2(g, h))
# target_node_embedding = h[node_index].expand((num_candidate_dst, -1))
# candidate_dst_h = h[mask]
h = torch.cat((candidate_dst_h, sampled_feature, target_node_embedding), dim = 1)
feature_dist = self.regressor(h).squeeze() + node_neighbors_mask
feature_dist = F.softmax(feature_dist, dim=0)
dist = torch.distributions.OneHotCategorical(probs=feature_dist)
sample = dist.sample()
log_prob = dist.log_prob(sample)
sample_ = torch.zeros(g.number_of_nodes() - 1).to(g.device)
sample_[mask] = sample
return sample_, log_prob
class Value_Predictor(torch.nn.Module):
def __init__(self, in_feats, h_feats, n_class = 7):
super(Value_Predictor, self).__init__()
self.gc1 = dgl.nn.GraphConv(in_feats, h_feats, norm='both')
self.gc2 = dgl.nn.GraphConv(h_feats, h_feats, norm='both')
self.regressor = torch.nn.Linear(h_feats, n_class)
self.projector = torch.nn.Linear(h_feats+n_class, n_class)
self.activation = torch.nn.LeakyReLU()
self.n_class = n_class
def forward(self, g, node_index, label):
g, node_index = dgl.khop_in_subgraph(g, node_index, 2)
h1 = self.activation(self.gc1(g, g.ndata['feat']))
h1 = self.activation(self.gc2(g, h1))
logits = F.log_softmax(self.regressor(h1), dim = -1)
h = torch.cat((h1[node_index], logits[node_index]), dim = -1)
h = self.projector(h)
return F.cross_entropy(h.reshape(1, self.n_class), label.reshape(1))
class GCN(torch.nn.Module):
# code copied from dgl examples
def __init__(self, in_feats, h_feats, num_classes):
super(GCN, self).__init__()
self.conv1 = dgl.nn.GraphConv(in_feats, h_feats, norm='both')
self.conv2 = dgl.nn.GraphConv(h_feats, num_classes, norm='both')
def forward(self, g, in_feat):
h = self.conv1(g, in_feat)
h = F.relu(h)
h = self.conv2(g, h)
return h
def train(self, g, model):
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
best_val_acc = 0
best_test_acc = 0
features = g.ndata['feat']
labels = g.ndata['label']
train_mask = g.ndata['train_mask']
val_mask = g.ndata['val_mask']
test_mask = g.ndata['test_mask']
test_idx = None
best_model = None
for e in range(1000):
# Forward
logits = model(g, features)
# Compute prediction
pred = logits.argmax(1)
# Compute loss
# Note that you should only compute the losses of the nodes in the training set.
loss = F.cross_entropy(logits[train_mask], labels[train_mask])
# Compute accuracy on training/validation/test
train_acc = (pred[train_mask] == labels[train_mask]).float().mean()
val_acc = (pred[val_mask] == labels[val_mask]).float().mean()
test_acc = (pred[test_mask] == labels[test_mask]).float().mean()
# Save the best validation accuracy and the corresponding test accuracy.
if best_val_acc < val_acc:
best_model = copy.deepcopy(model)
best_val_acc = val_acc
best_test_acc = test_acc
test_idx = test_mask.nonzero(as_tuple=True)[0][(pred[test_mask] == labels[test_mask]).nonzero(as_tuple=True)[0]]
# Backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('Victim model has validation accuracy: {:.2f}, testing accuracy: {:.2f}'.format(best_val_acc.item()*100, best_test_acc.item()*100))
return best_model, test_idx
class SGC(torch.nn.Module):
# code copied from dgl examples
def __init__(self, in_feats, h_feats, num_classes):
super(SGC, self).__init__()
self.conv = dgl.nn.SGConv(in_feats, num_classes, k=2)
def forward(self, g, in_feat):
h = self.conv(g, in_feat)
return h
def train(self, g, model):
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
best_val_acc = 0
best_test_acc = 0
features = g.ndata['feat']
labels = g.ndata['label']
train_mask = g.ndata['train_mask']
val_mask = g.ndata['val_mask']
test_mask = g.ndata['test_mask']
test_idx = None
best_model = None
for e in range(1000):
# Forward
logits = model(g, features)
# Compute prediction
pred = logits.argmax(1)
# Compute loss
# Note that you should only compute the losses of the nodes in the training set.
loss = F.cross_entropy(logits[train_mask], labels[train_mask])
# Compute accuracy on training/validation/test
train_acc = (pred[train_mask] == labels[train_mask]).float().mean()
val_acc = (pred[val_mask] == labels[val_mask]).float().mean()
test_acc = (pred[test_mask] == labels[test_mask]).float().mean()
# Save the best validation accuracy and the corresponding test accuracy.
if best_val_acc < val_acc:
best_model = copy.deepcopy(model)
best_val_acc = val_acc
best_test_acc = test_acc
test_idx = test_mask.nonzero(as_tuple=True)[0][(pred[test_mask] == labels[test_mask]).nonzero(as_tuple=True)[0]]
# Backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('Victim model has validation accuracy: {:.2f}, testing accuracy: {:.2f}'.format(best_val_acc.item()*100, best_test_acc.item()*100))
return best_model, test_idx
class APPNP(torch.nn.Module):
# code copied from dgl examples
def __init__(self, in_feats, h_feats, num_classes):
super(APPNP, self).__init__()
self.mlp = torch.nn.Linear(in_feats, num_classes)
self.conv = dgl.nn.APPNPConv(k=3, alpha=0.5)
def forward(self, g, in_feat):
in_feat = self.mlp(in_feat)
h = self.conv(g, in_feat)
return h
def train(self, g, model):
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
best_val_acc = 0
best_test_acc = 0
features = g.ndata['feat']
labels = g.ndata['label']
train_mask = g.ndata['train_mask']
val_mask = g.ndata['val_mask']
test_mask = g.ndata['test_mask']
test_idx = None
best_model = None
for e in range(1000):
# Forward
logits = model(g, features)
# Compute prediction
pred = logits.argmax(1)
# Compute loss
# Note that you should only compute the losses of the nodes in the training set.
loss = F.cross_entropy(logits[train_mask], labels[train_mask])
# Compute accuracy on training/validation/test
train_acc = (pred[train_mask] == labels[train_mask]).float().mean()
val_acc = (pred[val_mask] == labels[val_mask]).float().mean()
test_acc = (pred[test_mask] == labels[test_mask]).float().mean()
# Save the best validation accuracy and the corresponding test accuracy.
if best_val_acc < val_acc:
best_model = copy.deepcopy(model)
best_val_acc = val_acc
best_test_acc = test_acc
test_idx = test_mask.nonzero(as_tuple=True)[0][(pred[test_mask] == labels[test_mask]).nonzero(as_tuple=True)[0]]
# Backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('Victim model has validation accuracy: {:.2f}, testing accuracy: {:.2f}'.format(best_val_acc.item()*100, best_test_acc.item()*100))
return best_model, test_idx
class GAT(torch.nn.Module):
def __init__(self,in_size, hid_size, out_size, heads=[4, 4]):
super().__init__()
self.gat_layers = torch.nn.ModuleList()
# two-layer GAT
self.gat_layers.append(dgl.nn.GATConv(in_size, hid_size, heads[0], feat_drop=0., attn_drop=0., activation=F.elu))
self.gat_layers.append(dgl.nn.GATConv(hid_size*heads[0], out_size, heads[1], feat_drop=0., attn_drop=0., activation=None))
def forward(self, g, inputs):
h = inputs
for i, layer in enumerate(self.gat_layers):
h = layer(g, h)
if i == 1: # last layer
h = h.mean(1)
else: # other layer(s)
h = h.flatten(1)
return h
def train(self, g, model):
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
best_val_acc = 0
best_test_acc = 0
features = g.ndata['feat']
labels = g.ndata['label']
train_mask = g.ndata['train_mask']
val_mask = g.ndata['val_mask']
test_mask = g.ndata['test_mask']
test_idx = None
best_model = None
for e in range(1000):
# Forward
logits = model(g, features)
# Compute prediction
pred = logits.argmax(1)
# Compute loss
# Note that you should only compute the losses of the nodes in the training set.
loss = F.cross_entropy(logits[train_mask], labels[train_mask])
# Compute accuracy on training/validation/test
train_acc = (pred[train_mask] == labels[train_mask]).float().mean()
val_acc = (pred[val_mask] == labels[val_mask]).float().mean()
test_acc = (pred[test_mask] == labels[test_mask]).float().mean()
# Save the best validation accuracy and the corresponding test accuracy.
if best_val_acc < val_acc:
best_model = copy.deepcopy(model)
best_val_acc = val_acc
best_test_acc = test_acc
test_idx = test_mask.nonzero(as_tuple=True)[0][(pred[test_mask] == labels[test_mask]).nonzero(as_tuple=True)[0]]
# Backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('Victim model has validation accuracy: {:.2f}, testing accuracy: {:.2f}'.format(best_val_acc.item()*100, best_test_acc.item()*100))
return best_model, test_idx
class BernoulliStraightThrough(torch.distributions.Bernoulli):
r"""
Creates a reparameterizable :class:`OneHotCategorical` distribution based on the straight-
through gradient estimator from [1].
[1] Estimating or Propagating Gradients Through Stochastic Neurons for Conditional Computation
(Bengio et al, 2013)
"""
has_rsample = True
def rsample(self, sample_shape=torch.Size()):
samples = self.sample(sample_shape)
probs = self._param # cached via @lazy_property
return samples + (probs - probs.detach())