-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathhyperparameters.py
77 lines (63 loc) · 2 KB
/
hyperparameters.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Copyright (c) 2023 Graphcore Ltd. All rights reserved.
#
import dataclasses
import os
from typing import List, Dict, Any, Optional
import yaml
@dataclasses.dataclass
class DatasetHyperparameters:
name: str
version: Optional[str] = None
add_inverse_train: Optional[bool] = True
add_inverse_test: Optional[bool] = True
@dataclasses.dataclass
class ModelHyperparameters:
input_dim: int
hidden_dims: List
message_fct: str
aggregation_fct: str
num_mlp_layers: int
relation_learning: str
adversarial_temperature: float
@dataclasses.dataclass
class ExecutionHyperparameters:
batch_size_train: int
batch_size_test: int
num_negative: int
check_negatives: bool
lr: float
num_epochs: int
dtype: str
loss_scale: int
replicas: int
device_iterations: int
gradient_accumulation: int
do_valid: bool
do_test: bool
edge_dropout: Optional[float] = 0.0
pipeline: Optional[Dict] = None
device: Optional[str] = "ipu"
@dataclasses.dataclass
class Config:
dataset: DatasetHyperparameters
model: ModelHyperparameters
execution: ExecutionHyperparameters
def config_from_yaml(path: str):
with open(path, "r", encoding="utf-8") as file:
params = yaml.load(file, Loader=yaml.FullLoader)
return Config(
dataset=DatasetHyperparameters(**params["dataset"]),
model=ModelHyperparameters(**params["model"]),
execution=ExecutionHyperparameters(**params["execution"]),
)
def config_to_yaml(config: Config, path: str):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as file:
yaml.dump(dataclasses.asdict(config), file)
def recursive_replace(config: Config, new_config: Dict[str, Any]) -> Config:
for key, val in new_config.items():
if dataclasses.is_dataclass(getattr(config, key)):
setattr(config, key, recursive_replace(getattr(config, key), val))
else:
setattr(config, key, val)
return config