-
Notifications
You must be signed in to change notification settings - Fork 0
/
feat_init.py
314 lines (229 loc) · 9.66 KB
/
feat_init.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
import os
import json
import argparse
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from copy import deepcopy
from sklearn.metrics import accuracy_score
from dataset import get_dataset, Transd2Ind, DataGraphSAINT
from utils import seed_everything
class MLP(nn.Module):
def __init__(
self,
num_features,
num_classes,
hidden_dim,
dropout):
super(MLP, self).__init__()
self.dropout = dropout
self.layers = nn.ModuleList([nn.Linear(num_features, hidden_dim), nn.Linear(hidden_dim, num_classes)])
self.reset_parameter()
def reset_parameter(self):
for lin in self.layers:
nn.init.xavier_uniform_(lin.weight.data)
if lin.bias is not None:
lin.bias.data.zero_()
def forward(self, x):
x = F.dropout(x, self.dropout, training=self.training)
for ix, layer in enumerate(self.layers):
x = layer(x)
if ix != len(self.layers) - 1:
x = F.relu(x)
x = F.dropout(x, self.dropout, training=self.training)
return F.log_softmax(x, dim=1)
class GraphAgent:
def __init__(self, args, data):
self.args = args
self.data = data
self.n_syn = int(len(data.idx_train) * args.reduction_rate)
print(self.n_syn)
self.d = (data.x_train).shape[1]
self.num_classes = data.num_classes
self.x_syn = nn.Parameter(torch.FloatTensor(self.n_syn, self.d).cuda())
self.y_syn = torch.LongTensor(self.generate_labels_syn(data.y_full[data.idx_train], args.reduction_rate)).cuda()
self.reset_parameters()
def reset_parameters(self):
nn.init.xavier_uniform_(self.x_syn.data)
def train(self):
args = self.args
data = self.data
optimizer_feat = torch.optim.Adam(
[self.x_syn], lr=args.lr_feat, weight_decay=args.wd_feat
)
model = self.mlp_trainer(args, data, verbose=False)
model.train()
for i in range(args.epoch):
output = model(self.x_syn)
loss = F.nll_loss(output, self.y_syn)
optimizer_feat.zero_grad()
loss.backward()
optimizer_feat.step()
x_syn, y_syn = self.x_syn.detach(), self.y_syn
dir = f"./initial_feat/{args.dataset}"
if not os.path.isdir(dir):
os.makedirs(dir)
torch.save(
x_syn, f"{dir}/x_init_{args.reduction_rate}_{args.expID}.pt",
)
acc = self.test_with_val(
x_syn,
y_syn
)
return acc
def test_with_val(self, x_syn, y_syn):
args = self.args
data = self.data
x_full = data.x_full
y_full = data.y_full
idx_train = data.idx_train
idx_val = data.idx_val
idx_test = data.idx_test
model = MLP(num_features=self.d, num_classes=self.num_classes, hidden_dim=args.hidden_dim, dropout=args.dropout).cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
best_acc_val = 0
y_train = (y_full[idx_train]).cpu().numpy()
y_val = (y_full[idx_val]).cpu().numpy()
y_test = (y_full[idx_test]).cpu().numpy()
epochs = 2000
lr = args.lr
for i in range(epochs):
if i == epochs // 2 and i > 0:
lr = lr * 0.1
optimizer = torch.optim.Adam(
model.parameters(), lr=args.lr, weight_decay=args.weight_decay
)
model.train()
optimizer.zero_grad()
output = model.forward(x_syn)
loss_train = F.nll_loss(output, y_syn)
loss_train.backward()
optimizer.step()
with torch.no_grad():
model.eval()
output = model.forward(data.x_val)
loss_val = F.nll_loss(output, y_full[idx_val])
pred = output.max(1)[1]
pred = pred.cpu().numpy()
acc_val = accuracy_score(y_val, pred)
if acc_val > best_acc_val:
best_acc_val = acc_val
weights = deepcopy(model.state_dict())
model.load_state_dict(weights)
model.eval()
output = model.forward(x_full)
loss_test = F.nll_loss(output[idx_test], y_full[idx_test])
pred = output.max(1)[1].cpu().numpy()
acc_train = accuracy_score(y_train, pred[idx_train])
acc_val = accuracy_score(y_val, pred[idx_val])
acc_test = accuracy_score(y_test, pred[idx_test])
print(
f"Test set results: test_loss= {loss_test.item():.4f}, train_acc= {acc_train:.4f}, val_acc= {acc_val:.4f}, test_acc= {acc_test:.4f}\n"
)
return acc_test
def mlp_trainer(self, args, data, verbose):
x_full = data.x_full
y_full = data.y_full
idx_train = data.idx_train
idx_val = data.idx_val
idx_test = data.idx_test
model = MLP(num_features=x_full.shape[1], num_classes=data.num_classes, hidden_dim=args.hidden_dim, dropout=args.dropout).cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
best_acc_val = 0
y_train = (y_full[idx_train]).cpu().numpy()
y_val = (y_full[idx_val]).cpu().numpy()
y_test = (y_full[idx_test]).cpu().numpy()
lr = args.lr
for i in range(args.epoch_mlp):
if i == args.epoch_mlp // 2 and i > 0:
lr = lr * 0.1
optimizer = torch.optim.Adam(
model.parameters(), lr=args.lr, weight_decay=args.weight_decay
)
model.train()
optimizer.zero_grad()
output = model.forward(data.x_train)
loss_train = F.nll_loss(output, y_full[idx_train])
loss_train.backward()
optimizer.step()
with torch.no_grad():
model.eval()
output = model.forward(data.x_val)
loss_val = F.nll_loss(output, y_full[idx_val])
pred = output.max(1)[1]
pred = pred.cpu().numpy()
acc_val = accuracy_score(y_val, pred)
if acc_val > best_acc_val:
best_acc_val = acc_val
weights = deepcopy(model.state_dict())
model.load_state_dict(weights)
if verbose:
model.eval()
output = model.forward(x_full)
loss_test = F.nll_loss(output[idx_test], y_full[idx_test])
pred = output.max(1)[1].cpu().numpy()
acc_train = accuracy_score(y_train, pred[idx_train])
acc_val = accuracy_score(y_val, pred[idx_val])
acc_test = accuracy_score(y_test, pred[idx_test])
print(
f"Test set results: test_loss= {loss_test.item():.4f}, train_acc= {acc_train:.4f}, val_acc= {acc_val:.4f}, test_acc= {acc_test:.4f}"
)
return model
def generate_labels_syn(self, train_label, reduction_rate):
from collections import Counter
n = len(train_label)
counter = Counter(train_label.cpu().numpy())
num_class_dict = {}
sorted_counter = sorted(counter.items(), key=lambda x: x[1])
sum_ = 0
y_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 * reduction_rate) - sum_
self.syn_class_indices[c] = [len(y_syn), len(y_syn) + num_class_dict[c]]
y_syn += [c] * num_class_dict[c]
else:
num_class_dict[c] = max(int(num * reduction_rate), 1)
sum_ += num_class_dict[c]
self.syn_class_indices[c] = [len(y_syn), len(y_syn) + num_class_dict[c]]
y_syn += [c] * num_class_dict[c]
self.num_class_dict = num_class_dict
return y_syn
parser = argparse.ArgumentParser()
parser.add_argument("--gpu_id", type=int, default=1, help="gpu id")
parser.add_argument("--seed", type=int, default=15)
parser.add_argument("--config", type=str, default='./config/config_init.json')
parser.add_argument("--runs", type=int, default=10)
parser.add_argument("--expID", type=int, default=0)
parser.add_argument("--dataset", type=str, default="citeseer") # [citeseer, pubmed, ogbn-arxiv, flickr, reddit, squirrel, twitch-gamer]
parser.add_argument("--reduction_rate", type=float, default=0.5)
parser.add_argument("--normalize_features", type=bool, default=True)
parser.add_argument("--hidden_dim", type=int, default=256)
args = parser.parse_args([])
with open(args.config, "r") as config_file:
config = json.load(config_file)
if args.dataset in config:
config = config[args.dataset]
for key, value in config.items():
setattr(args, key, value)
torch.cuda.set_device(args.gpu_id)
seed_everything(args.seed)
data_graphsaint = ['flickr', 'reddit', 'ogbn-arxiv']
if args.dataset in data_graphsaint:
data = DataGraphSAINT(args.dataset)
else:
data_full = get_dataset(args.dataset, args.normalize_features)
data = Transd2Ind(data_full)
data = data.cuda()
accs = []
for ep in range(args.runs):
args.expID = ep
agent = GraphAgent(args=args, data=data)
acc = agent.train()
accs.append(acc)
print(accs)
mean_acc = np.mean(accs)
std_acc = np.std(accs)
print(f"Mean ACC: {mean_acc}\t Std: {std_acc}")