class_docstr: This implements the mixup data augmentation in the paper
  "mixup: Beyond Empirical Risk Minimization" (https://arxiv.org/abs/1710.09412)
class_docstr_tok: ["This","implements","the","mixup","data","augmentation","in","the","paper","''","mixup",":","Beyond","Empirical","Risk","Minimization","''","(","https",":","\/\/arxiv.org\/abs\/1710.09412",")"]
docstr: Args:
      sample: the batch data.
docstr_tok: ["Args",":","sample",":","the","batch","data","."]
code: def __call__(self, sample: Dict[str, Any]) -> Dict[str, Any]:
      """
      Args:
          sample: the batch data.
      """
      assert len(sample["target"]) % 2 == 0, "Batch size should be even"
  
      if torch.is_tensor(sample["input"]) and sample["input"].ndim == 4:
          # This is the simple case of image data batch (i.e. 4D tensor).
          # We support more advanved joint mixup and cutmix in this case.
          if self.mode == "elem":
              lam = self._mix_elem(sample["input"])
          elif self.mode == "pair":
              lam = self._mix_pair(sample["input"])
          else:
              lam = self._mix_batch(sample["input"])
  
          sample["target"] = mixup_target(
              sample["target"],
              self.num_classes,
              lam=lam,
              smoothing=self.label_smoothing,
          )
      else:
          # This is the complex case of video data batch (i.e. 5D tensor) or more complex
          # data batch. We only support mixup augmentation in batch mode.
          if sample["target"].ndim == 1:
              assert (
                  self.num_classes is not None
              ), "num_classes is expected for 1D target"
  
              off_value = self.label_smoothing / self.num_classes
              on_value = 1.0 - self.label_smoothing + off_value
  
              sample["target"] = one_hot(
                  sample["target"],
                  self.num_classes,
                  on_value=on_value,
                  off_value=off_value,
                  device=sample["target"].device,
              )
          else:
              assert (
                  sample["target"].ndim == 2
              ), "target tensor shape must be 1D or 2D"
  
          c = Beta(self.mixup_alpha, self.mixup_alpha).sample()
  
          sample["target"] = c * sample["target"] + (1.0 - c) * sample[
              "target"
          ].flip(0)
          sample["input"] = _recursive_mixup(sample["input"], c)
  
      return sample
  
imports: import collections.abc
  import math
  from typing import Any, Dict, Optional, Tuple
  import numpy
  import torch
  from torch.distributions.beta import Beta
label_desc: Use image node_id 7 for calling the MixupTransform obj's underlying member method code with example usage: obj.__call__(sample) and returns: sample