cli¶
Classes
Extension of jsonargparse’s ArgumentParser for pytorch-lightning |
|
Implementation of a configurable command line tool for pytorch-lightning |
|
Saves a LightningCLI config to the log_dir when training starts |
-
class
pytorch_lightning.utilities.cli.
LightningArgumentParser
(*args, parse_as_dict=True, **kwargs)[source]¶ Bases:
jsonargparse.
Extension of jsonargparse’s ArgumentParser for pytorch-lightning
Initialize argument parser that supports configuration file input
For full details of accepted arguments see ArgumentParser.__init__.
-
class
pytorch_lightning.utilities.cli.
LightningCLI
(model_class, datamodule_class=None, save_config_callback=<class 'pytorch_lightning.utilities.cli.SaveConfigCallback'>, trainer_class=<class 'pytorch_lightning.trainer.trainer.Trainer'>, trainer_defaults=None, seed_everything_default=None, description='pytorch-lightning trainer command line tool', env_prefix='PL', env_parse=False, parser_kwargs=None, subclass_mode_model=False, subclass_mode_data=False)[source]¶ Bases:
object
Implementation of a configurable command line tool for pytorch-lightning
Receives as input pytorch-lightning classes, which are instantiated using a parsed configuration file and/or command line args and then runs trainer.fit. Parsing of configuration from environment variables can be enabled by setting
env_parse=True
. A full configuration yaml would be parsed fromPL_CONFIG
if set. Individual settings are so parsed from variables named for examplePL_TRAINER__MAX_EPOCHS
.Example, first implement the
trainer.py
tool as:from mymodels import MyModel from pytorch_lightning.utilities.cli import LightningCLI LightningCLI(MyModel)
Then in a shell, run the tool with the desired configuration:
$ python trainer.py --print_config > config.yaml $ nano config.yaml # modify the config as desired $ python trainer.py --cfg config.yaml
Warning
LightningCLI
is in beta and subject to change.- Parameters
model_class¶ (
Type
[LightningModule
]) – The LightningModule class to train on.datamodule_class¶ (
Optional
[Type
[LightningDataModule
]]) – An optional LightningDataModule class.save_config_callback¶ (
Type
[SaveConfigCallback
]) – A callback class to save the training config.trainer_class¶ (
Type
[Trainer
]) – An optional extension of the Trainer class.trainer_defaults¶ (
Optional
[Dict
[str
,Any
]]) – Set to override Trainer defaults or add persistent callbacks.seed_everything_default¶ (
Optional
[int
]) – Default value for seed_everything argument.description¶ (
str
) – Description of the tool shown when running –help.env_parse¶ (
bool
) – Whether environment variable parsing is enabled.parser_kwargs¶ (
Optional
[Dict
[str
,Any
]]) – Additional arguments to instantiate LightningArgumentParser.subclass_mode_model¶ (
bool
) – Whether model can be any subclass of the given class.Whether datamodule can be any subclass of the given class.
-
add_arguments_to_parser
(parser)[source]¶ Implement to add extra arguments to parser or link arguments
- Parameters
parser¶ (
LightningArgumentParser
) – The argument parser object to which arguments can be added- Return type
-
add_core_arguments_to_parser
()[source]¶ Adds arguments from the core classes to the parser
- Return type
-
before_instantiate_classes
()[source]¶ Implement to run some code before instantiating the classes
- Return type
-
fit
()[source]¶ Runs fit of the instantiated trainer class and prepared fit keyword arguments
- Return type
-
instantiate_datamodule
()[source]¶ Instantiates the datamodule using self.config_init[‘data’] if given
- Return type
-
instantiate_trainer
()[source]¶ Instantiates the trainer using self.config_init[‘trainer’]
- Return type
-
class
pytorch_lightning.utilities.cli.
SaveConfigCallback
(parser, config, config_filename='config.yaml')[source]¶ Bases:
pytorch_lightning.callbacks.base.Callback
Saves a LightningCLI config to the log_dir when training starts