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

Utilities for Argument Parsing within Lightning Components.

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 variables from OS environment 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.

Examples

>>> 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.

Examples

>>> 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