class_docstr: The Tacotron loss implementation
  The loss consists of an MSE loss on the spectrogram, a BCE gate loss
  and a guided attention loss (if enabled) that attempts to make the
  attention matrix diagonal
  The output of the moduel is a LossStats tuple, which includes both the
  total loss
  Arguments
  ---------
  guided_attention_sigma: float
      The guided attention sigma factor, controling the "width" of
      the mask
  gate_loss_weight: float
      The constant by which the gate loss will be multiplied
  mel_loss_weight: float
      The constant by which the mel loss will be multiplied
  spk_emb_loss_weight: float
      The constant by which the speaker embedding loss will be multiplied - placeholder for future work
  spk_emb_loss_type: str
      Type of the speaker embedding loss - placeholder for future work
  guided_attention_weight: float
      The weight for the guided attention
  guided_attention_scheduler: callable
      The scheduler class for the guided attention loss
  guided_attention_hard_stop: int
      The number of epochs after which guided attention will be compeltely
      turned off
  Example:
  >>> import torch
  >>> _ = torch.manual_seed(42)
  >>> from speechbrain.lobes.models.MSTacotron2 import Loss
  >>> loss = Loss(guided_attention_sigma=0.2)
  >>> mel_target = torch.randn(2, 80, 861)
  >>> gate_target = torch.randn(1722, 1)
  >>> mel_out = torch.randn(2, 80, 861)
  >>> mel_out_postnet = torch.randn(2, 80, 861)
  >>> gate_out = torch.randn(2, 861)
  >>> alignments = torch.randn(2, 861, 173)
  >>> pred_mel_lens = torch.randn(2)
  >>> targets = mel_target, gate_target
  >>> model_outputs = mel_out, mel_out_postnet, gate_out, alignments, pred_mel_lens
  >>> input_lengths = torch.tensor([173,  91])
  >>> target_lengths = torch.tensor([861, 438])
  >>> spk_embs = None
  >>> loss(model_outputs, targets, input_lengths, target_lengths, spk_embs, 1)
  TacotronLoss(loss=tensor([4.8566]), mel_loss=tensor(4.0097), spk_emb_loss=tensor([0.]), gate_loss=tensor(0.8460), attn_loss=tensor(0.0010), attn_weight=tensor(1.))
docstr: Computes the loss
  Arguments
  ---------
  model_output: tuple
      the output of the model's forward():
      (mel_outputs, mel_outputs_postnet, gate_outputs, alignments)
  targets: tuple
      the targets
  input_lengths: torch.Tensor
      a (batch, length) tensor of input lengths
  target_lengths: torch.Tensor
      a (batch, length) tensor of target (spectrogram) lengths
  spk_embs: torch.Tensor
      Speaker embedding input for the loss computation - placeholder for future work
  epoch: int
      the current epoch number (used for the scheduling of the guided attention
      loss) A StepScheduler is typically used
  Returns
  -------
  result: LossStats
      the total loss - and individual losses (mel and gate)
docstr_tok: ["Computes","the","loss","Arguments","--","--","--","--","-","model_output",":","tuple","the","output","of","the","model","'s","forward","(",")",":","(","mel_outputs",",","mel_outputs_postnet",",","gate_outputs",",","alignments",")","targets",":","tuple","the","targets","input_lengths",":","torch.Tensor","a","(","batch",",","length",")","tensor","of","input","lengths","target_lengths",":","torch.Tensor","a","(","batch",",","length",")","tensor","of","target","(","spectrogram",")","lengths","spk_embs",":","torch.Tensor","Speaker","embedding","input","for","the","loss","computation","-","placeholder","for","future","work","epoch",":","int","the","current","epoch","number","(","used","for","the","scheduling","of","the","guided","attention","loss",")","A","StepScheduler","is","typically","used","Returns","--","--","--","-","result",":","LossStats","the","total","loss","-","and","individual","losses","(","mel","and","gate",")"]
code: def forward(
      self,
      model_output,
      targets,
      input_lengths,
      target_lengths,
      spk_embs,
      epoch,
  ):
      """Computes the loss
      Arguments
      ---------
      model_output: tuple
          the output of the model's forward():
          (mel_outputs, mel_outputs_postnet, gate_outputs, alignments)
      targets: tuple
          the targets
      input_lengths: torch.Tensor
          a (batch, length) tensor of input lengths
      target_lengths: torch.Tensor
          a (batch, length) tensor of target (spectrogram) lengths
      spk_embs: torch.Tensor
          Speaker embedding input for the loss computation - placeholder for future work
      epoch: int
          the current epoch number (used for the scheduling of the guided attention
          loss) A StepScheduler is typically used
      Returns
      -------
      result: LossStats
          the total loss - and individual losses (mel and gate)
      """
      mel_target, gate_target = targets[0], targets[1]
      mel_target.requires_grad = False
      gate_target.requires_grad = False
      gate_target = gate_target.view(-1, 1)
  
      (
          mel_out,
          mel_out_postnet,
          gate_out,
          alignments,
          pred_mel_lens,
      ) = model_output
  
      gate_out = gate_out.view(-1, 1)
      mel_loss = self.mse_loss(mel_out, mel_target) + self.mse_loss(
          mel_out_postnet, mel_target
      )
  
      mel_loss = self.mel_loss_weight * mel_loss
  
      gate_loss = self.gate_loss_weight * self.bce_loss(
          gate_out, gate_target
      )
      attn_loss, attn_weight = self.get_attention_loss(
          alignments, input_lengths, target_lengths, epoch
      )
  
      # Speaker embedding loss placeholder - for future work
      spk_emb_loss = torch.Tensor([0]).to(mel_loss.device)
  
      if self.spk_emb_loss_type == "scl_loss":
          target_spk_embs, preds_spk_embs = spk_embs
  
          cos_sim_scores = self.cos_sim(preds_spk_embs, target_spk_embs)
          spk_emb_loss = -torch.div(
              torch.sum(cos_sim_scores), len(cos_sim_scores)
          )
  
      if self.spk_emb_loss_type == "cos_emb_loss":
          target_spk_embs, preds_spk_embs = spk_embs
          spk_emb_loss = self.cos_emb_loss(
              target_spk_embs,
              preds_spk_embs,
              torch.ones(len(target_spk_embs)).to(
                  target_spk_embs.device
              ),
          )
  
      if self.spk_emb_loss_type == "triplet_loss":
          anchor_spk_embs, pos_spk_embs, neg_spk_embs = spk_embs
          if anchor_spk_embs is not None:
              spk_emb_loss = self.triplet_loss(
                  anchor_spk_embs, pos_spk_embs, neg_spk_embs
              )
  
      spk_emb_loss = self.spk_emb_loss_weight * spk_emb_loss
  
      total_loss = mel_loss + spk_emb_loss + gate_loss + attn_loss
      return LossStats(
          total_loss,
          mel_loss,
          spk_emb_loss,
          gate_loss,
          attn_loss,
          attn_weight,
      )
  
imports: from math import sqrt
  from speechbrain.nnet.loss.guidedattn_loss import GuidedAttentionLoss
  import torch
  from torch import nn
  from torch.nn import functional
  from collections import namedtuple
  import pickle
  from speechbrain.lobes.models.Tacotron2 import LinearNorm, Postnet, Encoder, Decoder, get_mask_from_lengths
label_desc: Use image node_id 2 for calling the Loss obj's underlying member method code with example usage: obj.forward(model_output, targets, input_lengths, target_lengths, spk_embs, epoch) and returns: LossStats