class_docstr: Sentence-level relation extraction dataset
docstr: Args:
      pred_result: a list of predicted label (id)
          Make sure that the `shuffle` param is set to `False` when getting the loader.
      use_name: if True, `pred_result` contains predicted relation names instead of ids
  Return:
      {'acc': xx}
docstr_tok: ["Args",":","pred_result",":","a","list","of","predicted","label","(","id",")","Make","sure","that","the","`","shuffle","`","param","is","set","to","`","False","`","when","getting","the","loader",".","use_name",":","if","True",",","`","pred_result","`","contains","predicted","relation","names","instead","of","ids","Return",":","{","'acc","'",":","xx","}"]
code: def eval(self, pred_result, use_name=False):
      """
      Args:
          pred_result: a list of predicted label (id)
              Make sure that the `shuffle` param is set to `False` when getting the loader.
          use_name: if True, `pred_result` contains predicted relation names instead of ids
      Return:
          {'acc': xx}
      """
      correct = 0
      total = len(self.data)
      correct_positive = 0
      pred_positive = 0
      gold_positive = 0
      neg = -1
      for name in ["NA", "na", "no_relation", "Other", "Others"]:
          if name in self.rel2id:
              if use_name:
                  neg = name
              else:
                  neg = self.rel2id[name]
              break
      for i in range(total):
          if use_name:
              golden = self.data[i]["relation"]
          else:
              golden = self.rel2id[self.data[i]["relation"]]
          if golden == pred_result[i]:
              correct += 1
              if golden != neg:
                  correct_positive += 1
          if golden != neg:
              gold_positive += 1
          if pred_result[i] != neg:
              pred_positive += 1
      acc = float(correct) / float(total)
      try:
          micro_p = float(correct_positive) / float(pred_positive)
      except:
          micro_p = 0
      try:
          micro_r = float(correct_positive) / float(gold_positive)
      except:
          micro_r = 0
      try:
          micro_f1 = 2 * micro_p * micro_r / (micro_p + micro_r)
      except:
          micro_f1 = 0
      result = {
          "acc": acc,
          "micro_p": micro_p,
          "micro_r": micro_r,
          "micro_f1": micro_f1,
      }
      logging.info("Evaluation result: {}.".format(result))
      return result
  
imports: import torch
  import torch.utils.data
  import os, random, json, logging
  import numpy
  import sklearn.metrics
label_desc: Use image node_id 5 for calling the SentenceREDataset obj's underlying member method code with example usage: obj.eval(pred_result, use_name) and returns: result