Shortcuts

Configure hyperparameters from the CLI (Advanced)

Customize arguments by subcommand

To customize arguments by subcommand, pass the config before the subcommand:

$ python main.py [before] [subcommand] [after]
$ python main.py  ...         fit       ...

For example, here we set the Trainer argument [max_steps = 100] for the full training routine and [max_steps = 10] for testing:

# config.yaml
fit:
    trainer:
        max_steps: 100
test:
    trainer:
        max_epochs: 10

now you can toggle this behavior by subcommand:

# full routine with max_steps = 100
$ python main.py --config config.yaml fit

# test only with max_epochs = 10
$ python main.py --config config.yaml test

Run from cloud yaml configs

For certain enterprise workloads, Lightning CLI supports running from hosted configs:

$ python main.py [subcommand] --config s3://bucket/config.yaml

For more options, refer to Remote filesystems.


Use a config via environment variables

For certain CI/CD systems, it’s useful to pass in raw yaml config as environment variables:

$ python main.py fit --trainer "$TRAINER_CONFIG" --model "$MODEL_CONFIG" [...]

Run from environment variables directly

The Lightning CLI can convert every possible CLI flag into an environment variable. To enable this, add to parser_kwargs the default_env argument:

cli = LightningCLI(..., parser_kwargs={"default_env": True})

now use the --help CLI flag with any subcommand:

$ python main.py fit --help

which will show you ALL possible environment variables that can be set:

usage: main.py [options] fit [-h] [-c CONFIG]
                            ...

optional arguments:
...
ARG:   --model.out_dim OUT_DIM
ENV:   PL_FIT__MODEL__OUT_DIM
                        (type: int, default: 10)
ARG:   --model.learning_rate LEARNING_RATE
ENV:   PL_FIT__MODEL__LEARNING_RATE
                        (type: float, default: 0.02)

now you can customize the behavior via environment variables:

# set the options via env vars
$ export PL_FIT__MODEL__LEARNING_RATE=0.01
$ export PL_FIT__MODEL__OUT_DIM=5

$ python main.py fit

Set default config files

To set a path to a config file of defaults, use the default_config_files argument:

cli = LightningCLI(MyModel, MyDataModule, parser_kwargs={"default_config_files": ["my_cli_defaults.yaml"]})

or if you want defaults per subcommand:

cli = LightningCLI(MyModel, MyDataModule, parser_kwargs={"fit": {"default_config_files": ["my_fit_defaults.yaml"]}})

Enable variable interpolation

In certain cases where multiple settings need to share a value, consider using variable interpolation. For instance:

model:
  encoder_layers: 12
  decoder_layers:
  - ${model.encoder_layers}
  - 4

To enable variable interpolation, first install omegaconf:

pip install omegaconf

Then set omegaconf when instantiating the LightningCLI class:

cli = LightningCLI(MyModel, parser_kwargs={"parser_mode": "omegaconf"})

After this, the CLI will automatically perform interpolation in yaml files:

python main.py --model.encoder_layers=12

For more details about the interpolation support and its limitations, have a look at the jsonargparse and the omegaconf documentations.

Note

There are many use cases in which variable interpolation is not the correct approach. When a parameter must always be derived from other settings, it shouldn’t be up to the CLI user to do this in a config file. For example, if the data and model both require batch_size and must be the same value, then Argument linking should be used instead of interpolation.


© Copyright Copyright (c) 2018-2022, Lightning AI et al... Revision f4fcad36.

Built with Sphinx using a theme provided by Read the Docs.
Read the Docs v: latest
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.