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
      current_epoch = self.hparams.epoch_counter.current
      tokens, token_lens = batch.tokens
      tokens_eos, token_eos_lens = batch.tokens_eos
  
      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:
          if len(predictions) == 4:
              p_ctc, p_ce, logits_transducer, wav_lens = predictions
              CTC_loss = self.hparams.ctc_cost(
                  p_ctc, tokens, wav_lens, token_lens
              )
              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
              )
          elif len(predictions) == 3:
              # one of the 2 heads (CTC or CE) is still computed
              # CTC alive
              if current_epoch <= self.hparams.number_of_ctc_epochs:
                  p_ctc, p_transducer, wav_lens = predictions
                  CTC_loss = self.hparams.ctc_cost(
                      p_ctc, tokens, wav_lens, token_lens
                  )
                  loss_transducer = self.hparams.transducer_cost(
                      logits_transducer, tokens, wav_lens, token_lens
                  )
                  loss = (
                      self.hparams.ctc_weight * CTC_loss
                      + (1 - self.hparams.ctc_weight) * loss_transducer
                  )
              # CE for decoder alive
              else:
                  p_ce, logits_transducer, wav_lens = predictions
                  CE_loss = self.hparams.ce_cost(
                      p_ce, tokens_eos, length=token_eos_lens
                  )
                  # Transducer loss use logits from RNN-T model.
                  loss_transducer = self.hparams.transducer_cost(
                      logits_transducer, tokens, wav_lens, token_lens
                  )
                  loss = (
                      self.hparams.ce_weight * CE_loss
                      + (1 - self.hparams.ctc_weight) * loss_transducer
                  )
          else:
              logits_transducer, wav_lens = predictions
              # Transducer loss use logits from RNN-T model.
              loss = self.hparams.transducer_cost(
                  logits_transducer, tokens, wav_lens, token_lens
              )
      else:
          logits_transducer, wav_lens, predicted_tokens = predictions
          # Transducer loss use logits from RNN-T model.
          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(
              predicted_tokens, task="decode_from_list"
          )
  
          # Convert indices to words
          target_words = undo_padding(tokens, token_lens)
          target_words = self.tokenizer(
              target_words, task="decode_from_list"
          )
  
          self.wer_metric.append(ids, predicted_words, target_words)
          self.cer_metric.append(ids, predicted_words, target_words)
  
      return loss
  
file_path: speechbrain/recipes/CommonVoice/ASR/transducer/train.py
imports: import sys
  import torch
  import logging
  import speechbrain
  import torchaudio
  from speechbrain.utils.distributed import run_on_main, if_main_process
  from speechbrain.utils.data_utils import undo_padding
  from speechbrain.tokenizers.SentencePiece import SentencePiece
  from hyperpyyaml import load_hyperpyyaml
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