code: def class_reduce(
      num: Tensor,
      denom: Tensor,
      weights: Tensor,
      class_reduction: Literal[
          "micro", "macro", "weighted", "none", None
      ] = "none",
  ) -> Tensor:
      """Reduce classification metrics of the form ``num / denom * weights``.
  
      For example for calculating standard accuracy the num would be number of true positives per class, denom would be
      the support per class, and weights would be a tensor of 1s.
  
      Args:
          num: numerator tensor
          denom: denominator tensor
          weights: weights for each class
          class_reduction: reduction method for multiclass problems:
  
              - ``'micro'``: calculate metrics globally (default)
              - ``'macro'``: calculate metrics for each label, and find their unweighted mean.
              - ``'weighted'``: calculate metrics for each label, and find their weighted mean.
              - ``'none'`` or ``None``: returns calculated metric per class
  
      Raises:
          ValueError:
              If ``class_reduction`` is none of ``"micro"``, ``"macro"``, ``"weighted"``, ``"none"`` or ``None``.
  
      """
      valid_reduction = ("micro", "macro", "weighted", "none", None)
      fraction = (
          torch.sum(num) / torch.sum(denom)
          if class_reduction == "micro"
          else num / denom
      )
  
      # We need to take care of instances where the denom can be 0
      # for some (or all) classes which will produce nans
      fraction[fraction != fraction] = 0
  
      if class_reduction == "micro":
          return fraction
      if class_reduction == "macro":
          return torch.mean(fraction)
      if class_reduction == "weighted":
          return torch.sum(
              fraction * (weights.float() / torch.sum(weights))
          )
      if class_reduction == "none" or class_reduction is None:
          return fraction
  
      raise ValueError(
          f"Reduction parameter {class_reduction} unknown. Choose between one of these: {valid_reduction}"
      )
  
file_path: torchmetrics/src/torchmetrics/utilities/distributed.py
imports: from typing import Any, List, Optional
  import torch
  from torch import Tensor
  from torch.nn import functional
  from typing_extensions import Literal
label_desc: Use image node_id 2 for calling a global function with example usage: class_reduce(num, denom, weights, class_reduction) and returns: fraction, torch, torch, fraction