Shortcuts

LightningCLI

class pytorch_lightning.cli.LightningCLI(model_class=None, datamodule_class=None, save_config_callback=<class 'pytorch_lightning.cli.SaveConfigCallback'>, save_config_kwargs=None, trainer_class=<class 'pytorch_lightning.trainer.trainer.Trainer'>, trainer_defaults=None, seed_everything_default=True, description='pytorch-lightning trainer command line tool', env_prefix='PL', env_parse=False, parser_kwargs=None, subclass_mode_model=False, subclass_mode_data=False, args=None, run=True, auto_registry=False, **kwargs)[source]

Bases: object

Implementation 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 env_parse=True. A full configuration yaml would be parsed from PL_CONFIG if set. Individual settings are so parsed from variables named for example PL_TRAINER__MAX_EPOCHS.

For more info, read the CLI docs.

Warning

LightningCLI is in beta and subject to change.

Parameters
  • model_class (Union[Type[LightningModule], Callable[..., LightningModule], None]) – An optional LightningModule class to train on or a callable which returns a LightningModule instance when called. If None, you can pass a registered model with --model=MyModel.

  • datamodule_class (Union[Type[LightningDataModule], Callable[..., LightningDataModule], None]) – An optional LightningDataModule class or a callable which returns a LightningDataModule instance when called. If None, 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 the Trainer class or a callable which returns a Trainer instance 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 the seed_everything() seed value. Set to True to automatically choose a seed value. Setting it to False will avoid calling seed_everything.

  • description (str) – Description of the tool shown when running --help.

  • env_prefix (str) – Prefix for environment variables.

  • env_parse (bool) – Whether environment variable parsing is enabled.

  • parser_kwargs (Union[Dict[str, Any], Dict[str, Dict[str, Any]], None]) – Additional arguments to instantiate each LightningArgumentParser.

  • subclass_mode_model (bool) – Whether model can be any subclass of the given class.

  • subclass_mode_data (bool) –

    Whether datamodule can be any subclass of the given class.

  • args (Union[List[str], Dict[str, Any], object, None]) – Arguments to parse. If None the arguments are taken from sys.argv. Command line style arguments can be given in a list. Alternatively, structured config options can be given in a dict or jsonargparse.Namespace.

  • run (bool) – Whether subcommands should be added to run a Trainer method. If set to False, the trainer and model classes will be instantiated only.

  • auto_registry (bool) – Whether to automatically fill up the registries with all defined subclasses.

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

None

add_core_arguments_to_parser(parser)[source]

Adds arguments from the core classes to the parser.

Return type

None

add_default_arguments_to_parser(parser)[source]

Adds default arguments to the parser.

Return type

None

before_instantiate_classes()[source]

Implement to run some code before instantiating the classes.

Return type

None

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.

  • optimizer (Optimizer) – The optimizer.

  • lr_scheduler (Union[_LRScheduler, ReduceLROnPlateau, None]) – The learning rate scheduler (if used).

Return type

Any

init_parser(**kwargs)[source]

Method that instantiates the argument parser.

Return type

LightningArgumentParser

instantiate_classes()[source]

Instantiates the classes and sets their attributes.

Return type

None

instantiate_trainer(**kwargs)[source]

Instantiates the trainer.

Parameters

kwargs (Any) – Any custom trainer arguments.

Return type

Trainer

Creates argument links for optimizers and learning rate schedulers that specified a link_to.

Return type

None

parse_arguments(parser, args)[source]

Parses command line arguments and stores it in self.config.

Return type

None

setup_parser(add_subcommands, main_kwargs, subparser_kwargs)[source]

Initialize and setup the parser, subcommands, and arguments.

Return type

None

static subcommands()[source]

Defines the list of available subcommands and the arguments to skip.

Return type

Dict[str, Set[str]]

Read the Docs v: stable
Versions
latest
stable
1.8.3post1
1.8.3.post0
1.8.3
1.8.2
1.8.1
1.8.0.post1
1.8.0
1.7.7
1.7.6
1.7.5
1.7.4
1.7.3
1.7.2
1.7.1
1.7.0
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.10
1.5.9
1.5.8
1.5.7
1.5.6
1.5.5
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.9
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.8
1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.10
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.0
0.5.3
0.4.9
Downloads
html
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.