CLI

In addition to loading from YAML, YAHP also supports loading data from CLI arguments.

Consider the following YAML called cli.yaml:

bar: 3.0

Now, consider the following script called cli.py that defines the Hparams and loads this YAML file:

 1# Copyright 2021 MosaicML. All Rights Reserved.
 2
 3import os
 4from dataclasses import dataclass
 5from typing import Optional
 6
 7import yahp as hp
 8
 9
10@dataclass
11class CLIExample(hp.Hparams):
12    foo: int = hp.required('foo field')
13    bar: float = hp.optional('bar field', default=1.0)
14    baz: Optional[str] = hp.optional('baz', default=None)
15
16
17# load parameters from cli.yaml
18# NOTE: cli.yaml does NOT define `foo`, so it will need to be specified
19# on the command line.
20# Try running: examples/cli.py --help
21hparams = CLIExample.create(os.path.join(os.path.dirname(__file__), 'cli.yaml'))
22
23print(hparams)

If you were to run python cli.py, YAHP would complain that it does not have a value for foo. This is because foo is a required field, but no value is specified in the YAML nor is there a default value.

Instead, foo can be set on the command line. Arugments specified in YAML can also be overridden via the command line.

Try running python cli.py --foo 2 --bar 3.0. This command should print:

SimpleExample:
    bar: 3.0
    baz: null
    foo: 2