argparse_utils¶
Functions
Extends existing argparse by default Trainer attributes. |
|
Create an instance from CLI arguments. |
|
Scans the Trainer signature and returns argument names, types and default values. |
|
Parse CLI arguments, required for custom bool types. |
|
|
|
Parse environment arguments if they are defined. |
-
pytorch_lightning.utilities.argparse_utils.
add_argparse_args
(cls, parent_parser)[source]¶ Extends existing argparse by default Trainer attributes.
- Parameters
parent_parser¶ (
ArgumentParser
) – The custom cli arguments parser, which will be extended by the Trainer default arguments.
Only arguments of the allowed types (str, float, int, bool) will extend the parent_parser.
Examples
>>> import argparse >>> import pprint >>> from pytorch_lightning import Trainer >>> parser = argparse.ArgumentParser() >>> parser = Trainer.add_argparse_args(parser) >>> args = parser.parse_args([])
- Return type
-
pytorch_lightning.utilities.argparse_utils.
from_argparse_args
(cls, args, **kwargs)[source]¶ Create an instance from CLI arguments. Eventually use varibles from OS environement which are defined as “PL_<CLASS-NAME>_<CLASS_ARUMENT_NAME>”
- Parameters
args¶ (
Union
[Namespace
,ArgumentParser
]) – The parser or namespace to take arguments from. Only known arguments will be parsed and passed to theTrainer
.**kwargs¶ – Additional keyword arguments that may override ones in the parser or namespace. These must be valid Trainer arguments.
Example
>>> from pytorch_lightning import Trainer >>> parser = ArgumentParser(add_help=False) >>> parser = Trainer.add_argparse_args(parser) >>> parser.add_argument('--my_custom_arg', default='something') >>> args = Trainer.parse_argparser(parser.parse_args("")) >>> trainer = Trainer.from_argparse_args(args, logger=False)
-
pytorch_lightning.utilities.argparse_utils.
get_init_arguments_and_types
(cls)[source]¶ Scans the Trainer signature and returns argument names, types and default values.
- Returns
(argument name, set with argument types, argument default value).
- Return type
List with tuples of 3 values
Examples
>>> from pytorch_lightning import Trainer >>> args = get_init_arguments_and_types(Trainer)
-
pytorch_lightning.utilities.argparse_utils.
parse_argparser
(cls, arg_parser)[source]¶ Parse CLI arguments, required for custom bool types.
- Return type
-
pytorch_lightning.utilities.argparse_utils.
parse_env_variables
(cls, template='PL_%(cls_name)s_%(cls_argument)s')[source]¶ Parse environment arguments if they are defined.
Example
>>> from pytorch_lightning import Trainer >>> parse_env_variables(Trainer) Namespace() >>> import os >>> os.environ["PL_TRAINER_GPUS"] = '42' >>> os.environ["PL_TRAINER_BLABLABLA"] = '1.23' >>> parse_env_variables(Trainer) Namespace(gpus=42) >>> del os.environ["PL_TRAINER_GPUS"]
- Return type