class_docstr: Meter to calculate top-k precision for single-label or multi-label
  image classification task. Note, ties are resolved randomly.
class_docstr_tok: ["Meter","to","calculate","top-k","precision","for","single-label","or","multi-label","image","classification","task",".","Note",",","ties","are","resolved","randomly","."]
docstr: args:
      model_output: tensor of shape (B, C) where each value is
                    either logit or class probability.
      target:       tensor of shape (B, C), which is one-hot /
                    multi-label encoded, or tensor of shape (B) /
                    (B, 1), integer encoded
docstr_tok: ["args",":","model_output",":","tensor","of","shape","(","B",",","C",")","where","each","value","is","either","logit","or","class","probability",".","target",":","tensor","of","shape","(","B",",","C",")",",","which","is","one-hot","\/","multi-label","encoded",",","or","tensor","of","shape","(","B",")","\/","(","B",",","1",")",",","integer","encoded"]
code: def update(self, model_output, target, **kwargs):
      """
      args:
          model_output: tensor of shape (B, C) where each value is
                        either logit or class probability.
          target:       tensor of shape (B, C), which is one-hot /
                        multi-label encoded, or tensor of shape (B) /
                        (B, 1), integer encoded
      """
      # Convert target to 0/1 encoding if isn't
      target = maybe_convert_to_one_hot(target, model_output)
  
      # If Pytorch AMP is being used, model outputs are probably fp16
      # Since .topk() is not compatible with fp16, we promote the model outputs to full precision
      _, pred_classes = model_output.float().topk(
          max(self._topk), dim=1, largest=True, sorted=True
      )
      pred_mask_tensor = torch.zeros(target.size())
      for i, k in enumerate(self._topk):
          pred_mask_tensor.zero_()
          self._curr_correct_predictions_k[i] += torch.sum(
              # torch.min is used to simulate AND between binary
              # tensors. If tensors are not binary, this will fail.
              torch.min(
                  pred_mask_tensor.scatter_(
                      1, pred_classes[:, :k], 1.0
                  ),
                  target.float(),
              )
          ).item()
      self._curr_sample_count += model_output.shape[0]
  
file_path: ClassyVision/classy_vision/meters/precision_meter.py
imports: from typing import Any, Dict, Sequence
  import torch
  from classy_vision.generic.distributed_util import all_reduce_sum
  from classy_vision.generic.util import is_pos_int, maybe_convert_to_one_hot
  from classy_vision.meters import ClassyMeter
  from .None import register_meter
label_desc: Use image node_id 8 for calling the PrecisionAtKMeter obj's underlying member method code with example usage: obj.update(model_output, target) without return types