File size: 1,901 Bytes
d23177a |
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 |
# pip install pytrec-eval-terrier
import pytrec_eval
# conda install sentence-transformers -c conda-forge
from sentence_transformers import SentenceTransformer
import pandas as pd
from collections import defaultdict
import torch
from tqdm import tqdm
if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
def load_dataset(path):
df = pd.read_parquet(path, engine="pyarrow")
return df
path = r'D:\datasets\H2Retrieval\data_sample5k'
qrels_pd = load_dataset(path + r'\qrels.parquet.gz')
corpus = load_dataset(path + r'\corpus.parquet.gz')
queries = load_dataset(path + r'\queries.parquet.gz')
qrels = defaultdict(dict)
for i, e in qrels_pd.iterrows():
qrels[e['qid']][e['cid']] = e['score']
model = SentenceTransformer(r'D:\models\bce', device='cuda:0')
corpusEmbeds = model.encode(corpus['text'].values, normalize_embeddings=True, show_progress_bar=True, batch_size=24)
queriesEmbeds = model.encode(queries['text'].values, normalize_embeddings=True, show_progress_bar=True, batch_size=24)
queriesEmbeds = torch.tensor(queriesEmbeds, device=device)
corpusEmbeds = corpusEmbeds.T
corpusEmbeds = torch.tensor(corpusEmbeds, device=device)
@torch.no_grad()
def getTopK(corpusEmbeds, qEmbeds, k=10):
scores = qEmbeds @ corpusEmbeds
top_k_indices = torch.argsort(scores, descending=True)[:k]
scores = scores.cpu()
top_k_indices = top_k_indices.cpu()
retn = {}
for x in top_k_indices:
x = int(x)
retn[corpus['cid'][x]] = float(scores[x])
return retn
results = {}
for i in tqdm(range(len(queries)), desc="Converting"):
results[queries['qid'][i]] = getTopK(corpusEmbeds, queriesEmbeds[i])
evaluator = pytrec_eval.RelevanceEvaluator(qrels, {'ndcg'})
tmp = evaluator.evaluate(results)
ndcg = 0
for x in tmp.values():
ndcg += x['ndcg']
ndcg /= len(queries)
print(f'ndcg_10: {ndcg*100:.2f}%') |