/
representation_aggregation.py
485 lines (418 loc) · 20.4 KB
/
representation_aggregation.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
import os
import pathlib
import json
import argparse
import csv
import sys
csv.field_size_limit(sys.maxsize)
import pickle
import numpy as np
from eval.eval_bm25_coliee2021 import read_label_file, ranking_eval
from eval.eval_dpr_coliee2021 import read_run_separate
def read_encoded_ctx_file(encoded_ctx_file: str):
"""
Returns dictionary containing the encoded passages and their vector embeddings
:param encoded_ctx_file:
:return:
"""
with open(encoded_ctx_file, mode="rb") as f:
p_emb = pickle.load(f)
# create dictionary, so that i can lookup embeddings
p_emb_dict = {}
for passage in p_emb:
p_emb_dict.update({passage[0]: passage[1]})
return p_emb_dict
def read_encoded_qa_file(encoded_qa_file: str):
"""
Returns dictionary containing the encoded queries and their vector embeddings
:param encoded_qa_file:
:return:
"""
# now i need the query encoder
with open(encoded_qa_file, mode="rb") as f:
q_emb = pickle.load(f)
# create dictionary, so that i can lookup embeddings
q_emb_dict = {}
for passage in q_emb:
q_emb_dict.update({passage[0][0]: passage[1]})
return q_emb_dict
def dict_ids_with_embeddings(q_emb_dict):
# dictionary with query_id and all the embeddings to it
q_ids_w_emb = {}
for key, value in q_emb_dict.items():
query_id = key.split('_')[0]
if q_ids_w_emb.get(query_id):
embeddings = q_ids_w_emb.get(query_id)
embeddings.append(value)
q_ids_w_emb.update({query_id: embeddings})
else:
q_ids_w_emb.update({query_id: [value]})
return q_ids_w_emb
def aggregate_emb_avg(q_ids_w_emb: dict):
# avg
q_ids_avg_emb = {}
for key, value in q_ids_w_emb.items():
q_ids_avg_emb.update({key: np.mean(value, axis=0)})
return q_ids_avg_emb
def aggregate_emb_sum(q_ids_w_emb: dict):
# avg
q_ids_avg_emb = {}
for key, value in q_ids_w_emb.items():
q_ids_avg_emb.update({key: np.sum(value, axis=0)})
return q_ids_avg_emb
def aggregate_emb_max(q_ids_w_emb: dict):
# avg
q_ids_avg_emb = {}
for key, value in q_ids_w_emb.items():
q_ids_avg_emb.update({key: np.max(value, axis=0)})
return q_ids_avg_emb
def aggregate_emb_min(q_ids_w_emb: dict):
# avg
q_ids_avg_emb = {}
for key, value in q_ids_w_emb.items():
q_ids_avg_emb.update({key: np.min(value, axis=0)})
return q_ids_avg_emb
def aggregate_emb_scores(q_ids_w_emb: dict):
p_ids_avg_emb = {}
for key, value in q_ids_w_emb.items():
list_emb = [emb[0] for emb in value]
list_weights = [emb[1] for emb in value]
p_ids_avg_emb.update({key: np.dot(list_weights, list_emb)})
return p_ids_avg_emb
def aggregate_emb_vrrf(q_ids_w_emb: dict):
p_ids_avg_emb = {}
for key, value in q_ids_w_emb.items():
list_emb = [emb[0] for emb in value]
list_weights = [(1/(60+(1001- emb[1]))) for emb in value]
p_ids_avg_emb.update({key: np.dot(list_weights, list_emb)})
return p_ids_avg_emb
def aggregate_ids_with_embeddings(q_ids_w_emb: dict, aggregation_mode: str):
"""
Aggregates the embeddings according to the aggregation mode
:param q_ids_w_emb:
:return:
"""
if aggregation_mode == 'avg':
q_ids_agg_emb = aggregate_emb_avg(q_ids_w_emb)
elif aggregation_mode == 'sum':
q_ids_agg_emb = aggregate_emb_sum(q_ids_w_emb)
elif aggregation_mode == 'max':
q_ids_agg_emb = aggregate_emb_max(q_ids_w_emb)
elif aggregation_mode == 'min':
q_ids_agg_emb = aggregate_emb_min(q_ids_w_emb)
elif aggregation_mode == 'vrrf':
q_ids_agg_emb = aggregate_emb_vrrf(q_ids_w_emb)
elif aggregation_mode == 'vscores':
q_ids_agg_emb = aggregate_emb_scores(q_ids_w_emb)
elif aggregation_mode == 'vranks':
q_ids_agg_emb = aggregate_emb_scores(q_ids_w_emb)
else:
print('No valid aggregation mode entered')
return None
return q_ids_agg_emb
def aggregate_p_in_run(run: dict, p_emb_dict: dict):
"""
aggregates passages from run
:param run:
:param p_emb_dict:
:return:
"""
run_p_embs = {}
for q_id, retrieved_lists in run.items():
run_p_embs.update({q_id: {}})
for q_p_number, ranked_list in retrieved_lists.items():
for p_id, score in ranked_list.items():
p_emb = p_emb_dict.get(p_id)
p_id_short = p_id.split('_')[0]
if run_p_embs.get(q_id).get(p_id_short):
list_emb = run_p_embs.get(q_id).get(p_id_short)
list_emb.append(p_emb)
run_p_embs.get(q_id).update({p_id_short: list_emb})
else:
run_p_embs.get(q_id).update({p_id_short: [p_emb]})
return run_p_embs
def aggregate_run_in_p_with_scores(run: dict, p_emb_dict: dict):
run_p_embs = {}
for q_id, retrieved_lists in run.items():
run_p_embs.update({q_id: {}})
for q_p_number, ranked_list in retrieved_lists.items():
for p_id, score in ranked_list.items():
p_emb = p_emb_dict.get(p_id)
p_id_short = p_id.split('_')[0]
if run_p_embs.get(q_id).get(p_id_short):
list_emb = run_p_embs.get(q_id).get(p_id_short)
list_emb.append((p_emb, score))
run_p_embs.get(q_id).update({p_id_short: list_emb})
else:
run_p_embs.get(q_id).update({p_id_short: [(p_emb, score)]})
return run_p_embs
def aggregate_passage_embeddings_in_run(run: dict, p_emb_dict: dict, aggregation_mode: str):
# first i do only from the ranked lists
if aggregation_mode=='vrrf' or aggregation_mode=='vranks' or aggregation_mode=='vscores':
run_p_embs = aggregate_run_in_p_with_scores(run, p_emb_dict)
else:
run_p_embs = aggregate_p_in_run(run, p_emb_dict)
# now for each document, merge the same documents which overlap and then average/sum/min/max
run_q_id_p_id_aggregated = {}
for q_id, retrieved_lists in run_p_embs.items():
p_ids_agg_emb = aggregate_ids_with_embeddings(retrieved_lists, aggregation_mode)
run_q_id_p_id_aggregated.update({q_id: p_ids_agg_emb})
return run_q_id_p_id_aggregated
def aggregate_passage_embeddings_whole_doc(run: dict, p_emb_dict: dict, aggregation_mode: str):
p_ids_w_emb = dict_ids_with_embeddings(p_emb_dict)
p_ids_agg_emb = aggregate_ids_with_embeddings(p_ids_w_emb, aggregation_mode)
# now add this to the run, so that i have the same representation as for the first option
run_pd_id_emb_agg = {}
for q_id, retrieved_lists in run.items():
run_pd_id_emb_agg.update({q_id: {}})
for q_p_number, ranked_list in retrieved_lists.items():
for p_id, score in ranked_list.items():
p_id_short = p_id.split('_')[0]
run_pd_id_emb_agg.get(q_id).update({p_id_short: p_ids_agg_emb.get(p_id_short)})
return run_pd_id_emb_agg
def score_run_dot_product(run_pd_id_emb_agg: dict, q_ids_agg_emb: dict):
"""
Score the runs with the dot product between the query and candidate embedding
:param run_pd_id_emb_agg:
:param q_ids_agg_emb:
:return:
"""
run_scores_aggregated_emb = {}
for q_id, retrieved_list in run_pd_id_emb_agg.items():
run_scores_aggregated_emb.update({q_id: {}})
q_emb = q_ids_agg_emb.get(q_id)
for candidate_id, candidate_emb in retrieved_list.items():
# normally i dont want to have int here... but maybe then trec eval works...
run_scores_aggregated_emb.get(q_id).update({candidate_id: int(np.vdot(q_emb, candidate_emb))})
run_scores_aggregated_emb_sorted = {}
for q_id, run in run_scores_aggregated_emb.items():
run_scores_aggregated_emb_sorted.update(
{q_id: {k: v for k, v in sorted(run.items(), key=lambda item: item[1], reverse=True)}})
return run_scores_aggregated_emb_sorted
def aggregate_eval(encoded_ctx_file, encoded_qa_file, output_file, label_file, aggregation_mode, candidate_mode, output_dir, output_file_name):
"""
reads in embeddings from query and candidate file, aggregates them according to the candidate and aggregation mode
and evaluates the ranking
:param encoded_ctx_file:
:param encoded_qa_file:
:param output_file:
:param label_file:
:param candidate_mode:
:param aggregation_mode:
:return:
"""
p_emb_dict = read_encoded_ctx_file(encoded_ctx_file)
q_emb_dict = read_encoded_qa_file(encoded_qa_file)
# now open the output to create the matchings
if aggregation_mode == 'vrrf' or aggregation_mode == 'vranks':
run = read_run_separate(output_file, scores="ranks")
else:
run = read_run_separate(output_file, scores="scores")
qrels = read_label_file(label_file)
# different pooling strategies: pool the query and passage document representation
# passage aggregation: first pool independently, then maybe pool with interaction? lets see...
# first for the query
q_ids_w_emb = dict_ids_with_embeddings(q_emb_dict)
if aggregation_mode=='vscores' or aggregation_mode=='vrrf' or aggregation_mode=='vranks':
q_ids_agg_emb = aggregate_ids_with_embeddings(q_ids_w_emb, 'sum')
else:
q_ids_agg_emb = aggregate_ids_with_embeddings(q_ids_w_emb, aggregation_mode)
# then aggregate the candidate documents
# stop i can only aggregate the embeddings for the passages which got retrieved by one document!
# so i also need to take into account the run and the top1000s
# two possibilities: take the embeddings which are only in the retrieved list
# or take the document embedding from the whole corpus
# first i do only from the ranked lists
if candidate_mode == 'p_from_retrieved_list':
run_pd_id_emb_agg = aggregate_passage_embeddings_in_run(run, p_emb_dict, aggregation_mode)
# i could also make a weighting: how many passages of the document got retrieved, how many not
# (or influence on the representation of the retrieved passages on the overall representation)
# or include homogeneity (how many passages got retrieved)
# or weighting of the ranks of the embeddings of the passages! then the more often!
# embedding multiply with the reciprical rank of the document! then the overlap and the embedding
# this is the second option where the candidate document embedding consists of all passages
elif candidate_mode == 'p_from_whole_doc':
run_pd_id_emd_agg = aggregate_passage_embeddings_whole_doc(run, p_emb_dict, aggregation_mode)
# will only work with a harder cutoff i think... maybe @200 or 500
# now i have the aggregated embeddings of the query document and the candidate documents in the run form
# now score run with the dot product of the embeddings
run_scores_emb_agg = score_run_dot_product(run_pd_id_emb_agg, q_ids_agg_emb)
with open(os.path.join(output_dir, 'run_aggregated_test_{}.pickle'.format(aggregation_mode)), 'wb') as f:
pickle.dump(run_scores_emb_agg, f)
# then evaluate run with evaluation of whole document runs right?
ranking_eval(qrels, run_scores_emb_agg, output_dir, output_file_name)
def main(args):
aggregate_eval(args.encoded_ctx_file, args.encoded_qa_file, args.output_top1000s, args.label_file,
args.aggregation_mode, args.candidate_mode, args.output_dir, args.output_file_name)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--encoded_ctx_file",
required=True,
type=str,
default=None,
help="Path to the encoded ctx file, with the encodings of the passages in the corpus"
)
parser.add_argument(
"--encoded_qa_file",
required=True,
type=str,
default=None,
help="Path to the encoded qa file, with the encodings of the query passages",
)
parser.add_argument(
"--output_top1000s",
required=True,
type=str,
default=None,
help="Path to the output file from the dense_retriever.py search with the topNs result",
)
parser.add_argument(
"--label_file",
required=True,
type=str,
default=None,
help="Path to the label file",
)
parser.add_argument(
"--aggregation_mode",
required=True,
type=str,
default='sum',
choices = ['sum', 'avg', 'max', 'min', 'vrrf', 'vscores', 'vranks'],
help="Aggregation mode for aggregating the embedding representations of query and candidate documents:"
"choose between sum/max/min/avg/vrrf/vscores/vranks",
)
parser.add_argument(
"--candidate_mode",
required=True,
type=str,
default='p_from_retrieved_list',
choices=['p_from_retrieved_list', 'p_from_whole_doc'],
help="Determines which paragraph embeddings to choose for aggregation of the candidate document embedding:"
"either take only the embeddings from the passages from the retrieved list or take the embeddings from all passages in the candidate document",
)
parser.add_argument(
"--output_dir",
required=True,
type=str,
default=None,
help="Path to the output directory where the evaluation will be stored",
)
parser.add_argument(
"--output_file_name",
required=True,
type=str,
default='eval.txt',
help="Name of the file containing the evaluation measures",
)
args = parser.parse_args()
main(args)
# mode = ['val', 'vrrf', 'p_from_retrieved_list']
# encoded_ctx_file = '/mnt/c/Users/salthamm/Documents/phd/data/coliee2021/task1/dpr/legal_task2/legalbert/encoded_ctx_file/ctx_separate_para_dense2_0.pkl'
# encoded_qa_file = '/mnt/c/Users/salthamm/Documents/phd/data/coliee2021/task1/dpr/legal_task2/legalbert/encoded_qa_file/{}_separate_para_questions_tensors.pkl'.format(
# mode[0])
# output_file = '/mnt/c/Users/salthamm/Documents/phd/data/coliee2021/task1/dpr/legal_task2/legalbert/output/{}/{}_separate_para_top1000.json'.format(
# mode[0], mode[0])
#
# if mode[0] == 'train':
# label_file = '/mnt/c/Users/salthamm/Documents/phd/data/coliee2021/task1/train/train_wo_val_labels.json'
# elif mode[0] == 'val':
# label_file = '/mnt/c/Users/salthamm/Documents/phd/data/coliee2021/task1/val/val_labels.json'
# elif mode[0] == 'test':
# label_file = '/mnt/c/Users/salthamm/Documents/phd/data/coliee2021/task1/test/task1_test_labels_2021.json'
# else:
# raise ValueError('No valid mode chosen, choose between train, val and test')
#
# aggregation_mode = mode[1]
# candidate_mode = mode[2]
#
# output_dir = '/mnt/c/Users/salthamm/Documents/phd/data/coliee2021/task1/dpr/legal_task2/legalbert/eval/{}'.format(
# mode[0])
# output_file_name = 'eval_dpr_aggregate_embeddings_{}_aggregation_{}.txt'.format(mode[0], aggregation_mode)
#
# p_emb_dict = read_encoded_ctx_file(encoded_ctx_file)
# q_emb_dict = read_encoded_qa_file(encoded_qa_file)
# # now open the output to create the matchings
# if aggregation_mode=='vrrf' or aggregation_mode=='vranks':
# run = read_run_separate(output_file, scores="ranks")
# else:
# run = read_run_separate(output_file, scores="scores")
#
# qrels = read_label_file(label_file)
#
# # different pooling strategies: pool the query and passage document representation
# # passage aggregation: first pool independently, then maybe pool with interaction? lets see...
# # first for the query
# q_ids_w_emb = dict_ids_with_embeddings(q_emb_dict)
# q_ids_agg_emb = aggregate_ids_with_embeddings(q_ids_w_emb, aggregation_mode)
#
# # then aggregate the candidate documents
# # stop i can only aggregate the embeddings for the passages which got retrieved by one document!
# # so i also need to take into account the run and the top1000s
# # two possibilities: take the embeddings which are only in the retrieved list
# # or take the document embedding from the whole corpus
#
# # first i do only from the ranked lists
# if candidate_mode == 'p_from_retrieved_list':
# run_pd_id_emb_agg = aggregate_passage_embeddings_in_run(run, p_emb_dict, aggregation_mode)
#
# if aggregation_mode == 'vrrf' or aggregation_mode == 'vranks' or aggregation_mode == 'vscores':
# run_p_embs = {}
# for q_id, retrieved_lists in run.items():
# run_p_embs.update({q_id: {}})
# for q_p_number, ranked_list in retrieved_lists.items():
# for p_id, score in ranked_list.items():
# p_emb = p_emb_dict.get(p_id)
# p_id_short = p_id.split('_')[0]
# if run_p_embs.get(q_id).get(p_id_short):
# list_emb = run_p_embs.get(q_id).get(p_id_short)
# list_emb.append((p_emb, score))
# run_p_embs.get(q_id).update({p_id_short: list_emb})
# else:
# run_p_embs.get(q_id).update({p_id_short: [(p_emb, score)]})
# run_p_embs = aggregate_run_in_p_with_scores(run, p_emb_dict)
# else:
# run_p_embs = aggregate_p_in_run(run, p_emb_dict)
#
# # now for each document, merge the same documents which overlap and then average/sum/min/max
# run_q_id_p_id_aggregated = {}
# for q_id, retrieved_lists in run_p_embs.items():
# #p_ids_agg_emb = aggregate_ids_with_embeddings(retrieved_lists, aggregation_mode)
# p_ids_avg_emb = {}
# for key, value in retrieved_lists.items():
# list_emb = [emb[0] for emb in value]
# list_weights = [(1 / (60 + (1001 - emb[1]))) for emb in value]
# print(np.dot(list_weights, list_emb).shape())
# p_ids_avg_emb.update({key: np.dot(list_weights, list_emb)})
#
# run_q_id_p_id_aggregated.update({q_id: p_ids_avg_emb})
#
# # i could also make a weighting: how many passages of the document got retrieved, how many not
# # (or influence on the representation of the retrieved passages on the overall representation)
# # or include homogeneity (how many passages got retrieved)
#
# # or weighting of the ranks of the embeddings of the passages! then the more often!
# # embedding multiply with the reciprical rank of the document! then the overlap and the embedding
#
# # this is the second option where the candidate document embedding consists of all passages
# elif candidate_mode == 'p_from_whole_doc':
# run_pd_id_emd_agg = aggregate_passage_embeddings_whole_doc(run, p_emb_dict, aggregation_mode)
# # will only work with a harder cutoff i think... maybe @200 or 500
#
# # now i have the aggregated embeddings of the query document and the candidate documents in the run form
# # now score run with the dot product of the embeddings
# run_scores_emb_agg = score_run_dot_product(run_pd_id_emb_agg, q_ids_agg_emb)
#
# # then evaluate run with evaluation of whole document runs right?
# ranking_eval(qrels, run_scores_emb_agg, output_dir, output_file_name)
# learn: ffn, cnn, transformer? learn on the retrieved embeddings, not on the whole doc
# but i could take into account how many other passages the document has, which did not get retrieved (somehow like bm25 on the passages!)
# which passages got retrieved, how many other passages got retrieved, something like that... to take into account the homogeneity of the doc
# instead of RRF!
# try it out!
# der pool ist ja schon query dependent also kann dann auch die aggregierung query dependent sein
# bei parade genau dasselbe
# überlegungen etwas auf der dokumentenebene zu trainieren
# learn cnn, ffn, transformer, svm on the representations also for document ranking? does that make sense?
# begin with svm, labels from the qrels, and embeddings from the output files!