docstr: Computes the loss (Transducer+(CTC+NLL)) given predictions and targets.
docstr_tok: ["Computes","the","loss","(","Transducer+","(","CTC+NLL",")",")","given","predictions","and","targets","."]
code: def compute_objectives(self, predictions, batch, stage):
      """Computes the loss (Transducer+(CTC+NLL)) given predictions and targets."""
  
      ids = batch.id
      tokens, token_lens = batch.tokens
      tokens_eos, token_eos_lens = batch.tokens_eos
  
      # Train returns 4 elements vs 3 for val and test
      if len(predictions) == 4:
          p_ctc, p_ce, logits_transducer, wav_lens = predictions
      else:
          logits_transducer, wav_lens, predicted_tokens = predictions
  
      if stage == sb.Stage.TRAIN:
          if hasattr(self.hparams, "wav_augment"):
              tokens = self.hparams.wav_augment.replicate_labels(tokens)
              token_lens = self.hparams.wav_augment.replicate_labels(
                  token_lens
              )
              tokens_eos = self.hparams.wav_augment.replicate_labels(
                  tokens_eos
              )
              token_eos_lens = (
                  self.hparams.wav_augment.replicate_labels(
                      token_eos_lens
                  )
              )
          if hasattr(self.hparams, "fea_augment"):
              tokens = self.hparams.fea_augment.replicate_labels(tokens)
              token_lens = self.hparams.fea_augment.replicate_labels(
                  token_lens
              )
              tokens_eos = self.hparams.fea_augment.replicate_labels(
                  tokens_eos
              )
              token_eos_lens = (
                  self.hparams.fea_augment.replicate_labels(
                      token_eos_lens
                  )
              )
  
      if stage == sb.Stage.TRAIN:
          CTC_loss = 0.0
          CE_loss = 0.0
          if p_ctc is not None:
              CTC_loss = self.hparams.ctc_cost(
                  p_ctc, tokens, wav_lens, token_lens
              )
          if p_ce is not None:
              CE_loss = self.hparams.ce_cost(
                  p_ce, tokens_eos, length=token_eos_lens
              )
          loss_transducer = self.hparams.transducer_cost(
              logits_transducer, tokens, wav_lens, token_lens
          )
          loss = (
              self.hparams.ctc_weight * CTC_loss
              + self.hparams.ce_weight * CE_loss
              + (1 - (self.hparams.ctc_weight + self.hparams.ce_weight))
              * loss_transducer
          )
      else:
          loss = self.hparams.transducer_cost(
              logits_transducer, tokens, wav_lens, token_lens
          )
  
      if stage != sb.Stage.TRAIN:
          # Decode token terms to words
          predicted_words = [
              self.tokenizer.decode_ids(utt_seq).split(" ")
              for utt_seq in predicted_tokens
          ]
          target_words = [wrd.split(" ") for wrd in batch.wrd]
          self.wer_metric.append(ids, predicted_words, target_words)
          self.cer_metric.append(ids, predicted_words, target_words)
  
      return loss
  
file_path: speechbrain/recipes/LibriSpeech/ASR/transducer/train.py
imports: import sys
  import torch
  import logging
  import speechbrain
  from speechbrain.utils.distributed import run_on_main, if_main_process
  from hyperpyyaml import load_hyperpyyaml
  from pathlib import Path
label_desc: Use image node_id 2 for calling the ASR obj's underlying member method code with example usage: obj.compute_objectives(predictions, batch, stage) and returns: loss