class_docstr: Class that manages the training loop. See speechbrain.core.Brain.
  
  Arguments
  ---------
  modules : dict of str:torch.nn.Module pairs
      These modules are passed to the optimizer by default if they have
      trainable parameters, and will have ``train()``/``eval()`` called on them.
  opt_class : torch.optim class
      A torch optimizer constructor that takes only the list of
      parameters (e.g. a lambda or partial function definition). By default,
      this will be passed all modules in ``modules`` at the
      beginning of the ``fit()`` method. This behavior can be changed
      by overriding the ``configure_optimizers()`` method.
  hparams : dict
      Each key:value pair should consist of a string key and a hyperparameter
      that is used within the overridden methods. These will
      be accessible via an ``hparams`` attribute, using "dot" notation:
      e.g., self.hparams.model(x).
  run_opts : dict
      A set of options to change the runtime environment.
class_docstr_tok: ["Class","that","manages","the","training","loop",".","See","speechbrain.core.Brain",".","Arguments","--","--","--","--","-","modules",":","dict","of","str",":","torch.nn.Module","pairs","These","modules","are","passed","to","the","optimizer","by","default","if","they","have","trainable","parameters",",","and","will","have","``","train","(",")","``","\/","``","eval","(",")","``","called","on","them",".","opt_class",":","torch.optim","class","A","torch","optimizer","constructor","that","takes","only","the","list","of","parameters","(","e.g",".","a","lambda","or","partial","function","definition",")",".","By","default",",","this","will","be","passed","all","modules","in","``","modules","``","at","the","beginning","of","the","``","fit","(",")","``","method",".","This","behavior","can","be","changed","by","overriding","the","``","configure_optimizers","(",")","``","method",".","hparams",":","dict","Each","key",":","value","pair","should","consist","of","a","string","key","and","a","hyperparameter","that","is","used","within","the","overridden","methods",".","These","will","be","accessible","via","an","``","hparams","``","attribute",",","using","``","dot","''","notation",":","e.g.",",","self.hparams.model","(","x",")",".","run_opts",":","dict","A","set","of","options","to","change","the","runtime","environment","."]
docstr: Computes the loss given the predicted and targeted outputs.
  
  Arguments
  ---------
  predictions : tensor
      The output tensor from `compute_forward`.
  batch : PaddedBatch
      This batch object contains all the relevant tensors for computation.
  stage : sb.Stage
      One of sb.Stage.TRAIN, sb.Stage.VALID, or sb.Stage.TEST.
  
  Returns
  -------
  loss : torch.Tensor
      A one-element tensor used for backpropagating the gradient.
docstr_tok: ["Computes","the","loss","given","the","predicted","and","targeted","outputs",".","Arguments","--","--","--","--","-","predictions",":","tensor","The","output","tensor","from","`","compute_forward","`",".","batch",":","PaddedBatch","This","batch","object","contains","all","the","relevant","tensors","for","computation",".","stage",":","sb.Stage","One","of","sb.Stage.TRAIN",",","sb.Stage.VALID",",","or","sb.Stage.TEST",".","Returns","--","--","--","-","loss",":","torch.Tensor","A","one-element","tensor","used","for","backpropagating","the","gradient","."]
code: def compute_objectives(self, predictions, batch, stage):
      """Computes the loss given the predicted and targeted outputs.
  
      Arguments
      ---------
      predictions : tensor
          The output tensor from `compute_forward`.
      batch : PaddedBatch
          This batch object contains all the relevant tensors for computation.
      stage : sb.Stage
          One of sb.Stage.TRAIN, sb.Stage.VALID, or sb.Stage.TEST.
  
      Returns
      -------
      loss : torch.Tensor
          A one-element tensor used for backpropagating the gradient.
      """
  
      (
          preds,
          noise,
          noisy_sample,
          feats,
          lens,
          autoencoder_out,
          feats_done,
          lens_done,
          pred_done,
      ) = predictions
      if self.train_diffusion:
          # NOTE: Padding of the latent space can affect the lengths
          lens_diffusion = (
              autoencoder_out.latent_length
              if self.diffusion_mode == DiffusionMode.LATENT
              else lens
          )
          loss = self.hparams.compute_cost(
              reshape_feats(preds),
              reshape_feats(noise),
              length=lens_diffusion,
          )
      else:
          loss = torch.tensor(0.0, device=self.device)
  
      # Append this batch of losses to the loss metric for easy
      self.loss_metric.append(
          batch.file_name, preds, noise, lens, reduction="batch"
      )
  
      if self.use_done_detector:
          max_len = feats.size(2)
          lens_target = (lens * max_len).int() - 1
          loss_done = self.hparams.compute_cost_done(
              pred_done.squeeze(-1), lens_target, length=lens_done
          )
          self.done_loss_metric.append(
              batch.file_name,
              pred_done.squeeze(-1),
              lens_target,
              length=lens_done,
              reduction="batch",
          )
      else:
          loss_done = None
  
      loss_autoencoder = None
      if (
          self.diffusion_mode == DiffusionMode.LATENT
          and self.train_autoencoder
      ):
          loss_autoencoder = self.hparams.compute_cost_autoencoder(
              autoencoder_out, feats, length=lens
          )
          self.autoencoder_loss_metric.append(
              batch.file_name,
              autoencoder_out,
              feats,
              length=lens,
              reduction="batch",
          )
          loss_laplacian = self.modules.compute_cost_laplacian(
              autoencoder_out.rec, length=lens
          )
  
          self.autoencoder_laplacian_loss_stats_metric.append(
              batch.file_name,
              autoencoder_out.rec,
              length=lens,
              reduction="batch",
          )
  
          loss_autoencoder += (
              self.hparams.loss_laplacian_weight * loss_laplacian
          )
  
          max_len = autoencoder_out.rec.size(2)
          rec_mask = length_to_mask(lens * max_len, max_len).unsqueeze(
              1
          )
          rec_mask = match_shape(rec_mask, autoencoder_out.rec)
          rec_denorm = self.modules.global_norm.denormalize(
              autoencoder_out.rec
          )
          self.autoencoder_rec_dist_stats_metric.append(
              batch.file_name, rec_denorm, mask=rec_mask
          )
          max_len = autoencoder_out.latent.size(2)
          latent_mask = length_to_mask(
              lens * max_len, max_len
          ).unsqueeze(1)
          latent_mask = match_shape(latent_mask, autoencoder_out.latent)
          self.autoencoder_latent_dist_stats_metric.append(
              batch.file_name, autoencoder_out.latent, mask=latent_mask
          )
  
      return loss, loss_autoencoder, loss_done
  
imports: import logging
  import sys
  import torch
  import speechbrain
  import os
  from collections import namedtuple
  from audiomnist_prepare import prepare_audiomnist
  from hyperpyyaml import load_hyperpyyaml
  from torchaudio import functional
  from speechbrain.dataio.dataio import length_to_mask, write_audio
  from speechbrain.dataio.dataset import apply_overfit_test
  from speechbrain.utils import data_utils
  from speechbrain.utils.train_logger import plot_spectrogram
  from enum import Enum
  from speechbrain.utils.distributed import run_on_main
  from speechbrain.utils.data_utils import dict_value_combinations, dist_stats, masked_mean, masked_min, masked_max, masked_std, match_shape
label_desc: Use image node_id 16 for calling the DiffusionBrain obj's underlying member method code with example usage: obj.compute_objectives(predictions, batch, stage) and returns: loss,  loss_autoencoder,  loss_done