docstr: Computes the loss (CTC+NLL) given predictions and targets.
docstr_tok: ["Computes","the","loss","(","CTC+NLL",")","given","predictions","and","targets","."]
code: def compute_objectives(self, predictions, batch, stage):
      """Computes the loss (CTC+NLL) given predictions and targets."""
  
      (
          p_ctc,
          p_seq,
          wav_lens,
          predicted_tokens,
      ) = predictions
  
      ids = batch.id
      tokens_eos, tokens_eos_lens = batch.tokens_eos
      tokens, tokens_lens = batch.tokens
  
      # Augment Labels
      if stage == sb.Stage.TRAIN:
          if hasattr(self.hparams, "wav_augment"):
              tokens = self.hparams.wav_augment.replicate_labels(tokens)
              tokens_lens = self.hparams.wav_augment.replicate_labels(
                  tokens_lens
              )
              tokens_eos = self.hparams.wav_augment.replicate_labels(
                  tokens_eos
              )
              tokens_eos_lens = (
                  self.hparams.wav_augment.replicate_labels(
                      tokens_eos_lens
                  )
              )
          if hasattr(self.hparams, "fea_augment"):
              tokens = self.hparams.fea_augment.replicate_labels(tokens)
              tokens_lens = self.hparams.fea_augment.replicate_labels(
                  tokens_lens
              )
              tokens_eos = self.hparams.fea_augment.replicate_labels(
                  tokens_eos
              )
              tokens_eos_lens = (
                  self.hparams.fea_augment.replicate_labels(
                      tokens_eos_lens
                  )
              )
  
      loss_seq = self.hparams.seq_cost(
          p_seq, tokens_eos, length=tokens_eos_lens
      )
      loss_ctc = self.hparams.ctc_cost(
          p_ctc, tokens, wav_lens, tokens_lens
      )
      loss = (
          self.hparams.ctc_weight * loss_ctc
          + (1 - self.hparams.ctc_weight) * loss_seq
      )
  
      if stage != sb.Stage.TRAIN:
          current_epoch = self.hparams.epoch_counter.current
          valid_search_interval = self.hparams.valid_search_interval
          if current_epoch % valid_search_interval == 0 or (
              stage == sb.Stage.TEST
          ):
              # Decode token terms to words
              predicted_words = self.tokenizer(
                  predicted_tokens, task="decode_from_list"
              )
  
              # Convert indices to words
              target_words = undo_padding(tokens, tokens_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)
  
          # 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/CommonVoice/ASR/transformer/train.py
imports: import sys
  import torch
  import torchaudio
  import logging
  import speechbrain
  from hyperpyyaml import load_hyperpyyaml
  from speechbrain.tokenizers.SentencePiece import SentencePiece
  from speechbrain.utils.distributed import run_on_main, if_main_process
  from speechbrain.utils.data_utils import undo_padding
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