-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathml_example_after.py
More file actions
47 lines (31 loc) · 1.16 KB
/
ml_example_after.py
File metadata and controls
47 lines (31 loc) · 1.16 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
from dataclasses import dataclass
from simple_parsing import ArgumentParser
# create a parser, as usual
parser = ArgumentParser()
@dataclass
class MyModelHyperParameters:
"""Hyperparameters of MyModel."""
# Learning rate of the Adam optimizer.
learning_rate: float = 0.05
# Momentum of the optimizer.
momentum: float = 0.01
@dataclass
class TrainingConfig:
"""Training configuration settings."""
data_dir: str = "/data"
log_dir: str = "/logs"
checkpoint_dir: str = "checkpoints"
# automatically add arguments for all the fields of the classes above:
parser.add_arguments(MyModelHyperParameters, dest="hparams")
parser.add_arguments(TrainingConfig, dest="config")
args = parser.parse_args()
# Create an instance of each class and populate its values from the command line arguments:
hyperparameters: MyModelHyperParameters = args.hparams
config: TrainingConfig = args.config
class MyModel:
def __init__(self, hyperparameters: MyModelHyperParameters, config: TrainingConfig):
# hyperparameters:
self.hyperparameters = hyperparameters
# config:
self.config = config
m = MyModel(hyperparameters, config)