-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtrainer.py
440 lines (351 loc) · 17.2 KB
/
trainer.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import json
import logging
import numpy as np
import torch
import torch.nn.functional as F
from collections import defaultdict
from collections import deque
from torch import optim
from torch.autograd import Variable
from tqdm import tqdm
from args import read_options
from data_loader import *
from matcher import *
from tensorboardX import SummaryWriter
class Trainer(object):
def __init__(self, arg):
super(Trainer, self).__init__()
for k, v in vars(arg).items(): setattr(self, k, v)
self.meta = not self.no_meta
if self.random_embed:
use_pretrain = False
else:
use_pretrain = True
logging.info('LOADING SYMBOL ID AND SYMBOL EMBEDDING')
if self.test or self.random_embed:
self.load_symbol2id()
use_pretrain = False
else:
# load pretrained embedding
self.load_embed()
self.use_pretrain = use_pretrain
# if self.embed_model == 'RESCAL':
# self.num_ents = len(self.ent2id.keys()) - 1
# self.pad_id_ent = self.num_ents
# self.num_rels = len(self.rel2id.keys()) - 1
# self.pad_id_rel = self.num_rels
# self.matcher = RescalMatcher(self.embed_dim, self.num_ents, self.num_rels, use_pretrain=self.use_pretrain, ent_embed=self.ent_embed, rel_matrices=self.rel_matrices,dropout=self.dropout, attn_layers=self.n_attn, n_head=self.n_head, batch_size=self.batch_size, process_steps=self.process_steps, finetune=self.fine_tune, aggregate=self.aggregate)
# else:
self.num_symbols = len(self.symbol2id.keys()) - 1 # one for 'PAD'
self.pad_id = self.num_symbols
self.matcher = EmbedMatcher(self.embed_dim, self.num_symbols, use_pretrain=self.use_pretrain, embed=self.symbol2vec, dropout=self.dropout, batch_size=self.batch_size, process_steps=self.process_steps, finetune=self.fine_tune, aggregate=self.aggregate)
self.matcher.cuda()
self.batch_nums = 0
if self.test:
self.writer = None
else:
self.writer = SummaryWriter('logs/' + self.prefix)
self.parameters = filter(lambda p: p.requires_grad, self.matcher.parameters())
self.optim = optim.Adam(self.parameters, lr=self.lr, weight_decay=self.weight_decay)
self.scheduler = optim.lr_scheduler.MultiStepLR(self.optim, milestones=[200000], gamma=0.5)
self.ent2id = json.load(open(self.dataset + '/ent2ids'))
self.num_ents = len(self.ent2id.keys())
logging.info('BUILDING CONNECTION MATRIX')
degrees = self.build_connection(max_=self.max_neighbor)
logging.info('LOADING CANDIDATES ENTITIES')
self.rel2candidates = json.load(open(self.dataset + '/rel2candidates.json'))
# load answer dict
self.e1rel_e2 = defaultdict(list)
self.e1rel_e2 = json.load(open(self.dataset + '/e1rel_e2.json'))
def load_symbol2id(self):
# if self.embed_model == 'RESCAL':
# self.rel2id = json.load(open(self.dataset + '/relation2ids'))
# self.ent2id = json.load(open(self.dataset + '/ent2ids'))
# self.rel2id['PAD'] = len(self.rel2id.keys())
# self.ent2id['PAD'] = len(self.ent2id.keys())
# self.ent_embed = None
# self.rel_matrices = None
# return
symbol_id = {}
rel2id = json.load(open(self.dataset + '/relation2ids'))
ent2id = json.load(open(self.dataset + '/ent2ids'))
i = 0
for key in rel2id.keys():
if key not in ['','OOV']:
symbol_id[key] = i
i += 1
for key in ent2id.keys():
if key not in ['', 'OOV']:
symbol_id[key] = i
i += 1
symbol_id['PAD'] = i
self.symbol2id = symbol_id
self.symbol2vec = None
def load_embed(self):
# if self.embed_model == 'RESCAL':
# self.rel2id = json.load(open(self.dataset + '/relation2ids'))
# self.ent2id = json.load(open(self.dataset + '/ent2ids'))
# self.rel2id['PAD'] = len(self.rel2id.keys())
# self.ent2id['PAD'] = len(self.ent2id.keys())
# self.ent_embed = np.loadtxt(self.dataset + '/entity2vec.' + self.embed_model)
# self.rel_matrices = np.loadtxt(self.dataset + '/relation2vec.' + self.embed_model)
# self.ent_embed = np.concatenate((self.ent_embed, np.zeros((1,self.embed_dim))),axis=0)
# self.rel_matrices = np.concatenate((self.rel_matrices, np.zeros((1, self.embed_dim * self.embed_dim))), axis=0)
# return
symbol_id = {}
rel2id = json.load(open(self.dataset + '/relation2ids'))
ent2id = json.load(open(self.dataset + '/ent2ids'))
logging.info('LOADING PRE-TRAINED EMBEDDING')
if self.embed_model in ['DistMult', 'TransE', 'ComplEx', 'RESCAL']:
ent_embed = np.loadtxt(self.dataset + '/entity2vec.' + self.embed_model)
rel_embed = np.loadtxt(self.dataset + '/relation2vec.' + self.embed_model)
if self.embed_model == 'ComplEx':
# normalize the complex embeddings
ent_mean = np.mean(ent_embed, axis=1, keepdims=True)
ent_std = np.std(ent_embed, axis=1, keepdims=True)
rel_mean = np.mean(rel_embed, axis=1, keepdims=True)
rel_std = np.std(rel_embed, axis=1, keepdims=True)
eps = 1e-3
ent_embed = (ent_embed - ent_mean) / (ent_std + eps)
rel_embed = (rel_embed - rel_mean) / (rel_std + eps)
assert ent_embed.shape[0] == len(ent2id.keys())
assert rel_embed.shape[0] == len(rel2id.keys())
i = 0
embeddings = []
for key in rel2id.keys():
if key not in ['','OOV']:
symbol_id[key] = i
i += 1
embeddings.append(list(rel_embed[rel2id[key],:]))
for key in ent2id.keys():
if key not in ['', 'OOV']:
symbol_id[key] = i
i += 1
embeddings.append(list(ent_embed[ent2id[key],:]))
symbol_id['PAD'] = i
embeddings.append(list(np.zeros((rel_embed.shape[1],))))
embeddings = np.array(embeddings)
assert embeddings.shape[0] == len(symbol_id.keys())
self.symbol2id = symbol_id
self.symbol2vec = embeddings
def build_connection(self, max_=100):
# if self.embed_model == 'RESCAL':
# self.connections = np.ones((self.num_ents, max_, 2)).astype(int)
# self.connections[:,:,0] = self.pad_id_rel
# self.connections[:,:,1] = self.pad_id_ent
# self.e1_rele2 = defaultdict(list)
# self.e1_degrees = defaultdict(int)
# with open(self.dataset + '/path_graph') as f:
# lines = f.readlines()
# for line in tqdm(lines):
# e1,rel,e2 = line.rstrip().split()
# self.e1_rele2[e1].append((self.rel2id[rel], self.ent2id[e2]))
# self.e1_rele2[e2].append((self.rel2id[rel+'_inv'], self.ent2id[e1]))
# else:
self.connections = (np.ones((self.num_ents, max_, 2)) * self.pad_id).astype(int)
self.e1_rele2 = defaultdict(list)
self.e1_degrees = defaultdict(int)
with open(self.dataset + '/path_graph') as f:
lines = f.readlines()
for line in tqdm(lines):
e1,rel,e2 = line.rstrip().split()
self.e1_rele2[e1].append((self.symbol2id[rel], self.symbol2id[e2]))
self.e1_rele2[e2].append((self.symbol2id[rel+'_inv'], self.symbol2id[e1]))
degrees = {}
for ent, id_ in self.ent2id.items():
neighbors = self.e1_rele2[ent]
if len(neighbors) > max_:
neighbors = neighbors[:max_]
# degrees.append(len(neighbors))
degrees[ent] = len(neighbors)
self.e1_degrees[id_] = len(neighbors) # add one for self conn
for idx, _ in enumerate(neighbors):
self.connections[id_, idx, 0] = _[0]
self.connections[id_, idx, 1] = _[1]
# json.dump(degrees, open(self.dataset + '/degrees', 'w'))
# assert 1==2
return degrees
def save(self, path=None):
if not path:
path = self.save_path
torch.save(self.matcher.state_dict(), path)
def load(self):
self.matcher.load_state_dict(torch.load(self.save_path))
def get_meta(self, left, right):
left_connections = Variable(torch.LongTensor(np.stack([self.connections[_,:,:] for _ in left], axis=0))).cuda()
left_degrees = Variable(torch.FloatTensor([self.e1_degrees[_] for _ in left])).cuda()
right_connections = Variable(torch.LongTensor(np.stack([self.connections[_,:,:] for _ in right], axis=0))).cuda()
right_degrees = Variable(torch.FloatTensor([self.e1_degrees[_] for _ in right])).cuda()
return (left_connections, left_degrees, right_connections, right_degrees)
def train(self):
logging.info('START TRAINING...')
best_hits10 = 0.0
losses = deque([], self.log_every)
margins = deque([], self.log_every)
# if self.embed_model == 'RESCAL':
# self.symbol2id = self.ent2id
for data in train_generate(self.dataset, self.batch_size, self.train_few, self.symbol2id, self.ent2id, self.e1rel_e2):
support, query, false, support_left, support_right, query_left, query_right, false_left, false_right = data
# TODO more elegant solution
support_meta = self.get_meta(support_left, support_right)
query_meta = self.get_meta(query_left, query_right)
false_meta = self.get_meta(false_left, false_right)
support = Variable(torch.LongTensor(support)).cuda()
query = Variable(torch.LongTensor(query)).cuda()
false = Variable(torch.LongTensor(false)).cuda()
if self.no_meta:
# for ablation
query_scores = self.matcher(query, support)
false_scores = self.matcher(false, support)
else:
query_scores = self.matcher(query, support, query_meta, support_meta)
false_scores = self.matcher(false, support, false_meta, support_meta)
margin_ = query_scores - false_scores
margins.append(margin_.mean().item())
loss = F.relu(self.margin - margin_).mean()
self.writer.add_scalar('MARGIN', np.mean(margins), self.batch_nums)
losses.append(loss.item())
self.optim.zero_grad()
loss.backward()
# nn.utils.clip_grad_norm(self.parameters, self.grad_clip)
self.optim.step()
if self.batch_nums % self.eval_every == 0:
hits10, hits5, mrr = self.eval(meta=self.meta)
self.writer.add_scalar('HITS10', hits10, self.batch_nums)
self.writer.add_scalar('HITS5', hits5, self.batch_nums)
self.writer.add_scalar('MAP', mrr, self.batch_nums)
self.save()
if hits10 > best_hits10:
self.save(self.save_path + '_bestHits10')
best_hits10 = hits10
# if self.batch_nums % (4 * self.eval_every) == 0:
# hits10_, hits5_, mrr_ = self.eval(meta=self.meta, mode='test')
# self.writer.add_scalar('HITS10-test', hits10_, self.batch_nums)
# self.writer.add_scalar('HITS5-test', hits5_, self.batch_nums)
# self.writer.add_scalar('MAP-test', mrr_, self.batch_nums)
if self.batch_nums % self.log_every == 0:
# self.save()
# logging.info('AVG. BATCH_LOSS: {.2f} AT STEP {}'.format(np.mean(losses), self.batch_nums))
self.writer.add_scalar('Avg_batch_loss', np.mean(losses), self.batch_nums)
self.batch_nums += 1
self.scheduler.step()
if self.batch_nums == self.max_batches:
self.save()
break
def eval(self, mode='dev', meta=False):
self.matcher.eval()
symbol2id = self.symbol2id
few = self.few
logging.info('EVALUATING ON %s DATA' % mode.upper())
if mode == 'dev':
test_tasks = json.load(open(self.dataset + '/dev_tasks.json'))
else:
test_tasks = json.load(open(self.dataset + '/test_tasks.json'))
rel2candidates = self.rel2candidates
hits10 = []
hits5 = []
hits1 = []
mrr = []
for query_ in test_tasks.keys():
hits10_ = []
hits5_ = []
hits1_ = []
mrr_ = []
candidates = rel2candidates[query_]
support_triples = test_tasks[query_][:few]
support_pairs = [[symbol2id[triple[0]], symbol2id[triple[2]]] for triple in support_triples]
if meta:
support_left = [self.ent2id[triple[0]] for triple in support_triples]
support_right = [self.ent2id[triple[2]] for triple in support_triples]
support_meta = self.get_meta(support_left, support_right)
support = Variable(torch.LongTensor(support_pairs)).cuda()
for triple in test_tasks[query_][few:]:
true = triple[2]
query_pairs = []
query_pairs.append([symbol2id[triple[0]], symbol2id[triple[2]]])
if meta:
query_left = []
query_right = []
query_left.append(self.ent2id[triple[0]])
query_right.append(self.ent2id[triple[2]])
for ent in candidates:
if (ent not in self.e1rel_e2[triple[0]+triple[1]]) and ent != true:
query_pairs.append([symbol2id[triple[0]], symbol2id[ent]])
if meta:
query_left.append(self.ent2id[triple[0]])
query_right.append(self.ent2id[ent])
query = Variable(torch.LongTensor(query_pairs)).cuda()
if meta:
query_meta = self.get_meta(query_left, query_right)
scores = self.matcher(query, support, query_meta, support_meta)
scores.detach()
scores = scores.data
else:
scores = self.matcher(query, support)
scores.detach()
scores = scores.data
scores = scores.cpu().numpy()
sort = list(np.argsort(scores))[::-1]
rank = sort.index(0) + 1
if rank <= 10:
hits10.append(1.0)
hits10_.append(1.0)
else:
hits10.append(0.0)
hits10_.append(0.0)
if rank <= 5:
hits5.append(1.0)
hits5_.append(1.0)
else:
hits5.append(0.0)
hits5_.append(0.0)
if rank <= 1:
hits1.append(1.0)
hits1_.append(1.0)
else:
hits1.append(0.0)
hits1_.append(0.0)
mrr.append(1.0/rank)
mrr_.append(1.0/rank)
logging.critical('{} Hits10:{:.3f}, Hits5:{:.3f}, Hits1:{:.3f} MRR:{:.3f}'.format(query_, np.mean(hits10_), np.mean(hits5_), np.mean(hits1_), np.mean(mrr_)))
logging.info('Number of candidates: {}, number of text examples {}'.format(len(candidates), len(hits10_)))
# print query_ + ':'
# print 'HITS10: ', np.mean(hits10_)
# print 'HITS5: ', np.mean(hits5_)
# print 'HITS1: ', np.mean(hits1_)
# print 'MAP: ', np.mean(mrr_)
logging.critical('HITS10: {:.3f}'.format(np.mean(hits10)))
logging.critical('HITS5: {:.3f}'.format(np.mean(hits5)))
logging.critical('HITS1: {:.3f}'.format(np.mean(hits1)))
logging.critical('MAP: {:.3f}'.format(np.mean(mrr)))
self.matcher.train()
return np.mean(hits10), np.mean(hits5), np.mean(mrr)
def test_(self):
self.load()
logging.info('Pre-trained model loaded')
self.eval(mode='dev', meta=self.meta)
self.eval(mode='test', meta=self.meta)
if __name__ == '__main__':
args = read_options()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(levelname)s: - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
fh = logging.FileHandler('./logs_/log-{}.txt'.format(args.prefix))
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.addHandler(fh)
# setup random seeds
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(args.seed)
trainer = Trainer(args)
if args.test:
trainer.test_()
else:
trainer.train()
# trainer.eval()