Source code for openspeech.callbacks

# MIT License
#
# Copyright (c) 2021 Soohwan Kim and Sangchun Ha and Soyoung Cho
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, W

import os
import pytorch_lightning as pl


[docs]class CheckpointEveryNSteps(pl.Callback): """ Save a checkpoint every N steps, instead of Lightning's default that checkpoints based on validation loss. Args: save_step_frequency: how often to save in steps use_modelcheckpoint_filename: just use the ModelCheckpoint callback's default filename, don't use ours. """ def __init__( self, save_step_frequency, use_modelcheckpoint_filename=False, ) -> None: self.save_step_frequency = save_step_frequency self.use_modelcheckpoint_filename = use_modelcheckpoint_filename
[docs] def on_batch_end(self, trainer: pl.Trainer, _): """ Check if we should save a checkpoint after every train batch """ epoch = trainer.current_epoch global_step = trainer.global_step if global_step % self.save_step_frequency == 0: if self.use_modelcheckpoint_filename: filename = trainer.checkpoint_callback.filename else: filename = f"{epoch=}_{global_step=}.ckpt" ckpt_path = os.path.join(trainer.checkpoint_callback.dirpath, filename) trainer.save_checkpoint(ckpt_path)