class_docstr: This class evaluates an Information Retrieval (IR) setting.
  
  Given a set of queries and a large corpus set. It will retrieve for each query the top-k most similar document. It measures
  Mean Reciprocal Rank (MRR), Recall@k, and Normalized Discounted Cumulative Gain (NDCG)
class_docstr_tok: ["This","class","evaluates","an","Information","Retrieval","(","IR",")","setting",".","Given","a","set","of","queries","and","a","large","corpus","set",".","It","will","retrieve","for","each","query","the","top-k","most","similar","document",".","It","measures","Mean","Reciprocal","Rank","(","MRR",")",",","Recall","@","k",",","and","Normalized","Discounted","Cumulative","Gain","(","NDCG",")"]
code: def compute_metrices(
      self, model, corpus_model=None, corpus_embeddings: Tensor = None
  ) -> Dict[str, float]:
      if corpus_model is None:
          corpus_model = model
  
      max_k = max(
          max(self.mrr_at_k),
          max(self.ndcg_at_k),
          max(self.accuracy_at_k),
          max(self.precision_recall_at_k),
          max(self.map_at_k),
      )
  
      # Compute embedding for the queries
      query_embeddings = model.encode(
          self.queries,
          show_progress_bar=self.show_progress_bar,
          batch_size=self.batch_size,
          convert_to_tensor=True,
      )
  
      queries_result_list = {}
      for name in self.score_functions:
          queries_result_list[name] = [
              [] for _ in range(len(query_embeddings))
          ]
  
      # Iterate over chunks of the corpus
      for corpus_start_idx in trange(
          0,
          len(self.corpus),
          self.corpus_chunk_size,
          desc="Corpus Chunks",
          disable=not self.show_progress_bar,
      ):
          corpus_end_idx = min(
              corpus_start_idx + self.corpus_chunk_size,
              len(self.corpus),
          )
  
          # Encode chunk of corpus
          if corpus_embeddings is None:
              sub_corpus_embeddings = corpus_model.encode(
                  self.corpus[corpus_start_idx:corpus_end_idx],
                  show_progress_bar=False,
                  batch_size=self.batch_size,
                  convert_to_tensor=True,
              )
          else:
              sub_corpus_embeddings = corpus_embeddings[
                  corpus_start_idx:corpus_end_idx
              ]
  
          # Compute cosine similarites
          for name, score_function in self.score_functions.items():
              pair_scores = score_function(
                  query_embeddings, sub_corpus_embeddings
              )
  
              # Get top-k values
              (
                  pair_scores_top_k_values,
                  pair_scores_top_k_idx,
              ) = torch.topk(
                  pair_scores,
                  min(max_k, len(pair_scores[0])),
                  dim=1,
                  largest=True,
                  sorted=False,
              )
              pair_scores_top_k_values = (
                  pair_scores_top_k_values.cpu().tolist()
              )
              pair_scores_top_k_idx = (
                  pair_scores_top_k_idx.cpu().tolist()
              )
  
              for query_itr in range(len(query_embeddings)):
                  for sub_corpus_id, score in zip(
                      pair_scores_top_k_idx[query_itr],
                      pair_scores_top_k_values[query_itr],
                  ):
                      corpus_id = self.corpus_ids[
                          corpus_start_idx + sub_corpus_id
                      ]
                      if (
                          len(queries_result_list[name][query_itr])
                          < max_k
                      ):
                          heapq.heappush(
                              queries_result_list[name][query_itr],
                              (score, corpus_id),
                          )  # heaqp tracks the quantity of the first element in the tuple
                      else:
                          heapq.heappushpop(
                              queries_result_list[name][query_itr],
                              (score, corpus_id),
                          )
  
      for name in queries_result_list:
          for query_itr in range(len(queries_result_list[name])):
              for doc_itr in range(
                  len(queries_result_list[name][query_itr])
              ):
                  score, corpus_id = queries_result_list[name][
                      query_itr
                  ][doc_itr]
                  queries_result_list[name][query_itr][doc_itr] = {
                      "corpus_id": corpus_id,
                      "score": score,
                  }
  
      logger.info("Queries: {}".format(len(self.queries)))
      logger.info("Corpus: {}\n".format(len(self.corpus)))
  
      # Compute scores
      scores = {
          name: self.compute_metrics(queries_result_list[name])
          for name in self.score_functions
      }
  
      # Output
      for name in self.score_function_names:
          logger.info("Score-Function: {}".format(name))
          self.output_scores(scores[name])
  
      return scores
  
file_path: sentence-transformers/sentence_transformers/evaluation/InformationRetrievalEvaluator.py
imports: from .None import SentenceEvaluator
  import torch
  from torch import Tensor
  import logging
  from tqdm import trange
  from ..util import cos_sim, dot_score
  import os
  import numpy
  from typing import List, Dict, Set, Callable
  import heapq
label_desc: Use image node_id 3 for calling the InformationRetrievalEvaluator obj's underlying member method code with example usage: obj.compute_metrices(model, corpus_model, corpus_embeddings) and returns: scores