class_docstr:     Calculate precision, recall, F1 for each labels
  
  Parameters
  ----------
  metrics: list
      Indicate the metric for the class to return.
      Note that each metric must be one of ``precision``, ``recall``, ``F1``, ``accuracy``.
      And the results' order is the same as the metrics.
class_docstr_tok: ["Calculate","precision",",","recall",",","F1","for","each","labels","Parameters","--","--","--","--","--","metrics",":","list","Indicate","the","metric","for","the","class","to","return",".","Note","that","each","metric","must","be","one","of","``","precision","``",",","``","recall","``",",","``","F1","``",",","``","accuracy","``",".","And","the","results","'","order","is","the","same","as","the","metrics","."]
docstr:     The function to calculate the expected metrics for each labels
  
  Parameters
  ----------
  ground_truth: torch.Tensor
      Ground truth (correct) target values, 1d tensor
  predict: torch.Tensor
      The predicted target values generated by classifier, 1d tensor
  average: string or None, [None (default), 'micro', 'macro', 'weighted']
      If set ``None``, it will return the scores for each class.
          Otherwise, it will be reduced by the strategy as follows:
  
      ``'micro'``:
          Calculate metrics globally by counting the total true positives,
          false negatives and false positives.
  
      ``'macro'``:
          Calculate metrics for each label, and calculate the unweighted
          average values.  This does not take label imbalance into account.
  
      ``'weighted'``:
          Calculate metrics for each label, and calculate the weighted
          average value. Note that the weight is the number of the true
          instances for each label.
  
  zero_division: "warning", 0, 1, default="warning"
          Sets the value to return when there is a zero division.
  
          If set to "warning", this acts as 0, but warnings are also raised.
  
  sample_weight: None
      The sample weight. It is not implemented yet.
  
  Returns
  -------
  scores: list[object]
      Return the expected metrics initialized in init function in ``precision``, ``recall``,             ``F1`` order
docstr_tok: ["The","function","to","calculate","the","expected","metrics","for","each","labels","Parameters","--","--","--","--","--","ground_truth",":","torch.Tensor","Ground","truth","(","correct",")","target","values",",","1d","tensor","predict",":","torch.Tensor","The","predicted","target","values","generated","by","classifier",",","1d","tensor","average",":","string","or","None",",","[","None","(","default",")",",","'micro","'",",","'macro","'",",","'weighted","'","]","If","set","``","None","``",",","it","will","return","the","scores","for","each","class",".","Otherwise",",","it","will","be","reduced","by","the","strategy","as","follows",":","``","'micro","'","``",":","Calculate","metrics","globally","by","counting","the","total","true","positives",",","false","negatives","and","false","positives",".","``","'macro","'","``",":","Calculate","metrics","for","each","label",",","and","calculate","the","unweighted","average","values",".","This","does","not","take","label","imbalance","into","account",".","``","'weighted","'","``",":","Calculate","metrics","for","each","label",",","and","calculate","the","weighted","average","value",".","Note","that","the","weight","is","the","number","of","the","true","instances","for","each","label",".","zero_division",":","``","warning","''",",","0",",","1",",","default=","''","warning","''","Sets","the","value","to","return","when","there","is","a","zero","division",".","If","set","to","``","warning","''",",","this","acts","as","0",",","but","warnings","are","also","raised",".","sample_weight",":","None","The","sample","weight",".","It","is","not","implemented","yet",".","Returns","--","--","--","-","scores",":","list","[","object","]","Return","the","expected","metrics","initialized","in","init","function","in","``","precision","``",",","``","recall","``",",","``","F1","``","order"]
code: def calculate_scores(
      self,
      ground_truth,
      predict,
      average=None,
      zero_division="warning",
      sample_weight=None,
  ):
      """
              The function to calculate the expected metrics for each labels
  
          Parameters
          ----------
          ground_truth: torch.Tensor
              Ground truth (correct) target values, 1d tensor
          predict: torch.Tensor
              The predicted target values generated by classifier, 1d tensor
          average: string or None, [None (default), 'micro', 'macro', 'weighted']
              If set ``None``, it will return the scores for each class.
                  Otherwise, it will be reduced by the strategy as follows:
  
              ``'micro'``:
                  Calculate metrics globally by counting the total true positives,
                  false negatives and false positives.
  
              ``'macro'``:
                  Calculate metrics for each label, and calculate the unweighted
                  average values.  This does not take label imbalance into account.
  
              ``'weighted'``:
                  Calculate metrics for each label, and calculate the weighted
                  average value. Note that the weight is the number of the true
                  instances for each label.
  
          zero_division: "warning", 0, 1, default="warning"
                  Sets the value to return when there is a zero division.
  
                  If set to "warning", this acts as 0, but warnings are also raised.
  
          sample_weight: None
              The sample weight. It is not implemented yet.
  
          Returns
          -------
          scores: list[object]
              Return the expected metrics initialized in init function in ``precision``, ``recall``, \
              ``F1`` order
          """
      ground_truth_np, predict_np = self._check_available(
          ground_truth, predict, zero_division
      )
  
      # calculate accuracy
      scores = ground_truth_np == predict_np
      accuracy_score = np.average(scores)
  
      if self.metrics is ["precision"]:
          return [accuracy_score]
  
      mcm = self._calculate_confusion_matrix(
          ground_truth=ground_truth_np, predict=predict_np
      )
  
      tp_sum = mcm[:, 1, 1]
      pred_sum = tp_sum + mcm[:, 0, 1]
      gt_sum = tp_sum + mcm[:, 1, 0]
  
      if average == "micro":
          tp_sum = np.array([tp_sum.sum()])
          pred_sum = np.array([pred_sum.sum()])
          gt_sum = np.array([gt_sum.sum()])
  
      # calculate precision and recall
      precision = self._prf_divide(
          tp_sum, pred_sum, zero_division=zero_division
      )
      recall = self._prf_divide(
          tp_sum, gt_sum, zero_division=zero_division
      )
  
      # calculate F_beta
      beta2 = 1**2  # note: only F1 here
      denominator = beta2 * precision + recall
  
      denominator[denominator == 0.0] = 1  # avoid division by 0
      f_score = (1 + beta2) * precision * recall / denominator
  
      if average == "weighted":
          weighted = gt_sum
      else:
          weighted = None
  
      if average is not None:
          precision = np.average(precision, weights=weighted)
          recall = np.average(recall, weights=weighted)
          f_score = np.average(f_score, weights=weighted)
      scores = []
  
      for metric_name in self.metrics:
          if metric_name == "precision":
              scores.append(precision)
          elif metric_name == "recall":
              scores.append(recall)
          elif metric_name == "F1":
              scores.append(f_score)
          elif metric_name == "accuracy":
              scores.append(accuracy_score)
          else:
              raise NotImplementedError()
      return scores
  
file_path: graph4nlp/graph4nlp/pytorch/modules/evaluation/accuracy.py
imports: import copy
  import warnings
  import numpy
  import torch
  from graph4nlp.pytorch.modules.evaluation.base import EvaluationMetricBase
label_desc: Use image node_id 2 for calling the Accuracy obj's underlying member method code with example usage: obj.calculate_scores(ground_truth, predict, average, zero_division, sample_weight) and returns: scores, list