paddlespeech.s2t.training.trainer module
- class paddlespeech.s2t.training.trainer.Trainer(config, args)[source]
Bases:
object
An experiment template in order to structure the training code and take care of saving, loading, logging, visualization stuffs. It's intended to be flexible and simple.
So it only handles output directory (create directory for the output, create a checkpoint directory, dump the config in use and create visualizer and logger) in a standard way without enforcing any input-output protocols to the model and dataloader. It leaves the main part for the user to implement their own (setup the model, criterion, optimizer, define a training step, define a validation function and customize all the text and visual logs). It does not save too much boilerplate code. The users still have to write the forward/backward/update mannually, but they are free to add non-standard behaviors if needed. We have some conventions to follow. 1. Experiment should have
model
,optimizer
,train_loader
andvalid_loader
,config
andargs
attributes. 2. The config should have atraining
field, which hasvalid_interval
,save_interval
andmax_iteration
keys. It is used as the trigger to invoke validation, checkpointing and stop of the experiment. 3. There are four methods, namelytrain_batch
,valid
,setup_model
andsetup_dataloader
that should be implemented. Feel free to add/overwrite other methods and standalone functions if you need.- Parameters:
- config: yacs.config.CfgNode
The configuration used for the experiment.
- args: argparse.Namespace
The parsed command line arguments.
- Examples
- --------
- >>> def main_sp(config, args):
- >>> exp = Trainer(config, args)
- >>> exp.setup()
- >>> exp.run()
- >>>
- >>> config = get_cfg_defaults()
- >>> parser = default_argument_parser()
- >>> args = parser.parse_args()
- >>> if args.config:
- >>> config.merge_from_file(args.config)
- >>> if args.opts:
- >>> config.merge_from_list(args.opts)
- >>> config.freeze()
- >>>
- >>> if args.ngpu > 1:
- >>> dist.spawn(main_sp, args=(config, args), nprocs=args.ngpu)
- >>> else:
- >>> main_sp(config, args)
- Attributes:
parallel
A flag indicating whether the experiment should run with multiprocessing.
- train
Methods
align
()The align.
destory
()Close visualizer to avoid hanging after training
do_train
()The training process control by epoch.
Save the configuration used for this experiment.
export
()The test.
batch_sampler seed by epoch
Reset the train loader seed and increment epoch.
restore
()Resume from latest checkpoint at checkpoints in the output directory or load a specified checkpoint.
Resume from latest checkpoint at checkpoints in the output directory or load a specified checkpoint.
run
()The routine of the experiment after setup.
Do CTC alignment
Do Model Export
run_test
()Do Test/Decode
save
([tag, infos])Save checkpoint (model parameters and optimizer states).
setup
()Setup the experiment.
Setup training dataloader and validation dataloader.
Setup model, criterion and optimizer, etc.
Create a directory used for output.
Initialize a visualizer to log the experiment.
test
()The test.
The training loop.
valid
()The validation.
after_train_batch
before_train
eval
- dump_config()[source]
Save the configuration used for this experiment.
It is saved in to
config.yaml
in the output directory at the beginning of the experiment.
- property parallel
A flag indicating whether the experiment should run with multiprocessing.
- restore()[source]
Resume from latest checkpoint at checkpoints in the output directory or load a specified checkpoint.
If
args.checkpoint_path
is not None, load the checkpoint, else resume training.
- resume_or_scratch()[source]
Resume from latest checkpoint at checkpoints in the output directory or load a specified checkpoint.
If
args.checkpoint_path
is not None, load the checkpoint, else resume training.
- run()[source]
The routine of the experiment after setup. This method is intended to be used by the user.
- save(tag=None, infos: dict = None)[source]
Save checkpoint (model parameters and optimizer states).
- Args:
tag (int or str, optional): None for step, else using tag, e.g epoch. Defaults to None. infos (dict, optional): meta data to save. Defaults to None.
- setup_dataloader()[source]
Setup training dataloader and validation dataloader. A subclass should implement this method.
- setup_model()[source]
Setup model, criterion and optimizer, etc. A subclass should implement this method.
- setup_visualizer()[source]
Initialize a visualizer to log the experiment.
The visual log is saved in the output directory.
Notes
Only the main process has a visualizer with it. Use multiple visualizers in multiprocess to write to a same log file may cause unexpected behaviors.
- property train