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: Gets called at the beginning of each epoch.
  
  Arguments
  ---------
  stage : sb.Stage
      One of sb.Stage.TRAIN, sb.Stage.VALID, or sb.Stage.TEST.
  epoch : int
      The currently-starting epoch. This is passed
      `None` during the test stage.
docstr_tok: ["Gets","called","at","the","beginning","of","each","epoch",".","Arguments","--","--","--","--","-","stage",":","sb.Stage","One","of","sb.Stage.TRAIN",",","sb.Stage.VALID",",","or","sb.Stage.TEST",".","epoch",":","int","The","currently-starting","epoch",".","This","is","passed","`","None","`","during","the","test","stage","."]
code: def on_stage_start(self, stage, epoch=None):
      """Gets called at the beginning of each epoch.
  
      Arguments
      ---------
      stage : sb.Stage
          One of sb.Stage.TRAIN, sb.Stage.VALID, or sb.Stage.TEST.
      epoch : int
          The currently-starting epoch. This is passed
          `None` during the test stage.
      """
  
      self.train_diffusion = (epoch is None) or (
          epoch >= self.hparams.train_diffusion_start_epoch
      )
      self.train_autoencoder = (
          (epoch is not None)
          and (self.diffusion_mode == DiffusionMode.LATENT)
          and (epoch <= self.hparams.train_autoencoder_stop_epoch)
      )
  
      # Set up statistics trackers for this stage
      self.loss_metric = sb.utils.metric_stats.MetricStats(
          metric=self.hparams.compute_cost
      )
      if self.use_done_detector:
          self.done_loss_metric = sb.utils.metric_stats.MetricStats(
              metric=self.hparams.compute_cost_done
          )
  
      self.mask_value_norm = self.modules.min_level_norm(
          torch.tensor(self.hparams.pad_level_db, device=self.device)
      )
  
      if self.hparams.enable_train_metrics:
          self.data_dist_stats_metric = (
              sb.utils.metric_stats.MultiMetricStats(
                  metric=dist_stats, batch_eval=True
              )
          )
  
      if self.diffusion_mode == DiffusionMode.LATENT:
          self.autoencoder_loss_metric = (
              sb.utils.metric_stats.MultiMetricStats(
                  metric=self.hparams.compute_cost_autoencoder.details,
                  batch_eval=True,
              )
          )
          self.autoencoder_rec_dist_stats_metric = (
              sb.utils.metric_stats.MultiMetricStats(
                  metric=dist_stats, batch_eval=True
              )
          )
          self.autoencoder_latent_dist_stats_metric = (
              sb.utils.metric_stats.MultiMetricStats(
                  metric=dist_stats, batch_eval=True
              )
          )
          self.autoencoder_laplacian_loss_stats_metric = (
              sb.utils.metric_stats.MetricStats(
                  metric=self.hparams.compute_cost_laplacian,
                  batch_eval=True,
              )
          )
  
      self.sample_mean_metric = sb.utils.metric_stats.MetricStats(
          metric=masked_mean
      )
      self.sample_std_metric = sb.utils.metric_stats.MetricStats(
          metric=masked_std
      )
      self.sample_min_metric = sb.utils.metric_stats.MetricStats(
          metric=masked_min
      )
      self.sample_max_metric = sb.utils.metric_stats.MetricStats(
          metric=masked_max
      )
      self.sample_metrics = [
          self.sample_mean_metric,
          self.sample_std_metric,
          self.sample_min_metric,
          self.sample_max_metric,
      ]
      if stage == sb.Stage.TRAIN:
          self.modules.global_norm.unfreeze()
      else:
          self.modules.global_norm.freeze()
  
      if (
          self.hparams.enable_reference_samples
          or stage != sb.Stage.TRAIN
      ) and not hasattr(self, "vocoder"):
          self.vocoder = self.hparams.vocoder()
      if not hasattr(self, "reference_batch"):
          self.reference_batch = None
      self.reference_samples_neeed = False
      self.is_conditioned = hasattr(
          self.hparams, "use_cond_emb"
      ) and any(self.hparams.use_cond_emb.values())
      self.latent_mask_value = None
      self.use_done_detector = "done_detector" in self.modules
  
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 35 for calling the DiffusionBrain obj's underlying member method code with example usage: obj.on_stage_start(stage, epoch) without return types