Limour commited on
Commit
c8ca558
1 Parent(s): e2f0fc5

Upload 2 files

Browse files
Files changed (2) hide show
  1. G2Retrieval_bce.py +27 -22
  2. test_pytrec_eval.py +1 -1
G2Retrieval_bce.py CHANGED
@@ -51,7 +51,7 @@ else:
51
 
52
 
53
  @torch.no_grad()
54
- def getTopK(corpusEmbeds, qEmbeds, qid, k=10):
55
  scores = qEmbeds @ corpusEmbeds
56
  top_k_indices = torch.argsort(scores, descending=True)[:k]
57
  scores = scores.cpu()
@@ -62,24 +62,29 @@ def getTopK(corpusEmbeds, qEmbeds, qid, k=10):
62
  retn.append((qid, corpus['cid'][x], float(scores[x])))
63
  return retn
64
 
65
-
66
- with torch.no_grad():
67
- results = []
68
- for i in tqdm(range(len(queries)), desc="Converting"):
69
- results.extend(getTopK(corpusEmbeds, queriesEmbeds[i], queries['qid'][i]))
70
-
71
- results = pd.DataFrame(results, columns=['qid', 'cid', 'score'])
72
- results['score'] = results['score'].astype(float)
73
- tmp = ndcg_in_all(qrels, results)
74
- ndcgs = torch.tensor([x for x in tmp.values()], device=device)
75
-
76
- mean = torch.mean(ndcgs)
77
- std = torch.std(ndcgs)
78
-
79
- print(f'NDCG@10: {mean*100:.2f}±{std*100:.2f}')
80
-
81
- # 手动释放CUDA缓存内存
82
- del queriesEmbeds
83
- del corpusEmbeds
84
- del model
85
- torch.cuda.empty_cache()
 
 
 
 
 
 
51
 
52
 
53
  @torch.no_grad()
54
+ def getTopK(corpusEmbeds, qEmbeds, qid, k=200):
55
  scores = qEmbeds @ corpusEmbeds
56
  top_k_indices = torch.argsort(scores, descending=True)[:k]
57
  scores = scores.cpu()
 
62
  retn.append((qid, corpus['cid'][x], float(scores[x])))
63
  return retn
64
 
65
+ def print_ndcgs(k):
66
+ with torch.no_grad():
67
+ results = []
68
+ for i in tqdm(range(len(queries)), desc="Converting"):
69
+ results.extend(getTopK(corpusEmbeds, queriesEmbeds[i], queries['qid'][i], k=k))
70
+
71
+ results = pd.DataFrame(results, columns=['qid', 'cid', 'score'])
72
+ results['score'] = results['score'].astype(float)
73
+ tmp = ndcg_in_all(qrels, results)
74
+ ndcgs = torch.tensor([x for x in tmp.values()], device=device)
75
+
76
+ mean = torch.mean(ndcgs)
77
+ std = torch.std(ndcgs)
78
+
79
+ print(f'NDCG@{k}: {mean*100:.2f}±{std*100:.2f}')
80
+
81
+ print_ndcgs(3)
82
+ print_ndcgs(10)
83
+ print_ndcgs(50)
84
+ print_ndcgs(100)
85
+ print_ndcgs(200)
86
+ # # 手动释放CUDA缓存内存
87
+ # del queriesEmbeds
88
+ # del corpusEmbeds
89
+ # del model
90
+ # torch.cuda.empty_cache()
test_pytrec_eval.py CHANGED
@@ -39,7 +39,7 @@ def ndcg_in_all(qrels, results):
39
  retn = {}
40
  _qrels = {qid: group for qid, group in qrels.groupby('qid')}
41
  _results = {qid: group for qid, group in results.groupby('qid')}
42
- for qid in tqdm(_qrels, desc="计算 ndcg 中..."):
43
  retn[qid] = ndcg(_qrels[qid], _results[qid])
44
  return retn
45
 
 
39
  retn = {}
40
  _qrels = {qid: group for qid, group in qrels.groupby('qid')}
41
  _results = {qid: group for qid, group in results.groupby('qid')}
42
+ for qid in tqdm(_results, desc="计算 ndcg 中..."):
43
  retn[qid] = ndcg(_qrels[qid], _results[qid])
44
  return retn
45