class_docstr: Calculates confusion matrix for multi-class data.
  
  - ``update`` must receive output of the form ``(y_pred, y)``.
  - `y_pred` must contain logits and has the following shape (batch_size, num_classes, ...).
    If you are doing binary classification, see Note for an example on how to get this.
  - `y` should have the following shape (batch_size, ...) and contains ground-truth class indices
    with or without the background class. During the computation, argmax of `y_pred` is taken to determine
    predicted classes.
  
  Args:
      num_classes: Number of classes, should be > 1. See notes for more details.
      average: confusion matrix values averaging schema: None, "samples", "recall", "precision".
          Default is None. If `average="samples"` then confusion matrix values are normalized by the number of seen
          samples. If `average="recall"` then confusion matrix values are normalized such that diagonal values
          represent class recalls. If `average="precision"` then confusion matrix values are normalized such that
          diagonal values represent class precisions.
      output_transform: a callable that is used to transform the
          :class:`~ignite.engine.engine.Engine`'s ``process_function``'s output into the
          form expected by the metric. This can be useful if, for example, you have a multi-output model and
          you want to compute the metric with respect to one of the outputs.
      device: specifies which device updates are accumulated on. Setting the metric's
          device to be the same as your ``update`` arguments ensures the ``update`` method is non-blocking. By
          default, CPU.
  
  Note:
      The confusion matrix is formatted such that columns are predictions and rows are targets.
      For example, if you were to plot the matrix, you could correctly assign to the horizontal axis
      the label "predicted values" and to the vertical axis the label "actual values".
  
  Note:
      In case of the targets `y` in `(batch_size, ...)` format, target indices between 0 and `num_classes` only
      contribute to the confusion matrix and others are neglected. For example, if `num_classes=20` and target index
      equal 255 is encountered, then it is filtered out.
  
  Examples:
  
      For more information on how metric works with :class:`~ignite.engine.engine.Engine`, visit :ref:`attach-engine`.
  
      .. include:: defaults.rst
          :start-after: :orphan:
  
      .. testcode:: 1
  
          metric = ConfusionMatrix(num_classes=3)
          metric.attach(default_evaluator, 'cm')
          y_true = torch.tensor([0, 1, 0, 1, 2])
          y_pred = torch.tensor([
              [0.0, 1.0, 0.0],
              [0.0, 1.0, 0.0],
              [1.0, 0.0, 0.0],
              [0.0, 1.0, 0.0],
              [0.0, 1.0, 0.0],
          ])
          state = default_evaluator.run([[y_pred, y_true]])
          print(state.metrics['cm'])
  
      .. testoutput:: 1
  
          tensor([[1, 1, 0],
                  [0, 2, 0],
                  [0, 1, 0]])
  
      If you are doing binary classification with a single output unit, you may have to transform your network output,
      so that you have one value for each class. E.g. you can transform your network output into a one-hot vector
      with:
  
      .. testcode:: 2
  
          def binary_one_hot_output_transform(output):
              y_pred, y = output
              y_pred = torch.sigmoid(y_pred).round().long()
              y_pred = ignite.utils.to_onehot(y_pred, 2)
              y = y.long()
              return y_pred, y
  
          metric = ConfusionMatrix(num_classes=2, output_transform=binary_one_hot_output_transform)
          metric.attach(default_evaluator, 'cm')
          y_true = torch.tensor([0, 1, 0, 1, 0])
          y_pred = torch.tensor([0, 0, 1, 1, 0])
          state = default_evaluator.run([[y_pred, y_true]])
          print(state.metrics['cm'])
  
      .. testoutput:: 2
  
          tensor([[2, 1],
                  [1, 1]])
class_docstr_tok: ["Calculates","confusion","matrix","for","multi-class","data",".","-","``","update","``","must","receive","output","of","the","form","``","(","y_pred",",","y",")","``",".","-","`","y_pred","`","must","contain","logits","and","has","the","following","shape","(","batch_size",",","num_classes",",","...",")",".","If","you","are","doing","binary","classification",",","see","Note","for","an","example","on","how","to","get","this",".","-","`","y","`","should","have","the","following","shape","(","batch_size",",","...",")","and","contains","ground-truth","class","indices","with","or","without","the","background","class",".","During","the","computation",",","argmax","of","`","y_pred","`","is","taken","to","determine","predicted","classes",".","Args",":","num_classes",":","Number","of","classes",",","should","be",">","1",".","See","notes","for","more","details",".","average",":","confusion","matrix","values","averaging","schema",":","None",",","``","samples","''",",","``","recall","''",",","``","precision","''",".","Default","is","None",".","If","`","average=","''","samples","''","`","then","confusion","matrix","values","are","normalized","by","the","number","of","seen","samples",".","If","`","average=","''","recall","''","`","then","confusion","matrix","values","are","normalized","such","that","diagonal","values","represent","class","recalls",".","If","`","average=","''","precision","''","`","then","confusion","matrix","values","are","normalized","such","that","diagonal","values","represent","class","precisions",".","output_transform",":","a","callable","that","is","used","to","transform","the",":","class",":","`","~ignite.engine.engine.Engine","`","'s","``","process_function","``","'s","output","into","the","form","expected","by","the","metric",".","This","can","be","useful","if",",","for","example",",","you","have","a","multi-output","model","and","you","want","to","compute","the","metric","with","respect","to","one","of","the","outputs",".","device",":","specifies","which","device","updates","are","accumulated","on",".","Setting","the","metric's","device","to","be","the","same","as","your","``","update","``","arguments","ensures","the","``","update","``","method","is","non-blocking",".","By","default",",","CPU",".","Note",":","The","confusion","matrix","is","formatted","such","that","columns","are","predictions","and","rows","are","targets",".","For","example",",","if","you","were","to","plot","the","matrix",",","you","could","correctly","assign","to","the","horizontal","axis","the","label","``","predicted","values","''","and","to","the","vertical","axis","the","label","``","actual","values","''",".","Note",":","In","case","of","the","targets","`","y","`","in","`","(","batch_size",",","...",")","`","format",",","target","indices","between","0","and","`","num_classes","`","only","contribute","to","the","confusion","matrix","and","others","are","neglected",".","For","example",",","if","`","num_classes=20","`","and","target","index","equal","255","is","encountered",",","then","it","is","filtered","out",".","Examples",":","For","more","information","on","how","metric","works","with",":","class",":","`","~ignite.engine.engine.Engine","`",",","visit",":","ref",":","`","attach-engine","`",".","..","include",":",":","defaults.rst",":","start-after",":",":","orphan",":","..","testcode",":",":","1","metric","=","ConfusionMatrix","(","num_classes=3",")","metric.attach","(","default_evaluator",",","'cm","'",")","y_true","=","torch.tensor","(","[","0",",","1",",","0",",","1",",","2","]",")","y_pred","=","torch.tensor","(","[","[","0.0",",","1.0",",","0.0","]",",","[","0.0",",","1.0",",","0.0","]",",","[","1.0",",","0.0",",","0.0","]",",","[","0.0",",","1.0",",","0.0","]",",","[","0.0",",","1.0",",","0.0","]",",","]",")","state","=","default_evaluator.run","(","[","[","y_pred",",","y_true","]","]",")","print","(","state.metrics","[","'cm","'","]",")","..","testoutput",":",":","1","tensor","(","[","[","1",",","1",",","0","]",",","[","0",",","2",",","0","]",",","[","0",",","1",",","0","]","]",")","If","you","are","doing","binary","classification","with","a","single","output","unit",",","you","may","have","to","transform","your","network","output",",","so","that","you","have","one","value","for","each","class",".","E.g",".","you","can","transform","your","network","output","into","a","one-hot","vector","with",":","..","testcode",":",":","2","def","binary_one_hot_output_transform","(","output",")",":","y_pred",",","y","=","output","y_pred","=","torch.sigmoid","(","y_pred",")",".round","(",")",".long","(",")","y_pred","=","ignite.utils.to_onehot","(","y_pred",",","2",")","y","=","y.long","(",")","return","y_pred",",","y","metric","=","ConfusionMatrix","(","num_classes=2",",","output_transform=binary_one_hot_output_transform",")","metric.attach","(","default_evaluator",",","'cm","'",")","y_true","=","torch.tensor","(","[","0",",","1",",","0",",","1",",","0","]",")","y_pred","=","torch.tensor","(","[","0",",","0",",","1",",","1",",","0","]",")","state","=","default_evaluator.run","(","[","[","y_pred",",","y_true","]","]",")","print","(","state.metrics","[","'cm","'","]",")","..","testoutput",":",":","2","tensor","(","[","[","2",",","1","]",",","[","1",",","1","]","]",")"]
code: def reset(self) -> None:
      self.confusion_matrix = torch.zeros(
          self.num_classes,
          self.num_classes,
          dtype=torch.int64,
          device=self._device,
      )
      self._num_examples = 0
  
imports: import numbers
  from typing import Callable, Optional, Sequence, Tuple, Union
  import torch
  from ignite.exceptions import NotComputableError
  from ignite.metrics.metric import Metric, reinit__is_reduced, sync_all_reduce
  from ignite.metrics.metrics_lambda import MetricsLambda
label_desc: Use image node_id 2 for calling the ConfusionMatrix obj's underlying member method code with example usage: obj.reset() without return types