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."""
  
      current_epoch = self.hparams.epoch_counter.current
      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
  
      if stage == sb.Stage.TRAIN and 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
          )
  
      loss_seq = self.hparams.seq_cost(
          p_seq, tokens_eos, length=tokens_eos_lens
      )
  
      # Add ctc loss if necessary
      if (
          stage == sb.Stage.TRAIN
          and current_epoch <= self.hparams.number_of_ctc_epochs
      ):
          loss_ctc = self.hparams.ctc_cost(
              p_ctc, tokens, wav_lens, tokens_lens
          )
          loss = self.hparams.ctc_weight * loss_ctc
          loss += (1 - self.hparams.ctc_weight) * loss_seq
      else:
          loss = loss_seq
  
      if stage != sb.Stage.TRAIN:
          # Decode token terms to words
          predicted_words = [
              tokenizer.decode_ids(utt_seq).split(" ")
              for utt_seq in predicted_tokens
          ]
          target_words = [wrd.split(" ") for wrd in batch.wrd]
          if self.hparams.remove_spaces:
              predicted_words = ["".join(p) for p in predicted_words]
              target_words = ["".join(t) for t in target_words]
  
      if stage != sb.Stage.TRAIN:
          self.cer_metric.append(ids, predicted_words, target_words)
  
      return loss
  
file_path: speechbrain/recipes/AISHELL-1/ASR/seq2seq/train.py
imports: import sys
  import torch
  import logging
  import speechbrain
  from speechbrain.utils.distributed import run_on_main
  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