forked from snap-stanford/KGReasoning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
executable file
·691 lines (609 loc) · 37.7 KB
/
models.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#!/usr/bin/python3
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import logging
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from dataloader import TestDataset, TrainDataset, SingledirectionalOneShotIterator
import random
import pickle
import math
import collections
import itertools
import time
from tqdm import tqdm
import os
def Identity(x):
return x
class BoxOffsetIntersection(nn.Module):
def __init__(self, dim):
super(BoxOffsetIntersection, self).__init__()
self.dim = dim
self.layer1 = nn.Linear(self.dim, self.dim)
self.layer2 = nn.Linear(self.dim, self.dim)
nn.init.xavier_uniform_(self.layer1.weight)
nn.init.xavier_uniform_(self.layer2.weight)
def forward(self, embeddings):
layer1_act = F.relu(self.layer1(embeddings))
layer1_mean = torch.mean(layer1_act, dim=0)
gate = torch.sigmoid(self.layer2(layer1_mean))
offset, _ = torch.min(embeddings, dim=0)
return offset * gate
class CenterIntersection(nn.Module):
def __init__(self, dim):
super(CenterIntersection, self).__init__()
self.dim = dim
self.layer1 = nn.Linear(self.dim, self.dim)
self.layer2 = nn.Linear(self.dim, self.dim)
nn.init.xavier_uniform_(self.layer1.weight)
nn.init.xavier_uniform_(self.layer2.weight)
def forward(self, embeddings):
layer1_act = F.relu(self.layer1(embeddings)) # (num_conj, dim)
attention = F.softmax(self.layer2(layer1_act), dim=0) # (num_conj, dim)
embedding = torch.sum(attention * embeddings, dim=0)
return embedding
class BetaIntersection(nn.Module):
def __init__(self, dim):
super(BetaIntersection, self).__init__()
self.dim = dim
self.layer1 = nn.Linear(2 * self.dim, 2 * self.dim)
self.layer2 = nn.Linear(2 * self.dim, self.dim)
nn.init.xavier_uniform_(self.layer1.weight)
nn.init.xavier_uniform_(self.layer2.weight)
def forward(self, alpha_embeddings, beta_embeddings):
all_embeddings = torch.cat([alpha_embeddings, beta_embeddings], dim=-1)
layer1_act = F.relu(self.layer1(all_embeddings)) # (num_conj, batch_size, 2 * dim)
attention = F.softmax(self.layer2(layer1_act), dim=0) # (num_conj, batch_size, dim)
alpha_embedding = torch.sum(attention * alpha_embeddings, dim=0)
beta_embedding = torch.sum(attention * beta_embeddings, dim=0)
return alpha_embedding, beta_embedding
class BetaProjection(nn.Module):
def __init__(self, entity_dim, relation_dim, hidden_dim, projection_regularizer, num_layers):
super(BetaProjection, self).__init__()
self.entity_dim = entity_dim
self.relation_dim = relation_dim
self.hidden_dim = hidden_dim
self.num_layers = num_layers
self.layer1 = nn.Linear(self.entity_dim + self.relation_dim, self.hidden_dim) # 1st layer
self.layer0 = nn.Linear(self.hidden_dim, self.entity_dim) # final layer
for nl in range(2, num_layers + 1):
setattr(self, "layer{}".format(nl), nn.Linear(self.hidden_dim, self.hidden_dim))
for nl in range(num_layers + 1):
nn.init.xavier_uniform_(getattr(self, "layer{}".format(nl)).weight)
self.projection_regularizer = projection_regularizer
def forward(self, e_embedding, r_embedding):
x = torch.cat([e_embedding, r_embedding], dim=-1)
for nl in range(1, self.num_layers + 1):
x = F.relu(getattr(self, "layer{}".format(nl))(x))
x = self.layer0(x)
x = self.projection_regularizer(x)
return x
class Regularizer():
def __init__(self, base_add, min_val, max_val):
self.base_add = base_add
self.min_val = min_val
self.max_val = max_val
def __call__(self, entity_embedding):
return torch.clamp(entity_embedding + self.base_add, self.min_val, self.max_val)
class KGReasoning(nn.Module):
def __init__(self, nentity, nrelation, hidden_dim, gamma,
geo, test_batch_size=1,
box_mode=None, use_cuda=False,
query_name_dict=None, beta_mode=None):
super(KGReasoning, self).__init__()
self.nentity = nentity
self.nrelation = nrelation
self.hidden_dim = hidden_dim
self.epsilon = 2.0
self.geo = geo
self.use_cuda = use_cuda
self.batch_entity_range = torch.arange(nentity).to(torch.float).repeat(test_batch_size, 1).cuda() if self.use_cuda else torch.arange(nentity).to(torch.float).repeat(test_batch_size, 1) # used in test_step
self.query_name_dict = query_name_dict
self.gamma = nn.Parameter(
torch.Tensor([gamma]),
requires_grad=False
)
self.embedding_range = nn.Parameter(
torch.Tensor([(self.gamma.item() + self.epsilon) / hidden_dim]),
requires_grad=False
)
self.entity_dim = hidden_dim
self.relation_dim = hidden_dim
if self.geo == 'box':
self.entity_embedding = nn.Parameter(torch.zeros(nentity, self.entity_dim)) # centor for entities
activation, cen = box_mode
self.cen = cen # hyperparameter that balances the in-box distance and the out-box distance
if activation == 'none':
self.func = Identity
elif activation == 'relu':
self.func = F.relu
elif activation == 'softplus':
self.func = F.softplus
elif self.geo == 'vec':
self.entity_embedding = nn.Parameter(torch.zeros(nentity, self.entity_dim)) # center for entities
elif self.geo == 'beta':
self.entity_embedding = nn.Parameter(torch.zeros(nentity, self.entity_dim * 2)) # alpha and beta
self.entity_regularizer = Regularizer(1, 0.05, 1e9) # make sure the parameters of beta embeddings are positive
self.projection_regularizer = Regularizer(1, 0.05, 1e9) # make sure the parameters of beta embeddings after relation projection are positive
nn.init.uniform_(
tensor=self.entity_embedding,
a=-self.embedding_range.item(),
b=self.embedding_range.item()
)
self.relation_embedding = nn.Parameter(torch.zeros(nrelation, self.relation_dim))
nn.init.uniform_(
tensor=self.relation_embedding,
a=-self.embedding_range.item(),
b=self.embedding_range.item()
)
if self.geo == 'box':
self.offset_embedding = nn.Parameter(torch.zeros(nrelation, self.entity_dim))
nn.init.uniform_(
tensor=self.offset_embedding,
a=0.,
b=self.embedding_range.item()
)
self.center_net = CenterIntersection(self.entity_dim)
self.offset_net = BoxOffsetIntersection(self.entity_dim)
elif self.geo == 'vec':
self.center_net = CenterIntersection(self.entity_dim)
elif self.geo == 'beta':
hidden_dim, num_layers = beta_mode
self.center_net = BetaIntersection(self.entity_dim)
self.projection_net = BetaProjection(self.entity_dim * 2,
self.relation_dim,
hidden_dim,
self.projection_regularizer,
num_layers)
def forward(self, positive_sample, negative_sample, subsampling_weight, batch_queries_dict, batch_idxs_dict):
if self.geo == 'box':
return self.forward_box(positive_sample, negative_sample, subsampling_weight, batch_queries_dict, batch_idxs_dict)
elif self.geo == 'vec':
return self.forward_vec(positive_sample, negative_sample, subsampling_weight, batch_queries_dict, batch_idxs_dict)
elif self.geo == 'beta':
return self.forward_beta(positive_sample, negative_sample, subsampling_weight, batch_queries_dict, batch_idxs_dict)
def embed_query_box(self, queries, query_structure, idx):
'''
Iterative embed a batch of queries with same structure using Query2box
queries: a flattened batch of queries
'''
all_relation_flag = True
for ele in query_structure[-1]: # whether the current query tree has merged to one branch and only need to do relation traversal, e.g., path queries or conjunctive queries after the intersection
if ele not in ['r', 'n']:
all_relation_flag = False
break
if all_relation_flag:
if query_structure[0] == 'e':
embedding = torch.index_select(self.entity_embedding, dim=0, index=queries[:, idx])
if self.use_cuda:
offset_embedding = torch.zeros_like(embedding).cuda()
else:
offset_embedding = torch.zeros_like(embedding)
idx += 1
else:
embedding, offset_embedding, idx = self.embed_query_box(queries, query_structure[0], idx)
for i in range(len(query_structure[-1])):
if query_structure[-1][i] == 'n':
assert False, "box cannot handle queries with negation"
else:
r_embedding = torch.index_select(self.relation_embedding, dim=0, index=queries[:, idx])
r_offset_embedding = torch.index_select(self.offset_embedding, dim=0, index=queries[:, idx])
embedding += r_embedding
offset_embedding += self.func(r_offset_embedding)
idx += 1
else:
embedding_list = []
offset_embedding_list = []
for i in range(len(query_structure)):
embedding, offset_embedding, idx = self.embed_query_box(queries, query_structure[i], idx)
embedding_list.append(embedding)
offset_embedding_list.append(offset_embedding)
embedding = self.center_net(torch.stack(embedding_list))
offset_embedding = self.offset_net(torch.stack(offset_embedding_list))
return embedding, offset_embedding, idx
def embed_query_vec(self, queries, query_structure, idx):
'''
Iterative embed a batch of queries with same structure using GQE
queries: a flattened batch of queries
'''
all_relation_flag = True
for ele in query_structure[-1]: # whether the current query tree has merged to one branch and only need to do relation traversal, e.g., path queries or conjunctive queries after the intersection
if ele not in ['r', 'n']:
all_relation_flag = False
break
if all_relation_flag:
if query_structure[0] == 'e':
embedding = torch.index_select(self.entity_embedding, dim=0, index=queries[:, idx])
idx += 1
else:
embedding, idx = self.embed_query_vec(queries, query_structure[0], idx)
for i in range(len(query_structure[-1])):
if query_structure[-1][i] == 'n':
assert False, "vec cannot handle queries with negation"
else:
r_embedding = torch.index_select(self.relation_embedding, dim=0, index=queries[:, idx])
embedding += r_embedding
idx += 1
else:
embedding_list = []
for i in range(len(query_structure)):
embedding, idx = self.embed_query_vec(queries, query_structure[i], idx)
embedding_list.append(embedding)
embedding = self.center_net(torch.stack(embedding_list))
return embedding, idx
def embed_query_beta(self, queries, query_structure, idx):
'''
Iterative embed a batch of queries with same structure using BetaE
queries: a flattened batch of queries
'''
all_relation_flag = True
for ele in query_structure[-1]: # whether the current query tree has merged to one branch and only need to do relation traversal, e.g., path queries or conjunctive queries after the intersection
if ele not in ['r', 'n']:
all_relation_flag = False
break
if all_relation_flag:
if query_structure[0] == 'e':
embedding = self.entity_regularizer(torch.index_select(self.entity_embedding, dim=0, index=queries[:, idx]))
idx += 1
else:
alpha_embedding, beta_embedding, idx = self.embed_query_beta(queries, query_structure[0], idx)
embedding = torch.cat([alpha_embedding, beta_embedding], dim=-1)
for i in range(len(query_structure[-1])):
if query_structure[-1][i] == 'n':
assert (queries[:, idx] == -2).all()
embedding = 1./embedding
else:
r_embedding = torch.index_select(self.relation_embedding, dim=0, index=queries[:, idx])
embedding = self.projection_net(embedding, r_embedding)
idx += 1
alpha_embedding, beta_embedding = torch.chunk(embedding, 2, dim=-1)
else:
alpha_embedding_list = []
beta_embedding_list = []
for i in range(len(query_structure)):
alpha_embedding, beta_embedding, idx = self.embed_query_beta(queries, query_structure[i], idx)
alpha_embedding_list.append(alpha_embedding)
beta_embedding_list.append(beta_embedding)
alpha_embedding, beta_embedding = self.center_net(torch.stack(alpha_embedding_list), torch.stack(beta_embedding_list))
return alpha_embedding, beta_embedding, idx
def cal_logit_beta(self, entity_embedding, query_dist):
alpha_embedding, beta_embedding = torch.chunk(entity_embedding, 2, dim=-1)
entity_dist = torch.distributions.beta.Beta(alpha_embedding, beta_embedding)
logit = self.gamma - torch.norm(torch.distributions.kl.kl_divergence(entity_dist, query_dist), p=1, dim=-1)
return logit
def forward_beta(self, positive_sample, negative_sample, subsampling_weight, batch_queries_dict, batch_idxs_dict):
all_idxs, all_alpha_embeddings, all_beta_embeddings = [], [], []
all_union_idxs, all_union_alpha_embeddings, all_union_beta_embeddings = [], [], []
for query_structure in batch_queries_dict:
if 'u' in self.query_name_dict[query_structure] and 'DNF' in self.query_name_dict[query_structure]:
alpha_embedding, beta_embedding, _ = \
self.embed_query_beta(self.transform_union_query(batch_queries_dict[query_structure],
query_structure),
self.transform_union_structure(query_structure),
0)
all_union_idxs.extend(batch_idxs_dict[query_structure])
all_union_alpha_embeddings.append(alpha_embedding)
all_union_beta_embeddings.append(beta_embedding)
else:
alpha_embedding, beta_embedding, _ = self.embed_query_beta(batch_queries_dict[query_structure],
query_structure,
0)
all_idxs.extend(batch_idxs_dict[query_structure])
all_alpha_embeddings.append(alpha_embedding)
all_beta_embeddings.append(beta_embedding)
if len(all_alpha_embeddings) > 0:
all_alpha_embeddings = torch.cat(all_alpha_embeddings, dim=0).unsqueeze(1)
all_beta_embeddings = torch.cat(all_beta_embeddings, dim=0).unsqueeze(1)
all_dists = torch.distributions.beta.Beta(all_alpha_embeddings, all_beta_embeddings)
if len(all_union_alpha_embeddings) > 0:
all_union_alpha_embeddings = torch.cat(all_union_alpha_embeddings, dim=0).unsqueeze(1)
all_union_beta_embeddings = torch.cat(all_union_beta_embeddings, dim=0).unsqueeze(1)
all_union_alpha_embeddings = all_union_alpha_embeddings.view(all_union_alpha_embeddings.shape[0]//2, 2, 1, -1)
all_union_beta_embeddings = all_union_beta_embeddings.view(all_union_beta_embeddings.shape[0]//2, 2, 1, -1)
all_union_dists = torch.distributions.beta.Beta(all_union_alpha_embeddings, all_union_beta_embeddings)
if type(subsampling_weight) != type(None):
subsampling_weight = subsampling_weight[all_idxs+all_union_idxs]
if type(positive_sample) != type(None):
if len(all_alpha_embeddings) > 0:
positive_sample_regular = positive_sample[all_idxs] # positive samples for non-union queries in this batch
positive_embedding = self.entity_regularizer(torch.index_select(self.entity_embedding, dim=0, index=positive_sample_regular).unsqueeze(1))
positive_logit = self.cal_logit_beta(positive_embedding, all_dists)
else:
positive_logit = torch.Tensor([]).to(self.entity_embedding.device)
if len(all_union_alpha_embeddings) > 0:
positive_sample_union = positive_sample[all_union_idxs] # positive samples for union queries in this batch
positive_embedding = self.entity_regularizer(torch.index_select(self.entity_embedding, dim=0, index=positive_sample_union).unsqueeze(1).unsqueeze(1))
positive_union_logit = self.cal_logit_beta(positive_embedding, all_union_dists)
positive_union_logit = torch.max(positive_union_logit, dim=1)[0]
else:
positive_union_logit = torch.Tensor([]).to(self.entity_embedding.device)
positive_logit = torch.cat([positive_logit, positive_union_logit], dim=0)
else:
positive_logit = None
if type(negative_sample) != type(None):
if len(all_alpha_embeddings) > 0:
negative_sample_regular = negative_sample[all_idxs]
batch_size, negative_size = negative_sample_regular.shape
negative_embedding = self.entity_regularizer(torch.index_select(self.entity_embedding, dim=0, index=negative_sample_regular.view(-1)).view(batch_size, negative_size, -1))
negative_logit = self.cal_logit_beta(negative_embedding, all_dists)
else:
negative_logit = torch.Tensor([]).to(self.entity_embedding.device)
if len(all_union_alpha_embeddings) > 0:
negative_sample_union = negative_sample[all_union_idxs]
batch_size, negative_size = negative_sample_union.shape
negative_embedding = self.entity_regularizer(torch.index_select(self.entity_embedding, dim=0, index=negative_sample_union.view(-1)).view(batch_size, 1, negative_size, -1))
negative_union_logit = self.cal_logit_beta(negative_embedding, all_union_dists)
negative_union_logit = torch.max(negative_union_logit, dim=1)[0]
else:
negative_union_logit = torch.Tensor([]).to(self.entity_embedding.device)
negative_logit = torch.cat([negative_logit, negative_union_logit], dim=0)
else:
negative_logit = None
return positive_logit, negative_logit, subsampling_weight, all_idxs+all_union_idxs
def transform_union_query(self, queries, query_structure):
'''
transform 2u queries to two 1p queries
transform up queries to two 2p queries
'''
if self.query_name_dict[query_structure] == '2u-DNF':
queries = queries[:, :-1] # remove union -1
elif self.query_name_dict[query_structure] == 'up-DNF':
queries = torch.cat([torch.cat([queries[:, :2], queries[:, 5:6]], dim=1), torch.cat([queries[:, 2:4], queries[:, 5:6]], dim=1)], dim=1)
queries = torch.reshape(queries, [queries.shape[0]*2, -1])
return queries
def transform_union_structure(self, query_structure):
if self.query_name_dict[query_structure] == '2u-DNF':
return ('e', ('r',))
elif self.query_name_dict[query_structure] == 'up-DNF':
return ('e', ('r', 'r'))
def cal_logit_box(self, entity_embedding, query_center_embedding, query_offset_embedding):
delta = (entity_embedding - query_center_embedding).abs()
distance_out = F.relu(delta - query_offset_embedding)
distance_in = torch.min(delta, query_offset_embedding)
logit = self.gamma - torch.norm(distance_out, p=1, dim=-1) - self.cen * torch.norm(distance_in, p=1, dim=-1)
return logit
def forward_box(self, positive_sample, negative_sample, subsampling_weight, batch_queries_dict, batch_idxs_dict):
all_center_embeddings, all_offset_embeddings, all_idxs = [], [], []
all_union_center_embeddings, all_union_offset_embeddings, all_union_idxs = [], [], []
for query_structure in batch_queries_dict:
if 'u' in self.query_name_dict[query_structure]:
center_embedding, offset_embedding, _ = \
self.embed_query_box(self.transform_union_query(batch_queries_dict[query_structure],
query_structure),
self.transform_union_structure(query_structure),
0)
all_union_center_embeddings.append(center_embedding)
all_union_offset_embeddings.append(offset_embedding)
all_union_idxs.extend(batch_idxs_dict[query_structure])
else:
center_embedding, offset_embedding, _ = self.embed_query_box(batch_queries_dict[query_structure],
query_structure,
0)
all_center_embeddings.append(center_embedding)
all_offset_embeddings.append(offset_embedding)
all_idxs.extend(batch_idxs_dict[query_structure])
if len(all_center_embeddings) > 0 and len(all_offset_embeddings) > 0:
all_center_embeddings = torch.cat(all_center_embeddings, dim=0).unsqueeze(1)
all_offset_embeddings = torch.cat(all_offset_embeddings, dim=0).unsqueeze(1)
if len(all_union_center_embeddings) > 0 and len(all_union_offset_embeddings) > 0:
all_union_center_embeddings = torch.cat(all_union_center_embeddings, dim=0).unsqueeze(1)
all_union_offset_embeddings = torch.cat(all_union_offset_embeddings, dim=0).unsqueeze(1)
all_union_center_embeddings = all_union_center_embeddings.view(all_union_center_embeddings.shape[0]//2, 2, 1, -1)
all_union_offset_embeddings = all_union_offset_embeddings.view(all_union_offset_embeddings.shape[0]//2, 2, 1, -1)
if type(subsampling_weight) != type(None):
subsampling_weight = subsampling_weight[all_idxs+all_union_idxs]
if type(positive_sample) != type(None):
if len(all_center_embeddings) > 0:
positive_sample_regular = positive_sample[all_idxs]
positive_embedding = torch.index_select(self.entity_embedding, dim=0, index=positive_sample_regular).unsqueeze(1)
positive_logit = self.cal_logit_box(positive_embedding, all_center_embeddings, all_offset_embeddings)
else:
positive_logit = torch.Tensor([]).to(self.entity_embedding.device)
if len(all_union_center_embeddings) > 0:
positive_sample_union = positive_sample[all_union_idxs]
positive_embedding = torch.index_select(self.entity_embedding, dim=0, index=positive_sample_union).unsqueeze(1).unsqueeze(1)
positive_union_logit = self.cal_logit_box(positive_embedding, all_union_center_embeddings, all_union_offset_embeddings)
positive_union_logit = torch.max(positive_union_logit, dim=1)[0]
else:
positive_union_logit = torch.Tensor([]).to(self.entity_embedding.device)
positive_logit = torch.cat([positive_logit, positive_union_logit], dim=0)
else:
positive_logit = None
if type(negative_sample) != type(None):
if len(all_center_embeddings) > 0:
negative_sample_regular = negative_sample[all_idxs]
batch_size, negative_size = negative_sample_regular.shape
negative_embedding = torch.index_select(self.entity_embedding, dim=0, index=negative_sample_regular.view(-1)).view(batch_size, negative_size, -1)
negative_logit = self.cal_logit_box(negative_embedding, all_center_embeddings, all_offset_embeddings)
else:
negative_logit = torch.Tensor([]).to(self.entity_embedding.device)
if len(all_union_center_embeddings) > 0:
negative_sample_union = negative_sample[all_union_idxs]
batch_size, negative_size = negative_sample_union.shape
negative_embedding = torch.index_select(self.entity_embedding, dim=0, index=negative_sample_union.view(-1)).view(batch_size, 1, negative_size, -1)
negative_union_logit = self.cal_logit_box(negative_embedding, all_union_center_embeddings, all_union_offset_embeddings)
negative_union_logit = torch.max(negative_union_logit, dim=1)[0]
else:
negative_union_logit = torch.Tensor([]).to(self.entity_embedding.device)
negative_logit = torch.cat([negative_logit, negative_union_logit], dim=0)
else:
negative_logit = None
return positive_logit, negative_logit, subsampling_weight, all_idxs+all_union_idxs
def cal_logit_vec(self, entity_embedding, query_embedding):
distance = entity_embedding - query_embedding
logit = self.gamma - torch.norm(distance, p=1, dim=-1)
return logit
def forward_vec(self, positive_sample, negative_sample, subsampling_weight, batch_queries_dict, batch_idxs_dict):
all_center_embeddings, all_idxs = [], []
all_union_center_embeddings, all_union_idxs = [], []
for query_structure in batch_queries_dict:
if 'u' in self.query_name_dict[query_structure]:
center_embedding, _ = self.embed_query_vec(self.transform_union_query(batch_queries_dict[query_structure],
query_structure),
self.transform_union_structure(query_structure), 0)
all_union_center_embeddings.append(center_embedding)
all_union_idxs.extend(batch_idxs_dict[query_structure])
else:
center_embedding, _ = self.embed_query_vec(batch_queries_dict[query_structure], query_structure, 0)
all_center_embeddings.append(center_embedding)
all_idxs.extend(batch_idxs_dict[query_structure])
if len(all_center_embeddings) > 0:
all_center_embeddings = torch.cat(all_center_embeddings, dim=0).unsqueeze(1)
if len(all_union_center_embeddings) > 0:
all_union_center_embeddings = torch.cat(all_union_center_embeddings, dim=0).unsqueeze(1)
all_union_center_embeddings = all_union_center_embeddings.view(all_union_center_embeddings.shape[0]//2, 2, 1, -1)
if type(subsampling_weight) != type(None):
subsampling_weight = subsampling_weight[all_idxs+all_union_idxs]
if type(positive_sample) != type(None):
if len(all_center_embeddings) > 0:
positive_sample_regular = positive_sample[all_idxs]
positive_embedding = torch.index_select(self.entity_embedding, dim=0, index=positive_sample_regular).unsqueeze(1)
positive_logit = self.cal_logit_vec(positive_embedding, all_center_embeddings)
else:
positive_logit = torch.Tensor([]).to(self.entity_embedding.device)
if len(all_union_center_embeddings) > 0:
positive_sample_union = positive_sample[all_union_idxs]
positive_embedding = torch.index_select(self.entity_embedding, dim=0, index=positive_sample_union).unsqueeze(1).unsqueeze(1)
positive_union_logit = self.cal_logit_vec(positive_embedding, all_union_center_embeddings)
positive_union_logit = torch.max(positive_union_logit, dim=1)[0]
else:
positive_union_logit = torch.Tensor([]).to(self.entity_embedding.device)
positive_logit = torch.cat([positive_logit, positive_union_logit], dim=0)
else:
positive_logit = None
if type(negative_sample) != type(None):
if len(all_center_embeddings) > 0:
negative_sample_regular = negative_sample[all_idxs]
batch_size, negative_size = negative_sample_regular.shape
negative_embedding = torch.index_select(self.entity_embedding, dim=0, index=negative_sample_regular.view(-1)).view(batch_size, negative_size, -1)
negative_logit = self.cal_logit_vec(negative_embedding, all_center_embeddings)
else:
negative_logit = torch.Tensor([]).to(self.entity_embedding.device)
if len(all_union_center_embeddings) > 0:
negative_sample_union = negative_sample[all_union_idxs]
batch_size, negative_size = negative_sample_union.shape
negative_embedding = torch.index_select(self.entity_embedding, dim=0, index=negative_sample_union.view(-1)).view(batch_size, 1, negative_size, -1)
negative_union_logit = self.cal_logit_vec(negative_embedding, all_union_center_embeddings)
negative_union_logit = torch.max(negative_union_logit, dim=1)[0]
else:
negative_union_logit = torch.Tensor([]).to(self.entity_embedding.device)
negative_logit = torch.cat([negative_logit, negative_union_logit], dim=0)
else:
negative_logit = None
return positive_logit, negative_logit, subsampling_weight, all_idxs+all_union_idxs
@staticmethod
def train_step(model, optimizer, train_iterator, args, step):
model.train()
optimizer.zero_grad()
positive_sample, negative_sample, subsampling_weight, batch_queries, query_structures = next(train_iterator)
batch_queries_dict = collections.defaultdict(list)
batch_idxs_dict = collections.defaultdict(list)
for i, query in enumerate(batch_queries): # group queries with same structure
batch_queries_dict[query_structures[i]].append(query)
batch_idxs_dict[query_structures[i]].append(i)
for query_structure in batch_queries_dict:
if args.cuda:
batch_queries_dict[query_structure] = torch.LongTensor(batch_queries_dict[query_structure]).cuda()
else:
batch_queries_dict[query_structure] = torch.LongTensor(batch_queries_dict[query_structure])
if args.cuda:
positive_sample = positive_sample.cuda()
negative_sample = negative_sample.cuda()
subsampling_weight = subsampling_weight.cuda()
positive_logit, negative_logit, subsampling_weight, _ = model(positive_sample, negative_sample, subsampling_weight, batch_queries_dict, batch_idxs_dict)
negative_score = F.logsigmoid(-negative_logit).mean(dim=1)
positive_score = F.logsigmoid(positive_logit).squeeze(dim=1)
positive_sample_loss = - (subsampling_weight * positive_score).sum()
negative_sample_loss = - (subsampling_weight * negative_score).sum()
positive_sample_loss /= subsampling_weight.sum()
negative_sample_loss /= subsampling_weight.sum()
loss = (positive_sample_loss + negative_sample_loss)/2
loss.backward()
optimizer.step()
log = {
'positive_sample_loss': positive_sample_loss.item(),
'negative_sample_loss': negative_sample_loss.item(),
'loss': loss.item(),
}
return log
@staticmethod
def test_step(model, easy_answers, hard_answers, args, test_dataloader, query_name_dict, save_result=False, save_str="", save_empty=False):
model.eval()
step = 0
total_steps = len(test_dataloader)
logs = collections.defaultdict(list)
with torch.no_grad():
for negative_sample, queries, queries_unflatten, query_structures in tqdm(test_dataloader, disable=not args.print_on_screen):
batch_queries_dict = collections.defaultdict(list)
batch_idxs_dict = collections.defaultdict(list)
for i, query in enumerate(queries):
batch_queries_dict[query_structures[i]].append(query)
batch_idxs_dict[query_structures[i]].append(i)
for query_structure in batch_queries_dict:
if args.cuda:
batch_queries_dict[query_structure] = torch.LongTensor(batch_queries_dict[query_structure]).cuda()
else:
batch_queries_dict[query_structure] = torch.LongTensor(batch_queries_dict[query_structure])
if args.cuda:
negative_sample = negative_sample.cuda()
_, negative_logit, _, idxs = model(None, negative_sample, None, batch_queries_dict, batch_idxs_dict)
queries_unflatten = [queries_unflatten[i] for i in idxs]
query_structures = [query_structures[i] for i in idxs]
argsort = torch.argsort(negative_logit, dim=1, descending=True)
ranking = argsort.clone().to(torch.float)
if len(argsort) == args.test_batch_size: # if it is the same shape with test_batch_size, we can reuse batch_entity_range without creating a new one
ranking = ranking.scatter_(1, argsort, model.batch_entity_range) # achieve the ranking of all entities
else: # otherwise, create a new torch Tensor for batch_entity_range
if args.cuda:
ranking = ranking.scatter_(1,
argsort,
torch.arange(model.nentity).to(torch.float).repeat(argsort.shape[0],
1).cuda()
) # achieve the ranking of all entities
else:
ranking = ranking.scatter_(1,
argsort,
torch.arange(model.nentity).to(torch.float).repeat(argsort.shape[0],
1)
) # achieve the ranking of all entities
for idx, (i, query, query_structure) in enumerate(zip(argsort[:, 0], queries_unflatten, query_structures)):
hard_answer = hard_answers[query]
easy_answer = easy_answers[query]
num_hard = len(hard_answer)
num_easy = len(easy_answer)
assert len(hard_answer.intersection(easy_answer)) == 0
cur_ranking = ranking[idx, list(easy_answer) + list(hard_answer)]
cur_ranking, indices = torch.sort(cur_ranking)
masks = indices >= num_easy
if args.cuda:
answer_list = torch.arange(num_hard + num_easy).to(torch.float).cuda()
else:
answer_list = torch.arange(num_hard + num_easy).to(torch.float)
cur_ranking = cur_ranking - answer_list + 1 # filtered setting
cur_ranking = cur_ranking[masks] # only take indices that belong to the hard answers
mrr = torch.mean(1./cur_ranking).item()
h1 = torch.mean((cur_ranking <= 1).to(torch.float)).item()
h3 = torch.mean((cur_ranking <= 3).to(torch.float)).item()
h10 = torch.mean((cur_ranking <= 10).to(torch.float)).item()
logs[query_structure].append({
'MRR': mrr,
'HITS1': h1,
'HITS3': h3,
'HITS10': h10,
'num_hard_answer': num_hard,
})
if step % args.test_log_steps == 0:
logging.info('Evaluating the model... (%d/%d)' % (step, total_steps))
step += 1
metrics = collections.defaultdict(lambda: collections.defaultdict(int))
for query_structure in logs:
for metric in logs[query_structure][0].keys():
if metric in ['num_hard_answer']:
continue
metrics[query_structure][metric] = sum([log[metric] for log in logs[query_structure]])/len(logs[query_structure])
metrics[query_structure]['num_queries'] = len(logs[query_structure])
return metrics