docstr: Gets called at the end of a stage (TRAIN, VALID, Or TEST)
docstr_tok: ["Gets","called","at","the","end","of","a","stage","(","TRAIN",",","VALID",",","Or","TEST",")"]
code: def on_stage_end(self, stage, stage_loss, epoch):
      """Gets called at the end of a stage (TRAIN, VALID, Or TEST)"""
      if stage == sb.Stage.VALID:
          # Update learning rate
          self.scheduler_g.step()
          self.scheduler_d.step()
          lr_g = self.optimizer_g.param_groups[-1]["lr"]
          lr_d = self.optimizer_d.param_groups[-1]["lr"]
  
          self.hparams.train_logger.log_stats(  # 1#2#
              stats_meta={"Epoch": epoch, "lr_g": lr_g, "lr_d": lr_d},
              train_stats=self.last_loss_stats[sb.Stage.TRAIN],
              valid_stats=self.last_loss_stats[sb.Stage.VALID],
          )
          # The tensorboard_logger writes a summary to stdout and to the logfile.
          if self.hparams.use_tensorboard:
              self.tensorboard_logger.log_stats(
                  stats_meta={
                      "Epoch": epoch,
                      "lr_g": lr_g,
                      "lr_d": lr_d,
                  },
                  train_stats=self.last_loss_stats[sb.Stage.TRAIN],
                  valid_stats=self.last_loss_stats[sb.Stage.VALID],
              )
  
          # Save the current checkpoint and delete previous checkpoints.
          epoch_metadata = {
              **{"epoch": epoch},
              **self.last_loss_stats[sb.Stage.VALID],
          }
          self.checkpointer.save_and_keep_only(
              meta=epoch_metadata,
              end_of_epoch=True,
              min_keys=["loss"],
              ckpt_predicate=(
                  lambda ckpt: (
                      ckpt.meta["epoch"]
                      % self.hparams.keep_checkpoint_interval
                      != 0
                  )
              )
              if self.hparams.keep_checkpoint_interval is not None
              else None,
          )
  
          self.run_inference_sample("Valid")
  
      # We also write statistics about test data to stdout and to the TensorboardLogger.
      if stage == sb.Stage.TEST:
          self.hparams.train_logger.log_stats(  # 1#2#
              {"Epoch loaded": self.hparams.epoch_counter.current},
              test_stats=self.last_loss_stats[sb.Stage.TEST],
          )
          if self.hparams.use_tensorboard:
              self.tensorboard_logger.log_stats(
                  {"Epoch loaded": self.hparams.epoch_counter.current},
                  test_stats=self.last_loss_stats[sb.Stage.TEST],
              )
          self.run_inference_sample("Test")
  
file_path: speechbrain/recipes/LibriTTS/vocoder/hifigan/train.py
imports: import sys
  import torch
  from hyperpyyaml import load_hyperpyyaml
  import speechbrain
  from speechbrain.utils.data_utils import scalarize
  import torchaudio
  import os
label_desc: Use image node_id 8 for calling the HifiGanBrain obj's underlying member method code with example usage: obj.on_stage_end(stage, stage_loss, epoch) without return types