Shortcuts

argparse

Functions

add_argparse_args

Extends existing argparse by default attributes for cls.

from_argparse_args

Create an instance from CLI arguments.

get_init_arguments_and_types

Scans the class signature and returns argument names, types and default values.

parse_argparser

Parse CLI arguments, required for custom bool types.

parse_env_variables

Parse environment arguments if they are defined.

Classes

ParseArgparserDataType

class pytorch_lightning.utilities.argparse.ParseArgparserDataType(*_, **__)[source]

Bases: abc.ABC

pytorch_lightning.utilities.argparse.add_argparse_args(cls, parent_parser, *, use_argument_group=True)[source]

Extends existing argparse by default attributes for cls.

Parameters
  • cls (Type[Trainer]) – Lightning class

  • parent_parser (ArgumentParser) – The custom cli arguments parser, which will be extended by the class’s default arguments.

  • use_argument_group (bool) – By default, this is True, and uses add_argument_group to add a new group. If False, this will use old behavior.

Return type

Union[_ArgumentGroup, ArgumentParser]

Returns

If use_argument_group is True, returns parent_parser to keep old workflows. If False, will return the new ArgumentParser object.

Only arguments of the allowed types (str, float, int, bool) will extend the parent_parser.

Raises

RuntimeError – If parent_parser is not an ArgumentParser instance

Examples

# Option 1: Default usage. >>> import argparse >>> from pytorch_lightning import Trainer >>> parser = argparse.ArgumentParser() >>> parser = Trainer.add_argparse_args(parser) >>> args = parser.parse_args([])

# Option 2: Disable use_argument_group (old behavior). >>> import argparse >>> from pytorch_lightning import Trainer >>> parser = argparse.ArgumentParser() >>> parser = Trainer.add_argparse_args(parser, use_argument_group=False) >>> args = parser.parse_args([])

pytorch_lightning.utilities.argparse.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
  • cls (Type[ParseArgparserDataType]) – Lightning class

  • args (Union[Namespace, ArgumentParser]) – The parser or namespace to take arguments from. Only known arguments will be parsed and passed to the Trainer.

  • **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)
Return type

ParseArgparserDataType

pytorch_lightning.utilities.argparse.get_init_arguments_and_types(cls)[source]

Scans the class 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.parse_argparser(cls, arg_parser)[source]

Parse CLI arguments, required for custom bool types.

Return type

Namespace

pytorch_lightning.utilities.argparse.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

Namespace

Read the Docs v: latest
Versions
latest
stable
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
ipynb-update
docs-search
Downloads
html
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.