class_docstr: This metric computes numerator and denominator for Overall Accuracy between logits and labels.
  When doing distributed training/evaluation the result of res=TopKClassificationAccuracy(logits, labels) calls
  will be all-reduced between all workers using SUM operations.
  Here contains two numbers res=[correctly_predicted, total_samples]. Accuracy=correctly_predicted/total_samples.
  
  If used with PytorchLightning LightningModule, include correct_count and total_count inside validation_step results.
  Then aggregate (sum) then at the end of validation epoch to correctly compute validation WER.
  
  Example:
      def validation_step(self, batch, batch_idx):
          ...
          correct_count, total_count = self._accuracy(logits, labels)
          self.val_outputs = {'val_loss': loss_value, 'val_correct_count': correct_count, 'val_total_count': total_count}
          return self.val_outputs
  
      def on_validation_epoch_end(self):
          ...
          val_loss_mean = torch.stack([x['val_loss'] for x in self.val_outputs]).mean()
          correct_counts = torch.stack([x['val_correct_counts'] for x in self.val_outputs])
          total_counts = torch.stack([x['val_total_counts'] for x in self.val_outputs])
  
          topk_scores = compute_topk_accuracy(correct_counts, total_counts)
  
          tensorboard_log = {'val_loss': val_loss_mean}
          for top_k, score in zip(self._accuracy.top_k, topk_scores):
              tensorboard_log['val_epoch_top@{}'.format(top_k)] = score
          
          self.val_outputs.clear()  # free memory
          return {'log': tensorboard_log}
  
  Args:
      top_k: Optional list of integers. Defaults to [1].
  
  Returns:
      res: a torch.Tensor object with two elements: [correct_count, total_count]. To correctly compute average
      accuracy, compute acc=correct_count/total_count
class_docstr_tok: ["This","metric","computes","numerator","and","denominator","for","Overall","Accuracy","between","logits","and","labels",".","When","doing","distributed","training\/evaluation","the","result","of","res=TopKClassificationAccuracy","(","logits",",","labels",")","calls","will","be","all-reduced","between","all","workers","using","SUM","operations",".","Here","contains","two","numbers","res=","[","correctly_predicted",",","total_samples","]",".","Accuracy=correctly_predicted\/total_samples",".","If","used","with","PytorchLightning","LightningModule",",","include","correct_count","and","total_count","inside","validation_step","results",".","Then","aggregate","(","sum",")","then","at","the","end","of","validation","epoch","to","correctly","compute","validation","WER",".","Example",":","def","validation_step","(","self",",","batch",",","batch_idx",")",":","...","correct_count",",","total_count","=","self._accuracy","(","logits",",","labels",")","self.val_outputs","=","{","'val_loss","'",":","loss_value",",","'val_correct_count","'",":","correct_count",",","'val_total_count","'",":","total_count","}","return","self.val_outputs","def","on_validation_epoch_end","(","self",")",":","...","val_loss_mean","=","torch.stack","(","[","x","[","'val_loss","'","]","for","x","in","self.val_outputs","]",")",".mean","(",")","correct_counts","=","torch.stack","(","[","x","[","'val_correct_counts","'","]","for","x","in","self.val_outputs","]",")","total_counts","=","torch.stack","(","[","x","[","'val_total_counts","'","]","for","x","in","self.val_outputs","]",")","topk_scores","=","compute_topk_accuracy","(","correct_counts",",","total_counts",")","tensorboard_log","=","{","'val_loss","'",":","val_loss_mean","}","for","top_k",",","score","in","zip","(","self._accuracy.top_k",",","topk_scores",")",":","tensorboard_log","[","'val_epoch_top","@","{","}","'.format","(","top_k",")","]","=","score","self.val_outputs.clear","(",")","#","free","memory","return","{","'log","'",":","tensorboard_log","}","Args",":","top_k",":","Optional","list","of","integers",".","Defaults","to","[","1","]",".","Returns",":","res",":","a","torch.Tensor","object","with","two","elements",":","[","correct_count",",","total_count","]",".","To","correctly","compute","average","accuracy",",","compute","acc=correct_count\/total_count"]
code: def __init__(self, top_k=None, dist_sync_on_step=False):
      super().__init__(dist_sync_on_step=dist_sync_on_step)
  
      if top_k is None:
          top_k = [1]
  
      self.top_k = top_k
      self.add_state(
          "correct_counts_k",
          default=torch.zeros(len(self.top_k)),
          dist_reduce_fx="sum",
          persistent=False,
      )
      self.add_state(
          "total_counts_k",
          default=torch.zeros(len(self.top_k)),
          dist_reduce_fx="sum",
          persistent=False,
      )
  
imports: import logging
  import re
  import string
  from collections import Counter
  from typing import List, Union
  import torch
  from torchmetrics import Metric
label_desc: Use image node_id 1 to create a new TopKClassificationAccuracy object from inherited base classes: Metric with example: obj = TopKClassificationAccuracy(top_k, dist_sync_on_step)