class_docstr: Class for classifier training"
      
class_docstr_tok: ["Class","for","classifier","training","''"]
docstr: Computes the loss using class-id as label.
          
docstr_tok: ["Computes","the","loss","using","class-id","as","label","."]
code: def compute_objectives(self, predictions, batch, stage):
      """Computes the loss using class-id as label."""
      predictions, lens = predictions
      uttid = batch.id
      classid, _ = batch.class_string_encoded
  
      loss = self.hparams.compute_cost(predictions, classid, lens)
  
      if stage != sb.Stage.TEST:
          if hasattr(self.hparams.lr_annealing, "on_batch_end"):
              self.hparams.lr_annealing.on_batch_end(self.optimizer)
  
      # Append this batch of losses to the loss metric for easy
      self.loss_metric.append(
          uttid, predictions, classid, lens, reduction="batch"
      )
  
      # Confusion matrices
      if stage != sb.Stage.TRAIN:
          y_true = classid.cpu().detach().numpy().squeeze(-1)
          y_pred = (
              predictions.cpu().detach().numpy().argmax(-1).squeeze(-1)
          )
  
      if stage == sb.Stage.VALID:
          confusion_matix = confusion_matrix(
              y_true,
              y_pred,
              labels=sorted(self.hparams.label_encoder.ind2lab.keys()),
          )
          self.valid_confusion_matrix += confusion_matix
      if stage == sb.Stage.TEST:
          confusion_matix = confusion_matrix(
              y_true,
              y_pred,
              labels=sorted(self.hparams.label_encoder.ind2lab.keys()),
          )
          self.test_confusion_matrix += confusion_matix
  
      # Compute Accuracy using MetricStats
      self.acc_metric.append(
          uttid, predict=predictions, target=classid, lengths=lens
      )
  
      if stage != sb.Stage.TRAIN:
          self.error_metrics.append(uttid, predictions, classid, lens)
  
      return loss
  
file_path: speechbrain/recipes/ESC50/classification/train_classifier.py
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 sklearn.metrics import confusion_matrix
  import numpy
  from confusion_matrix_fig import create_cm_fig
label_desc: Use image node_id 2 for calling the ESC50Brain obj's underlying member method code with example usage: obj.compute_objectives(predictions, batch, stage) and returns: loss