docstr: This function gather losses from different processes and converts to float.
docstr_tok: ["This","function","gather","losses","from","different","processes","and","converts","to","float","."]
code: def gather_metrics(
      self,
      prediction: Union[Tensor, Dict],
      target: Union[Tensor, Dict],
      extras: Dict[str, Any],
  ) -> Union[Tensor, Dict[str, Tensor]]:
      """
      This function gather losses from different processes and converts to float.
      """
      if extras is None:
          extras = {}
  
      loss = extras.get("loss", None)
  
      if loss is None:
          loss = 0.0
  
      if isinstance(loss, Tensor):
          return loss
      elif isinstance(loss, Number):
          return torch.tensor(loss, device=self.device)
      elif isinstance(loss, Dict):
          loss.pop(None, None)
  
          for k, v in loss.items():
              if isinstance(v, Number):
                  loss[k] = torch.tensor(loss, device=self.device)
              elif not isinstance(v, Tensor):
                  logger.error(
                      "Loss metric supports Number, Tensor, or Dict of Tensors."
                      f" Got {v} with {type(v)} type under key {k}."
                  )
  
          return loss
      else:
          logger.error(
              "Loss metric supports Number, Tensor, or Dict of Tensors."
              f" Got {loss} with {type(loss)} type."
          )
  
file_path: ml-cvnets/metrics/misc.py
imports: from numbers import Number
  from typing import Any, Dict, Union
  import torch
  from torch import Tensor
  from metrics import METRICS_REGISTRY
  from metrics.metric_base import AverageMetric
  from utils import logger
label_desc: Use image node_id 1 for calling the LossMetric obj's underlying member method code with example usage: obj.gather_metrics(prediction, target, extras) and returns: loss, torch, loss