class_docstr: The Top-k Accuracy metric. This is a standalone metric.
  It is defined using torchmetrics.functional accuracy with top_k
class_docstr_tok: ["The","Top-k","Accuracy","metric",".","This","is","a","standalone","metric",".","It","is","defined","using","torchmetrics.functional","accuracy","with","top_k"]
docstr: Update the running top-k accuracy given the true and predicted labels.
  Parameter `task_labels` is used to decide how to update the inner
  dictionary: if Float, only the dictionary value related to that task
  is updated. If Tensor, all the dictionary elements belonging to the
  task labels will be updated.
  
  :param predicted_y: The model prediction. Both labels and logit vectors
      are supported.
  :param true_y: The ground truth. Both labels and one-hot vectors
      are supported.
  :param task_labels: the int task label associated to the current
      experience or the task labels vector showing the task label
      for each pattern.
  
  :return: None.
docstr_tok: ["Update","the","running","top-k","accuracy","given","the","true","and","predicted","labels",".","Parameter","`","task_labels","`","is","used","to","decide","how","to","update","the","inner","dictionary",":","if","Float",",","only","the","dictionary","value","related","to","that","task","is","updated",".","If","Tensor",",","all","the","dictionary","elements","belonging","to","the","task","labels","will","be","updated",".",":","param","predicted_y",":","The","model","prediction",".","Both","labels","and","logit","vectors","are","supported",".",":","param","true_y",":","The","ground","truth",".","Both","labels","and","one-hot","vectors","are","supported",".",":","param","task_labels",":","the","int","task","label","associated","to","the","current","experience","or","the","task","labels","vector","showing","the","task","label","for","each","pattern",".",":","return",":","None","."]
code: def update(
      self,
      predicted_y: Tensor,
      true_y: Tensor,
      task_labels: Union[float, Tensor],
  ) -> None:
      """
      Update the running top-k accuracy given the true and predicted labels.
      Parameter `task_labels` is used to decide how to update the inner
      dictionary: if Float, only the dictionary value related to that task
      is updated. If Tensor, all the dictionary elements belonging to the
      task labels will be updated.
  
      :param predicted_y: The model prediction. Both labels and logit vectors
          are supported.
      :param true_y: The ground truth. Both labels and one-hot vectors
          are supported.
      :param task_labels: the int task label associated to the current
          experience or the task labels vector showing the task label
          for each pattern.
  
      :return: None.
      """
      if len(true_y) != len(predicted_y):
          raise ValueError(
              "Size mismatch for true_y and predicted_y tensors"
          )
  
      if isinstance(task_labels, Tensor) and len(task_labels) != len(
          true_y
      ):
          raise ValueError(
              "Size mismatch for true_y and task_labels tensors"
          )
  
      true_y = torch.as_tensor(true_y)
      predicted_y = torch.as_tensor(predicted_y)
  
      if isinstance(task_labels, int):
          total_patterns = len(true_y)
          self._topk_acc_dict[task_labels].update(
              self._compute_topk_acc(
                  predicted_y, true_y, top_k=self.top_k
              ),
              total_patterns,
          )
      elif isinstance(task_labels, Tensor):
          for pred, true, t in zip(predicted_y, true_y, task_labels):
              self._topk_acc_dict[int(t)].update(
                  self._compute_topk_acc(pred, true, top_k=self.top_k),
                  1,
              )
      else:
          raise ValueError(
              f"Task label type: {type(task_labels)}, "
              f"expected int/float or Tensor"
          )
  
imports: from typing import TYPE_CHECKING, List, Union, Dict
  import torch
  from torch import Tensor
  import torchmetrics
  from torchmetrics.functional import accuracy
  from avalanche.evaluation import Metric, GenericPluginMetric
  from avalanche.evaluation.metrics.mean import Mean
  from avalanche.evaluation.metric_utils import phase_and_task
  from collections import defaultdict
  from packaging import version
label_desc: Use image node_id 2 for calling the TopkAccuracy obj's underlying member method code with example usage: obj.update(predicted_y, true_y, task_labels) without return types