class_docstr: Class that manages the training loop. See speechbrain.core.Brain.
class_docstr_tok: ["Class","that","manages","the","training","loop",".","See","speechbrain.core.Brain","."]
docstr: Computes the loss given the predicted and targeted outputs. We here
  do multi-task learning and the loss is a weighted sum of the ctc + seq2seq
  costs.
  
  Arguments
  ---------
  predictions : dict
      The output dict from `compute_forward`.
  batch : PaddedBatch
      This batch object contains all the relevant tensors for computation.
  stage : sb.Stage
      One of sb.Stage.TRAIN, sb.Stage.VALID, or sb.Stage.TEST.
  
  Returns
  -------
  loss : torch.Tensor
      A one-element tensor used for backpropagating the gradient.
docstr_tok: ["Computes","the","loss","given","the","predicted","and","targeted","outputs",".","We","here","do","multi-task","learning","and","the","loss","is","a","weighted","sum","of","the","ctc","+","seq2seq","costs",".","Arguments","--","--","--","--","-","predictions",":","dict","The","output","dict","from","`","compute_forward","`",".","batch",":","PaddedBatch","This","batch","object","contains","all","the","relevant","tensors","for","computation",".","stage",":","sb.Stage","One","of","sb.Stage.TRAIN",",","sb.Stage.VALID",",","or","sb.Stage.TEST",".","Returns","--","--","--","-","loss",":","torch.Tensor","A","one-element","tensor","used","for","backpropagating","the","gradient","."]
code: def compute_objectives(self, predictions, batch, stage):
      """Computes the loss given the predicted and targeted outputs. We here
      do multi-task learning and the loss is a weighted sum of the ctc + seq2seq
      costs.
  
      Arguments
      ---------
      predictions : dict
          The output dict from `compute_forward`.
      batch : PaddedBatch
          This batch object contains all the relevant tensors for computation.
      stage : sb.Stage
          One of sb.Stage.TRAIN, sb.Stage.VALID, or sb.Stage.TEST.
  
      Returns
      -------
      loss : torch.Tensor
          A one-element tensor used for backpropagating the gradient.
      """
      # Compute sequence loss against targets with EOS
      tokens_eos, tokens_eos_lens = self.prepare_tokens(
          stage, batch.tokens_eos
      )
      loss = sb.nnet.losses.nll_loss(
          log_probabilities=predictions["seq_logprobs"],
          targets=tokens_eos,
          length=tokens_eos_lens,
          label_smoothing=self.hparams.label_smoothing,
      )
  
      # Add ctc loss if necessary. The total cost is a weighted sum of
      # ctc loss + seq2seq loss
      if self.is_ctc_active(stage):
          # Load tokens without EOS as CTC targets
          tokens, tokens_lens = self.prepare_tokens(stage, batch.tokens)
          loss_ctc = self.hparams.ctc_cost(
              predictions["ctc_logprobs"],
              tokens,
              self.feat_lens,
              tokens_lens,
          )
          loss *= 1 - self.hparams.ctc_weight
          loss += self.hparams.ctc_weight * loss_ctc
  
      if stage != sb.Stage.TRAIN:
          # Converted predicted tokens from indexes to words
          predicted_words = [
              self.hparams.tokenizer.decode_ids(prediction).split(" ")
              for prediction in predictions["tokens"]
          ]
          target_words = [words.split(" ") for words in batch.words]
  
          # Monitor word error rate and character error rated at
          # valid and test time.
          self.wer_metric.append(
              batch.id, predicted_words, target_words
          )
          self.cer_metric.append(
              batch.id, predicted_words, target_words
          )
  
      return loss
  
file_path: orion/examples/speechbrain_tutorial/train.py
imports: import logging
  import sys
  import speechbrain
  import torch
  from hyperpyyaml import load_hyperpyyaml
  from mini_librispeech_prepare import prepare_mini_librispeech
  from speechbrain.utils.distributed import run_on_main
label_desc: Use image node_id 5 for calling the ASR obj's underlying member method code with example usage: obj.compute_objectives(predictions, batch, stage) and returns: loss