code: def evaluate_model_on_dataset(
      model,
      dataloader,
      dataset_name,
      device,
      logger,
      number_of_samples,
      eval_bm45_acc=False,
      path_to_file_to_write_results=None,
  ):
      model.eval()
  
      eval_accuracy = 0
      nb_eval_steps, nb_eval_examples = 0, 0
  
      for (
          input_ids,
          input_mask,
          segment_ids,
          label_ids,
          entity_mask,
      ) in tqdm(dataloader, desc="Evaluating"):
          input_ids = input_ids.to(device)
          input_mask = input_mask.to(device)
          segment_ids = segment_ids.to(device)
          label_ids = label_ids.to(device)
          entity_mask = entity_mask.to(device)
  
          with torch.no_grad():
              tmp_eval_loss, logits = model(
                  input_ids,
                  segment_ids,
                  input_mask,
                  label_ids,
                  entity_mask=entity_mask,
              )
  
          logits = logits.detach().cpu().numpy()
          label_ids = label_ids.to("cpu").numpy()
          tmp_eval_accuracy = utils.accuracy(logits, label_ids)
  
          eval_accuracy += tmp_eval_accuracy
  
          nb_eval_examples += input_ids.size(0)
          nb_eval_steps += 1
  
      logger.info("\n")
  
      normalized_eval_accuracy = eval_accuracy / nb_eval_examples
  
      result = {"normalized_accuracy": normalized_eval_accuracy}
      result["unnormalized_accuracy"] = (
          eval_accuracy / number_of_samples
      )
      result["candidate_generation_recall"] = (
          nb_eval_examples / number_of_samples
      )
  
      if eval_bm45_acc:
          result[
              "normalized_bm45_recall_@"
          ] = utils.eval_precision_bm45_dataloader(
              dataloader, [1, 5, 10, 20, 40, 60, 80, 100]
          )
          result[
              "unnormalized_bm45_recall_@"
          ] = utils.eval_precision_bm45_dataloader(
              dataloader,
              [1, 5, 10, 20, 40, 60, 80, 100],
              number_of_samples,
          )
  
      if path_to_file_to_write_results is None:
          logger.info(
              "***** Eval results - {} ({} / {} samples) *****\n".format(
                  dataset_name, nb_eval_examples, number_of_samples
              )
          )
          for key in sorted(result.keys()):
              logger.info("  %s = %s", key, str(result[key]))
      else:
          with open(path_to_file_to_write_results, "a+") as writer:
              logger.info(
                  "***** Eval results - {} ({} / {} samples) *****\n".format(
                      dataset_name, nb_eval_examples, number_of_samples
                  )
              )
              writer.write(
                  "***** Eval results - {} ({} / {} samples) *****\n".format(
                      dataset_name, nb_eval_examples, number_of_samples
                  )
              )
              for key in sorted(result.keys()):
                  logger.info("  %s = %s", key, str(result[key]))
                  writer.write("%s = %s\n" % (key, str(result[key])))
              writer.write("\n")
  
      logger.info("\n")
  
      return result
  
file_path: BLINK/blink/candidate_ranking/evaluate.py
imports: import time
  import utils
  import torch
  import utils
  import argparse
  import os
  from bert_reranking import BertReranker
  from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset
  from tqdm import tqdm
label_desc: Use image node_id 1 for calling a global function with example usage: evaluate_model_on_dataset(model, dataloader, dataset_name, device, logger, number_of_samples, eval_bm45_acc, path_to_file_to_write_results) and returns: result