class_docstr: Wrapper metric for altering the output of classification metrics.
  
  This metric works together with classification metrics that returns multiple values (one value per class) such that
  label information can be automatically included in the output.
  
  Args:
      metric: base metric that should be wrapped. It is assumed that the metric outputs a single
          tensor that is split along the first dimension.
      labels: list of strings indicating the different classes.
      prefix: string that is prepended to the metric names.
      postfix: string that is appended to the metric names.
  
  Example::
      Basic example where the output of a metric is unwrapped into a dictionary with the class index as keys:
  
      >>> import torch
      >>> _ = torch.manual_seed(42)
      >>> from torchmetrics.wrappers import ClasswiseWrapper
      >>> from torchmetrics.classification import MulticlassAccuracy
      >>> metric = ClasswiseWrapper(MulticlassAccuracy(num_classes=3, average=None))
      >>> preds = torch.randn(10, 3).softmax(dim=-1)
      >>> target = torch.randint(3, (10,))
      >>> metric(preds, target)  # doctest: +NORMALIZE_WHITESPACE
      {'multiclassaccuracy_0': tensor(0.5000),
      'multiclassaccuracy_1': tensor(0.7500),
      'multiclassaccuracy_2': tensor(0.)}
  
  Example::
      Using custom name via prefix and postfix:
  
      >>> import torch
      >>> _ = torch.manual_seed(42)
      >>> from torchmetrics.wrappers import ClasswiseWrapper
      >>> from torchmetrics.classification import MulticlassAccuracy
      >>> metric_pre = ClasswiseWrapper(MulticlassAccuracy(num_classes=3, average=None), prefix="acc-")
      >>> metric_post = ClasswiseWrapper(MulticlassAccuracy(num_classes=3, average=None), postfix="-acc")
      >>> preds = torch.randn(10, 3).softmax(dim=-1)
      >>> target = torch.randint(3, (10,))
      >>> metric_pre(preds, target)  # doctest: +NORMALIZE_WHITESPACE
      {'acc-0': tensor(0.5000),
       'acc-1': tensor(0.7500),
       'acc-2': tensor(0.)}
      >>> metric_post(preds, target)  # doctest: +NORMALIZE_WHITESPACE
      {'0-acc': tensor(0.5000),
       '1-acc': tensor(0.7500),
       '2-acc': tensor(0.)}
  
  Example::
      Providing labels as a list of strings:
  
      >>> from torchmetrics.wrappers import ClasswiseWrapper
      >>> from torchmetrics.classification import MulticlassAccuracy
      >>> metric = ClasswiseWrapper(
      ...    MulticlassAccuracy(num_classes=3, average=None),
      ...    labels=["horse", "fish", "dog"]
      ... )
      >>> preds = torch.randn(10, 3).softmax(dim=-1)
      >>> target = torch.randint(3, (10,))
      >>> metric(preds, target)  # doctest: +NORMALIZE_WHITESPACE
      {'multiclassaccuracy_horse': tensor(0.3333),
      'multiclassaccuracy_fish': tensor(0.6667),
      'multiclassaccuracy_dog': tensor(0.)}
  
  Example::
      Classwise can also be used in combination with :class:`~torchmetrics.MetricCollection`. In this case, everything
      will be flattened into a single dictionary:
  
      >>> from torchmetrics import MetricCollection
      >>> from torchmetrics.wrappers import ClasswiseWrapper
      >>> from torchmetrics.classification import MulticlassAccuracy, MulticlassRecall
      >>> labels = ["horse", "fish", "dog"]
      >>> metric = MetricCollection(
      ...     {'multiclassaccuracy': ClasswiseWrapper(MulticlassAccuracy(num_classes=3, average=None), labels),
      ...     'multiclassrecall': ClasswiseWrapper(MulticlassRecall(num_classes=3, average=None), labels)}
      ... )
      >>> preds = torch.randn(10, 3).softmax(dim=-1)
      >>> target = torch.randint(3, (10,))
      >>> metric(preds, target)  # doctest: +NORMALIZE_WHITESPACE
      {'multiclassaccuracy_horse': tensor(0.),
       'multiclassaccuracy_fish': tensor(0.3333),
       'multiclassaccuracy_dog': tensor(0.4000),
       'multiclassrecall_horse': tensor(0.),
       'multiclassrecall_fish': tensor(0.3333),
       'multiclassrecall_dog': tensor(0.4000)}
class_docstr_tok: ["Wrapper","metric","for","altering","the","output","of","classification","metrics",".","This","metric","works","together","with","classification","metrics","that","returns","multiple","values","(","one","value","per","class",")","such","that","label","information","can","be","automatically","included","in","the","output",".","Args",":","metric",":","base","metric","that","should","be","wrapped",".","It","is","assumed","that","the","metric","outputs","a","single","tensor","that","is","split","along","the","first","dimension",".","labels",":","list","of","strings","indicating","the","different","classes",".","prefix",":","string","that","is","prepended","to","the","metric","names",".","postfix",":","string","that","is","appended","to","the","metric","names",".","Example",":",":","Basic","example","where","the","output","of","a","metric","is","unwrapped","into","a","dictionary","with","the","class","index","as","keys",":",">",">",">","import","torch",">",">",">","_","=","torch.manual_seed","(","42",")",">",">",">","from","torchmetrics.wrappers","import","ClasswiseWrapper",">",">",">","from","torchmetrics.classification","import","MulticlassAccuracy",">",">",">","metric","=","ClasswiseWrapper","(","MulticlassAccuracy","(","num_classes=3",",","average=None",")",")",">",">",">","preds","=","torch.randn","(","10",",","3",")",".softmax","(","dim=-1",")",">",">",">","target","=","torch.randint","(","3",",","(","10",",",")",")",">",">",">","metric","(","preds",",","target",")","#","doctest",":","+NORMALIZE_WHITESPACE","{","'multiclassaccuracy_0","'",":","tensor","(","0.5000",")",",","'multiclassaccuracy_1","'",":","tensor","(","0.7500",")",",","'multiclassaccuracy_2","'",":","tensor","(","0",".",")","}","Example",":",":","Using","custom","name","via","prefix","and","postfix",":",">",">",">","import","torch",">",">",">","_","=","torch.manual_seed","(","42",")",">",">",">","from","torchmetrics.wrappers","import","ClasswiseWrapper",">",">",">","from","torchmetrics.classification","import","MulticlassAccuracy",">",">",">","metric_pre","=","ClasswiseWrapper","(","MulticlassAccuracy","(","num_classes=3",",","average=None",")",",","prefix=","''","acc-","''",")",">",">",">","metric_post","=","ClasswiseWrapper","(","MulticlassAccuracy","(","num_classes=3",",","average=None",")",",","postfix=","''","-acc","''",")",">",">",">","preds","=","torch.randn","(","10",",","3",")",".softmax","(","dim=-1",")",">",">",">","target","=","torch.randint","(","3",",","(","10",",",")",")",">",">",">","metric_pre","(","preds",",","target",")","#","doctest",":","+NORMALIZE_WHITESPACE","{","'acc-0","'",":","tensor","(","0.5000",")",",","'acc-1","'",":","tensor","(","0.7500",")",",","'acc-2","'",":","tensor","(","0",".",")","}",">",">",">","metric_post","(","preds",",","target",")","#","doctest",":","+NORMALIZE_WHITESPACE","{","'","0-acc","'",":","tensor","(","0.5000",")",",","'","1-acc","'",":","tensor","(","0.7500",")",",","'","2-acc","'",":","tensor","(","0",".",")","}","Example",":",":","Providing","labels","as","a","list","of","strings",":",">",">",">","from","torchmetrics.wrappers","import","ClasswiseWrapper",">",">",">","from","torchmetrics.classification","import","MulticlassAccuracy",">",">",">","metric","=","ClasswiseWrapper","(","...","MulticlassAccuracy","(","num_classes=3",",","average=None",")",",","...","labels=","[","``","horse","''",",","``","fish","''",",","``","dog","''","]","...",")",">",">",">","preds","=","torch.randn","(","10",",","3",")",".softmax","(","dim=-1",")",">",">",">","target","=","torch.randint","(","3",",","(","10",",",")",")",">",">",">","metric","(","preds",",","target",")","#","doctest",":","+NORMALIZE_WHITESPACE","{","'multiclassaccuracy_horse","'",":","tensor","(","0.3333",")",",","'multiclassaccuracy_fish","'",":","tensor","(","0.6667",")",",","'multiclassaccuracy_dog","'",":","tensor","(","0",".",")","}","Example",":",":","Classwise","can","also","be","used","in","combination","with",":","class",":","`","~torchmetrics.MetricCollection","`",".","In","this","case",",","everything","will","be","flattened","into","a","single","dictionary",":",">",">",">","from","torchmetrics","import","MetricCollection",">",">",">","from","torchmetrics.wrappers","import","ClasswiseWrapper",">",">",">","from","torchmetrics.classification","import","MulticlassAccuracy",",","MulticlassRecall",">",">",">","labels","=","[","``","horse","''",",","``","fish","''",",","``","dog","''","]",">",">",">","metric","=","MetricCollection","(","...","{","'multiclassaccuracy","'",":","ClasswiseWrapper","(","MulticlassAccuracy","(","num_classes=3",",","average=None",")",",","labels",")",",","...","'multiclassrecall","'",":","ClasswiseWrapper","(","MulticlassRecall","(","num_classes=3",",","average=None",")",",","labels",")","}","...",")",">",">",">","preds","=","torch.randn","(","10",",","3",")",".softmax","(","dim=-1",")",">",">",">","target","=","torch.randint","(","3",",","(","10",",",")",")",">",">",">","metric","(","preds",",","target",")","#","doctest",":","+NORMALIZE_WHITESPACE","{","'multiclassaccuracy_horse","'",":","tensor","(","0",".",")",",","'multiclassaccuracy_fish","'",":","tensor","(","0.3333",")",",","'multiclassaccuracy_dog","'",":","tensor","(","0.4000",")",",","'multiclassrecall_horse","'",":","tensor","(","0",".",")",",","'multiclassrecall_fish","'",":","tensor","(","0.3333",")",",","'multiclassrecall_dog","'",":","tensor","(","0.4000",")","}"]
code: def __init__(
      self,
      metric: Metric,
      labels: Optional[List[str]] = None,
      prefix: Optional[str] = None,
      postfix: Optional[str] = None,
  ) -> None:
      super().__init__()
      if not isinstance(metric, Metric):
          raise ValueError(
              f"Expected argument `metric` to be an instance of `torchmetrics.Metric` but got {metric}"
          )
      self.metric = metric
  
      if labels is not None and not (
          isinstance(labels, list)
          and all(isinstance(lab, str) for lab in labels)
      ):
          raise ValueError(
              f"Expected argument `labels` to either be `None` or a list of strings but got {labels}"
          )
      self.labels = labels
  
      if prefix is not None and not isinstance(prefix, str):
          raise ValueError(
              f"Expected argument `prefix` to either be `None` or a string but got {prefix}"
          )
      self._prefix = prefix
  
      if postfix is not None and not isinstance(postfix, str):
          raise ValueError(
              f"Expected argument `postfix` to either be `None` or a string but got {postfix}"
          )
      self._postfix = postfix
  
      self._update_count = 1
  
file_path: torchmetrics/src/torchmetrics/wrappers/classwise.py
imports: from typing import Any, Dict, List, Optional, Sequence, Union
  from torch import Tensor
  from torchmetrics.metric import Metric
  from torchmetrics.utilities.imports import _MATPLOTLIB_AVAILABLE
  from torchmetrics.utilities.plot import _AX_TYPE, _PLOT_OUT_TYPE
  from torchmetrics.wrappers.abstract import WrapperMetric
label_desc: Use image node_id 1 to create a new ClasswiseWrapper object from inherited base classes: WrapperMetric with example: obj = ClasswiseWrapper(metric, labels, prefix, postfix)