class_docstr: This metric computes the number of True Positive, False Negative, and False Positive examples per class.
  When doing distributed training/evaluation the result of res=ClassificationReport(predictions, labels) calls
  will be all-reduced between all workers using SUM operations.
  
  If used with PytorchLightning LightningModule, include TPs, FNs, and FPs inside validation_step results.
  Then aggregate them at the end of validation epoch to correctly compute validation precision, recall, f1
  using get_precision_recall_f1().
  
  Example:
      def validation_step(self, batch, batch_idx):
          ...
          tp, fn, fp, _ = self.classification_report(preds, labels)
  
          return {'val_loss': val_loss, 'tp': tp, 'fn': fn, 'fp': fp}
  
      def on_validation_epoch_end(self):
          ...
          # calculate metrics and classification report
          precision, recall, f1, report = self.classification_report.compute()
  
          logging.info(report)
  
          self.log('val_loss', avg_loss, prog_bar=True)
          self.log('precision', precision)
          self.log('f1', f1)
          self.log('recall', recall)
  
  Args:
      num_classes: number of classes in the dataset
      label_ids (optional): label name to label id mapping
      mode: how to compute the average
      dist_sync_on_step: sync across ddp
      process_group: which processes to sync across
  Return:
      aggregated precision, recall, f1, report
class_docstr_tok: ["This","metric","computes","the","number","of","True","Positive",",","False","Negative",",","and","False","Positive","examples","per","class",".","When","doing","distributed","training\/evaluation","the","result","of","res=ClassificationReport","(","predictions",",","labels",")","calls","will","be","all-reduced","between","all","workers","using","SUM","operations",".","If","used","with","PytorchLightning","LightningModule",",","include","TPs",",","FNs",",","and","FPs","inside","validation_step","results",".","Then","aggregate","them","at","the","end","of","validation","epoch","to","correctly","compute","validation","precision",",","recall",",","f1","using","get_precision_recall_f1","(",")",".","Example",":","def","validation_step","(","self",",","batch",",","batch_idx",")",":","...","tp",",","fn",",","fp",",","_","=","self.classification_report","(","preds",",","labels",")","return","{","'val_loss","'",":","val_loss",",","'tp","'",":","tp",",","'fn","'",":","fn",",","'fp","'",":","fp","}","def","on_validation_epoch_end","(","self",")",":","...","#","calculate","metrics","and","classification","report","precision",",","recall",",","f1",",","report","=","self.classification_report.compute","(",")","logging.info","(","report",")","self.log","(","'val_loss","'",",","avg_loss",",","prog_bar=True",")","self.log","(","'precision","'",",","precision",")","self.log","(","'f1","'",",","f1",")","self.log","(","'recall","'",",","recall",")","Args",":","num_classes",":","number","of","classes","in","the","dataset","label_ids","(","optional",")",":","label","name","to","label","id","mapping","mode",":","how","to","compute","the","average","dist_sync_on_step",":","sync","across","ddp","process_group",":","which","processes","to","sync","across","Return",":","aggregated","precision",",","recall",",","f1",",","report"]
docstr: Aggregates and then calculates logs classification report similar to sklearn.metrics.classification_report.
  Typically used during epoch_end.
  
  Return:
      aggregated precision, recall, f1, report
docstr_tok: ["Aggregates","and","then","calculates","logs","classification","report","similar","to","sklearn.metrics.classification_report",".","Typically","used","during","epoch_end",".","Return",":","aggregated","precision",",","recall",",","f1",",","report"]
code: def compute(
      self,
  ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
      """
      Aggregates and then calculates logs classification report similar to sklearn.metrics.classification_report.
      Typically used during epoch_end.
  
      Return:
          aggregated precision, recall, f1, report
      """
      total_examples = torch.sum(self.num_examples_per_class)
      num_non_empty_classes = torch.nonzero(
          self.num_examples_per_class
      ).size(0)
  
      precision = torch.true_divide(
          self.tp * 100, (self.tp + self.fp + METRIC_EPS)
      )
      recall = torch.true_divide(
          self.tp * 100, (self.tp + self.fn + METRIC_EPS)
      )
      f1 = torch.true_divide(
          2 * precision * recall, (precision + recall + METRIC_EPS)
      )
  
      report = "\n{:50s}   {:10s}   {:10s}   {:10s}   {:10s}".format(
          "label", "precision", "recall", "f1", "support"
      )
      for i in range(len(self.tp)):
          label = f"label_id: {i}"
          if self.ids_to_labels and i in self.ids_to_labels:
              label = f"{self.ids_to_labels[i]} ({label})"
  
          report += (
              "\n{:50s}   {:8.2f}   {:8.2f}   {:8.2f}   {:8.0f}".format(
                  label,
                  precision[i],
                  recall[i],
                  f1[i],
                  self.num_examples_per_class[i],
              )
          )
  
      micro_precision = torch.true_divide(
          torch.sum(self.tp) * 100,
          torch.sum(self.tp + self.fp) + METRIC_EPS,
      )
      micro_recall = torch.true_divide(
          torch.sum(self.tp) * 100,
          torch.sum(self.tp + self.fn) + METRIC_EPS,
      )
      micro_f1 = torch.true_divide(
          2 * micro_precision * micro_recall,
          (micro_precision + micro_recall + METRIC_EPS),
      )
  
      macro_precision = torch.sum(precision) / num_non_empty_classes
      macro_recall = torch.sum(recall) / num_non_empty_classes
      macro_f1 = torch.sum(f1) / num_non_empty_classes
      weighted_precision = (
          torch.sum(precision * self.num_examples_per_class)
          / total_examples
      )
      weighted_recall = (
          torch.sum(recall * self.num_examples_per_class)
          / total_examples
      )
      weighted_f1 = (
          torch.sum(f1 * self.num_examples_per_class) / total_examples
      )
  
      report += "\n-------------------"
  
      report += (
          "\n{:50s}   {:8.2f}   {:8.2f}   {:8.2f}   {:8.0f}".format(
              "micro avg",
              micro_precision,
              micro_recall,
              micro_f1,
              total_examples,
          )
      )
  
      report += (
          "\n{:50s}   {:8.2f}   {:8.2f}   {:8.2f}   {:8.0f}".format(
              "macro avg",
              macro_precision,
              macro_recall,
              macro_f1,
              total_examples,
          )
      )
      report += (
          "\n{:50s}   {:8.2f}   {:8.2f}   {:8.2f}   {:8.0f}".format(
              "weighted avg",
              weighted_precision,
              weighted_recall,
              weighted_f1,
              total_examples,
          )
          + "\n"
      )
  
      self.total_examples = total_examples
  
      if self.mode == "macro":
          return macro_precision, macro_recall, macro_f1, report
      elif self.mode == "weighted":
          return (
              weighted_precision,
              weighted_recall,
              weighted_f1,
              report,
          )
      elif self.mode == "micro":
          return micro_precision, micro_recall, micro_f1, report
      elif self.mode == "all":
          return precision, recall, f1, report
      else:
          raise ValueError(
              f'{self.mode} mode is not supported. Choose "macro" to get aggregated numbers \
              or "all" to get values for each class.'
          )
  
file_path: NeMo/nemo/collections/nlp/metrics/classification_report.py
imports: from typing import Any, Dict, Optional, Tuple
  import torch
  from torchmetrics import Metric
  from torchmetrics.utilities.data import METRIC_EPS
label_desc: Use image node_id 3 for calling the ClassificationReport obj's underlying member method code with example usage: obj.compute() and returns: macro_precision,  macro_recall,  macro_f1,  report, weighted_precision,  weighted_recall,  weighted_f1,  report, micro_precision,  micro_recall,  micro_f1,  report, precision,  recall,  f1,  report