LightningCLI¶
- class lightning.pytorch.cli.LightningCLI(model_class=None, datamodule_class=None, save_config_callback=<class 'lightning.pytorch.cli.SaveConfigCallback'>, save_config_kwargs=None, trainer_class=<class 'lightning.pytorch.trainer.trainer.Trainer'>, trainer_defaults=None, seed_everything_default=True, parser_kwargs=None, parser_class=<class 'lightning.pytorch.cli.LightningArgumentParser'>, subclass_mode_model=False, subclass_mode_data=False, args=None, run=True, auto_configure_optimizers=True, load_from_checkpoint_support=True)[source]¶
Bases:
objectImplementation of a configurable command line tool for pytorch-lightning.
Receives as input pytorch-lightning classes (or callables which return pytorch-lightning classes), which are called / instantiated using a parsed configuration file and / or command line args.
Parsing of configuration from environment variables can be enabled by setting
parser_kwargs={"default_env": True}. A full configuration yaml would be parsed fromPL_CONFIGif set. Individual settings are so parsed from variables named for examplePL_TRAINER__MAX_EPOCHS.For more info, read the CLI docs.
- Parameters:
model_class¶ (
Union[type[LightningModule],Callable[...,LightningModule],None]) – An optionalLightningModuleclass to train on or a callable which returns aLightningModuleinstance when called. IfNone, you can pass a registered model with--model=MyModel.datamodule_class¶ (
Union[type[LightningDataModule],Callable[...,LightningDataModule],None]) – An optionalLightningDataModuleclass or a callable which returns aLightningDataModuleinstance when called. IfNone, you can pass a registered datamodule with--data=MyDataModule.save_config_callback¶ (
Optional[type[SaveConfigCallback]]) – A callback class to save the config.save_config_kwargs¶ (
Optional[dict[str,Any]]) – Parameters that will be used to instantiate the save_config_callback.trainer_class¶ (
Union[type[Trainer],Callable[...,Trainer]]) – An optional subclass of theTrainerclass or a callable which returns aTrainerinstance when called.trainer_defaults¶ (
Optional[dict[str,Any]]) – Set to override Trainer defaults or add persistent callbacks. The callbacks added through this argument will not be configurable from a configuration file and will always be present for this particular CLI. Alternatively, configurable callbacks can be added as explained in the CLI docs.seed_everything_default¶ (
Union[bool,int]) – Number for theseed_everything()seed value. Set to True to automatically choose a seed value. Setting it to False will avoid callingseed_everything.parser_kwargs¶ (
Union[dict[str,Any],dict[str,dict[str,Any]],None]) – Additional arguments to instantiate eachLightningArgumentParser.subclass_mode_model¶ (
bool) – Whether model can be any subclass of the given class.Whether datamodule can be any subclass of the given class.
args¶ (
Union[list[str],dict[str,Any],object,None]) – Arguments to parse. IfNonethe arguments are taken fromsys.argv. Command line style arguments can be given in alist. Alternatively, structured config options can be given in adictorjsonargparse.Namespace.run¶ (
bool) – Whether subcommands should be added to run aTrainermethod. If set toFalse, the trainer and model classes will be instantiated only.auto_configure_optimizers¶ (
bool) – Whether to automatically add default optimizer and lr_scheduler arguments.load_from_checkpoint_support¶ (
bool) – Whethersave_hyperparametersshould save the original parsed hyperparameters (instead of what__init__receives), such that it is possible forload_from_checkpointto correctly instantiate classes even when using complex nesting and dependency injection.
- add_arguments_to_parser(parser)[source]¶
Implement to add extra arguments to the parser or link arguments.
- Parameters:
parser¶ (
LightningArgumentParser) – The parser object to which arguments can be added- Return type:
- add_core_arguments_to_parser(parser)[source]¶
Adds arguments from the core classes to the parser.
- Return type:
- after_instantiate_classes()[source]¶
Implement to run some code after instantiating the classes.
- Return type:
- before_instantiate_classes()[source]¶
Implement to run some code before instantiating the classes.
- Return type:
- static configure_optimizers(lightning_module, optimizer, lr_scheduler=None)[source]¶
Override to customize the
configure_optimizers()method.- Parameters:
lightning_module¶ (
LightningModule) – A reference to the model.lr_scheduler¶ (
Union[LRScheduler,ReduceLROnPlateau,None]) – The learning rate scheduler (if used).
- Return type:
- static link_optimizers_and_lr_schedulers(parser)[source]¶
Creates argument links for optimizers and learning rate schedulers that specified a
link_to.- Return type:
- parse_arguments(parser, args)[source]¶
Parses command line arguments and stores it in
self.config.- Return type: