-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltr.py
1327 lines (1081 loc) · 55.7 KB
/
ltr.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 torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import datetime
import numpy as np
from torch.utils.data import TensorDataset, ConcatDataset, DataLoader
def torch_dcg_at_k(batch_rankings, cutoff=None, device='cpu'):
'''
ICML-nDCG, which places stronger emphasis on retrieving relevant documents
:param batch_rankings: [batch_size, ranking_size] rankings of labels (either standard or predicted by a system)
:param cutoff: the cutoff position
:return: [batch_size, 1] cumulative gains for each rank position
'''
if cutoff is None: # using whole list
cutoff = batch_rankings.size(1)
batch_numerators = torch.pow(2.0, batch_rankings[:, 0:cutoff]) - 1.0
# no expanding should also be OK due to the default broadcasting
batch_discounts = torch.log2(torch.arange(cutoff, dtype=torch.float, device=device).expand_as(batch_numerators) + 2.0)
batch_dcg_at_k = torch.sum(batch_numerators/batch_discounts, dim=1, keepdim=True)
return batch_dcg_at_k
def torch_dcg_at_ks(batch_rankings, max_cutoff, device='cpu'):
'''
:param batch_rankings: [batch_size, ranking_size] rankings of labels (either standard or predicted by a system)
:param max_cutoff: the maximum cutoff value
:return: [batch_size, max_cutoff] cumulative gains for each ranlok position
'''
batch_numerators = torch.pow(2.0, batch_rankings[:, 0:max_cutoff]) - 1.0
batch_discounts = torch.log2(torch.arange(max_cutoff, dtype=torch.float, device=device).expand_as(batch_numerators) + 2.0)
batch_dcg_at_ks = torch.cumsum(batch_numerators/batch_discounts, dim=1) # dcg w.r.t. each position
return batch_dcg_at_ks
def torch_ndcg_at_k(batch_predict_rankings, batch_ideal_rankings, k=None, device='cpu'):
batch_sys_dcg_at_k = torch_dcg_at_k(batch_predict_rankings, cutoff=k, device=device) # only using the cumulative gain at the final rank position
batch_ideal_dcg_at_k = torch_dcg_at_k(batch_ideal_rankings, cutoff=k, device=device)
batch_ndcg_at_k = batch_sys_dcg_at_k / batch_ideal_dcg_at_k
return batch_ndcg_at_k
def torch_ndcg_at_ks(batch_predict_rankings, batch_ideal_rankings, ks=None, device='cpu'):
valid_max_cutoff = batch_predict_rankings.size(1)
used_ks = [k for k in ks if k<=valid_max_cutoff] if valid_max_cutoff < max(ks) else ks
inds = torch.from_numpy(np.asarray(used_ks) - 1).type(torch.long)
batch_sys_dcgs = torch_dcg_at_ks(batch_predict_rankings, max_cutoff=max(used_ks), device=device)
batch_sys_dcg_at_ks = batch_sys_dcgs[:, inds] # get cumulative gains at specified rank positions
batch_ideal_dcgs = torch_dcg_at_ks(batch_ideal_rankings, max_cutoff=max(used_ks), device=device)
batch_ideal_dcg_at_ks = batch_ideal_dcgs[:, inds]
batch_ndcg_at_ks = batch_sys_dcg_at_ks / batch_ideal_dcg_at_ks
if valid_max_cutoff < max(ks):
padded_ndcg_at_ks = torch.zeros(batch_predict_rankings.size(0), len(ks))
padded_ndcg_at_ks[:, 0:len(used_ks)] = batch_ndcg_at_ks
return padded_ndcg_at_ks
else:
return batch_ndcg_at_ks
""" processing on letor datasets """
import os
import pickle
from pathlib import Path
import torch.utils.data as data
## due to the restriction of 4GB ##
max_bytes = 2**31 - 1
def pickle_save(target, file):
bytes_out = pickle.dumps(target, protocol=4)
with open(file, 'wb') as f_out:
for idx in range(0, len(bytes_out), max_bytes):
f_out.write(bytes_out[idx:idx + max_bytes])
def pickle_load(file):
file_size = os.path.getsize(file)
with open(file, 'rb') as f_in:
bytes_in = bytearray(0)
for _ in range(0, file_size, max_bytes):
bytes_in += f_in.read(max_bytes)
data = pickle.loads(bytes_in)
return data
from enum import Enum, unique, auto
@unique
class MASK_TYPE(Enum):
""" Supported ways of masking labels """
rand_mask_all = auto()
rand_mask_rele = auto()
@unique
class LABEL_TYPE(Enum):
""" The types of labels of supported datasets """
MultiLabel = auto()
Permutation = auto()
@unique
class SPLIT_TYPE(Enum):
""" The split-part of a dataset """
Train = auto()
Test = auto()
Validation = auto()
from sklearn.preprocessing import MinMaxScaler, RobustScaler, StandardScaler
SCALER_ID = ['MinMaxScaler', 'RobustScaler', 'StandardScaler', "SLog1P"]
SCALER_LEVEL = ['QUERY', 'DATASET']
MSLETOR_SEMI = ['MQ2007_Semi', 'MQ2008_Semi']
MSLETOR_LIST = ['MQ2007_List', 'MQ2008_List']
MSLETOR_SUPER = ['MQ2007_Super', 'MQ2008_Super']
MSLETOR = ['MQ2007_Super', 'MQ2008_Super', 'MQ2007_Semi', 'MQ2008_Semi', 'MQ2007_List', 'MQ2008_List']
'''
The dataset used in the IRGAN paper, which is a revised version of MQ2008_Semi by adding some document vectors per query
in order to mimic unlabeled documents. Unfortunately, the details on how to generate these personally added documents
are not described.
'''
IRGAN_MQ2008_SEMI = ['IRGAN_MQ2008_Semi']
MSLRWEB = ['MSLRWEB10K', 'MSLRWEB30K']
YAHOO_LTR = ['Set1', 'Set2']
YAHOO_LTR_5Fold = ['5FoldSet1', '5FoldSet2']
ISTELLA_LTR = ['Istella_S', 'Istella', 'Istella_X']
ISTELLA_MAX = 1000000 # As ISTELLA contain extremely large features, e.g., 1.79769313486e+308, we replace features of this kind with a constant 1000000
GLTR_LIBSVM = ['LTR_LibSVM', 'LTR_LibSVM_K']
GLTR_LETOR = ['LETOR', 'LETOR_K']
def get_scaler(scaler_id):
""" Initialize the scaler-object correspondingly """
assert scaler_id in SCALER_ID
if scaler_id == 'MinMaxScaler':
scaler = MinMaxScaler()
elif scaler_id == 'RobustScaler':
scaler = RobustScaler()
elif scaler_id == 'StandardScaler':
scaler = StandardScaler()
return scaler
def get_data_meta(data_id=None):
""" Get the meta-information corresponding to the specified dataset """
if data_id in MSLRWEB:
max_rele_level = 4
label_type = LABEL_TYPE.MultiLabel
num_features = 136
has_comment = False
fold_num = 5
elif data_id in MSLETOR_SUPER:
max_rele_level = 2
label_type = LABEL_TYPE.MultiLabel
num_features = 46
has_comment = True
fold_num = 5
elif data_id in MSLETOR_SEMI:
max_rele_level = 2
label_type = LABEL_TYPE.MultiLabel
num_features = 46
has_comment = True
fold_num = 5
elif data_id in MSLETOR_LIST:
max_rele_level = None
label_type = LABEL_TYPE.Permutation
num_features = 46
has_comment = True
fold_num = 5
elif data_id in YAHOO_LTR:
max_rele_level = 4
label_type = LABEL_TYPE.MultiLabel
num_features = 700 # libsvm format, rather than uniform number
has_comment = False
fold_num = 1
elif data_id in YAHOO_LTR_5Fold:
max_rele_level = 4
label_type = LABEL_TYPE.MultiLabel
num_features = 700 # libsvm format, rather than uniform number
has_comment = False
fold_num = 5
elif data_id in ISTELLA_LTR:
max_rele_level = 4
label_type = LABEL_TYPE.MultiLabel
num_features = 220 # libsvm format, rather than uniform number
fold_num = 1
if data_id in ['Istella_S', 'Istella']:
has_comment = False
else:
has_comment = True
else:
raise NotImplementedError
data_meta = dict(num_features=num_features, has_comment=has_comment, label_type=label_type,
max_rele_level=max_rele_level, fold_num=fold_num)
return data_meta
def np_arg_shuffle_ties(vec, descending=True):
''' the same as np_shuffle_ties, but return the corresponding indice '''
if len(vec.shape) > 1:
raise NotImplementedError
else:
length = vec.shape[0]
perm = np.random.permutation(length)
if descending:
sorted_shuffled_vec_inds = np.argsort(-vec[perm])
else:
sorted_shuffled_vec_inds = np.argsort(vec[perm])
shuffle_ties_inds = perm[sorted_shuffled_vec_inds]
return shuffle_ties_inds
def _parse_docid(comment):
parts = comment.strip().split()
return parts[2]
def _parse_qid_tok(tok):
assert tok.startswith('qid:')
return tok[4:]
def get_buffer_file_name(data_id, file, data_dict, presort=None):
""" Generate the file name """
min_rele = data_dict['min_rele']
if min_rele is not None and min_rele > 0:
fi_suffix = '_'.join(['MiR', str(min_rele)])
else:
fi_suffix = ''
min_docs = data_dict['min_docs']
if min_docs is not None and min_docs > 0:
if len(fi_suffix)>0:
fi_suffix = '_'.join([fi_suffix, 'MiD', str(min_docs)])
else:
fi_suffix = '_'.join(['MiD', str(min_docs)])
res_suffix = ''
if data_dict['binary_rele']:
res_suffix += '_B'
if data_dict['unknown_as_zero']:
res_suffix += '_UO'
pq_suffix = '_'.join([fi_suffix, 'PerQ']) if len(fi_suffix) > 0 else 'PerQ'
assert presort is not None
if presort: pq_suffix = '_'.join([pq_suffix, 'PreSort'])
# plus scaling
scale_data = data_dict['scale_data']
scaler_id = data_dict['scaler_id'] if 'scaler_id' in data_dict else None
scaler_level = data_dict['scaler_level'] if 'scaler_level' in data_dict else None
if scale_data:
assert scaler_id is not None and scaler_id in SCALER_ID and scaler_level in SCALER_LEVEL
if 'DATASET' == scaler_level:
pq_suffix = '_'.join([pq_suffix, 'DS', scaler_id])
else:
pq_suffix = '_'.join([pq_suffix, 'QS', scaler_id])
if data_id in YAHOO_LTR:
perquery_file = file[:file.find('.txt')].replace(data_id.lower() + '.', 'Buffered' + data_id + '/') + '_' + pq_suffix + res_suffix + '.np'
elif data_id in ISTELLA_LTR:
perquery_file = file[:file.find('.txt')].replace(data_id, 'Buffered_' + data_id) + '_' + pq_suffix + res_suffix + '.np'
else:
perquery_file = file[:file.find('.txt')].replace('Fold', 'BufferedFold') + '_' + pq_suffix + res_suffix +'.np'
return perquery_file
def resize_with_padding(input, target_size, padding=0):
output = np.zeros(target_size)
ori_length = len(input)
limitation = min(ori_length, target_size)
output[0:limitation] = input[0:limitation]
output[limitation+1::] = padding
return output
def iter_lines(lines, has_targets=True, one_indexed=True, missing=0.0, has_comment=False):
"""
Transforms an iterator of lines to an iterator of LETOR rows. Each row is represented by a (x, y, qid, comment) tuple.
Parameters
----------
lines : iterable of lines Lines to parse.
has_targets : bool, optional, i.e., the relevance label
Whether the file contains targets. If True, will expect the first token every line to be a real representing
the sample's target (i.e. score). If False, will use -1 as a placeholder for all targets.
one_indexed : bool, optional, i.e., whether the index of the first feature is 1
Whether feature ids are one-indexed. If True, will subtract 1 from each feature id.
missing : float, optional
Placeholder to use if a feature value is not provided for a sample.
Yields
------
x : array of floats Feature vector of the sample.
y : float Target value (score) of the sample, or -1 if no target was parsed.
qid : object Query id of the sample. This is currently guaranteed to be a string.
comment : str Comment accompanying the sample.
"""
for line in lines:
#print(line)
if has_comment:
data, _, comment = line.rstrip().partition('#')
toks = data.split()
else:
toks = line.rstrip().split()
num_features = 0
feature_vec = np.repeat(missing, 8)
std_score = -1.0
if has_targets:
std_score = float(toks[0])
toks = toks[1:]
qid = _parse_qid_tok(toks[0])
for tok in toks[1:]:
fid, _, val = tok.partition(':')
fid = int(fid)
val = float(val)
if one_indexed:
fid -= 1
assert fid >= 0
while len(feature_vec) <= fid:
ori_length = len(feature_vec)
new_feature_vec = np.ndarray([ori_length * 2])
new_feature_vec[0:ori_length] = feature_vec
orig = len(feature_vec)
feature_vec = new_feature_vec
feature_vec[orig:orig*2] = missing
#input('sdfsdfsdfsdfsdf')
# feature_vec.resize(len(feature_vec) * 2)
# feature_vec[orig:orig * 2] = missing
feature_vec[fid] = val
num_features = max(fid + 1, num_features)
assert num_features > 0
# feature_vec.resize(num_features)
feature_vec = resize_with_padding(feature_vec, num_features, 0)
if has_comment:
yield (feature_vec, std_score, qid, comment)
else:
yield (feature_vec, std_score, qid)
def parse_letor(source, has_targets=True, one_indexed=True, missing=0.0, has_comment=False):
"""
Parses a LETOR dataset from `source`.
Parameters
----------
source : string or iterable of lines String, file, or other file-like object to parse.
has_targets : bool, optional
one_indexed : bool, optional
missing : float, optional
Returns
-------
X : array of arrays of floats Feature matrix (see `iter_lines`).
y : array of floats Target vector (see `iter_lines`).
qids : array of objects Query id vector (see `iter_lines`).
comments : array of strs Comment vector (see `iter_lines`).
"""
max_width = 0
feature_vecs, std_scores, qids = [], [], []
if has_comment:
comments = []
it = iter_lines(source, has_targets=has_targets, one_indexed=one_indexed, missing=missing, has_comment=has_comment)
if has_comment:
for f_vec, s, qid, comment in it:
feature_vecs.append(f_vec)
std_scores.append(s)
qids.append(qid)
comments.append(comment)
max_width = max(max_width, len(f_vec))
else:
for f_vec, s, qid in it:
feature_vecs.append(f_vec)
std_scores.append(s)
qids.append(qid)
max_width = max(max_width, len(f_vec))
assert max_width > 0
all_features_mat = np.ndarray((len(feature_vecs), max_width), dtype=np.float64)
all_features_mat.fill(missing)
for i, x in enumerate(feature_vecs):
all_features_mat[i, :len(x)] = x
all_labels_vec = np.array(std_scores)
if has_comment:
docids = [_parse_docid(comment) for comment in comments]
#features, std_scores, qids, docids
return all_features_mat, all_labels_vec, qids, docids
else:
# features, std_scores, qids
return all_features_mat, all_labels_vec, qids
def clip_query_data(qid, list_docids=None, feature_mat=None, std_label_vec=None, binary_rele=False,
unknown_as_zero=False, clip_query=None, min_docs=None, min_rele=1, presort=None):
""" Clip the data associated with the same query if required """
if binary_rele: std_label_vec = np.clip(std_label_vec, a_min=-10, a_max=1) # to binary labels
if unknown_as_zero: std_label_vec = np.clip(std_label_vec, a_min=0, a_max=10) # convert unknown as zero
if clip_query:
if feature_mat.shape[0] < min_docs: # skip queries with documents that are fewer the pre-specified min_docs
return None
if (std_label_vec > 0).sum() < min_rele:
# skip queries with no standard relevant documents, since there is no meaning for both training and testing.
return None
assert presort is not None
if presort:
'''
Possible advantages: 1> saving time for evaluation;
2> saving time for some models, say the ones need optimal ranking
'''
des_inds = np_arg_shuffle_ties(std_label_vec, descending=True) # sampling by shuffling ties
feature_mat, std_label_vec = feature_mat[des_inds], std_label_vec[des_inds]
'''
if list_docids is None:
list_docids = None
else:
list_docids = []
for ind in des_inds:
list_docids.append(list_docids[ind])
'''
return (qid, feature_mat, std_label_vec)
def get_scaler_setting(data_id, scaler_id=None):
"""
A default scaler-setting for loading a dataset
:param data_id:
:param grid_search: used for grid-search
:return:
"""
''' According to {Introducing {LETOR} 4.0 Datasets}, "QueryLevelNorm version: Conduct query level normalization based on data in MIN version. This data can be directly used for learning. We further provide 5 fold partitions of this version for cross fold validation".
--> Thus there is no need to perform query_level_scale again for {MQ2007_super | MQ2008_super | MQ2007_semi | MQ2008_semi}
--> But for {MSLRWEB10K | MSLRWEB30K}, the query-level normalization is ## not conducted yet##.
--> For {Yahoo_LTR_Set_1 | Yahoo_LTR_Set_1 }, the query-level normalization is already conducted.
--> For Istella! LETOR, the query-level normalization is not conducted yet.
We note that ISTELLA contains extremely large features, e.g., 1.79769313486e+308, we replace features of this kind with a constant 1000000.
'''
if scaler_id is None:
if data_id in MSLRWEB or data_id in ISTELLA_LTR:
scale_data = True
scaler_id = 'StandardScaler' # ['MinMaxScaler', 'StandardScaler']
scaler_level = 'QUERY' # SCALER_LEVEL = ['QUERY', 'DATASET']
else:
scale_data = False
scaler_id = None
scaler_level = None
else:
scale_data = True
scaler_level = 'QUERY'
return scale_data, scaler_id, scaler_level
def iter_queries(in_file, presort=None, data_dict=None, scale_data=None, scaler_id=None, perquery_file=None, buffer=True):
'''
Transforms an iterator of rows to an iterator of queries (i.e., a unit of all the documents and labels associated
with the same query). Each query is represented by a (qid, feature_mat, std_label_vec) tuple.
:param in_file:
:param has_comment:
:param query_level_scale: perform query-level scaling, say normalization
:param scaler: MinMaxScaler | RobustScaler
:param unknown_as_zero: if not labled, regard the relevance degree as zero
:return:
'''
assert presort is not None
if os.path.exists(perquery_file): return pickle_load(perquery_file)
if scale_data: scaler = get_scaler(scaler_id=scaler_id)
min_docs, min_rele = data_dict['min_docs'], data_dict['min_rele']
unknown_as_zero, binary_rele, has_comment = data_dict['unknown_as_zero'], data_dict['binary_rele'], data_dict['has_comment']
clip_query = False
if min_rele is not None and min_rele > 0:
clip_query = True
if min_docs is not None and min_docs > 0:
clip_query = True
list_Qs = []
#output files
#print(in_file)
with open(in_file, encoding='iso-8859-1') as file_obj:
dict_data = dict()
if has_comment:
all_features_mat, all_labels_vec, qids, docids = parse_letor(file_obj.readlines(), has_comment=True)
for i in range(len(qids)):
f_vec = all_features_mat[i, :]
std_s = all_labels_vec[i]
qid = qids[i]
docid = docids[i]
if qid in dict_data:
dict_data[qid].append((std_s, docid, f_vec))
else:
dict_data[qid] = [(std_s, docid, f_vec)]
del all_features_mat
# unique qids
seen = set()
seen_add = seen.add
# sequential unique id
qids_unique = [x for x in qids if not (x in seen or seen_add(x))]
for qid in qids_unique:
tmp = list(zip(*dict_data[qid]))
list_labels_per_q = tmp[0]
if data_dict['data_id'] in MSLETOR_LIST:
''' convert the original rank-position into grade-labels '''
ranking_size = len(list_labels_per_q)
list_labels_per_q = [ranking_size-r for r in list_labels_per_q]
#list_docids_per_q = tmp[1]
list_features_per_q = tmp[2]
feature_mat = np.vstack(list_features_per_q)
if scale_data:
if data_dict['data_id'] in ISTELLA_LTR:
# due to the possible extremely large features, e.g., 1.79769313486e+308
feature_mat = scaler.fit_transform(np.clip(feature_mat, a_min=None, a_max=ISTELLA_MAX))
else:
feature_mat = scaler.fit_transform(feature_mat)
Q = clip_query_data(qid=qid, feature_mat=feature_mat, std_label_vec=np.array(list_labels_per_q),
binary_rele=binary_rele, unknown_as_zero=unknown_as_zero, clip_query=clip_query,
min_docs=min_docs, min_rele=min_rele, presort=presort)
if Q is not None:
list_Qs.append(Q)
else:
if data_dict['data_id'] in YAHOO_LTR:
all_features_mat, all_labels_vec, qids = parse_letor(file_obj.readlines(), has_comment=False, one_indexed=False)
else:
all_features_mat, all_labels_vec, qids = parse_letor(file_obj.readlines(), has_comment=False)
for i in range(len(qids)):
f_vec = all_features_mat[i, :]
std_s = all_labels_vec[i]
qid = qids[i]
if qid in dict_data:
dict_data[qid].append((std_s, f_vec))
else:
dict_data[qid] = [(std_s, f_vec)]
del all_features_mat
# unique qids
seen = set()
seen_add = seen.add
# sequential unique id
qids_unique = [x for x in qids if not (x in seen or seen_add(x))]
for qid in qids_unique:
tmp = list(zip(*dict_data[qid]))
list_labels_per_q = tmp[0]
if data_dict['data_id'] in MSLETOR_LIST:
''' convert the original rank-position into grade-labels '''
ranking_size = len(list_labels_per_q)
list_labels_per_q = [ranking_size-r for r in list_labels_per_q]
list_features_per_q = tmp[1]
feature_mat = np.vstack(list_features_per_q)
if scale_data:
if data_dict['data_id'] in ISTELLA_LTR:
# due to the possible extremely large features, e.g., 1.79769313486e+308
feature_mat = scaler.fit_transform(np.clip(feature_mat, a_min=None, a_max=ISTELLA_MAX))
else:
feature_mat = scaler.fit_transform(feature_mat)
Q = clip_query_data(qid=qid, feature_mat=feature_mat, std_label_vec=np.array(list_labels_per_q),
binary_rele=binary_rele, unknown_as_zero=unknown_as_zero, clip_query=clip_query,
min_docs=min_docs, min_rele=min_rele, presort=presort)
if Q is not None:
list_Qs.append(Q)
if buffer:
assert perquery_file is not None
parent_dir = Path(perquery_file).parent
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
pickle_save(list_Qs, file=perquery_file)
return list_Qs
## ---------------------------------------------------- ##
class LTRDataset(data.Dataset):
"""
Loading the specified dataset as torch.utils.data.Dataset.
We assume that checking the meaningfulness of given loading-setting is conducted beforehand.
"""
def __init__(self, split_type, file, data_id=None, data_dict=None, eval_dict=None, presort=True, hot=False, buffer=False, batch_size=1):
assert data_id is not None or data_dict is not None
if data_dict is None: data_dict = self.get_default_data_dict(data_id=data_id, batch_size=batch_size)
self.hot = hot
''' data property '''
self.label_type = data_dict['label_type']
''' split-specific settings '''
self.split_type = split_type
self.presort = presort
self.data_id = data_dict['data_id']
if data_dict['data_id'] in MSLETOR or data_dict['data_id'] in MSLRWEB \
or data_dict['data_id'] in YAHOO_LTR or data_dict['data_id'] in YAHOO_LTR_5Fold \
or data_dict['data_id'] in ISTELLA_LTR \
or data_dict['data_id'] == 'IRGAN_MQ2008_Semi': # supported datasets
perquery_file = get_buffer_file_name(data_id=data_id, file=file, data_dict=data_dict, presort=self.presort)
if hot:
torch_perquery_file = perquery_file.replace('.np', '_Hot.torch')
else:
torch_perquery_file = perquery_file.replace('.np', '.torch')
if eval_dict is not None:
mask_label, mask_ratio, mask_type = eval_dict['mask_label'], eval_dict['mask_ratio'], eval_dict['mask_type']
if mask_label:
mask_label_str = '_'.join([mask_type, 'Ratio', '{:,g}'.format(mask_ratio)])
torch_perquery_file = torch_perquery_file.replace('.torch', '_'+mask_label_str+'.torch')
else:
mask_label = False
if os.path.exists(torch_perquery_file):
print('loading buffered file ...')
self.list_torch_Qs = pickle_load(torch_perquery_file)
else:
self.list_torch_Qs = []
scale_data = data_dict['scale_data']
scaler_id = data_dict['scaler_id'] if 'scaler_id' in data_dict else None
list_Qs = iter_queries(in_file=file, presort=self.presort, data_dict=data_dict, scale_data=scale_data,
scaler_id=scaler_id, perquery_file=perquery_file, buffer=buffer)
list_inds = list(range(len(list_Qs)))
for ind in list_inds:
qid, doc_reprs, doc_labels = list_Qs[ind]
torch_q_doc_vectors = torch.from_numpy(doc_reprs).type(torch.FloatTensor)
#torch_q_doc_vectors = torch.unsqueeze(torch_q_doc_vectors, dim=0) # a default batch size of 1
torch_std_labels = torch.from_numpy(doc_labels).type(torch.FloatTensor)
#torch_std_labels = torch.unsqueeze(torch_std_labels, dim=0) # a default batch size of 1
self.list_torch_Qs.append((qid, torch_q_doc_vectors, torch_std_labels))
#buffer
#print('Num of q:', len(self.list_torch_Qs))
if buffer:
parent_dir = Path(torch_perquery_file).parent
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
pickle_save(self.list_torch_Qs, torch_perquery_file)
else:
raise NotImplementedError
def get_default_data_dict(self, data_id, scaler_id=None, batch_size=None):
''' a default setting for loading a dataset '''
min_docs = 1
min_rele = 1 # with -1, it means that we don't care with dumb queries that has no relevant documents. Say, for checking the statistics of an original dataset
scale_data, scaler_id, scaler_level = get_scaler_setting(data_id=data_id, scaler_id=scaler_id)
train_presort = False if data_id in MSLETOR_SEMI else True
batch_size = 10 if batch_size is None else batch_size
data_dict = dict(data_id=data_id, min_docs=min_docs, min_rele=min_rele, binary_rele=False,unknown_as_zero=False,
train_presort=train_presort, validation_presort=True, test_presort=True,
train_batch_size=batch_size, validation_batch_size=batch_size, test_batch_size=batch_size,
scale_data=scale_data, scaler_id=scaler_id, scaler_level=scaler_level)
data_meta = get_data_meta(data_id=data_id)
data_dict.update(data_meta)
return data_dict
def __len__(self):
return len(self.list_torch_Qs)
def __getitem__(self, index):
qid, torch_batch_rankings, torch_batch_std_labels = self.list_torch_Qs[index]
return qid, torch_batch_rankings, torch_batch_std_labels
def iter_hot(self):
list_inds = list(range(len(self.list_torch_Qs)))
for ind in list_inds:
qid, torch_batch_rankings, torch_batch_std_labels, torch_batch_std_hot_labels, batch_cnts = self.list_torch_Qs[ind]
yield qid, torch_batch_rankings, torch_batch_std_labels, torch_batch_std_hot_labels, batch_cnts
## Customize Sampler for Batch Processing ##
def pre_allocate_batch(dict_univ_bin, num_docs_per_batch):
'''
Based on the expected number of documents to process within a single batch, we merge the queries that have the same number of documents to form a batch
@param dict_univ_bin: [unique_value, bin of index]
@param num_docs_per_batch:
@return:
'''
list_batch_inds = []
if 1 == num_docs_per_batch: # a simple but time-consuming per-query processing, namely the batch_size is always one
for univ in dict_univ_bin:
bin = dict_univ_bin[univ]
for index in bin:
single_ind_as_batch = [index]
list_batch_inds.append(single_ind_as_batch)
return list_batch_inds
else:
for univ in dict_univ_bin:
bin = dict_univ_bin[univ]
bin_length = len(bin)
if univ * bin_length < num_docs_per_batch: # merge all queries as one batch
list_batch_inds.append(bin)
else:
if univ < num_docs_per_batch: # split with an approximate value
num_inds_per_batch = num_docs_per_batch // univ
for i in range(0, bin_length, num_inds_per_batch):
sub_bin = bin[i: min(i+num_inds_per_batch, bin_length)]
list_batch_inds.append(sub_bin)
else: # one single query as a batch
for index in bin:
single_ind_as_batch = [index]
list_batch_inds.append(single_ind_as_batch)
return list_batch_inds
class LETORSampler(data.Sampler):
'''
Customized sampler for LETOR datasets based on the observation that:
though the number of documents per query may differ, there are many queries that have the same number of documents, especially with a big dataset.
'''
def __init__(self, data_source, rough_batch_size=None):
list_num_docs = []
for qid, torch_batch_rankings, torch_batch_std_labels in data_source:
list_num_docs.append(torch_batch_std_labels.size(0))
dict_univ_bin = {}
for ind, univ in enumerate(list_num_docs):
if univ in dict_univ_bin:
dict_univ_bin[univ].append(ind)
else:
bin = [ind]
dict_univ_bin[univ] = bin
self.list_batch_inds = pre_allocate_batch(dict_univ_bin=dict_univ_bin, num_docs_per_batch=rough_batch_size)
def __iter__(self):
for batch_inds in self.list_batch_inds:
yield batch_inds
def load_data(data_id, file, split_type, batch_size):
_ltr_data = LTRDataset(data_id=data_id, file=file, split_type=split_type, batch_size=batch_size)
letor_sampler = LETORSampler(data_source=_ltr_data, rough_batch_size=batch_size)
ltr_data = torch.utils.data.DataLoader(_ltr_data, batch_sampler=letor_sampler, num_workers=0)
return ltr_data
class NeuralRanker(nn.Module):
"""
NeuralRanker is a class that represents a general learning-to-rank model.
Different learning-to-rank models inherit NeuralRanker, but differ in custom_loss_function, which corresponds to a particular loss function.
"""
def __init__(self, model_path="./model/neural_ranker.pt", id='AbsRanker', gpu=True, device="cuda:0"):
super(NeuralRanker, self).__init__()#add
self.id = id
self.model_path = model_path
self.gpu, self.device = gpu, device
self.init()
# if torch.cuda.device_count() > 0:
# # model = model.to('cuda:0')
# self = self.cuda()
def init(self):
# inner scoring function by using a hard-coded one as an example
self.sf = nn.Sequential(
nn.Linear(46, 128), nn.GELU(),#136#46
nn.Linear(128, 256), nn.GELU(),
nn.Linear(256, 512), nn.GELU(),
nn.Linear(512, 1), nn.GELU())
# nn.Linear(128, 64), nn.GELU(),
# nn.Linear(64, 32), nn.GELU(),
# nn.Linear(32, 1), nn.GELU())
if self.device !="cpu":
self.sf=self.sf.cuda()
self.optimizer = optim.Adam(self.sf.parameters(), lr = 0.001, weight_decay = 0.001)#0.0001
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
#add
def l2(self):
l2 = 0
for p in self.parameters():
l2 += (p ** 2).sum()
return l2
def save_model(self, model_path=None):
"""
save model
"""
if model_path is None:
model_path = self.model_path
dir_path = os.path.dirname(model_path)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
torch.save(self.state_dict(), model_path)
def load_model(self, model_path=None):
"""
load model
"""
if model_path is None:
model_path = self.model_path
self.load_state_dict(torch.load(model_path))
self.eval_mode()
def eval_mode(self):
'''
model.eval() is a kind of switch for some specific layers/parts of the model that behave differently
during training and inference (evaluating) time.
For example, Dropouts Layers, BatchNorm Layers etc. You need to turn off them during model evaluation,
and .eval() will do it for you. In addition, the common practice for evaluating/validation is using
torch.no_grad() in pair with model.eval() to turn off gradients computation:
'''
self.sf.eval()
def train_mode(self):
self.sf.train(mode=True)
def train(self, train_data, epoch_k=None, **kwargs):
'''
One epoch training using the entire training data
'''
self.train_mode()
assert 'presort' in kwargs
presort = kwargs['presort']
num_queries = 0
epoch_loss = torch.tensor([0.0], device=self.device)
for batch_ids, batch_q_doc_vectors, batch_std_labels in train_data: # batch_size, [batch_size, num_docs, num_features], [batch_size, num_docs]
num_queries += len(batch_ids)
if self.gpu: batch_q_doc_vectors, batch_std_labels = batch_q_doc_vectors.to(self.device), batch_std_labels.to(self.device)
batch_loss = self.train_op(batch_q_doc_vectors, batch_std_labels, batch_ids=batch_ids, epoch_k=epoch_k, presort=presort)
epoch_loss += batch_loss.item()
epoch_loss = epoch_loss/num_queries
return epoch_loss
def train_op(self, batch_q_doc_vectors, batch_std_labels, **kwargs):
'''
The training operation over a batch of queries.
@param batch_q_doc_vectors: [batch_size, num_docs, num_features], the latter two dimensions {num_docs, num_features} denote feature vectors associated with the same query.
@param batch_std_labels: [batch, ranking_size] each row represents the standard relevance labels for documents associated with the same query.
@param kwargs: optional arguments
@return:
'''
batch_preds = self.forward(batch_q_doc_vectors)
return self.custom_loss_function(batch_preds, batch_std_labels, **kwargs)
def custom_loss_function(self, batch_preds, batch_std_labels, **kwargs):
'''
The loss function to be customized
@param batch_preds: [batch, ranking_size] each row represents the predicted relevance values for documents associated with the same query.
@param batch_std_labels: [batch, ranking_size] each row represents the standard relevance labels for documents associated with the same query.
@param kwargs:
@return:
'''
pass
def forward(self, batch_q_doc_vectors):
'''
Forward pass through the scoring function, where each document is scored independently.
@param batch_q_doc_vectors: [batch_size, num_docs, num_features], the latter two dimensions {num_docs, num_features} denote feature vectors associated with the same query.
@return:
'''
batch_size, num_docs, num_features = batch_q_doc_vectors.size()
_batch_preds = self.sf(batch_q_doc_vectors)
batch_preds = _batch_preds.view(-1, num_docs) # [batch_size x num_docs, 1] -> [batch_size, num_docs]
return batch_preds
def predict(self, batch_q_doc_vectors):
'''
The relevance prediction.
@param batch_q_doc_vectors: [batch_size, num_docs, num_features], the latter two dimensions {num_docs, num_features} denote feature vectors associated with the same query.
@return:
'''
batch_preds = self.forward(batch_q_doc_vectors)
return batch_preds
def ndcg_at_k(self, test_data=None, k=10, presort=False, device='cpu'):
'''
Compute nDCG@k with the given data
An underlying assumption is that there is at least one relevant document, or ZeroDivisionError appears.
'''
self.eval_mode() # switch evaluation mode
num_queries = 0
sum_ndcg_at_k = torch.zeros(1)
for batch_ids, batch_q_doc_vectors, batch_std_labels in test_data: # batch_size, [batch_size, num_docs, num_features], [batch_size, num_docs]
if batch_std_labels.size(1) < k:
continue # skip if the number of documents is smaller than k
else:
num_queries += len(batch_ids)
if self.gpu: batch_q_doc_vectors = batch_q_doc_vectors.to(self.device)
batch_preds = self.predict(batch_q_doc_vectors)
if self.gpu: batch_preds = batch_preds.cpu()
_, batch_pred_desc_inds = torch.sort(batch_preds, dim=1, descending=True)
batch_predict_rankings = torch.gather(batch_std_labels, dim=1, index=batch_pred_desc_inds)
if presort:
batch_ideal_rankings = batch_std_labels
else:
batch_ideal_rankings, _ = torch.sort(batch_std_labels, dim=1, descending=True)
batch_ndcg_at_k = torch_ndcg_at_k(batch_predict_rankings=batch_predict_rankings,
batch_ideal_rankings=batch_ideal_rankings,
k=k, device=device)
sum_ndcg_at_k += torch.sum(batch_ndcg_at_k) # due to batch processing
avg_ndcg_at_k = sum_ndcg_at_k / num_queries
return avg_ndcg_at_k
def ndcg_at_ks(self, test_data=None, ks=[1, 5, 10], presort=False, device='cpu'):
'''
Compute nDCG with multiple cutoff values with the given data
An underlying assumption is that there is at least one relevant document, or ZeroDivisionError appears.
'''
self.eval_mode() # switch evaluation mode
num_queries = 0
sum_ndcg_at_ks = torch.zeros(len(ks))
#gpu
#sum_ndcg_at_ks = sum_ndcg_at_ks.to(self.device)
for batch_ids, batch_q_doc_vectors, batch_std_labels in test_data: # batch_size, [batch_size, num_docs, num_features], [batch_size, num_docs]
if self.gpu:
batch_q_doc_vectors = batch_q_doc_vectors.to(self.device)
#batch_std_labels =batch_std_labels.to(self.device)
batch_preds = self.predict(batch_q_doc_vectors)
#cpu
if self.gpu: batch_preds = batch_preds.cpu()
_, batch_pred_desc_inds = torch.sort(batch_preds, dim=1, descending=True)#按行排序
batch_predict_rankings = torch.gather(batch_std_labels, dim=1, index=batch_pred_desc_inds)#按索引取排序后。在标准值中取排序后的预测值
if presort:
batch_ideal_rankings = batch_std_labels
else:
batch_ideal_rankings, _ = torch.sort(batch_std_labels, dim=1, descending=True)
batch_ndcg_at_ks = torch_ndcg_at_ks(batch_predict_rankings=batch_predict_rankings,
batch_ideal_rankings=batch_ideal_rankings,
ks=ks, device=device)
#gpu
#batch_ndcg_at_ks=batch_ndcg_at_ks.to(self.device)
sum_ndcg_at_ks = torch.add(sum_ndcg_at_ks, torch.sum(batch_ndcg_at_ks, dim=0))
num_queries += len(batch_ids)
avg_ndcg_at_ks = sum_ndcg_at_ks / num_queries
#if first is False:
# print(batch_predict_rankings)
# print(batch_ideal_rankings)
return avg_ndcg_at_ks
def rankMSE_loss_function(relevance_preds=None, std_labels=None):