class_docstr: Class for sound class embedding training" 
class_docstr_tok: ["Class","for","sound","class","embedding","training","''"]
docstr: Steps taken before stage start
docstr_tok: ["Steps","taken","before","stage","start"]
code: def on_stage_start(self, stage, epoch=None):
      """Steps taken before stage start"""
  
      @torch.no_grad()
      def accuracy_value(predict, target, length):
          """Computes Accuracy"""
          # predict = predict.argmax(1, keepdim=True)
          nbr_correct, nbr_total = sb.utils.Accuracy.Accuracy(
              predict.unsqueeze(1), target, length
          )
          acc = torch.tensor([nbr_correct / nbr_total])
          return acc
  
      @torch.no_grad()
      def compute_fidelity(theta_out, predictions):
          """Computes top-`k` fidelity of interpreter."""
          predictions = F.softmax(predictions, dim=1)
          theta_out = F.softmax(theta_out, dim=1)
  
          pred_cl = torch.argmax(predictions, dim=1)
          k_top = torch.argmax(theta_out, dim=1)
  
          # 1 element for each sample in batch, is 0 if pred_cl is in top k
          temp = (k_top == pred_cl).float()
  
          return temp
  
      @torch.no_grad()
      def compute_faithfulness(wavs, predictions):
          """computes the faithfulness metric"""
          X_stft_logpower, X_stft, X_stft_power = self.preprocess(wavs)
          X2 = self.interpret_computation_steps(wavs)[0]
  
          _, _, predictions_masked, _ = self.classifier_forward(X2)
  
          predictions = F.softmax(predictions, dim=1)
          predictions_masked = F.softmax(predictions_masked, dim=1)
  
          # get the prediction indices
          pred_cl = predictions.argmax(dim=1, keepdim=True)
  
          # get the corresponding output probabilities
          predictions_selected = torch.gather(
              predictions, dim=1, index=pred_cl
          )
          predictions_masked_selected = torch.gather(
              predictions_masked, dim=1, index=pred_cl
          )
  
          faithfulness = (
              predictions_selected - predictions_masked_selected
          ).squeeze(1)
  
          return faithfulness
  
      @torch.no_grad()
      def compute_rec_error(preds, specs, length=None):
          """Calculates the reconstruction error"""
          if self.hparams.use_mask_output:
              preds = specs * preds
  
          return (specs - preds).pow(2).mean((-2, -1))
  
      @torch.no_grad()
      def compute_bern_ll(xhat, target_mask, length=None):
          """Computes bernoulli likelihood"""
          eps = 1e-10
          rec_loss = (
              -target_mask * torch.log(xhat + eps)
              - (1 - target_mask) * torch.log(1 - xhat + eps)
          ).mean((-2, -1))
          return rec_loss
  
      self.top_3_fidelity = MetricStats(metric=compute_fidelity)
      self.faithfulness = MetricStats(metric=compute_faithfulness)
      self.acc_metric = sb.utils.metric_stats.MetricStats(
          metric=accuracy_value, n_jobs=1
      )
      self.recons_err = sb.utils.metric_stats.MetricStats(
          metric=compute_rec_error
      )
      if self.hparams.use_mask_output:
          self.mask_ll = sb.utils.metric_stats.MetricStats(
              metric=compute_bern_ll
          )
      return super().on_stage_start(stage, epoch)
  
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 10 for calling the InterpreterESC50Brain obj's underlying member method code with example usage: obj.on_stage_start(stage, epoch) and returns: super, acc, temp, faithfulness, unknown, rec_loss