docstr: Given the network predictions and targets compute the total loss
docstr_tok: ["Given","the","network","predictions","and","targets","compute","the","total","loss"]
code: def compute_objectives(
      self, predictions, batch, stage, optim_name=""
  ):
      "Given the network predictions and targets compute the total loss"
      predict_wav = predictions
      predict_spec = self.compute_feats(predict_wav)
  
      clean_wav, lens = batch.clean_sig
      clean_spec = self.compute_feats(clean_wav)
      clean_paths = batch.clean_wav
  
      ids = self.compute_ids(batch.id, optim_name)
  
      # One is real, zero is fake
      if optim_name == "generator":
          target_score = torch.ones(
              self.batch_size, 1, device=self.device
          )
          est_score = self.est_score(predict_spec, clean_spec)
          self.mse_metric.append(
              ids, predict_spec, clean_spec, lens, reduction="batch"
          )
          mse_cost = self.hparams.compute_cost(
              predict_spec, clean_spec, lens
          )
  
      # D Learns to estimate the scores of clean speech
      elif optim_name == "D_clean":
          target_score = torch.ones(
              self.batch_size, 1, device=self.device
          )
          est_score = self.est_score(clean_spec, clean_spec)
  
      # D Learns to estimate the scores of enhanced speech
      elif optim_name == "D_enh" and self.sub_stage == SubStage.CURRENT:
          target_score = self.score(ids, predict_wav, clean_wav, lens)
          est_score = self.est_score(predict_spec, clean_spec)
  
          # Write enhanced wavs during discriminator training, because we
          # compute the actual score here and we can save it
          self.write_wavs(
              ids, predict_wav, clean_paths, target_score, lens
          )
  
      # D Relearns to estimate the scores of previous epochs
      elif (
          optim_name == "D_enh"
          and self.sub_stage == SubStage.HISTORICAL
      ):
          target_score = batch.score.unsqueeze(1).float()
          est_score = self.est_score(predict_spec, clean_spec)
  
      # D Learns to estimate the scores of noisy speech
      elif optim_name == "D_noisy":
          noisy_wav, _ = batch.noisy_sig
          noisy_spec = self.compute_feats(noisy_wav)
          target_score = self.score(ids, noisy_wav, clean_wav, lens)
          est_score = self.est_score(noisy_spec, clean_spec)
  
          # Save scores of noisy wavs
          self.save_noisy_scores(ids, target_score)
  
      if stage == sb.Stage.TRAIN:
          # Compute the cost
          cost = self.hparams.compute_cost(est_score, target_score)
          if optim_name == "generator":
              cost += self.hparams.mse_weight * mse_cost
              self.metrics["G"].append(cost.detach())
          else:
              self.metrics["D"].append(cost.detach())
  
      # On validation data compute scores
      if stage != sb.Stage.TRAIN:
          cost = self.hparams.compute_si_snr(
              predict_wav, clean_wav, lens
          )
          # Evaluate speech quality/intelligibility
          self.stoi_metric.append(
              batch.id, predict_wav, clean_wav, lens, reduction="batch"
          )
          self.pesq_metric.append(
              batch.id,
              predict=predict_wav,
              target=clean_wav,
              lengths=lens,
          )
  
          # Write wavs to file, for evaluation
          lens = lens * clean_wav.shape[1]
          for name, pred_wav, length in zip(
              batch.id, predict_wav, lens
          ):
              name += ".wav"
              enhance_path = os.path.join(
                  self.hparams.enhanced_folder, name
              )
              torchaudio.save(
                  enhance_path,
                  torch.unsqueeze(pred_wav[: int(length)].cpu(), 0),
                  16000,
              )
  
      return cost
  
imports: import os
  import sys
  import shutil
  import pickle
  import torch
  import torchaudio
  import speechbrain
  from pesq import pesq
  from enum import Enum, auto
  from hyperpyyaml import load_hyperpyyaml
  from speechbrain.utils.metric_stats import MetricStats
  from speechbrain.processing.features import spectral_magnitude
  from speechbrain.nnet.loss.stoi_loss import stoi_loss
  from speechbrain.utils.distributed import run_on_main
  from speechbrain.dataio.sampler import ReproducibleWeightedRandomSampler
label_desc: Use image node_id 4 for calling the MetricGanBrain obj's underlying member method code with example usage: obj.compute_objectives(predictions, batch, stage, optim_name) and returns: cost