-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_inductive_cl.py
327 lines (263 loc) · 13.5 KB
/
buffer_inductive_cl.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
from deeprobust.graph.data import Dataset
import numpy as np
import random
import time
import argparse
import torch
from utils import *
import torch.nn.functional as F
from utils_graphsaint import DataGraphSAINT
import logging
import sys
import datetime
import os
from tensorboardX import SummaryWriter
import deeprobust.graph.utils as utils
from itertools import repeat
from models.gat import GAT
from models.gcn import GCN
from models.sgc import SGC
def main(args):
# random seed setting
random.seed(args.seed_teacher)
np.random.seed(args.seed_teacher)
torch.manual_seed(args.seed_teacher)
torch.cuda.manual_seed(args.seed_teacher)
device = torch.device(args.device)
logging.info('args = {}'.format(args))
data_graphsaint = ['flickr', 'reddit']
if args.dataset in data_graphsaint:
data = DataGraphSAINT(args.dataset)
data_full = data.data_full
else:
data_full = get_dataset(args.dataset)
data = Transd2Ind(data_full)
features, adj, labels = data.feat_train, data.adj_train, data.labels_train
features_sort, adj_sort, labels_sort = data.feat_full, data.adj_full, data.labels_full
adj, features, labels = utils.to_tensor(adj, features, labels, device=device)
adj_sort, features_sort, labels_sort = utils.to_tensor(adj_sort, features_sort, labels_sort, device=device)
feat_val, adj_val, labels_val = data.feat_val, data.adj_val, data.labels_val
adj_val, feat_val, labels_val = utils.to_tensor(adj_val, feat_val, labels_val,device=device)
feat_test, adj_test, labels_test = data.feat_test, data.adj_test, data.labels_test
adj_test, feat_test, labels_test = utils.to_tensor(adj_test, feat_test, labels_test, device=device)
if utils.is_sparse_tensor(adj):
adj_norm = utils.normalize_adj_tensor(adj, sparse=True)
else:
adj_norm = utils.normalize_adj_tensor(adj)
if utils.is_sparse_tensor(adj_sort):
adj_norm_sort = utils.normalize_adj_tensor(adj_sort, sparse=True)
else:
adj_norm_sort = utils.normalize_adj_tensor(adj_sort)
adj = adj_norm
adj_sort = adj_norm_sort
trajectories = []
model_type = args.buffer_model_type
sorted_trainset = sort_training_nodes_in(data, adj, labels)
for it in range(0, args.num_experts):
logging.info(
'======================== {} -th number of experts for {}-model_type=============================='.format(
it, model_type))
model_class = eval(model_type)
model = model_class(nfeat=features.shape[1], nhid=args.teacher_hidden, dropout=args.teacher_dropout,
nlayers=args.teacher_nlayers,
nclass=data.nclass, device=device).to(device)
model.initialize()
model_parameters = list(model.parameters())
if args.optim == 'Adam':
optimizer_model = torch.optim.Adam(model_parameters, lr=args.lr_teacher, weight_decay=args.wd_teacher)
elif args.optim == 'SGD':
optimizer_model = torch.optim.SGD(model_parameters, lr=args.lr_teacher, momentum=args.mom_teacher,
weight_decay=args.wd_teacher)
timestamps = []
timestamps.append([p.detach().cpu() for p in model.parameters()])
best_val_acc = best_test_acc = 0
lr_schedule = [args.teacher_epochs // 2 + 1]
lr = args.lr_teacher
lam = float(args.lam)
T = float(args.T)
scheduler = args.scheduler
for e in range(args.teacher_epochs + 1):
model.train()
optimizer_model.zero_grad()
_, output = model.forward(features, adj)
size = training_scheduler(args.lam, e, T, scheduler)
print(size)
training_subset = sorted_trainset[:int(size * sorted_trainset.shape[0])]
# print(training_subset)
loss_buffer = F.nll_loss(output[training_subset], labels[training_subset])
acc_buffer = utils.accuracy(output, labels)
writer.add_scalar('buffer_train_loss_curve', loss_buffer.item(), e)
writer.add_scalar('buffer_train_acc_curve', acc_buffer.item(), e)
logging.info("Epochs: {} : Full graph train set results: loss= {:.4f}, accuracy= {:.4f} ".format(e,
loss_buffer.item(),
acc_buffer.item()))
loss_buffer.backward()
optimizer_model.step()
if e in lr_schedule and args.decay:
lr = args.lr_teacher*args.decay_factor
logging.info('NOTE! Decaying lr to :{}'.format(lr))
if args.optim == 'SGD':
optimizer_model = torch.optim.SGD(model_parameters, lr=lr, momentum=args.mom_teacher,weight_decay=args.wd_teacher)
elif args.optim == 'Adam':
optimizer_model = torch.optim.Adam(model_parameters, lr=lr,
weight_decay=args.wd_teacher)
optimizer_model.zero_grad()
if e % 10 == 0:
#logging.info("Epochs: {} : Train set training:, loss= {:.4f}".format(e, loss_buffer.item()))
model.eval()
_,output_val = model.predict(feat_val, adj_val)
loss_val = F.nll_loss(output_val, labels_val)
acc_val = utils.accuracy(output_val, labels_val)
writer.add_scalar('val_set_loss_curve', loss_val.item(), e)
writer.add_scalar('val_set_acc_curve', acc_val.item(), e)
# Full graph
_,output_test = model.predict(feat_test, adj_test)
loss_test = F.nll_loss(output_test, labels_test)
acc_test = utils.accuracy(output_test, labels_test)
logging.info('eval_acc = {}'.format(acc_val))
logging.info('test_acc = {}'.format(acc_test))
writer.add_scalar('test_set_loss_curve', loss_test.item(), e)
writer.add_scalar('test_set_acc_curve', acc_test.item(), e)
if acc_val > best_val_acc:
best_val_acc = acc_val
best_test_acc = acc_test
best_it = e
if e % args.param_save_interval == 0 and e>1:
timestamps.append([p.detach().cpu() for p in model.parameters()])
p_current = timestamps[-1]
p_0 = timestamps[0]
target_params = torch.cat([p_c.data.reshape(-1) for p_c in p_current], 0)
starting_params = torch.cat([p0.data.reshape(-1) for p0 in p_0], 0)
param_dist1 = torch.nn.functional.mse_loss(starting_params, target_params, reduction="sum")
writer.add_scalar('param_change', param_dist1.item(), e)
logging.info(
'==============================={}-th iter with length of {}-th tsp'.format(e, len(timestamps)))
logging.info("Valid set best results: accuracy= {:.4f}".format(best_val_acc.item()))
logging.info("Test set best results: accuracy= {:.4f} within best iteration = {}".format(best_test_acc.item(),best_it))
trajectories.append(timestamps)
if len(trajectories) == args.traj_save_interval:
n = 0
while os.path.exists(os.path.join(log_dir, "replay_buffer_{}.pt".format(n))):
n += 1
logging.info("Saving {}".format(os.path.join(log_dir, "replay_buffer_{}.pt".format(n))))
torch.save(trajectories, os.path.join(log_dir, "replay_buffer_{}.pt".format(n)))
trajectories = []
class GraphData:
def __init__(self, features, adj, labels, idx_train=None, idx_val=None, idx_test=None):
self.adj = adj
self.features = features
self.labels = labels
self.idx_train = idx_train
self.idx_val = idx_val
self.idx_test = idx_test
from torch_geometric.data import Data
from models.in_memory_dataset import InMemoryDataset
import scipy.sparse as sp
class Dpr2Pyg(InMemoryDataset):
def __init__(self, dpr_data, transform=None, **kwargs):
root = 'data/' # dummy root; does not mean anything
self.dpr_data = dpr_data
super(Dpr2Pyg, self).__init__(root, transform)
pyg_data = self.process()
self.data, self.slices = self.collate([pyg_data])
self.transform = transform
def process____(self):
dpr_data = self.dpr_data
try:
edge_index = torch.LongTensor(dpr_data.adj.nonzero().cpu()).cuda().T
except:
edge_index = torch.LongTensor(dpr_data.adj.nonzero()).cuda()
try:
x = torch.FloatTensor(dpr_data.features.cpu()).float().cuda()
except:
x = torch.FloatTensor(dpr_data.features).float().cuda()
try:
y = torch.LongTensor(dpr_data.labels.cpu()).cuda()
except:
y = dpr_data.labels
data = Data(x=x, edge_index=edge_index, y=y)
data.train_mask = None
data.val_mask = None
data.test_mask = None
return data
def process(self):
dpr_data = self.dpr_data
if type(dpr_data.adj) == torch.Tensor:
adj_selfloop = dpr_data.adj + torch.eye(dpr_data.adj.shape[0]).cuda()
edge_index_selfloop = adj_selfloop.nonzero().T
edge_index = edge_index_selfloop
edge_weight = adj_selfloop[edge_index_selfloop[0], edge_index_selfloop[1]]
else:
adj_selfloop = dpr_data.adj + sp.eye(dpr_data.adj.shape[0])
edge_index = torch.LongTensor(adj_selfloop.nonzero()).cuda()
edge_weight = torch.FloatTensor(adj_selfloop[adj_selfloop.nonzero()]).cuda()
try:
x = torch.FloatTensor(dpr_data.features.cpu()).float().cuda()
except:
x = torch.FloatTensor(dpr_data.features).float().cuda()
try:
y = torch.LongTensor(dpr_data.labels).cuda()
except:
y = dpr_data.labels
data = Data(x=x, edge_index=edge_index, y=y, edge_weight=edge_weight)
data.train_mask = None
data.val_mask = None
data.test_mask = None
return data
def get(self, idx):
data = self.data.__class__()
if hasattr(self.data, '__num_nodes__'):
data.num_nodes = self.data.__num_nodes__[idx]
for key in self.data.keys:
item, slices = self.data[key], self.slices[key]
s = list(repeat(slice(None), item.dim()))
s[self.data.__cat_dim__(key, item)] = slice(slices[idx],
slices[idx + 1])
data[key] = item[s]
return data
@property
def raw_file_names(self):
return ['some_file_1', 'some_file_2', ...]
@property
def processed_file_names(self):
return ['data.pt']
def _download(self):
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--device', type=str, default='cuda:0')
parser.add_argument('--dataset', type=str,default='flickr')
parser.add_argument('--teacher_epochs', type=int, default=1000, help='training epochs')
parser.add_argument('--teacher_nlayers', type=int, default=2)
parser.add_argument('--teacher_hidden', type=int, default=256)
parser.add_argument('--teacher_dropout', type=float, default=0.0)
parser.add_argument('--lr_teacher', type=float, default=0.01, help='initialization for buffer learning rate')
parser.add_argument('--wd_teacher', type=float, default=0)
parser.add_argument('--mom_teacher', type=float, default=0)
parser.add_argument('--seed_teacher', type=int, default=15, help='Random seed.')
parser.add_argument('--num_experts', type=int, default=200, help='training iterations')
parser.add_argument('--param_save_interval', type=int, default=10)
parser.add_argument('--traj_save_interval', type=int, default=10)
parser.add_argument('--save_log', type=str, default='logs', help='path to save logs')
parser.add_argument('--buffer_model_type', type=str, default='GCN', help='Default buffer_model type')
parser.add_argument('--optim', type=str, default='Adam', choices=['Adam', 'SGD'], help='Default buffer_model type')
parser.add_argument('--decay', type=int, default=0, choices=[1, 0], help='whether to decay lr at 1/2 training epochs')
parser.add_argument('--decay_factor', type=float, default=0.1, help='decay factor of lr at 1/2 training epochs')
parser.add_argument('--lam', default=0.70)
parser.add_argument('--T', default=200)
parser.add_argument('--scheduler', default='root')
args = parser.parse_args()
log_dir = './' + args.save_log + '/Buffer/{}-{}'.format(args.dataset,
datetime.datetime.now().strftime("%Y%m%d-%H%M%S-%f"))
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_format = '%(asctime)s %(message)s'
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format=log_format, datefmt='%m/%d %I:%M:%S %p')
fh = logging.FileHandler(os.path.join(log_dir, 'train.log'))
fh.setFormatter(logging.Formatter(log_format))
logging.getLogger().addHandler(fh)
logging.info('This is the log_dir: {}'.format(log_dir))
writer = SummaryWriter(log_dir + '/tbx_log')
main(args)
logging.info(args)
logging.info('Finish!, Log_dir: {}'.format(log_dir))