docstr: Computes the loss given predictions and targets.
docstr_tok: ["Computes","the","loss","given","predictions","and","targets","."]
code: def compute_objectives(self, predictions, batch, stage):
      """Computes the loss given predictions and targets."""
      (
          p_ctc,
          p_seq,
          asr_p_seq,
          mt_p_seq,
          wav_lens,
          hyps,
      ) = predictions
  
      ids = batch.id
  
      tokens_eos, tokens_eos_lens = batch.tokens_eos
      (
          transcription_eos,
          transcription_eos_lens,
      ) = batch.transcription_eos
      (
          transcription_tokens,
          transcription_lens,
      ) = batch.transcription_tokens
  
      # loss for different tasks
      # asr loss = ctc_weight * ctc loss + (1 - ctc_weight) * asr attention loss
      # mt loss = mt attention loss
      # st loss =
      # (1 - asr_weight - mt_weight) * st attention loss +
      # asr_weight * asr loss +
      # mt_weight * mt loss
      attention_loss = 0
      asr_ctc_loss = 0
      asr_attention_loss = 0
      mt_loss = 0
  
      # st attention loss
      attention_loss = self.hparams.seq_cost(
          p_seq,
          tokens_eos,
          length=tokens_eos_lens,
      )
  
      # asr attention loss
      if self.hparams.ctc_weight < 1 and self.hparams.asr_weight > 0:
          asr_attention_loss = self.hparams.seq_cost(
              asr_p_seq,
              transcription_eos,
              length=transcription_eos_lens,
          )
  
      # asr ctc loss
      if self.hparams.ctc_weight > 0 and self.hparams.asr_weight > 0:
          asr_ctc_loss = self.hparams.ctc_cost(
              p_ctc,
              transcription_tokens,
              wav_lens,
              transcription_lens,
          )
  
      # mt attention loss
      if self.hparams.mt_weight > 0:
          mt_loss = self.hparams.seq_cost(
              mt_p_seq,
              tokens_eos,
              length=tokens_eos_lens,
          )
  
      asr_loss = (self.hparams.ctc_weight * asr_ctc_loss) + (
          1 - self.hparams.ctc_weight
      ) * asr_attention_loss
      loss = (
          (1 - self.hparams.asr_weight - self.hparams.mt_weight)
          * attention_loss
          + self.hparams.asr_weight * asr_loss
          + self.hparams.mt_weight * mt_loss
      )
  
      if stage != sb.Stage.TRAIN:
          current_epoch = self.hparams.epoch_counter.current
          valid_search_interval = self.hparams.valid_search_interval
  
          if stage == sb.Stage.TEST:
              # 4 references bleu score
              predictions = [
                  en_detoeknizer.detokenize(
                      hparams["tokenizer"]
                      .decode_ids(utt_seq)
                      .split(" ")
                  )
                  for utt_seq in hyps
              ]
  
              four_references = [
                  batch.translation_0,
                  batch.translation_1,
                  batch.translation_2,
                  batch.translation_3,
              ]
  
              targets = []
              for reference in four_references:
                  detokenized_translation = [
                      en_detoeknizer.detokenize(translation.split(" "))
                      for translation in reference
                  ]
                  targets.append(detokenized_translation)
  
              self.bleu_metric.append(ids, predictions, targets)
          elif (
              current_epoch % valid_search_interval == 0
              and stage == sb.Stage.VALID
          ):
              predictions = [
                  en_detoeknizer.detokenize(
                      hparams["tokenizer"]
                      .decode_ids(utt_seq)
                      .split(" ")
                  )
                  for utt_seq in hyps
              ]
  
              targets = [
                  en_detoeknizer.detokenize(translation.split(" "))
                  for translation in batch.translation_0
              ]
              self.bleu_metric.append(ids, predictions, [targets])
  
          # compute the accuracy of the one-step-forward prediction
          self.acc_metric.append(p_seq, tokens_eos, tokens_eos_lens)
  
      return loss
  
file_path: speechbrain/recipes/Fisher-Callhome-Spanish/ST/transformer/train.py
imports: import sys
  import torch
  import logging
  import speechbrain
  from sacremoses import MosesDetokenizer
  from hyperpyyaml import load_hyperpyyaml
  from speechbrain.utils.distributed import run_on_main
label_desc: Use image node_id 2 for calling the ST obj's underlying member method code with example usage: obj.compute_objectives(predictions, batch, stage) and returns: loss