docstr: Compute possibly several loss terms: enhance, mimic, ctc, seq
docstr_tok: ["Compute","possibly","several","loss","terms",":","enhance",",","mimic",",","ctc",",","seq"]
code: def compute_objectives(self, predictions, batch, stage):
      """Compute possibly several loss terms: enhance, mimic, ctc, seq"""
  
      # Do not augment targets
      clean_wavs, lens = self.prepare_wavs(batch.clean_sig, stage)
      loss = 0
  
      # Compute enhancement loss
      if self.hparams.enhance_weight > 0:
          clean_stft = self.modules.enhance_model.stft(clean_wavs)
          clean_feats = self.modules.enhance_model.extract_feats(
              clean_stft
          )
          enhance_loss = self.hparams.enhance_loss(
              predictions["feats"], clean_feats, lens
          )
          loss += self.hparams.enhance_weight * enhance_loss
  
          if stage != sb.Stage.TRAIN:
              self.enh_metrics.append(
                  batch.id, predictions["feats"], clean_feats, lens
              )
              self.stoi_metrics.append(
                  ids=batch.id,
                  predict=predictions["wavs"],
                  target=clean_wavs,
                  lengths=lens,
              )
              self.pesq_metrics.append(
                  ids=batch.id,
                  predict=predictions["wavs"],
                  target=clean_wavs,
                  lengths=lens,
              )
              if stage == sb.Stage.TEST:
                  self.composite_metrics.append(
                      ids=batch.id,
                      predict=predictions["wavs"],
                      target=clean_wavs,
                      lengths=lens,
                  )
  
              if hasattr(self.hparams, "enh_dir"):
                  abs_lens = lens * predictions["wavs"].size(1)
                  for i, uid in enumerate(batch.id):
                      length = int(abs_lens[i])
                      wav = predictions["wavs"][i, :length].unsqueeze(0)
                      path = os.path.join(
                          self.hparams.enh_dir, uid + ".wav"
                      )
                      torchaudio.save(
                          path, wav.cpu(), sample_rate=16000
                      )
  
      # Compute mimic loss
      if self.hparams.mimic_weight > 0:
          enhance_mag = predictions["feats"]
          if hasattr(self.hparams, "perceptual_fbank"):
              enhance_mag = self.hparams.perceptual_fbank(enhance_mag)
              clean_feats = self.hparams.perceptual_fbank(clean_feats)
          clean_embed = self.modules.src_embedding.CNN(clean_feats)
          enh_embed = self.modules.src_embedding.CNN(enhance_mag)
          mimic_loss = self.hparams.mimic_loss(
              enh_embed, clean_embed, lens
          )
          loss += self.hparams.mimic_weight * mimic_loss
  
          if stage != sb.Stage.TRAIN:
              self.mimic_metrics.append(
                  batch.id, enh_embed, clean_embed, lens
              )
  
      # Compute hard ASR loss
      if self.hparams.ctc_weight > 0 and (
          not hasattr(self.hparams, "ctc_epochs")
          or self.hparams.epoch_counter.current
          < self.hparams.ctc_epochs
      ):
          tokens, token_lens = self.prepare_targets(batch.tokens, stage)
          ctc_loss = sb.nnet.losses.ctc_loss(
              predictions["ctc_pout"],
              tokens,
              lens,
              token_lens,
              self.hparams.blank_index,
          )
          loss += self.hparams.ctc_weight * ctc_loss
  
          if stage != sb.Stage.TRAIN and self.hparams.seq_weight == 0:
              predict = sb.decoders.ctc_greedy_decode(
                  predictions["ctc_pout"],
                  lens,
                  blank_id=self.hparams.blank_index,
              )
              self.err_rate_metrics.append(
                  ids=batch.id,
                  predict=predict,
                  target=tokens,
                  target_len=token_lens,
                  ind2lab=self.token_encoder.decode_ndim,
              )
  
      # Compute nll loss for seq2seq model
      if self.hparams.seq_weight > 0:
          tokens, token_lens = self.prepare_targets(
              batch.tokens_eos, stage
          )
          seq_loss = self.hparams.seq_loss(
              predictions["seq_pout"], tokens, token_lens
          )
          loss += self.hparams.seq_weight * seq_loss
  
          if stage != sb.Stage.TRAIN:
              if hasattr(self.hparams, "tokenizer"):
                  pred_words = [
                      self.token_encoder.decode_ids(token_seq)
                      for token_seq in predictions["hyps"]
                  ]
                  target_words = [
                      self.token_encoder.decode_ids(token_seq)
                      for token_seq in undo_padding(*batch.tokens)
                  ]
                  self.err_rate_metrics.append(
                      batch.id, pred_words, target_words
                  )
  
              else:
                  self.err_rate_metrics.append(
                      ids=batch.id,
                      predict=predictions["hyps"],
                      target=tokens,
                      target_len=token_lens,
                      ind2lab=self.token_encoder.decode_ndim,
                  )
  
      return loss
  
imports: import os
  import sys
  import torch
  import torchaudio
  import logging
  import speechbrain
  from pesq import pesq
  from pystoi import stoi
  from composite_eval import eval_composite
  from hyperpyyaml import load_hyperpyyaml
  from speechbrain.utils.data_utils import undo_padding
  from speechbrain.utils.distributed import run_on_main, if_main_process
label_desc: Use image node_id 5 for calling the MTLbrain obj's underlying member method code with example usage: obj.compute_objectives(predictions, batch, stage) and returns: loss