class_docstr: Class that manages the training loop. See speechbrain.core.Brain.
docstr: Computes the loss given the predicted and targeted outputs.
  
  Arguments
  ---------
  predictions : tensor
      The output tensor 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",".","Arguments","--","--","--","--","-","predictions",":","tensor","The","output","tensor","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.
  
      Arguments
      ---------
      predictions : tensor
          The output tensor 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.
      """
  
      _, lens = batch.sig
      spkid, _ = batch.spk_id_encoded
  
      if stage == sb.Stage.TRAIN and hasattr(
          self.hparams, "wav_augment"
      ):
          spkid = self.hparams.wav_augment.replicate_labels(spkid)
          lens = self.hparams.wav_augment.replicate_labels(lens)
  
      # Compute the cost function
      loss = sb.nnet.losses.nll_loss(predictions, spkid, lens)
  
      # Append this batch of losses to the loss metric for easy
      self.loss_metric.append(
          batch.id, predictions, spkid, lens, reduction="batch"
      )
  
      # Compute classification error at test time
      if stage != sb.Stage.TRAIN:
          self.error_metrics.append(batch.id, predictions, spkid, lens)
  
      return loss
  
file_path: speechbrain/templates/hyperparameter_optimization_speaker_id/train.py
imports: import os
  import sys
  import speechbrain
  from hyperpyyaml import load_hyperpyyaml
  from mini_librispeech_prepare import prepare_mini_librispeech
  from speechbrain.utils import hpopt
label_desc: Use image node_id 3 for calling the SpkIdBrain obj's underlying member method code with example usage: obj.compute_objectives(predictions, batch, stage) and returns: loss