class_docstr: Class for sound class embedding training" 
class_docstr_tok: ["Class","for","sound","class","embedding","training","''"]
docstr: Helper function to compute the objectives
docstr_tok: ["Helper","function","to","compute","the","objectives"]
code: def compute_objectives(self, pred, batch, stage):
      """Helper function to compute the objectives"""
      predictions, xhat, hcat, z_q_x, garbage = pred
  
      batch = batch.to(self.device)
      wavs, lens = batch.sig
  
      uttid = batch.id
      classid, _ = batch.class_string_encoded
  
      X_stft_logpower, X_stft, X_stft_power = self.preprocess(wavs)
  
      Tmax = xhat.shape[1]
  
      hcat_theta, embeddings, theta_out, _ = self.classifier_forward(
          xhat * X_stft_logpower[:, :Tmax, :]
      )
  
      # if there is a separator, we need to add sigmoid to the sum
      loss_fid = 0
  
      if self.hparams.use_mask_output:
          eps = 1e-10
          target_spec = X_stft_logpower[:, : xhat.shape[1], :]
          # target_mask = target_spec > (target_spec.max() * self.hparams.mask_th)
  
          target_mask = target_spec > (
              target_spec.max(keepdim=True, dim=-1)[0].max(
                  keepdim=True, dim=-2
              )[0]
              * self.hparams.mask_th
          )
          target_mask = target_mask.float()
          rec_loss = (
              -target_mask * torch.log(xhat + eps)
              - (1 - target_mask) * torch.log(1 - xhat + eps)
          ).mean()
      else:
          rec_loss = (
              (X_stft_logpower[:, : xhat.shape[1], :] - xhat)
              .pow(2)
              .mean()
          )
      if self.hparams.use_vq:
          loss_vq = F.mse_loss(z_q_x, hcat.detach())
          loss_commit = F.mse_loss(hcat, z_q_x.detach())
      else:
          loss_vq = 0
          loss_commit = 0
      self.acc_metric.append(
          uttid, predict=predictions, target=classid, length=lens
      )
  
      self.recons_err.append(
          uttid, xhat, X_stft_logpower[:, : xhat.shape[1], :]
      )
      if self.hparams.use_mask_output:
          self.mask_ll.append(uttid, xhat, target_mask)
  
      if stage == sb.Stage.VALID or stage == sb.Stage.TEST:
          self.top_3_fidelity.append(
              [batch.id] * theta_out.shape[0], theta_out, predictions
          )
          self.faithfulness.append(batch.id, wavs, predictions)
  
      if stage != sb.Stage.TEST:
          if hasattr(self.hparams.lr_annealing, "on_batch_end"):
              self.hparams.lr_annealing.on_batch_end(self.optimizer)
  
      return (
          self.hparams.rec_loss_coef * rec_loss
          + loss_vq
          + loss_commit
          + loss_fid
      )
  
imports: import os
  import sys
  import torch
  import torchaudio
  import speechbrain
  from hyperpyyaml import load_hyperpyyaml
  from speechbrain.utils.distributed import run_on_main
  from esc50_prepare import prepare_esc50
  from speechbrain.utils.metric_stats import MetricStats
  from os import makedirs
  import torch.nn.functional
  from speechbrain.processing.NMF import spectral_phase
  import matplotlib.pyplot
label_desc: Use image node_id 9 for calling the InterpreterESC50Brain obj's underlying member method code with example usage: obj.compute_objectives(pred, batch, stage) and returns: unknown