forked from zhw12/BERTRL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_bertrl.py
More file actions
300 lines (235 loc) · 10.9 KB
/
Copy patheval_bertrl.py
File metadata and controls
300 lines (235 loc) · 10.9 KB
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
import ipdb
import numpy as np
import torch
from sklearn import metrics
from collections import defaultdict, Counter
import argparse
from sklearn.metrics import average_precision_score
import itertools
# from transformers import AutoTokenizer
import re
import os
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--dataset", "-d", type=str,
help="Dataset string")
parser.add_argument("--bertrl_output_dir", "-o", type=str,
help="Output", default='')
parser.add_argument("--additional_suffix", "-suf", type=str,
help="Output", default='')
parser.add_argument("--show_rel", default=False, action='store_true',
help="show_rel ")
parser.add_argument('--head_tail_type', '-ht', type=str, default='',
help='head_tail_type')
parser.add_argument("--total_triples", type=int, default=0,
help="total_triples")
params = parser.parse_args()
params.main_dir = '/home'
# an_sufix = '_anonymized' if params.anonymized else ''
# data_dir = '/scratch/home/hanwen/BertLogic/output_WN18RR_v1_ind/'
# data_dir = f'{params.main_dir}/output_{params.dataset}_ind{an_sufix}'
if not params.bertrl_output_dir:
params.bert_output_dir = f'{params.main_dir}/output_{params.dataset}{params.additional_suffix}'
res = np.load(f'{params.bert_output_dir}/test_results_prediction_scores.npy')
t_res = torch.softmax(torch.from_numpy(res), 1)
bertrl_data_dir = f'{params.main_dir}/bertrl_data/{params.dataset}'
bertrl_data_file = f'{bertrl_data_dir}/test.tsv'
examples = [l.strip().split('\t') for l in open(bertrl_data_file)][1:]
assert len(examples) == res.shape[0], "score should have same size with examples"
dataset2triples = {'WN18RR_ind': 188*2, 'fb237_ind': 205*2, 'nell_ind': 476*2, 'fb237':492*2, 'WN18RR':638*2, 'nell':968*2,
'fb237_ind': 478*2, 'WN18RR_ind':441*2, 'nell_ind':100*2}
data_dir = f'{params.main_dir}/data'
for dataset_dir in os.listdir(data_dir):
test_file = os.path.join(data_dir, dataset_dir, 'test.txt')
if os.path.exists(test_file):
dataset2triples[dataset_dir] = len([l for l in open(test_file)]) * 2
# ipdb.set_trace()
dataset_short = '_'.join(params.dataset.split('_')[:1])
triple_test_data_dir = f'{params.main_dir}/data/{dataset_short}'
if 'inductive' in params.dataset:
triple_test_data_dir += '_ind'
triple_test_file = f'{triple_test_data_dir}/test.txt'
triples_test = [l.strip().split('\t') for l in open(triple_test_file)]
part = ''
if re.search('_rel\d+', params.dataset):
part = re.search('_rel\d+', params.dataset)[0]
# part = '_rel50'
triple_train_file = f'{params.main_dir}/data/{dataset_short}/train{part}.txt'
triples_train = [l.strip().split('\t') for l in open(triple_train_file)]
train_rels = set([tpl[1] for tpl in triples_train])
test_rels = set([tpl[1] for tpl in triples_test])
eid2relation = {i:tpl[1] for i, tpl in enumerate(triples_test * 2)}
if 'inductive' in params.dataset:
total_triples = dataset2triples.get(dataset_short+'_ind', 1000000)
else:
total_triples = dataset2triples.get(dataset_short, 1000000)
eid2pos = {}
eid2neg = defaultdict(list)
labels = []
eid2wrongs = defaultdict(list)
eid2corrects = defaultdict(list)
# another set of evaluation
eid2pos = defaultdict(list)
eid2neg = defaultdict(list)
eid2neg_lv2 = {}
for i, example in enumerate(examples):
label = int(example[0])
eid = example[1].split('-')[2] # test-neg-2-24, train-pos-2
eid = int(eid)
# if label == 1:
if 'pos' in example[1]:
assert label == 1
eid2pos[eid].append(i)
else:
e_negid = example[1].split('-')[3] # test-neg-2-24
eid2neg[eid].append(i)
if eid not in eid2neg_lv2:
eid2neg_lv2[eid] = defaultdict(list)
eid2neg_lv2[eid][e_negid].append(i)
if params.head_tail_type:
total_triples //= 2
if params.total_triples:
total_triples = total_triples
rel2testcnt = Counter()
for tpl in triples_test * 2:
rel2testcnt[tpl[1]] += 1
hit1 = 0
hits = Counter()
rel2hits = defaultdict(Counter)
rel2hits_base = defaultdict(Counter)
ranks = []
for eid, pos_is in eid2pos.items(): # all pos eids with examples
# if params.head_tail_type == 'tail':
# if eid >= total_triples:
# continue
# elif params.head_tail_type == 'head':
# if eid < total_triples:
# continue
# else:
# pass
pos_scores = t_res[pos_is, 1]
pos_max_score = torch.max(pos_scores).item()
neg_is = eid2neg[eid]
neg_eids = eid2neg_lv2.get(eid, [])
geq_j = 1 # rank
if not neg_is: # no negative, only positive
hit1 += 1
eid2corrects[eid].append([pos_is, pos_scores, [], []])
else:
neg_scores = t_res[neg_is, 1]
neg_max_score = torch.max(neg_scores).item()
neg_scores_of_eid = []
neg_lv2_scores_of_eid = defaultdict(list)
neg_scores_lists = []
for neg_eid in eid2neg_lv2[eid]:
neg_is_of_eid = eid2neg_lv2[eid][neg_eid]
neg_scores_ = t_res[neg_is_of_eid, 1] # previously a bug here, previously use neg_is which lowers the hit 2+ performance
neg_max_score_ = torch.max(neg_scores_).item()
neg_scores_of_eid.append(neg_max_score_)
neg_scores_lists.append(neg_scores_.sort(0, descending=True).values.tolist())
_scores_pos = pos_scores.sort(0, descending=True).values.tolist()
for _scores in neg_scores_lists:
for s1, s2 in itertools.zip_longest(_scores_pos, _scores, fillvalue=100):
if s1 < s2 or s1 == 100:
geq_j += 1
break
elif s1 == s2:
continue
else:
break
if geq_j <= 1:
eid2corrects[eid].append([pos_is, pos_scores, neg_is, neg_scores])
else:
if eid not in eid2wrongs:
eid2wrongs[eid].append([pos_is, pos_scores, neg_is, neg_scores])
# for neg_score in neg_scores_of_eid:
# if pos_max_score <= neg_score:
# geq_j += 1
# if pos_max_score > neg_max_score:
# hit1 += 1
# eid2corrects[eid].append([pos_is, pos_scores, neg_is, neg_scores])
# else:
# if eid not in eid2wrongs:
# eid2wrongs[eid].append([pos_is, pos_scores, neg_is, neg_scores])
cnt_neg_of_eid = len(eid2neg_lv2.get(eid, []))
rel = eid2relation[eid]
for hit in [1, 2, 3, 4, 5, 10, 20, 30, 40, 50]:
if geq_j <= hit:
hits[hit] += 1
rel2hits[rel][hit] += 1
if cnt_neg_of_eid + 1 <= hit:
rel2hits_base[rel][hit] += 1
ranks.append(geq_j)
if len(ranks) < total_triples:
ranks += [50] * (total_triples-len(ranks))
hits = {k: hits[k] for k in sorted(hits)}
mrr = np.mean(1 / np.array(ranks))
print('mrr:', mrr)
print(hits)
print({k:round(v/total_triples, 3) for k,v in hits.items()})
def show(eid, item=None, k=5):
if item is None:
if eid in eid2wrongs:
item = eid2wrongs[eid]
else:
item = eid2corrects[eid]
# for eid, item in eid2wrongs.items():
pos_is, pos_scores, neg_is, neg_scores = item[0]
pos_max_i = pos_is[torch.argmax(pos_scores)] # use pos_is to reconstruct example i
neg_max_i = neg_is[torch.argmax(neg_scores)]
neg_topk_is = [neg_is[ind] for ind in torch.topk(neg_scores, k).indices.tolist()]
neg_topk_scores = torch.topk(neg_scores, 5).values
pos_max_score = torch.max(pos_scores).item()
neg_max_score = torch.max(neg_scores).item()
# print(pos_max_score, neg_max_score)
# print(examples[pos_max_i], examples[neg_max_i], sep='\n')
print(examples[pos_max_i])
print('eid:', eid)
print('max pos score:', pos_max_score,
'max neg score:', neg_max_score)
print('-----')
print('max pos i:', pos_max_i, 'max neg i:', neg_max_i)
for i in pos_is:
print(i, t_res[i, 1].item(), '\t', examples[i])
for i in neg_topk_is:
print(i, t_res[i, 1].item(), '\t', examples[i])
print('----------------')
wrong_eids = sorted(list(eid2wrongs))
correct_eids = sorted(list(eid2corrects))
if params.show_rel:
for rel in sorted(rel2hits):
hits = rel2hits[rel]
total_triples_for_this_rel = rel2testcnt[rel]
if params.head_tail_type:
total_triples_for_this_rel //= 2
print(int(rel in train_rels), rel, hits[1], '/', total_triples_for_this_rel, {f'hit@{k}':round(hits[k]/total_triples_for_this_rel, 3) for k in [1, 2]})
for eid in correct_eids:
# print(eid2relation[eid])
print(examples[eid2pos[eid][0]])
# target_eids = sorted(target_eids)
# correct_target_eids = [eid for eid in target_eids if eid in eid2corrects]
# ipdb.set_trace()
if part:
unseen_hits = Counter() # hit for each unseen
unseen_hits_base = Counter() # base method hit for each unseen
unseen_hits_cnt = 0 # total number unseen triples
unseen_hits_rel_cnt = 0 # total number unseen relations
unseen_hits_macro = Counter()
unseen_hits_base_macro = Counter()
for rel, hits in rel2hits.items():
total_triples_for_this_rel = rel2testcnt[rel]
if rel not in train_rels:
unseen_hits_cnt += total_triples_for_this_rel
unseen_hits_rel_cnt += 1
for hit in [1, 2, 3, 4, 5, 10]:
unseen_hits[hit] += rel2hits[rel][hit]
unseen_hits_base[hit] += rel2hits_base[rel][hit]
unseen_hits_macro[hit] += rel2hits[rel][hit] / total_triples_for_this_rel
unseen_hits_base_macro[hit] += rel2hits_base[rel][hit] / total_triples_for_this_rel
print(unseen_hits)
print({f'hit@{k}':round(unseen_hits[k]/unseen_hits_cnt, 3) for k in [1, 2, 3, 4, 5, 10]})
print({f'hit@{k}':round(unseen_hits_macro[k]/unseen_hits_rel_cnt, 3) for k in [1, 2, 3, 4, 5, 10]})
print('----------------')
print(unseen_hits_base)
print({f'hit@{k}':round(unseen_hits_base[k]/unseen_hits_cnt, 3) for k in [1, 2, 3, 4, 5, 10]})
print({f'hit@{k}':round(unseen_hits_base_macro[k]/unseen_hits_rel_cnt, 3) for k in [1, 2, 3, 4, 5, 10]})