-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester_other_arcs.py
221 lines (195 loc) · 7.06 KB
/
tester_other_arcs.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
import os
import numpy as np
import torch
import torch.nn.functional as F
from utils import normalize_adj_to_sparse_tensor
from sklearn.metrics import accuracy_score
from model.sgc import SGC
from model.gcn import GCN
from model.appnp import APPNP
from model.chebnet import ChebNet
from model.chebnetII import ChebNetII
from model.bernnet import BernNet
from model.gprgnn import GPRGNN
class Evaluator:
def __init__(self, data, args):
self.data = data
self.args = args
n = int(len(data.idx_train) * args.reduction_rate)
self.n_syn = n
self.d = (data.x_full).shape[1]
self.y_syn = torch.LongTensor(self.generate_labels_syn(data)).cuda()
def generate_labels_syn(self, data):
from collections import Counter
y_train = (data.y_train).cpu().numpy()
counter = Counter(y_train)
num_class_dict = {}
n = len(y_train)
sorted_counter = sorted(counter.items(), key=lambda x: x[1])
sum_ = 0
labels_syn = []
self.syn_class_indices = {}
for ix, (c, num) in enumerate(sorted_counter):
if ix == len(sorted_counter) - 1:
num_class_dict[c] = int(n * self.args.reduction_rate) - sum_
self.syn_class_indices[c] = [
len(labels_syn),
len(labels_syn) + num_class_dict[c],
]
labels_syn += [c] * num_class_dict[c]
else:
num_class_dict[c] = max(int(num * self.args.reduction_rate), 1)
sum_ += num_class_dict[c]
self.syn_class_indices[c] = [
len(labels_syn),
len(labels_syn) + num_class_dict[c],
]
labels_syn += [c] * num_class_dict[c]
self.num_class_dict = num_class_dict
return labels_syn
def get_syn_data(self, expID):
args = self.args
dir = f"./saved_ours/{args.dataset}-{args.reduction_rate}"
if not os.path.isdir(dir):
os.makedirs(dir)
eigenvals_syn = torch.load(
f"{dir}/eigenvals_syn_{expID}.pt", map_location='cpu'
)
eigenvecs_syn = torch.load(
f"{dir}/eigenvecs_syn_{expID}.pt", map_location='cpu'
)
x_syn = torch.load(
f"{dir}/feat_{expID}.pt", map_location='cpu'
)
x_syn = x_syn
L_syn = eigenvecs_syn @ torch.diag(eigenvals_syn) @ eigenvecs_syn.T
return x_syn, L_syn
def test(self, test_model, expID, verbose=True):
args = self.args
data = self.data
x_syn, L_syn = self.get_syn_data(expID)
x_syn, L_syn = x_syn.cuda(), L_syn.cuda()
y_syn = self.y_syn
adj_syn = torch.eye(self.n_syn).cuda() - L_syn
print("======= testing %s" % test_model)
if test_model == "SGC":
model = SGC(
num_features=self.d,
num_classes=data.num_classes,
nlayers=args.nlayers,
lr=args.lr,
weight_decay=args.weight_decay,
).cuda()
elif test_model == "GCN":
model = GCN(
num_features=self.d,
num_classes=data.num_classes,
hidden_dim=args.hidden_dim,
nlayers=args.nlayers,
lr=args.lr,
weight_decay=args.weight_decay,
dropout=args.dropout,
).cuda()
elif test_model == "ChebNet":
model = ChebNet(
num_features=self.d,
num_classes=data.num_classes,
hidden_dim=args.hidden_dim,
nlayers=args.nlayers,
k=args.k,
lr=args.lr,
weight_decay=args.weight_decay,
dropout=args.dropout,
).cuda()
elif test_model == "APPNP":
model = APPNP(
num_features=self.d,
num_classes=data.num_classes,
hidden_dim=args.hidden_dim,
alpha=args.alpha,
k=args.k,
lr=args.lr,
weight_decay=args.weight_decay,
dropout=args.dropout,
).cuda()
elif test_model == "ChebNetII":
model = ChebNetII(
num_features=self.d,
num_classes=data.num_classes,
hidden_dim=args.hidden_dim,
k=args.k,
lr=args.lr,
lr_conv=args.lr_conv,
weight_decay=args.weight_decay,
wd_conv=args.wd_conv,
dropout=args.dropout,
dprate=args.dprate
).cuda()
elif test_model == "BernNet":
model = BernNet(
num_features=self.d,
num_classes=data.num_classes,
hidden_dim=args.hidden_dim,
k=args.k,
lr=args.lr,
lr_conv=args.lr_conv,
weight_decay=args.weight_decay,
wd_conv=args.wd_conv,
dropout=args.dropout,
dprate=args.dprate,
).cuda()
elif test_model == "GPRGNN":
model = GPRGNN(
num_features=self.d,
num_classes=data.num_classes,
hidden_dim=args.hidden_dim,
alpha=args.alpha,
k=args.k,
lr=args.lr,
lr_conv=args.lr_conv,
weight_decay=args.weight_decay,
wd_conv=args.wd_conv,
dropout=args.dropout,
dprate=args.dprate,
).cuda()
model.fit_with_val(
x_syn,
y_syn,
adj_syn,
data,
args.epochs,
verbose,
)
model.eval()
y_full = data.y_full
idx_test = data.idx_test
y_test = (y_full[idx_test]).cpu().numpy()
x_real_test = data.x_full
adj_real_test = data.adj_full
adj_real_test = normalize_adj_to_sparse_tensor(adj_real_test)
if test_model == "BernNet":
laplacian_mat, poly_item_test = model.get_poly_item(adj=adj_real_test, num_nodes=x_real_test.shape[0])
output = model.predict(x_real_test, laplacian_mat, poly_item_test)
else:
output = model.predict(x_real_test, adj_real_test)
pred = output.max(1)[1]
pred = pred.cpu().numpy()
loss_test = F.nll_loss(output[idx_test], y_full[idx_test])
acc_test = accuracy_score(y_test, pred[idx_test])
if verbose:
print(
"Test full set results:",
"loss= {:.4f}".format(loss_test.item()),
"accuracy= {:.4f}".format(acc_test.item()),
)
return acc_test
def train(self, verbose=True):
args = self.args
runs = args.runs
test_model = args.test_model
res = []
for ep in range(runs):
expID = ep
res.append(self.test(test_model=test_model, expID=expID, verbose=verbose))
res = np.array(res)
return res