-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_influence_values_sample.py
209 lines (188 loc) · 8.29 KB
/
get_influence_values_sample.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
import numpy as np
import os
import time
from tqdm import tqdm
from scipy.sparse import dok_matrix, save_npz, load_npz
from sklearn.preprocessing import normalize
import torch
import torch.nn.functional as F
import torch_geometric.utils
from torch_geometric.nn.models import GCN, GraphSAGE, GAT
from torch_geometric.utils import sort_edge_index
import torch_geometric.transforms as T
from configs import get_arguments
from load_datasets import get_dataset
from attacks.link_attack import Attacker
from attacks.link_teller import LinkTeller
from attacks.link_stealing import LinkStealing
from models import GAT_ad
args = get_arguments()
dataset_name = args.dataset.lower()
epsilon = args.epsilon
if args.defense != 1:
epsilon = None
dataset = get_dataset('./datasets', dataset_name, epsilon=epsilon)
data = dataset[0]
normalize_feat = T.NormalizeFeatures()
data = normalize_feat(data)
print(torch_geometric.utils.is_undirected(data.edge_index))
print(data.num_edges/2)
self_loop = [range(data.num_nodes), range(data.num_nodes)]
self_loop = torch.tensor(self_loop, dtype=torch.long)
print(data.train_mask.sum(), data.val_mask.sum(), data.test_mask.sum())
print(data.train_mask.sum() / data.num_nodes, data.val_mask.sum() / data.num_nodes, data.test_mask.sum() / data.num_nodes)
if args.model.lower() == 'gcn':
gnn = GCN(in_channels=dataset.num_node_features,
hidden_channels=args.hidden_channels,
num_layers=args.num_layers,
out_channels=dataset.num_classes,
dropout=args.dropout,
jk='last')
elif args.model.lower() == 'sage':
gnn = GraphSAGE(in_channels=dataset.num_node_features,
hidden_channels=args.hidden_channels,
num_layers=args.num_layers,
out_channels=dataset.num_classes,
dropout=args.dropout,
jk='last',
aggr='max')
elif args.model.lower() == 'gat':
gnn = GAT(in_channels=dataset.num_node_features,
hidden_channels=args.hidden_channels,
num_layers=args.num_layers,
out_channels=dataset.num_classes,
dropout=args.dropout,
jk='last',
heads=8)
else:
raise NotImplementedError('GNN not implemented!')
model_dir = './src'
if not os.path.exists(model_dir):
os.mkdir(model_dir)
model_name = dataset_name + '_' + args.model.lower() + '_l' + str(args.num_layers)
if args.defense == 1:
model_name += '_ep' + '%.1f' % args.epsilon
elif args.defense == 2:
gnn = GAT_ad(in_channels=dataset.num_node_features,
hidden_channels=args.hidden_channels,
num_layers=args.num_layers,
out_channels=dataset.num_classes,
dropout=args.dropout,
jk='last')
model_name += '_ad' + '%.1f' % args.beta
gnn.load_state_dict(torch.load(os.path.join(model_dir, model_name + '.pt')))
gnn.eval()
out = F.softmax(gnn(data.x, data.edge_index), dim=-1)
pred = out.argmax(dim=-1)
correct = (pred[data.test_mask] == data.y[data.test_mask]).sum()
acc = torch.div(correct / data.test_mask.sum(), 1e-4, rounding_mode='floor') * 1e-4
print(f'Accuracy: {acc:.4f}')
result_dir = os.path.join('influence_values', dataset_name)
x = data.x.clone()
# when node features are not accessible, we randomly set it or pick one that is known
if args.efficient or args.combo or not args.feat:
x[:] = x[2]
rng = np.random.default_rng(0)
run_time_in = []
run_times = []
run_times_combo = []
for s in range(args.sample_num):
print('Sample:', str(s))
sample_dir = os.path.join(result_dir, str(s))
if not os.path.exists(sample_dir):
os.makedirs(sample_dir)
sample_nodes = rng.choice(dataset.data.num_nodes, args.sample_size, replace=False, shuffle=False)
sample_mask = torch.zeros(dataset.data.num_nodes, dtype=torch.bool)
sample_mask[sample_nodes] = 1
data = dataset[0]
data = data.subgraph(sample_mask)
data.edge_index = sort_edge_index(data.edge_index)
print(data.num_edges)
if args.attacker == 0:
attacker = Attacker(gnn, data.edge_index, x[sample_mask], sample=args.sample)
if args.efficient or args.combo:
res_name = os.path.join(sample_dir, 'attacker' + str(args.attacker) + '_' +
model_name + '_e.npz')
else:
feat = '_feat_' if args.feat else '_'
res_name = os.path.join(sample_dir, 'attacker' + str(args.attacker) + feat +
model_name + '.npz')
elif args.attacker == 1:
attacker = LinkTeller(gnn, data.edge_index, data.x)
norm = '_norm_' if args.norm else '_'
res_name = os.path.join(sample_dir, 'attacker' + str(args.attacker) + norm +
model_name + '.npz')
elif args.attacker == 2:
attacker = LinkStealing(gnn, data.edge_index, data.x, args.pos_type)
res_name = os.path.join(sample_dir, 'attacker' + str(args.attacker) + 'pos' + str(args.pos_type) + '_' +
model_name + '.npz')
else:
raise ValueError(f'attacker={args.attacker} error')
time_in = attacker.update_influence_matrix(sample_dir, defense_ep=epsilon)
run_time_in.append(time_in)
targets_all = range(data.num_nodes)
# continue attack
if os.path.exists(res_name) and not args.erase:
adj_values = load_npz(res_name).todok()
attacker.update_influence_value(adj_values)
# run the attack
run_time = 0
qbar = tqdm(targets_all)
for i, target_idx in enumerate(qbar):
qbar.set_description('Node %d' % target_idx)
start_time = time.time()
if args.attacker == 0:
if args.efficient or args.combo:
attacker.get_influence_value_efficient(target_idx)
else:
attacker.get_influence_value(target_idx)
elif args.attacker == 1:
attacker.get_influence_value(target_idx, efficient=args.efficient, norm=args.norm)
elif args.attacker == 2:
attacker.get_influence_value(target_idx)
run_time += time.time() - start_time
if i % 100 == 0:
adj_values = attacker.influence_values.tocoo()
adj_values.eliminate_zeros()
save_npz(res_name, adj_values)
run_times.append(run_time)
run_time = 0
if args.combo and args.attacker == 0:
# save the result from Maui_efficient
adj_values = attacker.influence_values.tocoo()
adj_values.eliminate_zeros()
save_npz(res_name, adj_values)
adj_values_e = load_npz(res_name)
adj_values = normalize(adj_values_e, norm='max', axis=1).tocoo()
adj_values_ = dok_matrix(adj_values.shape, dtype=float)
for i in range(adj_values.data.shape[0]):
if adj_values.data[i] < args.combo_bar:
adj_values_[adj_values.row[i], adj_values.col[i]] = -1e-8
attacker.update_influence_value(dok_matrix(adj_values_))
res_name = os.path.join(sample_dir, 'attacker' + str(args.attacker) + '_' +
model_name + '_c.npz')
qbar = tqdm(targets_all)
for i, target_idx in enumerate(qbar):
qbar.set_description('Combo-Node %d' % target_idx)
start_time = time.time()
attacker.get_influence_value(target_idx)
run_time += time.time() - start_time
if i % 100 == 0:
adj_values = attacker.influence_values
save_npz(res_name, adj_values.tocoo())
attacker.influence_values = normalize(attacker.influence_values, norm='max', axis=1) + normalize(
adj_values_e, norm='max', axis=1)
run_times_combo.append(run_time)
adj_values = attacker.influence_values.tocoo()
adj_values.eliminate_zeros()
save_npz(res_name, adj_values)
if len(run_time_in) > 0 and sum(run_time_in) > 0:
run_time_in = np.array(run_time_in)
print('Runtime_in: {:.1f}±{:.1f}'.format(
run_time_in.mean() // 0.01 * 0.01, run_time_in.std() // 0.01 * 0.01))
run_times = np.array(run_times)
print('Runtime: {:.1f}±{:.1f}'.format(run_times.mean()//0.01*0.01, run_times.std()//0.01*0.01))
if sum(run_times_combo) > 0:
run_times_combo = np.array(run_times_combo)
print('Runtime_combo: {:.1f}±{:.1f}'.format(
run_times_combo.mean() // 0.01 * 0.01, run_times_combo.std() // 0.01 * 0.01))