-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
645 lines (520 loc) · 26.1 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
from torch.nn import Linear
import torch.nn.functional as F
from utils import *
from torch import nn
from torch.nn import Parameter
from source import GCNConv
from torch_geometric.nn import GINConv, SAGEConv
from torch.nn.utils import spectral_norm
from scipy.sparse import coo_matrix
from torch_geometric.nn.conv.gcn_conv import gcn_norm
from homophily import edge_homophily, node_homophily, class_homophily, aggregation_homophily, node_homophily_abs
class channel_masker(nn.Module):
def __init__(self, args):
super(channel_masker, self).__init__()
self.weights = nn.Parameter(torch.distributions.Uniform(
0, 1).sample((args.num_features, 2)))
def reset_parameters(self):
self.weights = torch.nn.init.xavier_uniform_(self.weights)
def forward(self):
return self.weights
# def edge_homophily(graph, labels, ignore_negative=False):
# src_node, targ_node = graph[0], graph[1]
# matching = labels[src_node] == labels[targ_node]
# # labeled_mask = (labels[src_node] >= 0) * (labels[targ_node] >= 0)
#
# edge_hom = torch.mean(matching.float())
# # if ignore_negative:
# # edge_hom = np.mean(matching[labeled_mask])
# # else:
# # edge_hom = np.mean(matching)
# return edge_hom
class MLP_discriminator(torch.nn.Module):
def __init__(self, args):
super(MLP_discriminator, self).__init__()
self.args = args
self.lin = Linear(args.hidden, 1)
def reset_parameters(self):
self.lin.reset_parameters()
def forward(self, h, edge_index=None, mask_node=None):
h = self.lin(h)
return torch.sigmoid(h)
class MLP_encoder(torch.nn.Module):
def __init__(self, args):
super(MLP_encoder, self).__init__()
self.args = args
self.lin = Linear(args.num_features, args.hidden)
def reset_parameters(self):
self.lin.reset_parameters()
def clip_parameters(self, channel_weights):
for i in range(self.lin.weight.data.shape[1]):
self.lin.weight.data[:, i].data.clamp_(-self.args.clip_e * channel_weights[i],
self.args.clip_e * channel_weights[i])
# self.lin.weight.data[:,
# channels].clamp_(-self.args.clip_e, self.args.clip_e)
# self.lin.weight.data.clamp_(-self.args.clip_e, self.args.clip_e)
def forward(self, x, edge_index=None, mask_node=None):
h = self.lin(x)
return h
class GCN_encoder_scatter(torch.nn.Module):
def __init__(self, args):
super(GCN_encoder_scatter, self).__init__()
self.args = args
self.lin = Linear(args.num_features, args.hidden, bias=False)
self.bias = Parameter(torch.Tensor(args.hidden))
def clip_parameters(self, channel_weights):
for i in range(self.lin.weight.data.shape[1]):
self.lin.weight.data[:, i].data.clamp_(-self.args.clip_e * channel_weights[i],
self.args.clip_e * channel_weights[i])
# self.lin.weight.data[:,
# channels].clamp_(-self.args.clip_e, self.args.clip_e)
# self.lin.weight.data.clamp_(-self.args.clip_e, self.args.clip_e)
def reset_parameters(self):
self.lin.reset_parameters()
self.bias.data.fill_(0.0)
def forward(self, x, edge_index, adj_norm_sp):
h = self.lin(x)
h = propagate2(h, edge_index) + self.bias
return h
class GCN_2(nn.Module):
def __init__(self, args):
super(GCN_2, self).__init__()
self.body = GCN_Body(args.num_features, args.hidden, args.dropout)
self.fc = nn.Linear(args.hidden, args.hidden)
for m in self.modules():
self.weights_init(m)
def weights_init(self, m):
if isinstance(m, nn.Linear):
torch.nn.init.xavier_uniform_(m.weight.data)
if m.bias is not None:
m.bias.data.fill_(0.0)
def reset_parameters(self):
for m in self.modules():
self.weights_init(m)
def forward(self, x, edge_index, adj):
x = self.body(x, edge_index)
x = self.fc(x)
return x
class GCN_Body(nn.Module):
def __init__(self, nfeat, nhid, dropout):
super(GCN_Body, self).__init__()
self.gc1 = GCNConv(nfeat, nhid)
def forward(self, x, edge_index):
x = self.gc1(x, edge_index)
return x
class GCN_encoder_spmm(torch.nn.Module):
def __init__(self, args):
super(GCN_encoder_spmm, self).__init__()
self.args = args
self.lin = Linear(args.num_features, args.hidden, bias=False)
self.bias = Parameter(torch.Tensor(args.hidden))
def clip_parameters(self, channel_weights):
for i in range(self.lin.weight.data.shape[1]):
self.lin.weight.data[:, i].data.clamp_(-self.args.clip_e * channel_weights[i],
self.args.clip_e * channel_weights[i])
# self.lin.weight.data[:,
# channels].clamp_(-self.args.clip_e, self.args.clip_e)
# self.lin.weight.data.clamp_(-self.args.clip_e, self.args.clip_e)
def reset_parameters(self):
self.lin.reset_parameters()
self.bias.data.fill_(0.0)
def forward(self, x, edge_index, adj_norm_sp):
h = self.lin(x)
h = torch.spmm(adj_norm_sp, h) + self.bias
# h = propagate2(h, edge_index) + self.bias
return h
class GIN_encoder(nn.Module):
def __init__(self, args):
super(GIN_encoder, self).__init__()
self.args = args
self.mlp = nn.Sequential(
nn.Linear(args.num_features, args.hidden),
# nn.ReLU(),
nn.BatchNorm1d(args.hidden),
# nn.Linear(args.hidden, args.hidden),
)
self.conv = GINConv(self.mlp)
def clip_parameters(self, channel_weights):
for i in range(self.mlp[0].weight.data.shape[1]):
self.mlp[0].weight.data[:, i].data.clamp_(-self.args.clip_e * channel_weights[i],
self.args.clip_e * channel_weights[i])
# self.mlp[0].weight.data[:,
# channels].clamp_(-self.args.clip_e, self.args.clip_e)
# self.mlp[0].weight.data.clamp_(-self.args.clip_e, self.args.clip_e)
# for p in self.conv.parameters():
# p.data.clamp_(-self.args.clip_e, self.args.clip_e)
def reset_parameters(self):
self.conv.reset_parameters()
def forward(self, x, edge_index, adj_norm_sp):
h = self.conv(x, edge_index)
return h
class SAGE_encoder(nn.Module):
def __init__(self, args):
super(SAGE_encoder, self).__init__()
self.args = args
self.conv1 = SAGEConv(args.num_features, args.hidden, normalize=True)
self.conv1.aggr = 'mean'
self.transition = nn.Sequential(
nn.ReLU(),
nn.BatchNorm1d(args.hidden),
nn.Dropout(p=args.dropout)
)
self.conv2 = SAGEConv(args.hidden, args.hidden, normalize=True)
self.conv2.aggr = 'mean'
def reset_parameters(self):
self.conv1.reset_parameters()
self.conv2.reset_parameters()
def clip_parameters(self, channel_weights):
for i in range(self.conv1.lin_l.weight.data.shape[1]):
self.conv1.lin_l.weight.data[:, i].data.clamp_(-self.args.clip_e * channel_weights[i],
self.args.clip_e * channel_weights[i])
for i in range(self.conv1.lin_r.weight.data.shape[1]):
self.conv1.lin_r.weight.data[:, i].data.clamp_(-self.args.clip_e * channel_weights[i],
self.args.clip_e * channel_weights[i])
# for p in self.conv1.parameters():
# p.data.clamp_(-self.args.clip_e, self.args.clip_e)
# for p in self.conv2.parameters():
# p.data.clamp_(-self.args.clip_e, self.args.clip_e)
def forward(self, x, edge_index, adj_norm_sp):
x = self.conv1(x, edge_index)
x = self.transition(x)
h = self.conv2(x, edge_index)
return h
class MLP_classifier(torch.nn.Module):
def __init__(self, args):
super(MLP_classifier, self).__init__()
self.args = args
self.lin = Linear(args.hidden, args.num_classes)
def clip_parameters(self):
for p in self.lin.parameters():
p.data.clamp_(-self.args.clip_c, self.args.clip_c)
def reset_parameters(self):
self.lin.reset_parameters()
def forward(self, h, edge_index=None):
h = self.lin(h)
return h
class Graph_Editer(nn.Module):
def __init__(self, n, a, device):
super(Graph_Editer, self).__init__()
self.B = nn.Parameter(torch.FloatTensor(n, n))
self.transFeature = nn.Linear(a, a)
# self.transEdge = nn.Linear(a, a)
self.device = device
self.seed = 13
def reset_parameters(self):
self.transFeature.reset_parameters()
def modify_structure(self, edge_index, A2, sens, nodes_num, drop=0.5, add=0.05):
# in_hom = edge_homophily(edge_index, sens, ignore_negative=False)
src_node, targ_node = edge_index[0], edge_index[1]
matching = sens[src_node] == sens[targ_node]
# 去掉异配边
yipei = torch.where(matching == False)[0]
drop_index = torch.LongTensor(random.sample(range(yipei.shape[0]), int(yipei.shape[0] * drop))).to(edge_index.device)
yipei_drop = torch.index_select(yipei, 0, drop_index)
keep_indices = torch.ones(src_node.shape, dtype=torch.bool)
keep_indices[yipei_drop] = False
n_src_node = src_node[keep_indices]
n_targ_node = targ_node[keep_indices]
src_node2, targ_node2 = A2.indices()[0], A2.indices()[1]
matching2 = sens[src_node2] == sens[targ_node2]
matching3 = src_node2 == targ_node2
tongpei = torch.where(torch.logical_and(matching2 == True, matching3 == False) == True)[0]
add_index = torch.LongTensor(random.sample(range(tongpei.shape[0]), int(yipei_drop.shape[0]))).to(edge_index.device)
tongpei_add = torch.index_select(tongpei, 0, add_index)
keep_indices = torch.zeros(src_node2.shape, dtype=torch.bool)
keep_indices[tongpei_add] = True
a_src_node = src_node2[keep_indices]
a_targ_node = targ_node2[keep_indices]
m_src_node = torch.cat((a_src_node, n_src_node))
m_targ_node = torch.cat((a_targ_node, n_targ_node))
n_edge_index = torch.cat((m_src_node.unsqueeze(0), m_targ_node.unsqueeze(0)), dim=0)
# edge_index = remove_duplicates(edge_index)
eweight = torch.ones(n_edge_index.shape[1]).to(edge_index.device)
n_adj = torch.sparse_coo_tensor(n_edge_index, eweight, [nodes_num, nodes_num])
n_adj_dense = n_adj.to_dense()
sparse_n_adj = torch.sparse_coo_tensor(n_adj_dense.nonzero().T, n_adj_dense[n_adj_dense != 0],
n_adj_dense.size())
n_edge_index = sparse_n_adj.coalesce().indices()
return n_edge_index
def modify_structure1(self, edge_index, adj, A2, sens, nodes_num, drop=0.8, add=0.3):
random.seed(self.seed)
src_node, targ_node = edge_index[0], edge_index[1]
matching = sens[src_node] == sens[targ_node]
adj = adj.to_dense()
# in_hom = torch.mean(matching.float())
# edge_hom = edge_homophily(adj, sens)
# node_hom = node_homophily(adj, sens)
# class_hom = class_homophily(adj, sens)
# #agg_hom = aggregation_homophily(features, adj, sens)
# print("=======drop:{}========".format(drop))
# print("in_hom:{}".format(in_hom))
# print("edge_hom:{}".format(edge_hom))
# print("node_hom:{}".format(node_hom))
# print("class_hom:{}".format(class_hom))
# print("agg_hom:{}".format(agg_hom))
# 去掉异配边
yipei = torch.where(matching == False)[0]
drop_index = torch.LongTensor(random.sample(range(yipei.shape[0]), int(yipei.shape[0] * drop))).to(edge_index.device)
yipei_drop = torch.index_select(yipei, 0, drop_index)
keep_indices = torch.ones(src_node.shape, dtype=torch.bool)
keep_indices[yipei_drop] = False
n_src_node = src_node[keep_indices]
n_targ_node = targ_node[keep_indices]
# 加同配
src_node2, targ_node2 = A2.indices()[0], A2.indices()[1]
matching2 = sens[src_node2] == sens[targ_node2]
matching3 = src_node2 == targ_node2
tongpei = torch.where(torch.logical_and(matching2 == True, matching3 == False) == True)[0]
add_index = torch.LongTensor(random.sample(range(tongpei.shape[0]), int(yipei.shape[0] * drop))).to(edge_index.device)
tongpei_add = torch.index_select(tongpei, 0, add_index)
keep_indices = torch.zeros(src_node2.shape, dtype=torch.bool)
keep_indices[tongpei_add] = True
a_src_node = src_node2[keep_indices]
a_targ_node = targ_node2[keep_indices]
m_src_node = torch.cat((a_src_node, n_src_node))
m_targ_node = torch.cat((a_targ_node, n_targ_node))
# matching = sens[m_src_node] == sens[m_targ_node]
# out_hom = torch.mean(matching.float())
n_edge_index = torch.cat((m_src_node.unsqueeze(0), m_targ_node.unsqueeze(0)), dim=0)
# edge_index = remove_duplicates(edge_index)
eweight = torch.ones(n_edge_index.shape[1]).to(edge_index.device)
n_adj = torch.sparse_coo_tensor(n_edge_index, eweight, [nodes_num, nodes_num])
n_adj_dense = n_adj.to_dense()
sparse_n_adj = torch.sparse_coo_tensor(n_adj_dense.nonzero().T, n_adj_dense[n_adj_dense != 0],
n_adj_dense.size())
n_edge_index = sparse_n_adj.coalesce().indices()
src_node, targ_node = n_edge_index[0], n_edge_index[1]
matching = sens[src_node] == sens[targ_node]
# in_hom = torch.mean(matching.float())
# edge_hom = edge_homophily(n_adj_dense, sens)
# node_hom = node_homophily(n_adj_dense, sens)
# node_hom_abs = node_homophily_abs(n_adj_dense, sens)
# class_hom = class_homophily(n_adj_dense, sens)
# # agg_hom = aggregation_homophily(features, n_adj_dense, sens)
# print("=====after======")
# print("in_hom:{}".format(in_hom))
# print("edge_hom:{}".format(edge_hom))
# print("node_hom:{}".format(node_hom))
# print("node_hom_abs:{}".format(node_hom_abs))
# print("class_hom:{}".format(class_hom))
# print("agg_hom:{}".format(agg_hom))
# print(out_hom.item())
return n_edge_index
def modify_structure2(self, edge_index, adj, A2, sens, nodes_num, drop=0.6, add=0.3):
random.seed(self.seed)
src_node, targ_node = edge_index[0], edge_index[1]
matching = sens[src_node] == sens[targ_node]
adj = adj.to_dense()
# in_hom = torch.mean(matching.float())
# edge_hom = edge_homophily(adj, sens)
# node_hom = node_homophily(adj, sens)
# # node_hom_abs = node_homophily_abs(adj, sens)
# class_hom = class_homophily(adj, sens)
# # agg_hom = aggregation_homophily(features, adj, sens)
# print("=======drop:{}========".format(drop))
# print("in_hom:{}".format(in_hom))
# print("edge_hom:{}".format(edge_hom))
# print("node_hom:{}".format(node_hom))
# # print("node_hom_abs:{}".format(node_hom_abs))
# print("class_hom:{}".format(class_hom))
# print("agg_hom:{}".format(agg_hom))
# 去掉同配边
yipei = torch.where(matching == True)[0]
random.shuffle(yipei)
drop_index = torch.LongTensor(random.sample(range(yipei.shape[0]), int(yipei.shape[0] * drop))).to(edge_index.device)
yipei_drop = torch.index_select(yipei, 0, drop_index)
keep_indices = torch.ones(src_node.shape, dtype=torch.bool)
keep_indices[yipei_drop] = False
n_src_node = src_node[keep_indices]
n_targ_node = targ_node[keep_indices]
# 加异配
src_node2, targ_node2 = A2.indices()[0], A2.indices()[1]
matching2 = sens[src_node2] != sens[targ_node2]
matching3 = src_node2 == targ_node2
tongpei = torch.where(torch.logical_and(matching2 == True, matching3 == False) == True)[0]
random.shuffle(tongpei)
add_index = torch.LongTensor(random.sample(range(tongpei.shape[0]), int(yipei.shape[0] * drop))).to(edge_index.device)
tongpei_add = torch.index_select(tongpei, 0, add_index)
keep_indices = torch.zeros(src_node2.shape, dtype=torch.bool)
keep_indices[tongpei_add] = True
a_src_node = src_node2[keep_indices]
a_targ_node = targ_node2[keep_indices]
m_src_node = torch.cat((a_src_node, n_src_node))
m_targ_node = torch.cat((a_targ_node, n_targ_node))
matching = sens[m_src_node] == sens[m_targ_node]
out_hom = torch.mean(matching.float())
# print(in_hom.item())
# print(out_hom.item())
n_edge_index = torch.cat((m_src_node.unsqueeze(0), m_targ_node.unsqueeze(0)), dim=0)
# edge_index = remove_duplicates(edge_index)
eweight = torch.ones(n_edge_index.shape[1]).to(edge_index.device)
n_adj = torch.sparse_coo_tensor(n_edge_index, eweight, [nodes_num, nodes_num])
n_adj_dense = n_adj.to_dense()
sparse_n_adj = torch.sparse_coo_tensor(n_adj_dense.nonzero().T, n_adj_dense[n_adj_dense != 0],
n_adj_dense.size())
n_edge_index = sparse_n_adj.coalesce().indices()
src_node, targ_node = n_edge_index[0], n_edge_index[1]
matching = sens[src_node] == sens[targ_node]
# in_hom = torch.mean(matching.float())
# edge_hom = edge_homophily(n_adj_dense, sens)
# node_hom = node_homophily(n_adj_dense, sens)
# node_hom_abs = node_homophily_abs(n_adj_dense, sens)
# class_hom = class_homophily(n_adj_dense, sens)
# # agg_hom = aggregation_homophily(features, n_adj_dense, sens)
# print("=====after======")
# print("in_hom:{}".format(in_hom))
# print("edge_hom:{}".format(edge_hom))
# print("node_hom:{}".format(node_hom))
# print("node_hom_abs:{}".format(node_hom_abs))
# print("class_hom:{}".format(class_hom))
# # print("agg_hom:{}".format(agg_hom))
return n_edge_index
def forward(self, x):
x1 = x + 0.1 * self.transFeature(x)
return x1
class Graph_Editer2(nn.Module):
def __init__(self, n, a, device):
super(Graph_Editer2, self).__init__()
self.B = nn.Parameter(torch.FloatTensor(n, n))
self.transFeature = nn.Linear(a, a)
# self.transEdge = nn.Linear(a, a)
self.device = device
self.seed = 13
def reset_parameters(self):
self.transFeature.reset_parameters()
def modify_structure1(self, edge_index, features, adj, A2, sens, nodes_num, drop=0.8, add=0.3):
random.seed(self.seed)
src_node, targ_node = edge_index[0], edge_index[1]
matching = sens[src_node] == sens[targ_node]
adj = adj.to_dense()
in_hom = torch.mean(matching.float())
edge_hom = edge_homophily(adj, sens)
node_hom = node_homophily(adj, sens)
class_hom = class_homophily(adj, sens)
agg_hom = aggregation_homophily(features, adj, sens)
print("=======drop:{}========".format(drop))
print("in_hom:{}".format(in_hom))
print("edge_hom:{}".format(edge_hom))
print("node_hom:{}".format(node_hom))
print("class_hom:{}".format(class_hom))
print("agg_hom:{}".format(agg_hom))
# 去掉异配边
yipei = torch.where(matching == False)[0]
drop_index = torch.LongTensor(random.sample(range(yipei.shape[0]), int(yipei.shape[0] * drop))).to(edge_index.device)
yipei_drop = torch.index_select(yipei, 0, drop_index)
keep_indices = torch.ones(src_node.shape, dtype=torch.bool)
keep_indices[yipei_drop] = False
n_src_node = src_node[keep_indices]
n_targ_node = targ_node[keep_indices]
# 加同配
src_node2, targ_node2 = A2.indices()[0], A2.indices()[1]
matching2 = sens[src_node2] == sens[targ_node2]
matching3 = src_node2 == targ_node2
tongpei = torch.where(torch.logical_and(matching2 == True, matching3 == False) == True)[0]
add_index = torch.LongTensor(random.sample(range(tongpei.shape[0]), int(yipei.shape[0] * drop))).to(edge_index.device)
tongpei_add = torch.index_select(tongpei, 0, add_index)
keep_indices = torch.zeros(src_node2.shape, dtype=torch.bool)
keep_indices[tongpei_add] = True
a_src_node = src_node2[keep_indices]
a_targ_node = targ_node2[keep_indices]
m_src_node = torch.cat((a_src_node, n_src_node))
m_targ_node = torch.cat((a_targ_node, n_targ_node))
# matching = sens[m_src_node] == sens[m_targ_node]
# out_hom = torch.mean(matching.float())
n_edge_index = torch.cat((m_src_node.unsqueeze(0), m_targ_node.unsqueeze(0)), dim=0)
# edge_index = remove_duplicates(edge_index)
eweight = torch.ones(n_edge_index.shape[1]).to(edge_index.device)
n_adj = torch.sparse_coo_tensor(n_edge_index, eweight, [nodes_num, nodes_num])
n_adj_dense = n_adj.to_dense()
sparse_n_adj = torch.sparse_coo_tensor(n_adj_dense.nonzero().T, n_adj_dense[n_adj_dense != 0],
n_adj_dense.size())
n_edge_index = sparse_n_adj.coalesce().indices()
src_node, targ_node = n_edge_index[0], n_edge_index[1]
matching = sens[src_node] == sens[targ_node]
in_hom = torch.mean(matching.float())
edge_hom = edge_homophily(n_adj_dense, sens)
node_hom = node_homophily(n_adj_dense, sens)
node_hom_abs = node_homophily_abs(n_adj_dense, sens)
class_hom = class_homophily(n_adj_dense, sens)
agg_hom = aggregation_homophily(features, n_adj_dense, sens)
print("=====after======")
print("in_hom:{}".format(in_hom))
print("edge_hom:{}".format(edge_hom))
print("node_hom:{}".format(node_hom))
print("node_hom_abs:{}".format(node_hom_abs))
print("class_hom:{}".format(class_hom))
print("agg_hom:{}".format(agg_hom))
# print(out_hom.item())
return n_edge_index, in_hom, edge_hom, node_hom, node_hom_abs, class_hom, agg_hom
def modify_structure2(self, edge_index, features, adj, A2, sens, nodes_num, drop=0.6, add=0.3):
random.seed(self.seed)
src_node, targ_node = edge_index[0], edge_index[1]
matching = sens[src_node] == sens[targ_node]
adj = adj.to_dense()
in_hom = torch.mean(matching.float())
edge_hom = edge_homophily(adj, sens)
node_hom = node_homophily(adj, sens)
# node_hom_abs = node_homophily_abs(adj, sens)
class_hom = class_homophily(adj, sens)
agg_hom = aggregation_homophily(features, adj, sens)
print("=======drop:{}========".format(drop))
print("in_hom:{}".format(in_hom))
print("edge_hom:{}".format(edge_hom))
print("node_hom:{}".format(node_hom))
# print("node_hom_abs:{}".format(node_hom_abs))
print("class_hom:{}".format(class_hom))
print("agg_hom:{}".format(agg_hom))
# 去掉同配边
yipei = torch.where(matching == True)[0]
random.shuffle(yipei)
drop_index = torch.LongTensor(random.sample(range(yipei.shape[0]), int(yipei.shape[0] * drop))).to(edge_index.device)
yipei_drop = torch.index_select(yipei, 0, drop_index)
keep_indices = torch.ones(src_node.shape, dtype=torch.bool)
keep_indices[yipei_drop] = False
n_src_node = src_node[keep_indices]
n_targ_node = targ_node[keep_indices]
# 加异配
src_node2, targ_node2 = A2.indices()[0], A2.indices()[1]
matching2 = sens[src_node2] != sens[targ_node2]
matching3 = src_node2 == targ_node2
tongpei = torch.where(torch.logical_and(matching2 == True, matching3 == False) == True)[0]
random.shuffle(tongpei)
add_index = torch.LongTensor(random.sample(range(tongpei.shape[0]), int(yipei.shape[0] * drop))).to(edge_index.device)
tongpei_add = torch.index_select(tongpei, 0, add_index)
keep_indices = torch.zeros(src_node2.shape, dtype=torch.bool)
keep_indices[tongpei_add] = True
a_src_node = src_node2[keep_indices]
a_targ_node = targ_node2[keep_indices]
m_src_node = torch.cat((a_src_node, n_src_node))
m_targ_node = torch.cat((a_targ_node, n_targ_node))
matching = sens[m_src_node] == sens[m_targ_node]
out_hom = torch.mean(matching.float())
# print(in_hom.item())
# print(out_hom.item())
n_edge_index = torch.cat((m_src_node.unsqueeze(0), m_targ_node.unsqueeze(0)), dim=0)
# edge_index = remove_duplicates(edge_index)
eweight = torch.ones(n_edge_index.shape[1]).to(edge_index.device)
n_adj = torch.sparse_coo_tensor(n_edge_index, eweight, [nodes_num, nodes_num])
n_adj_dense = n_adj.to_dense()
sparse_n_adj = torch.sparse_coo_tensor(n_adj_dense.nonzero().T, n_adj_dense[n_adj_dense != 0],
n_adj_dense.size())
n_edge_index = sparse_n_adj.coalesce().indices()
src_node, targ_node = n_edge_index[0], n_edge_index[1]
matching = sens[src_node] == sens[targ_node]
in_hom = torch.mean(matching.float())
edge_hom = edge_homophily(n_adj_dense, sens)
node_hom = node_homophily(n_adj_dense, sens)
node_hom_abs = node_homophily_abs(n_adj_dense, sens)
class_hom = class_homophily(n_adj_dense, sens)
agg_hom = aggregation_homophily(features, n_adj_dense, sens)
print("=====after======")
print("in_hom:{}".format(in_hom))
print("edge_hom:{}".format(edge_hom))
print("node_hom:{}".format(node_hom))
print("node_hom_abs:{}".format(node_hom_abs))
print("class_hom:{}".format(class_hom))
print("agg_hom:{}".format(agg_hom))
return n_edge_index, in_hom, edge_hom, node_hom, node_hom_abs, class_hom, agg_hom
def forward(self, x):
x1 = x + 0.1 * self.transFeature(x)
return x1