-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.py
1254 lines (955 loc) · 48.9 KB
/
main.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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse, sys, json, pickle, torch, operator, random, os, time
from model import *
import numpy as np
from random import randint
from random import randrange
from sklearn.metrics import ndcg_score
from timeit import default_timer as timer
from datetime import timedelta
from random import shuffle
import more_itertools as mit
import torch.multiprocessing as mp
from copy import deepcopy
from pytictoc import TicToc
from batching import *
from multiprocessing import JoinableQueue, Queue, Process
from scipy.sparse import csr_matrix
def add_sparsified_types_to_negative_samples (head_entity_id, tail_entity_id, sparsifier, typeId2frequency, entityId2entityTypes, id2entity, unk_type_id, type2id, id2type):
current_head_types = []
current_tail_types = []
# get top head types
if head_entity_id in entityId2entityTypes:
current_head_types = entityId2entityTypes[head_entity_id]
headType2freq = {}
for h_type in current_head_types:
h_type_id = type2id[h_type]
if h_type_id in typeId2frequency:
headType2freq[h_type] = typeId2frequency[h_type_id]
sorted_headType2freq = sorted(headType2freq.items(), key=lambda kv: kv[1], reverse=True)[:sparsifier]
current_head_types = [type2id[item[0]] for item in sorted_headType2freq]
# get top tail types
if tail_entity_id in entityId2entityTypes:
current_tail_types = entityId2entityTypes[tail_entity_id]
tailType2freq = {}
for t_type in current_tail_types:
t_type_id = type2id[t_type]
if t_type_id in typeId2frequency:
tailType2freq[t_type] = typeId2frequency[t_type_id]
sorted_tailType2freq = sorted(tailType2freq.items(), key=lambda kv: kv[1], reverse=True)[:sparsifier]
current_tail_types = [type2id[item[0]] for item in sorted_tailType2freq]
if len(current_head_types)==0:
current_head_types = [unk_type_id]
if len(current_tail_types)==0:
current_tail_types = [unk_type_id]
headType_tailType_pairs = []
for h_type_id in current_head_types:
for t_type_id in current_tail_types:
headType_tailType_pairs.append(h_type_id)
headType_tailType_pairs.append(t_type_id)
for h_type_id in headType_tailType_pairs[0::2]:
if h_type_id != unk_type_id:
h_type_name = id2type[h_type_id]
if head_entity_id!=len(id2type) and h_type_name not in entityId2entityTypes[head_entity_id]:
print("ERROR head type")
if t_type_id != unk_type_id:
t_type_name = id2type[t_type_id]
if tail_entity_id!=len(id2type) and t_type_name not in entityId2entityTypes[tail_entity_id]:
print("ERROR tail type")
return headType_tailType_pairs
def build_entity2sparsifiedTypes (typeId2frequency, entityId2entityTypes, type2id, sparsifier, unk_type_id, id2type):
entity2sparsifiedTypes = {}
for entity_id in entityId2entityTypes:
current_entity_types = entityId2entityTypes[entity_id]
entityType2freq = {}
for e_type in current_entity_types:
e_type_id = type2id[e_type]
if e_type_id in typeId2frequency:
entityType2freq[e_type] = typeId2frequency[e_type_id]
sorted_entityType2freq = sorted(entityType2freq.items(), key=lambda kv: kv[1], reverse=True)[:sparsifier]
current_entity_types = [type2id[item[0]] for item in sorted_entityType2freq]
entity2sparsifiedTypes[entity_id] = current_entity_types
for e_type_id in entity2sparsifiedTypes[entity_id]:
if e_type_id == unk_type_id:
continue
e_type_name = id2type[e_type_id]
if e_type_name not in entityId2entityTypes[entity_id]:
print("ERROR entity type", e_type_name)
return entity2sparsifiedTypes
def sort_testing_facts_according_to_arity (new_all_tiled_fact, relation_tail_in_scores2idx, idx2relation_tail_in_scores):
arity2testFacts = {}
arity2relation_tail_pairs = {}
for fact_it, test_fact in enumerate(new_all_tiled_fact):
arity = len(test_fact)//2
if arity not in arity2testFacts:
arity2testFacts[arity] = []
arity2relation_tail_pairs[arity] = []
arity2testFacts[arity].append(test_fact)
current_rel_tail_pair = idx2relation_tail_in_scores[fact_it] # get r-t pair
arity2relation_tail_pairs[arity].append(current_rel_tail_pair)
test_facts_by_arities = []
new_relation_tail_in_scores2idx = {}
new_idx2relation_tail_in_scores = {}
idx = 0
for arity in arity2testFacts:
test_facts_by_arities.append(np.asarray(arity2testFacts[arity]))
all_r_t_pairs_with_current_arity = arity2relation_tail_pairs[arity]
for r_t_pair in all_r_t_pairs_with_current_arity:
new_idx2relation_tail_in_scores[idx] = r_t_pair
new_relation_tail_in_scores2idx[r_t_pair] = idx
idx += 1
test_facts_by_arities = np.asarray(test_facts_by_arities)
return test_facts_by_arities, new_relation_tail_in_scores2idx, new_idx2relation_tail_in_scores
def build_entity2types_dictionaries (dataset_name, entity2id):
entityName2entityTypes = {}
entityId2entityTypes = {}
entityType2entityNames = {}
entityType2entityIds = {}
entity2type_file = open(dataset_name + "/entity2types_ttv.txt", "r")
for line in entity2type_file:
splitted_line = line.strip().split("\t")
entity_name = splitted_line[0]
entity_type = splitted_line[1]
if entity_name not in entityName2entityTypes:
entityName2entityTypes[entity_name] = []
if entity_type not in entityName2entityTypes[entity_name]:
entityName2entityTypes[entity_name].append(entity_type)
if entity_type not in entityType2entityNames:
entityType2entityNames[entity_type] = []
if entity_name not in entityType2entityNames[entity_type]:
entityType2entityNames[entity_type].append(entity_name)
entity_id = entity2id[entity_name]
if entity_id not in entityId2entityTypes:
entityId2entityTypes[entity_id] = []
if entity_type not in entityId2entityTypes[entity_id]:
entityId2entityTypes[entity_id].append(entity_type)
if entity_type not in entityType2entityIds:
entityType2entityIds[entity_type] = []
if entity_id not in entityType2entityIds[entity_type]:
entityType2entityIds[entity_type].append(entity_id)
entity2type_file.close()
return entityName2entityTypes, entityId2entityTypes, entityType2entityNames, entityType2entityIds
def build_head2relation2tails (inputData, entity2id, relation2id, entityId2entityTypes, entitiesEvaluated):
head2relation2tails = {}
with open(inputData + "/n-ary_test.json") as f:
for line in f:
line = json.loads(line.strip().replace("'", "\""))
h = list(line.values())[0][0]
r = list(line.keys())[0]
t = list(line.values())[0][1]
hId = entity2id[h]
rId = relation2id[r]
tId = entity2id[t]
if entitiesEvaluated == 'both':
if hId not in entityId2entityTypes:
continue
if tId not in entityId2entityTypes:
continue
elif entitiesEvaluated == 'one':
if hId not in entityId2entityTypes and tId not in entityId2entityTypes:
continue
if hId in entityId2entityTypes and tId in entityId2entityTypes:
continue
elif entitiesEvaluated == 'none':
if hId in entityId2entityTypes or tId in entityId2entityTypes:
continue
if hId not in head2relation2tails:
head2relation2tails[hId] = {}
if rId not in head2relation2tails[hId]:
head2relation2tails[hId][rId] = []
if tId not in head2relation2tails[hId][rId]:
head2relation2tails[hId][rId].append(tId)
f.close()
return head2relation2tails
def build_type2relationType2frequency (dataset_name, buildTypeDictionaries):
type_relation_type_file = open(dataset_name + "/type2relation2type_ttv.txt", "r")
type2relationType2frequency = {}
for line in type_relation_type_file:
splitted_line = line.strip().split("\t")
head_type = splitted_line[0]
relation = splitted_line[1]
tail_type = splitted_line[2]
relationType = (relation, tail_type)
if head_type not in type2relationType2frequency:
type2relationType2frequency[head_type] = {}
if relationType not in type2relationType2frequency[head_type]:
type2relationType2frequency[head_type][relationType] = 1
else:
type2relationType2frequency[head_type][relationType] += 1
type_relation_type_file.close()
return type2relationType2frequency
def build_tensor_matrix (inputData, entity2id, relation2id, entityName2entityTypes, topNfilters, type2relationType2frequency, entityId2typeIds_with_sparsifier, type2id, id2type, device, entitiesEvaluated):
type_head_tail_entity_matrix = torch.zeros((len(type2id), len(entity2id)), requires_grad=False).to(device)
train_test_valid = ["train", "test"]
for ttv in train_test_valid:
with open(inputData + "/" + ttv + ".txt") as train_file:
for line in train_file:
splitted_line = line.strip().split()
head_entity = splitted_line[0]
tail_entity = splitted_line[1]
if head_entity in entityName2entityTypes:
head_entity_id = entity2id[head_entity]
head_types = entityName2entityTypes[head_entity]
for head_type in head_types:
head_type_id = type2id[head_type]
type_head_tail_entity_matrix[head_type_id][head_entity_id] = 1
if tail_entity in entityName2entityTypes:
tail_entity_id = entity2id[tail_entity]
tail_types = entityName2entityTypes[tail_entity]
for tail_type in tail_types:
tail_type_id = type2id[tail_type]
type_head_tail_entity_matrix[tail_type_id][tail_entity_id] = 1
train_file.close()
if entitiesEvaluated == "one":
for i in range (len(entity2id)):
current_column = type_head_tail_entity_matrix[:,i]
non_zero_entries = torch.nonzero(current_column).tolist()
if len(non_zero_entries)==0:
type_head_tail_entity_matrix[:,i] = 1
tailType_relation_headType_tensor = torch.zeros((len(type2id), len(relation2id), len(type2id)), requires_grad=False).to(device)
list_of_type_relation_type = []
for h_type in type2relationType2frequency:
for relation_type in type2relationType2frequency[h_type]:
relation = relation_type[0]
t_type = relation_type[1]
list_of_type_relation_type.append((h_type, relation, t_type))
list_of_filtered_type_relation_type = []
for h_type in type2relationType2frequency:
if topNfilters <= 0:
sorted_relation_tailType = sorted(type2relationType2frequency[h_type].items(), key=operator.itemgetter(1), reverse=True)
for list_idx, relationType_frequency in reversed(list(enumerate(sorted_relation_tailType))):
freq = relationType_frequency[1]
if freq <= (topNfilters*-1):
del sorted_relation_tailType[list_idx]
for relationType_frequency in sorted_relation_tailType:
relation = relationType_frequency[0][0]
t_type = relationType_frequency[0][1]
list_of_filtered_type_relation_type.append((h_type, relation, t_type))
list_of_type_relation_type = list_of_filtered_type_relation_type
for trt in list_of_type_relation_type:
head_type = trt[0]
relation = trt[1]
tail_type = trt[2]
head_type_id = type2id[head_type]
relation_id = relation2id[relation]
tail_type_id = type2id[tail_type]
tailType_relation_headType_tensor[tail_type_id][relation_id][head_type_id] = 1
return type_head_tail_entity_matrix, tailType_relation_headType_tensor
def chunks (L, n):
""" Yield successive n-sized chunks from L."""
for i in range(0, len(L), n):
yield L[i:i+n]
def my_precision_and_recall_at_k (scores, gt_head_id, head2relation2tails, all_k, id2entity, id2relation, idx2relation_tail_in_scores):
final_results = []
num_of_total_gt = 0
for r_id in head2relation2tails[gt_head_id]:
num_of_total_gt += len(head2relation2tails[gt_head_id][r_id])
sorted_scores_idx_all = (-scores).argsort() #descending sort
for k in all_k:
if k == 'all':
sorted_scores_idx = sorted_scores_idx_all
else:
sorted_scores_idx = sorted_scores_idx_all[:k] #top k
sorted_entity_ids = []
for idx in sorted_scores_idx:
r_id, tail_id = idx2relation_tail_in_scores[idx]
sorted_entity_ids.append((r_id, tail_id))
num_of_gt_in_top_k = 0
map_ = 0
map_numerator = 1
for map_denominator, rId_tailId in enumerate(sorted_entity_ids):
r_id = rId_tailId[0]
tail_id = rId_tailId[1]
if r_id in head2relation2tails[gt_head_id] and tail_id in head2relation2tails[gt_head_id][r_id]:
num_of_gt_in_top_k += 1
map_ += (map_numerator/(map_denominator+1))
map_numerator += 1
if k == 'all':
precision = num_of_gt_in_top_k/(len(scores))
else:
precision = num_of_gt_in_top_k/k
recall = num_of_gt_in_top_k/num_of_total_gt
if map_numerator == 1:
map_ = map_ / 1
else:
map_ = map_ / (map_numerator-1)
final_results.append(precision)
final_results.append(recall)
final_results.append(map_)
##NDCG
ndcg_y_true = []
for idx in idx2relation_tail_in_scores:
r_id, tail_id = idx2relation_tail_in_scores[idx]
if r_id in head2relation2tails[gt_head_id] and tail_id in head2relation2tails[gt_head_id][r_id]:
ndcg_y_true.append(1)
else:
ndcg_y_true.append(0)
ndcg_y_true = [ndcg_y_true]
ndcg_scores = [scores]
ndcg10 = ndcg_score(ndcg_y_true, ndcg_scores, k=10)
ndcg5 = ndcg_score(ndcg_y_true, ndcg_scores, k=5)
ndcg3 = ndcg_score(ndcg_y_true, ndcg_scores, k=3)
ndcg1 = ndcg_score(ndcg_y_true, ndcg_scores, k=1)
ndcgAll = ndcg_score(ndcg_y_true, ndcg_scores)
final_results.append(ndcg10)
final_results.append(ndcg5)
final_results.append(ndcg3)
final_results.append(ndcg1)
final_results.append(ndcgAll)
return final_results
def evaluate_all_relation_tail_pairs_v2 (head_id, id2entity, relation2id, id2relation, column, fact, model, arity, device, sparsifier, typeId2frequency, entityId2entityTypes, unk_type_id, type2id, id2type):
list_of_scores_per_head_with_filter = []
relation_tail_in_scores2idx = {}
idx2relation_tail_in_scores = {}
idx_iterator = 0
all_tiled_fact = np.array([])
new_tiled_fact = []
for r_id in id2relation:
r_id = int(r_id)
all_entities = []
for e in id2entity:
e = int(e)
relation_tail_in_scores2idx[(r_id, e)] = idx_iterator
all_entities.append(e)
idx2relation_tail_in_scores[idx_iterator] = (r_id, e)
idx_iterator += 1
tiled_fact = np.array(fact*len(all_entities)).reshape(len(all_entities),-1)
tiled_fact[:,column] = all_entities
replicated_relation = np.repeat(r_id, len(all_entities))
tiled_fact[:,0] = replicated_relation
tiled_fact[:,2] = replicated_relation
for current_fact in tiled_fact:
current_head_entity = current_fact[1]
current_tail_entity = current_fact[3]
headType_tailType_pairs = add_sparsified_types_to_negative_samples (current_head_entity, current_tail_entity, sparsifier, typeId2frequency, entityId2entityTypes, id2entity, unk_type_id, type2id, id2type)
current_fact = current_fact[:4]
current_fact = np.append(current_fact, headType_tailType_pairs)
new_tiled_fact.append(current_fact)
all_tiled_fact_group_by_arity, relation_tail_in_scores2idx, idx2relation_tail_in_scores = sort_testing_facts_according_to_arity(new_tiled_fact, relation_tail_in_scores2idx, idx2relation_tail_in_scores)
pred = None
for facts_with_same_arities in all_tiled_fact_group_by_arity:
batch_of_facts_with_same_arities = list(chunks(facts_with_same_arities, 2048))
arity = len(batch_of_facts_with_same_arities[0][0])//2
if pred == None:
pred = model(batch_of_facts_with_same_arities[0], arity, "testing", device)
else:
pred_tmp = model(batch_of_facts_with_same_arities[0], arity, "testing", device)
pred = torch.cat((pred, pred_tmp))
for batch_it in range(1, len(batch_of_facts_with_same_arities)):
pred_tmp = model(batch_of_facts_with_same_arities[batch_it], arity, "testing", device)
pred = torch.cat((pred, pred_tmp))
score_with_filter = pred.view(-1).detach().cpu().numpy()
list_of_scores_per_head_with_filter.append(score_with_filter)
return list_of_scores_per_head_with_filter, idx2relation_tail_in_scores
def get_filtered_relations_and_tails_filter1_filter2_hybrid (head_id, id2entity, type2relationType2frequency, topNfilters, column, fact, model, arity, device, type_head_tail_entity_matrix, tailType_relation_headType_tensor, entityName2entityTypes, type2id, relation2id, atLeast, sparsifier, typeId2frequency, entityId2entityTypes, unk_type_id, id2type, entity2sparsifiedTypes, entitiesEvaluated):
list_of_scores_per_head_with_filter = []
idx2relation_tail_in_scores = {}
h = id2entity[head_id]
tiled_fact = np.array([])
all_head_types = type_head_tail_entity_matrix[:,head_id]
tailType_relation_matrix = torch.matmul(tailType_relation_headType_tensor, all_head_types)
tailType_relation_matrix = torch.transpose(tailType_relation_matrix, 0, 1)
relation_entity_matrix = torch.matmul(tailType_relation_matrix, type_head_tail_entity_matrix)
relation_entity_matrix[relation_entity_matrix < atLeast] = 0
if torch.nonzero(relation_entity_matrix).shape[0] != 0:
filtered_relation_tail_pairs = torch.nonzero(relation_entity_matrix)
all_relations = filtered_relation_tail_pairs[:,0].tolist()
entities_without_duplicates = filtered_relation_tail_pairs[:,1].tolist()
r_e_tuples = list(zip(all_relations, entities_without_duplicates))
idx_iterator = range(0, len(r_e_tuples))
relation_tail_in_scores2idx = dict(zip(r_e_tuples, idx_iterator))
idx2relation_tail_in_scores = dict(zip(idx_iterator, r_e_tuples))
if len(entities_without_duplicates) > 0:
tiled_fact = np.array(fact*len(entities_without_duplicates)).reshape(len(entities_without_duplicates),-1)
tiled_fact[:,column] = entities_without_duplicates
tiled_fact[:,0] = all_relations
tiled_fact[:,2] = all_relations
head_sparsified_types = []
current_head_entity = tiled_fact[0][1]
if current_head_entity in entity2sparsifiedTypes:
head_sparsified_types = entity2sparsifiedTypes[current_head_entity]
if len(tiled_fact) > 0:
new_tiled_fact = []
for current_fact in tiled_fact:
current_head_entity = current_fact[1]
current_tail_entity = current_fact[3]
tail_sparsified_types = []
if current_tail_entity in entity2sparsifiedTypes:
tail_sparsified_types = entity2sparsifiedTypes[current_tail_entity]
if entitiesEvaluated == "one":
if len(head_sparsified_types)==0: #h has no types
head_sparsified_types = [unk_type_id]
if len(tail_sparsified_types)==0: #t has no types
tail_sparsified_types = [unk_type_id]
headType_tailType_pairs = []
for h_t in head_sparsified_types:
for t_t in tail_sparsified_types:
headType_tailType_pairs.append(h_t)
headType_tailType_pairs.append(t_t)
current_fact = current_fact[:4]
current_fact = np.append(current_fact, headType_tailType_pairs)
new_tiled_fact.append(current_fact)
new_tiled_fact, relation_tail_in_scores2idx, idx2relation_tail_in_scores = sort_testing_facts_according_to_arity(new_tiled_fact, relation_tail_in_scores2idx, idx2relation_tail_in_scores) # new_tiled_fact size: (num of mini batches, num of facts, arity)
pred = None
for facts_with_same_arities in new_tiled_fact:
batch_of_facts_with_same_arities = list(chunks(facts_with_same_arities, 256))
arity = len(batch_of_facts_with_same_arities[0][0])//2
if pred == None:
pred = model(batch_of_facts_with_same_arities[0], arity, "testing", device)
else:
pred_tmp = model(batch_of_facts_with_same_arities[0], arity, "testing", device)
pred = torch.cat((pred, pred_tmp))
for batch_it in range(1, len(batch_of_facts_with_same_arities)):
pred_tmp = model(batch_of_facts_with_same_arities[batch_it], arity, "testing", device)
pred = torch.cat((pred, pred_tmp))
score_with_filter = pred.view(-1).detach().cpu().numpy()
list_of_scores_per_head_with_filter.append(score_with_filter)
return list_of_scores_per_head_with_filter, idx2relation_tail_in_scores
def build_headTail2hTypetType (inputData, entity2id, type2id, entity2types, sparsifier, typeId2frequency, buildTypeDictionaries):
if buildTypeDictionaries == "True":
headTail2hTypetType = {}
entityId2typeIds_with_sparsifier = {}
train_test_valid = ["train", "test"]
for ttv in train_test_valid:
with open(inputData + "/n-ary_" + ttv + ".json") as f:
for line_number, line in enumerate(f):
line = json.loads(line.strip().replace("'", "\""))
h = list(line.values())[0][0]
t = list(line.values())[0][1]
if h in entity2types and t in entity2types: # both head and tail have types
h_types = entity2types[h]
t_types = entity2types[t]
h_id = entity2id[h]
t_id = entity2id[t]
h_t_tuple = (h_id, t_id)
if h_t_tuple not in headTail2hTypetType:
headTail2hTypetType[h_t_tuple] = []
for h_type in h_types:
h_type_id = type2id[h_type]
for t_type in t_types:
t_type_id = type2id[t_type]
types_tuple = (h_type_id, t_type_id)
if types_tuple not in headTail2hTypetType[h_t_tuple]:
headTail2hTypetType[h_t_tuple].append(types_tuple)
elif h in entity2types and t not in entity2types:
h_types = entity2types[h]
h_id = entity2id[h]
if h_id not in entityId2typeIds_with_sparsifier:
entityId2typeIds_with_sparsifier[h_id] = []
for h_type in h_types:
h_type_id = type2id[h_type]
if h_type_id not in entityId2typeIds_with_sparsifier[h_id]:
entityId2typeIds_with_sparsifier[h_id].append(h_type_id)
elif h not in entity2types and t in entity2types:
t_types = entity2types[t]
t_id = entity2id[t]
if t_id not in entityId2typeIds_with_sparsifier:
entityId2typeIds_with_sparsifier[t_id] = []
for t_type in t_types:
t_type_id = type2id[t_type]
if t_type_id not in entityId2typeIds_with_sparsifier[t_id]:
entityId2typeIds_with_sparsifier[t_id].append(t_type_id)
f.close()
if sparsifier >= 0:
for k in headTail2hTypetType:
current_headType2freq = {}
current_tailType2freq = {}
current_head_types = [item[0] for item in headTail2hTypetType[k]]
current_tail_types = [item[1] for item in headTail2hTypetType[k]]
current_unique_head_types = list(dict.fromkeys(current_head_types))
current_unique_tail_types = list(dict.fromkeys(current_tail_types))
current_headType2freq = {}
for h_type in current_unique_head_types:
current_headType2freq[h_type] = typeId2frequency[h_type]
current_tailType2freq = {}
for t_type in current_unique_tail_types:
current_tailType2freq[t_type] = typeId2frequency[t_type]
sorted_current_headType2freq = sorted(current_headType2freq.items(), key=lambda kv: kv[1], reverse=True)[:sparsifier]
sorted_current_tailType2freq = sorted(current_tailType2freq.items(), key=lambda kv: kv[1], reverse=True)[:sparsifier]
topNheadTypes = [item[0] for item in sorted_current_headType2freq]
topNtailTypes = [item[0] for item in sorted_current_tailType2freq]
new_headType_tailType_list = []
for hType_tTypy_tuple in headTail2hTypetType[k]:
if hType_tTypy_tuple[0] in topNheadTypes and hType_tTypy_tuple[1] in topNtailTypes:
new_headType_tailType_list.append(hType_tTypy_tuple)
headTail2hTypetType[k] = new_headType_tailType_list
for k in headTail2hTypetType:
if len(headTail2hTypetType[k]) > (sparsifier*sparsifier):
print("ERROR in the sparsification: the maximum length of each list must be", sparsifier*sparsifier)
# entityId2typeIds_with_sparsifier
for k in entityId2typeIds_with_sparsifier:
current_entityType2freq = {}
current_entity_types = entityId2typeIds_with_sparsifier[k] # get current head types
current_unique_entity_types = list(dict.fromkeys(current_entity_types)) # get current unique head types
# get entityType2freq
current_entityType2freq = {}
for e_type in current_unique_entity_types:
if e_type in typeId2frequency:
current_entityType2freq[e_type] = typeId2frequency[e_type]
else:
pass
sorted_current_entityType2freq = sorted(current_entityType2freq.items(), key=lambda kv: kv[1], reverse=True)[:sparsifier]
topNentityTypes = [item[0] for item in sorted_current_entityType2freq]
new_entiyType_list = []
for eType in entityId2typeIds_with_sparsifier[k]:
if eType in topNentityTypes:
new_entiyType_list.append(eType)
entityId2typeIds_with_sparsifier[k] = new_entiyType_list
for k in entityId2typeIds_with_sparsifier:
if len(entityId2typeIds_with_sparsifier[k]) > (sparsifier*sparsifier):
print("ERROR in the sparsification: the maximum length of each list must be", sparsifier*sparsifier)
for k in headTail2hTypetType:
headTail2hTypetType[k] = np.asarray(headTail2hTypetType[k])
for k in entityId2typeIds_with_sparsifier:
entityId2typeIds_with_sparsifier[k] = np.asarray(entityId2typeIds_with_sparsifier[k])
with open(inputData + "/headTail2hTypetType.pickle", "wb") as handle:
pickle.dump(headTail2hTypetType, handle, protocol=pickle.HIGHEST_PROTOCOL)
handle.close()
with open(inputData + "/entityId2typeIds_with_sparsifier.pickle", "wb") as handle:
pickle.dump(entityId2typeIds_with_sparsifier, handle, protocol=pickle.HIGHEST_PROTOCOL)
handle.close()
elif buildTypeDictionaries == "False":
with open(inputData + "/headTail2hTypetType.pickle", "rb") as handle:
headTail2hTypetType = pickle.load(handle)
handle.close()
with open(inputData + "/entityId2typeIds_with_sparsifier.pickle", "rb") as handle:
entityId2typeIds_with_sparsifier = pickle.load(handle)
handle.close()
return headTail2hTypetType, entityId2typeIds_with_sparsifier
def build_type2id_v2 (inputData):
type2id = {}
id2type = {}
type_counter = 0
with open(inputData + "/entity2types_ttv.txt") as entity2type_file:
for line in entity2type_file:
splitted_line = line.strip().split("\t")
entity_type = splitted_line[1]
if entity_type not in type2id:
type2id[entity_type] = type_counter
id2type[type_counter] = entity_type
type_counter += 1
entity2type_file.close()
return type2id, id2type
def build_typeId2frequency (dataset_name, type2id):
typeId2frequency = {}
type_relation_type_file = open(dataset_name + "/type2relation2type_ttv.txt", "r")
for line in type_relation_type_file:
splitted_line = line.strip().split("\t")
head_type = splitted_line[0]
tail_type = splitted_line[2]
head_type_id = type2id[head_type]
tail_type_id = type2id[tail_type]
if head_type_id not in typeId2frequency:
typeId2frequency[head_type_id] = 0
if tail_type_id not in typeId2frequency:
typeId2frequency[tail_type_id] = 0
typeId2frequency[head_type_id] += 1
typeId2frequency[tail_type_id] += 1
type_relation_type_file.close()
return typeId2frequency
def add_type_pair_to_fact (train, test, valid, headTail2hTypetType, entityId2typeIds_with_sparsifier, unk_type_id):
for it, ttv in enumerate([train, test, valid]):
if it == 0:
current_ttv = train
elif it == 1:
current_ttv = test
elif it == 2:
current_ttv = valid
arity2fact = {}
for d in current_ttv:
for fact_iterator, current_fact in enumerate(d):
current_head = current_fact[1]
current_tail = current_fact[3]
if (current_head, current_tail) in headTail2hTypetType and len(headTail2hTypetType[(current_head, current_tail)]) > 0:
list_of_indexes = headTail2hTypetType[(current_head, current_tail)]
current_fact = np.append(current_fact, list_of_indexes)
elif current_head in entityId2typeIds_with_sparsifier and current_tail not in entityId2typeIds_with_sparsifier:
list_of_indexes = entityId2typeIds_with_sparsifier[current_head]
for current_h_type in list_of_indexes:
current_fact = np.append(current_fact, [current_h_type, unk_type_id])
elif current_head not in entityId2typeIds_with_sparsifier and current_tail in entityId2typeIds_with_sparsifier:
list_of_indexes = entityId2typeIds_with_sparsifier[current_tail]
for current_t_type in list_of_indexes:
current_fact = np.append(current_fact, [unk_type_id, current_t_type])
elif current_head not in entityId2typeIds_with_sparsifier and current_tail not in entityId2typeIds_with_sparsifier:
current_fact = np.append(current_fact, [unk_type_id, unk_type_id])
else:
print("ERROR: it should go in one of the previous cases")
current_fact = tuple(current_fact)
new_arity = len(current_fact)
if new_arity not in arity2fact:
arity2fact[new_arity] = []
arity2fact[new_arity].append(current_fact)
new_list = []
for new_arity in sorted(arity2fact.keys()):
new_dict = {}
for fact in arity2fact[new_arity]:
new_dict[fact] = [1]
new_list.append(new_dict)
if it == 0:
train = new_list
elif it == 1:
test = new_list
elif it == 2:
valid = new_list
return train, test, valid
def evaluate_model_v2 (model, test, id2entity, type2relationType2frequency, topNfilters, atLeast, device, type2id, id2type, type_head_tail_entity_matrix, tailType_relation_headType_tensor, entityName2entityTypes, relation2id, head2relation2tails, id2relation, sparsifier, typeId2frequency, entityId2entityTypes, unk_type_id, entity2sparsifiedTypes, indir, entitiesEvaluated):
number_of_actual_heads_evaluated = 0
number_of_heads_not_evaluated_1 = 0
number_of_heads_not_evaluated_3 = 0
number_of_heads_not_evaluated_4 = 0
number_of_heads_not_evaluated_5 = 0
my_precision10 = 0
my_recall10 = 0
my_map10 = 0
my_precision5 = 0
my_recall5 = 0
my_map5 = 0
my_precision3 = 0
my_recall3 = 0
my_map3 = 0
my_precision1 = 0
my_recall1 = 0
my_map1 = 0
my_precisionAll = 0
my_recallAll = 0
my_mapAll = 0
my_ndcg10 = 0
my_ndcg5 = 0
my_ndcg3 = 0
my_ndcg1 = 0
my_ndcgAll = 0
visited_head = {}
print("evaluate_model")
model.eval()
t3 = TicToc()
t3.tic()
with torch.no_grad():
list_of_testing_facts = []
for test_fact_grouped_by_arity in test:
for test_fact in test_fact_grouped_by_arity:
list_of_testing_facts.append(test_fact)
for fact_progress, fact in enumerate(list_of_testing_facts):
list_of_scores_per_head_with_filter = []
fact = list(fact)
arity = int(len(fact)/2)
head_id = fact[1]
if fact_progress%100==0:
t3.toc("EVALUATING 100 FACTS:")
print("evaluation progress:", fact_progress, "/", len(list_of_testing_facts), " | number_of_actual_heads_evaluated:", number_of_actual_heads_evaluated)
sys.stdout.flush()
t3.tic()
column = 3
correct_index = fact[column]
if head_id not in head2relation2tails:
number_of_heads_not_evaluated_5 += 1
continue
if head_id not in id2entity:
continue
head_name = id2entity[head_id]
if head_name in visited_head:
number_of_heads_not_evaluated_3 += 1
continue
visited_head[head_name] = 1
if head_name in entityName2entityTypes:
list_of_scores_per_head_with_filter, idx2relation_tail_in_scores = get_filtered_relations_and_tails_filter1_filter2_hybrid (head_id, id2entity, type2relationType2frequency, topNfilters, column, fact, model, arity, device, type_head_tail_entity_matrix, tailType_relation_headType_tensor, entityName2entityTypes, type2id, relation2id, atLeast, sparsifier, typeId2frequency, entityId2entityTypes, unk_type_id, id2type, entity2sparsifiedTypes, entitiesEvaluated)
if head_name not in entityName2entityTypes:
number_of_heads_not_evaluated_4 += 1
if entitiesEvaluated=="none":
list_of_scores_per_head_with_filter, idx2relation_tail_in_scores = evaluate_all_relation_tail_pairs_v2 (head_id, id2entity, relation2id, id2relation, column, fact, model, arity, device, sparsifier, typeId2frequency, entityId2entityTypes, unk_type_id, type2id, id2type)
elif len(list_of_scores_per_head_with_filter)==0 or len(list_of_scores_per_head_with_filter[0])<10:
number_of_heads_not_evaluated_1 += 1
if entitiesEvaluated=="none":
list_of_scores_per_head_with_filter, idx2relation_tail_in_scores = evaluate_all_relation_tail_pairs_v2 (head_id, id2entity, relation2id, id2relation, column, fact, model, arity, device, sparsifier, typeId2frequency, entityId2entityTypes, unk_type_id, type2id, id2type)
if len(list_of_scores_per_head_with_filter)>0:
number_of_actual_heads_evaluated += 1
score_with_with_filter = np.concatenate(list_of_scores_per_head_with_filter).ravel()
scores = score_with_with_filter
final_results = my_precision_and_recall_at_k (scores, head_id, head2relation2tails, [10,5,3,1,'all'], id2entity, id2relation, idx2relation_tail_in_scores)
my_precision10 += final_results[0]
my_recall10 += final_results[1]
my_map10 += final_results[2]
my_precision5 += final_results[3]
my_recall5 += final_results[4]
my_map5 += final_results[5]
my_precision3 += final_results[6]
my_recall3 += final_results[7]
my_map3 += final_results[8]
my_precision1 += final_results[9]
my_recall1 += final_results[10]
my_map1 += final_results[11]
my_precisionAll += final_results[12]
my_recallAll += final_results[13]
my_mapAll += final_results[14]
my_ndcg10 += final_results[15]
my_ndcg5 += final_results[16]
my_ndcg3 += final_results[17]
my_ndcg1 += final_results[18]
my_ndcgAll += final_results[19]
print("number_of_actual_heads_evaluated:", number_of_actual_heads_evaluated)
print("number of heads not evaluated (because the filter was too strict and it did not generated any relation-tail pairs):", number_of_heads_not_evaluated_1)
print("number of heads not evaluated (because the head was already evaluated):", number_of_heads_not_evaluated_3)
print("number of heads not evaluated (because the head did not have a type):", number_of_heads_not_evaluated_4)
print("number of heads not evaluated (because the head did not have a type):", number_of_heads_not_evaluated_4)
print("number of heads not evaluated (because the head is not in the GT):", number_of_heads_not_evaluated_5)
if number_of_actual_heads_evaluated != 0:
print("my_precision10:", my_precision10, "/", number_of_actual_heads_evaluated, "=", my_precision10/number_of_actual_heads_evaluated)
print("my_recall10:", my_recall10, "/", number_of_actual_heads_evaluated, "=", my_recall10/number_of_actual_heads_evaluated)
print("my_map10:", my_map10, "/", number_of_actual_heads_evaluated, "=", my_map10/number_of_actual_heads_evaluated)
print("my_precision5:", my_precision5, "/", number_of_actual_heads_evaluated, "=", my_precision5/number_of_actual_heads_evaluated)
print("my_recall5:", my_recall5, "/", number_of_actual_heads_evaluated, "=", my_recall5/number_of_actual_heads_evaluated)
print("my_map5:", my_map5, "/", number_of_actual_heads_evaluated, "=", my_map5/number_of_actual_heads_evaluated)
print("my_precision3:", my_precision3, "/", number_of_actual_heads_evaluated, "=", my_precision3/number_of_actual_heads_evaluated)
print("my_recall3:", my_recall3, "/", number_of_actual_heads_evaluated, "=", my_recall3/number_of_actual_heads_evaluated)
print("my_map3:", my_map3, "/", number_of_actual_heads_evaluated, "=", my_map3/number_of_actual_heads_evaluated)
print("my_precision1:", my_precision1, "/", number_of_actual_heads_evaluated, "=", my_precision1/number_of_actual_heads_evaluated)
print("my_recall1:", my_recall1, "/", number_of_actual_heads_evaluated, "=", my_recall1/number_of_actual_heads_evaluated)
print("my_map1:", my_map1, "/", number_of_actual_heads_evaluated, "=", my_map1/number_of_actual_heads_evaluated)
print("my_precisionAll:", my_precisionAll, "/", number_of_actual_heads_evaluated, "=", my_precisionAll/number_of_actual_heads_evaluated)
print("my_recallAll:", my_recallAll, "/", number_of_actual_heads_evaluated, "=", my_recallAll/number_of_actual_heads_evaluated)
print("my_mapAll:", my_mapAll, "/", number_of_actual_heads_evaluated, "=", my_mapAll/number_of_actual_heads_evaluated)
print("my_ndcg10:", my_ndcg10, "/", number_of_actual_heads_evaluated, "=", my_ndcg10/number_of_actual_heads_evaluated)
print("my_ndcg5:", my_ndcg5, "/", number_of_actual_heads_evaluated, "=", my_ndcg5/number_of_actual_heads_evaluated)
print("my_ndcg3:", my_ndcg3, "/", number_of_actual_heads_evaluated, "=", my_ndcg3/number_of_actual_heads_evaluated)
print("my_ndcg1:", my_ndcg1, "/", number_of_actual_heads_evaluated, "=", my_ndcg1/number_of_actual_heads_evaluated)
print("my_ndcgAll:", my_ndcgAll, "/", number_of_actual_heads_evaluated, "=", my_ndcgAll/number_of_actual_heads_evaluated)
print("\n")
my_precision10 = my_precision10/number_of_actual_heads_evaluated
my_precision5 = my_precision5/number_of_actual_heads_evaluated
my_precision3 = my_precision3/number_of_actual_heads_evaluated
my_precision1 = my_precision1/number_of_actual_heads_evaluated
my_precisionAll = my_precisionAll/number_of_actual_heads_evaluated
my_recall10 = my_recall10/number_of_actual_heads_evaluated
my_recall5 = my_recall5/number_of_actual_heads_evaluated
my_recall3 = my_recall3/number_of_actual_heads_evaluated
my_recall1 = my_recall1/number_of_actual_heads_evaluated
my_recallAll = my_recallAll/number_of_actual_heads_evaluated
my_map10 = my_map10/number_of_actual_heads_evaluated
my_map5 = my_map5/number_of_actual_heads_evaluated
my_map3 = my_map3/number_of_actual_heads_evaluated
my_map1 = my_map1/number_of_actual_heads_evaluated
my_mapAll = my_mapAll/number_of_actual_heads_evaluated
my_ndcg10 = my_ndcg10/number_of_actual_heads_evaluated
my_ndcg5 = my_ndcg5/number_of_actual_heads_evaluated
my_ndcg3 = my_ndcg3/number_of_actual_heads_evaluated
my_ndcg1 = my_ndcg1/number_of_actual_heads_evaluated
my_ndcgAll = my_ndcgAll/number_of_actual_heads_evaluated
print(str(my_recall10) + "\t" + str(my_recall5) + "\t" + str(my_mapAll) + "\t" + str(my_ndcgAll))
else:
print("The number of actual heads evaluated is 0, meaning that the filter is too strict!")
def sort_new_batch_according_to_arity_2 (new_positive_facts_indexes_with_different_arity, new_negative_facts_indexes_with_different_arity):
list_of_arities_in_pos_and_neg_facts = []
x_by_arities = []
y_by_arities = []
arity2positiveFacts = {}
for pos_fact in new_positive_facts_indexes_with_different_arity:
arity = len(pos_fact)//2
if arity not in list_of_arities_in_pos_and_neg_facts:
list_of_arities_in_pos_and_neg_facts.append(arity)
if arity not in arity2positiveFacts:
arity2positiveFacts[arity] = []
arity2positiveFacts[arity].append(pos_fact)
arity2negativeFacts = {}
for neg_fact in new_negative_facts_indexes_with_different_arity:
arity = len(neg_fact)//2
if arity not in list_of_arities_in_pos_and_neg_facts:
list_of_arities_in_pos_and_neg_facts.append(arity)
if arity not in arity2negativeFacts:
arity2negativeFacts[arity] = []
arity2negativeFacts[arity].append(neg_fact)
list_of_arities_in_pos_and_neg_facts.sort()
x_by_arities = []
y_by_arities = []
for arity in list_of_arities_in_pos_and_neg_facts: