class_docstr: This class drops slices of the input spectrogram.
  
  Using `SpectrogramDrop` as an augmentation strategy helps a models learn to rely
  on all parts of the signal, since it can't expect a given part to be
  present.
  
  Reference:
      https://arxiv.org/abs/1904.08779
  
  Arguments
  ---------
  drop_length_low : int
      The low end of lengths for which to drop the
      spectrogram, in samples.
  drop_length_high : int
      The high end of lengths for which to drop the
      signal, in samples.
  drop_count_low : int
      The low end of number of times that the signal
      can be dropped.
  drop_count_high : int
      The high end of number of times that the signal
      can be dropped.
  replace: str
      - 'zeros': Masked values are replaced with zeros.
      - 'mean': Masked values are replaced with the mean value of the spectrogram.
      - 'rand': Masked values are replaced with random numbers ranging between
                the maximum and minimum values of the spectrogram.
      - 'cutcat': Masked values are replaced with chunks from other signals in the batch.
      - 'swap': Masked values are replaced with other chunks from the same sentence.
      - 'random_selection': A random selection among the approaches above.
  dim : int
      Corresponding dimension to mask. If dim=1, we apply time masking.
      If dim=2, we apply frequency masking.
  
  Example
  -------
  >>> # time-masking
  >>> drop = SpectrogramDrop(dim=1)
  >>> spectrogram = torch.rand(4, 150, 40)
  >>> print(spectrogram.shape)
  torch.Size([4, 150, 40])
  >>> out = drop(spectrogram)
  >>> print(out.shape)
  torch.Size([4, 150, 40])
  >>> # frequency-masking
  >>> drop = SpectrogramDrop(dim=2)
  >>> spectrogram = torch.rand(4, 150, 40)
  >>> print(spectrogram.shape)
  torch.Size([4, 150, 40])
  >>> out = drop(spectrogram)
  >>> print(out.shape)
  torch.Size([4, 150, 40])
docstr: Apply the DropChunk augmentation to the input spectrogram.
  
  This method randomly drops chunks of the input spectrogram to augment the data.
  
  Arguments
  ---------
  spectrogram : torch.Tensor
      Input spectrogram of shape `[batch, time, fea]`.
  
  Returns
  -------
  torch.Tensor
      Augmented spectrogram of shape `[batch, time, fea]`.
docstr_tok: ["Apply","the","DropChunk","augmentation","to","the","input","spectrogram",".","This","method","randomly","drops","chunks","of","the","input","spectrogram","to","augment","the","data",".","Arguments","--","--","--","--","-","spectrogram",":","torch.Tensor","Input","spectrogram","of","shape","`","[","batch",",","time",",","fea","]","`",".","Returns","--","--","--","-","torch.Tensor","Augmented","spectrogram","of","shape","`","[","batch",",","time",",","fea","]","`","."]
code: def forward(self, spectrogram):
      """
      Apply the DropChunk augmentation to the input spectrogram.
  
      This method randomly drops chunks of the input spectrogram to augment the data.
  
      Arguments
      ---------
      spectrogram : torch.Tensor
          Input spectrogram of shape `[batch, time, fea]`.
  
      Returns
      -------
      torch.Tensor
          Augmented spectrogram of shape `[batch, time, fea]`.
      """
  
      # Manage 4D tensors
      if spectrogram.dim() == 4:
          spectrogram = spectrogram.view(
              -1, spectrogram.shape[2], spectrogram.shape[3]
          )
  
      # Get the batch size
      batch_size, time_duration, fea_size = spectrogram.shape
  
      # Managing masking dimensions
      if self.dim == 1:
          D = time_duration
      else:
          D = fea_size
  
      # Randomly select the number of chunks to drop (same for all samples in the batch)
      n_masks = torch.randint(
          low=self.drop_count_low,
          high=self.drop_count_high + 1,
          size=(1,),
          device=spectrogram.device,
      )
  
      # Randomly sample the lengths of the chunks to drop
      mask_len = torch.randint(
          low=self.drop_length_low,
          high=self.drop_length_high,
          size=(batch_size, n_masks),
          device=spectrogram.device,
      ).unsqueeze(2)
  
      # Randomly sample the positions of the chunks to drop
      mask_pos = torch.randint(
          0,
          max(1, D, -mask_len.max()),
          (batch_size, n_masks),
          device=spectrogram.device,
      ).unsqueeze(2)
  
      # Compute the mask for the selected chunk positions
      arange = torch.arange(D, device=spectrogram.device).view(1, 1, -1)
      mask = (mask_pos <= arange) * (arange < (mask_pos + mask_len))
      mask = mask.any(dim=1)
      mask = mask.unsqueeze(2) if self.dim == 1 else mask.unsqueeze(1)
  
      # Determine the value to replace the masked chunks (zero or mean of the spectrogram)
      if self.replace == "random_selection":
          self.replace = random.choice(self.replace_opts[:-1])
  
      if self.replace == "zeros":
          spectrogram = spectrogram.masked_fill_(mask, 0.0)
      elif self.replace == "mean":
          mean = spectrogram.mean().detach()
          spectrogram = spectrogram.masked_fill_(mask, mean)
      elif self.replace == "rand":
          max_spectrogram = spectrogram.max().detach()
          min_spectrogram = spectrogram.min().detach()
          rand_spectrogram = torch.rand_like(spectrogram)
          rand_spectrogram = (
              rand_spectrogram * (max_spectrogram - min_spectrogram)
              + min_spectrogram
          )
          mask = mask.float()
          spectrogram = (
              1 - mask
          ) * spectrogram + mask * rand_spectrogram
      elif self.replace == "cutcat":
          rolled_spectrogram = torch.roll(spectrogram, shifts=1, dims=0)
          mask = mask.float()
          spectrogram = (
              1 - mask
          ) * spectrogram + mask * rolled_spectrogram
      elif self.replace == "swap":
          shift = torch.randint(
              low=1,
              high=spectrogram.shape[1],
              size=(1,),
              device=spectrogram.device,
          )
          rolled_spectrogram = torch.roll(
              spectrogram, shifts=shift.item(), dims=1
          )
          mask = mask.float()
          spectrogram = (
              1 - mask
          ) * spectrogram + mask * rolled_spectrogram
  
      return spectrogram.view(*spectrogram.shape)
  
file_path: speechbrain/speechbrain/augment/freq_domain.py
imports: import torch
  import random
label_desc: Use image node_id 2 for calling the SpectrogramDrop obj's underlying member method code with example usage: obj.forward(spectrogram) and returns: spectrogram