class_docstr: Combines different loss functions and returns the weighted sum of these losses.
  `loss_category` and `loss_weight` are two mandatory keys that allows us to combine
  different losses and compute their weighted sum. The `loss_category` specifies the category
  of a loss function and is a string (e.g., classification). The `loss_weight` specifies
  the contribution of a loss function and is a float value (e.g., 1.0). The sum of `loss_weight`s
  corresponding to different loss functions is not required to be 1.
  
  Args:
      opts: command-line arguments
  
  Example::
  # Example yaml config for combining classification and neural_augmentation loss function is given below.
  # Please note that configuration for each loss function should start with `-` in `composite_loss`.
  
  loss:
    category: "composite_loss"
    composite_loss:
      - loss_category: "classification"
        loss_weight: 1.0
        classification:
          name: "cross_entropy"
          cross_entropy:
            label_smoothing: 0.1
      - loss_category: "neural_augmentation"
        loss_weight: 1.0
        neural_augmentation:
          perceptual_metric: "psnr"
          target_value: [ 40, 10 ]
          curriculum_method: "cosine"
class_docstr_tok: ["Combines","different","loss","functions","and","returns","the","weighted","sum","of","these","losses",".","`","loss_category","`","and","`","loss_weight","`","are","two","mandatory","keys","that","allows","us","to","combine","different","losses","and","compute","their","weighted","sum",".","The","`","loss_category","`","specifies","the","category","of","a","loss","function","and","is","a","string","(","e.g.",",","classification",")",".","The","`","loss_weight","`","specifies","the","contribution","of","a","loss","function","and","is","a","float","value","(","e.g.",",","1.0",")",".","The","sum","of","`","loss_weight","`","s","corresponding","to","different","loss","functions","is","not","required","to","be","1",".","Args",":","opts",":","command-line","arguments","Example",":",":","#","Example","yaml","config","for","combining","classification","and","neural_augmentation","loss","function","is","given","below",".","#","Please","note","that","configuration","for","each","loss","function","should","start","with","`","-","`","in","`","composite_loss","`",".","loss",":","category",":","``","composite_loss","''","composite_loss",":","-","loss_category",":","``","classification","''","loss_weight",":","1.0","classification",":","name",":","``","cross_entropy","''","cross_entropy",":","label_smoothing",":","0.1","-","loss_category",":","``","neural_augmentation","''","loss_weight",":","1.0","neural_augmentation",":","perceptual_metric",":","``","psnr","''","target_value",":","[","40",",","10","]","curriculum_method",":","``","cosine","''"]
docstr: Compute the weighted sum of different loss functions.
  
  Args:
      input_sample: Input to the model.
      prediction: Model's output
      target: Ground truth labels
  
  Returns:
      A mapping of the form (str: scalar loss value) with `total_loss` as a mandatory key.
      The other keys corresponds to loss category names and their values contain category-specific
      scalar loss values. total_loss is weighted sum of these category-specific losses.
docstr_tok: ["Compute","the","weighted","sum","of","different","loss","functions",".","Args",":","input_sample",":","Input","to","the","model",".","prediction",":","Model","'s","output","target",":","Ground","truth","labels","Returns",":","A","mapping","of","the","form","(","str",":","scalar","loss","value",")","with","`","total_loss","`","as","a","mandatory","key",".","The","other","keys","corresponds","to","loss","category","names","and","their","values","contain","category-specific","scalar","loss","values",".","total_loss","is","weighted","sum","of","these","category-specific","losses","."]
code: def forward(
      self,
      input_sample: Any,
      prediction: Any,
      target: Any,
      *args,
      **kwargs,
  ) -> Any:
      """Compute the weighted sum of different loss functions.
  
      Args:
          input_sample: Input to the model.
          prediction: Model's output
          target: Ground truth labels
  
      Returns:
          A mapping of the form (str: scalar loss value) with `total_loss` as a mandatory key.
          The other keys corresponds to loss category names and their values contain category-specific
          scalar loss values. total_loss is weighted sum of these category-specific losses.
      """
      outputs = {}
      total_loss = 0.0
      for loss_name, loss_layer in self.loss_fns.items():
          loss_wt = self.loss_weights[loss_name]
          loss_val = loss_layer(
              input_sample=input_sample,
              prediction=prediction,
              target=target,
              *args,
              **kwargs,
          )
  
          if not isinstance(loss_val, (Tensor, Mapping)):
              logger.error(
                  "Loss value is expected as a scalar or dictionary of scalars with total_loss as a "
                  "mandatory key."
              )
  
          if isinstance(loss_val, Mapping) and "total_loss" in loss_val:
              loss_val = loss_val["total_loss"]
  
              if not isinstance(loss_val, Tensor):
                  logger.error(
                      f"Value corresponding to total_loss key in {loss_val} is expected to be scalar."
                      f"Got: {type(loss_val)}"
                  )
          # scale the loss
          loss_val = loss_val * loss_wt
  
          total_loss += loss_val
          outputs[loss_name] = loss_val
      outputs.update({"total_loss": total_loss})
      return outputs
  
file_path: ml-cvnets/loss_fn/composite_loss.py
imports: from __future__ import annotations
  import argparse
  import copy
  import json
  from typing import Any, List, Mapping, Tuple
  from torch import Tensor
  from loss_fn import LOSS_REGISTRY, BaseCriteria, build_loss_fn
  from options.utils import flatten_yaml_as_dict
  from utils import logger
label_desc: Use image node_id 4 for calling the CompositeLoss obj's underlying member method code with example usage: obj.forward(input_sample, prediction, target) and returns: outputs