forked from acbull/pyHGT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_paper_venue.py
326 lines (284 loc) · 13.2 KB
/
train_paper_venue.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
import sys
from data import *
from utils import *
from model import *
from warnings import filterwarnings
filterwarnings("ignore")
import argparse
parser = argparse.ArgumentParser(description='Training GNN on Paper-Venue (Journal) classification task')
'''
Dataset arguments
'''
parser.add_argument('--data_dir', type=str, default='./dataset/oag_output',
help='The address of preprocessed graph.')
parser.add_argument('--model_dir', type=str, default='./model_save',
help='The address for storing the models and optimization results.')
parser.add_argument('--task_name', type=str, default='PV',
help='The name of the stored models and optimization results.')
parser.add_argument('--cuda', type=int, default=0,
help='Avaiable GPU ID')
parser.add_argument('--domain', type=str, default='_CS',
help='CS, Medicion or All: _CS or _Med or (empty)')
'''
Model arguments
'''
parser.add_argument('--conv_name', type=str, default='hgt',
choices=['hgt', 'gcn', 'gat', 'rgcn', 'han', 'hetgnn'],
help='The name of GNN filter. By default is Heterogeneous Graph Transformer (hgt)')
parser.add_argument('--n_hid', type=int, default=400,
help='Number of hidden dimension')
parser.add_argument('--n_heads', type=int, default=8,
help='Number of attention head')
parser.add_argument('--n_layers', type=int, default=4,
help='Number of GNN layers')
parser.add_argument('--dropout', type=int, default=0.2,
help='Dropout ratio')
parser.add_argument('--sample_depth', type=int, default=6,
help='How many numbers to sample the graph')
parser.add_argument('--sample_width', type=int, default=128,
help='How many nodes to be sampled per layer per type')
'''
Optimization arguments
'''
parser.add_argument('--optimizer', type=str, default='adamw',
choices=['adamw', 'adam', 'sgd', 'adagrad'],
help='optimizer to use.')
parser.add_argument('--data_percentage', type=int, default=1.0,
help='Percentage of training and validation data to use')
parser.add_argument('--n_epoch', type=int, default=200,
help='Number of epoch to run')
parser.add_argument('--n_pool', type=int, default=4,
help='Number of process to sample subgraph')
parser.add_argument('--n_batch', type=int, default=32,
help='Number of batch (sampled graphs) for each epoch')
parser.add_argument('--repeat', type=int, default=2,
help='How many time to train over a singe batch (reuse data)')
parser.add_argument('--batch_size', type=int, default=256,
help='Number of output nodes for training')
parser.add_argument('--clip', type=int, default=0.25,
help='Gradient Norm Clipping')
args = parser.parse_args()
if args.cuda != -1:
device = torch.device("cuda:" + str(args.cuda))
else:
device = torch.device("cpu")
graph = dill.load(open(os.path.join(args.data_dir, 'graph%s.pk' % args.domain), 'rb'))
train_range = {t: True for t in graph.times if t != None and t < 2015}
valid_range = {t: True for t in graph.times if t != None and t >= 2015 and t <= 2016}
test_range = {t: True for t in graph.times if t != None and t > 2016}
types = graph.get_types()
'''
cand_list stores all the Journal, which is the classification domain.
'''
cand_list = list(graph.edge_list['venue']['paper']['PV_Journal'].keys())
'''
Use CrossEntropy (log-softmax + NLL) here, since each paper can be associated with one venue.
'''
criterion = nn.NLLLoss()
def node_classification_sample(seed, pairs, time_range, batch_size):
'''
sub-graph sampling and label preparation for node classification:
(1) Sample batch_size number of output nodes (papers)
'''
np.random.seed(seed)
target_ids = np.random.choice(list(pairs.keys()), batch_size, replace = False)
target_info = []
'''
(2) Get all the source_nodes (Journal) associated with these output nodes.
Collect their information and time as seed nodes for sampling sub-graph.
'''
for target_id in target_ids:
_, _time = pairs[target_id]
target_info += [[target_id, _time]]
'''
(3) Based on the seed nodes, sample a subgraph with 'sampled_depth' and 'sampled_number'
'''
feature, times, edge_list, _, _ = sample_subgraph(graph, time_range, \
inp = {'paper': np.array(target_info)}, \
sampled_depth = args.sample_depth, sampled_number = args.sample_width)
'''
(4) Mask out the edge between the output target nodes (paper) with output source nodes (Journal)
'''
masked_edge_list = []
for i in edge_list['paper']['venue']['rev_PV_Journal']:
if i[0] >= batch_size:
masked_edge_list += [i]
edge_list['paper']['venue']['rev_PV_Journal'] = masked_edge_list
masked_edge_list = []
for i in edge_list['venue']['paper']['PV_Journal']:
if i[1] >= batch_size:
masked_edge_list += [i]
edge_list['venue']['paper']['PV_Journal'] = masked_edge_list
'''
(5) Transform the subgraph into torch Tensor (edge_index is in format of pytorch_geometric)
'''
node_feature, node_type, edge_time, edge_index, edge_type, node_dict, edge_dict = \
to_torch(feature, times, edge_list, graph)
'''
(6) Prepare the labels for each output target node (paper), and their index in sampled graph.
(node_dict[type][0] stores the start index of a specific type of nodes)
'''
ylabel = torch.zeros(batch_size, dtype = torch.long)
for x_id, target_id in enumerate(target_ids):
ylabel[x_id] = cand_list.index(pairs[target_id][0])
x_ids = np.arange(batch_size) + node_dict['paper'][0]
return node_feature, node_type, edge_time, edge_index, edge_type, x_ids, ylabel
def prepare_data(pool):
'''
Sampled and prepare training and validation data using multi-process parallization.
'''
jobs = []
for batch_id in np.arange(args.n_batch):
p = pool.apply_async(node_classification_sample, args=(randint(), \
sel_train_pairs, train_range, args.batch_size))
jobs.append(p)
p = pool.apply_async(node_classification_sample, args=(randint(), \
sel_valid_pairs, valid_range, args.batch_size))
jobs.append(p)
return jobs
train_pairs = {}
valid_pairs = {}
test_pairs = {}
'''
Prepare all the souce nodes (Journal) associated with each target node (paper) as dict
'''
for target_id in graph.edge_list['paper']['venue']['rev_PV_Journal']:
for source_id in graph.edge_list['paper']['venue']['rev_PV_Journal'][target_id]:
_time = graph.edge_list['paper']['venue']['rev_PV_Journal'][target_id][source_id]
if _time in train_range:
if target_id not in train_pairs:
train_pairs[target_id] = [source_id, _time]
elif _time in valid_range:
if target_id not in valid_pairs:
valid_pairs[target_id] = [source_id, _time]
else:
if target_id not in test_pairs:
test_pairs[target_id] = [source_id, _time]
np.random.seed(43)
'''
Only train and valid with a certain percentage of data, if necessary.
'''
sel_train_pairs = {p : train_pairs[p] for p in np.random.choice(list(train_pairs.keys()), int(len(train_pairs) * args.data_percentage), replace = False)}
sel_valid_pairs = {p : valid_pairs[p] for p in np.random.choice(list(valid_pairs.keys()), int(len(valid_pairs) * args.data_percentage), replace = False)}
'''
Initialize GNN (model is specified by conv_name) and Classifier
'''
gnn = GNN(conv_name = args.conv_name, in_dim = len(graph.node_feature['paper']['emb'].values[0]) + 401, \
n_hid = args.n_hid, n_heads = args.n_heads, n_layers = args.n_layers, dropout = args.dropout,\
num_types = len(graph.get_types()), num_relations = len(graph.get_meta_graph()) + 1).to(device)
classifier = Classifier(args.n_hid, len(cand_list)).to(device)
model = nn.Sequential(gnn, classifier)
if args.optimizer == 'adamw':
optimizer = torch.optim.AdamW(model.parameters())
elif args.optimizer == 'adam':
optimizer = torch.optim.Adam(model.parameters())
elif args.optimizer == 'sgd':
optimizer = torch.optim.SGD(model.parameters(), lr = 0.1)
elif args.optimizer == 'adagrad':
optimizer = torch.optim.Adagrad(model.parameters())
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, 1000, eta_min=1e-6)
stats = []
res = []
best_val = 0
train_step = 1500
pool = mp.Pool(args.n_pool)
st = time.time()
jobs = prepare_data(pool)
for epoch in np.arange(args.n_epoch) + 1:
'''
Prepare Training and Validation Data
'''
train_data = [job.get() for job in jobs[:-1]]
valid_data = jobs[-1].get()
pool.close()
pool.join()
'''
After the data is collected, close the pool and then reopen it.
'''
pool = mp.Pool(args.n_pool)
jobs = prepare_data(pool)
et = time.time()
print('Data Preparation: %.1fs' % (et - st))
'''
Train (time < 2015)
'''
model.train()
train_losses = []
torch.cuda.empty_cache()
for _ in range(args.repeat):
for node_feature, node_type, edge_time, edge_index, edge_type, x_ids, ylabel in train_data:
node_rep = gnn.forward(node_feature.to(device), node_type.to(device), \
edge_time.to(device), edge_index.to(device), edge_type.to(device))
res = classifier.forward(node_rep[x_ids])
loss = criterion(res, ylabel.to(device))
optimizer.zero_grad()
torch.cuda.empty_cache()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip)
optimizer.step()
train_losses += [loss.cpu().detach().tolist()]
train_step += 1
scheduler.step(train_step)
del res, loss
'''
Valid (2015 <= time <= 2016)
'''
model.eval()
with torch.no_grad():
node_feature, node_type, edge_time, edge_index, edge_type, x_ids, ylabel = valid_data
node_rep = gnn.forward(node_feature.to(device), node_type.to(device), \
edge_time.to(device), edge_index.to(device), edge_type.to(device))
res = classifier.forward(node_rep[x_ids])
loss = criterion(res, ylabel.to(device))
'''
Calculate Valid NDCG. Update the best model based on highest NDCG score.
'''
valid_res = []
for ai, bi in zip(ylabel, res.argsort(descending = True)):
valid_res += [(bi == ai).int().tolist()]
valid_ndcg = np.average([ndcg_at_k(resi, len(resi)) for resi in valid_res])
if valid_ndcg > best_val:
best_val = valid_ndcg
torch.save(model, os.path.join(args.model_dir, args.task_name + '_' + args.conv_name))
print('UPDATE!!!')
st = time.time()
print(("Epoch: %d (%.1fs) LR: %.5f Train Loss: %.2f Valid Loss: %.2f Valid NDCG: %.4f") % \
(epoch, (st-et), optimizer.param_groups[0]['lr'], np.average(train_losses), \
loss.cpu().detach().tolist(), valid_ndcg))
stats += [[np.average(train_losses), loss.cpu().detach().tolist()]]
del res, loss
del train_data, valid_data
'''
Evaluate the trained model via test set (time > 2016)
'''
with torch.no_grad():
test_res = []
for _ in range(10):
node_feature, node_type, edge_time, edge_index, edge_type, x_ids, ylabel = \
node_classification_sample(randint(), test_pairs, test_range, args.batch_size)
paper_rep = gnn.forward(node_feature.to(device), node_type.to(device), \
edge_time.to(device), edge_index.to(device), edge_type.to(device))[x_ids]
res = classifier.forward(paper_rep)
for ai, bi in zip(ylabel, res.argsort(descending = True)):
test_res += [(bi == ai).int().tolist()]
test_ndcg = [ndcg_at_k(resi, len(resi)) for resi in test_res]
print('Last Test NDCG: %.4f' % np.average(test_ndcg))
test_mrr = mean_reciprocal_rank(test_res)
print('Last Test MRR: %.4f' % np.average(test_mrr))
best_model = torch.load(os.path.join(args.model_dir, args.task_name + '_' + args.conv_name))
best_model.eval()
gnn, classifier = best_model
with torch.no_grad():
test_res = []
for _ in range(10):
node_feature, node_type, edge_time, edge_index, edge_type, x_ids, ylabel = \
node_classification_sample(randint(), test_pairs, test_range, args.batch_size)
paper_rep = gnn.forward(node_feature.to(device), node_type.to(device), \
edge_time.to(device), edge_index.to(device), edge_type.to(device))[x_ids]
res = classifier.forward(paper_rep)
for ai, bi in zip(ylabel, res.argsort(descending = True)):
test_res += [(bi == ai).int().tolist()]
test_ndcg = [ndcg_at_k(resi, len(resi)) for resi in test_res]
print('Best Test NDCG: %.4f' % np.average(test_ndcg))
test_mrr = mean_reciprocal_rank(test_res)
print('Best Test MRR: %.4f' % np.average(test_mrr))