-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathflag.py
More file actions
51 lines (38 loc) · 1.39 KB
/
flag.py
File metadata and controls
51 lines (38 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from dataclasses import dataclass
from simple_parsing import ArgumentParser, parse
from simple_parsing.helpers import flag
@dataclass
class HParams:
"""Set of options for the training of a Model."""
num_layers: int = 4
num_units: int = 64
optimizer: str = "ADAM"
learning_rate: float = 0.001
train: bool = flag(default=True, negative_prefix="--no-")
# Example 1 using default flag, i.e. train set to True
args = parse(HParams)
print(args)
expected = """
HParams(num_layers=4, num_units=64, optimizer='ADAM', learning_rate=0.001, train=True)
"""
# Example 2 using the flags negative prefix
assert parse(HParams, args="--no-train") == HParams(train=False)
# showing what --help outputs
parser = ArgumentParser() # Create an argument parser
parser.add_arguments(HParams, dest="hparams") # add arguments for the dataclass
parser.print_help()
expected += """
usage: flag.py [-h] [--num_layers int] [--num_units int] [--optimizer str]
[--learning_rate float] [--train bool]
optional arguments:
-h, --help show this help message and exit
HParams ['hparams']:
Set of options for the training of a Model.
--num_layers int (default: 4)
--num_units int (default: 64)
--optimizer str (default: ADAM)
--learning_rate float
(default: 0.001)
--train bool, --no-train bool
(default: True)
"""