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
|
# ------------------------------------------------------------------------------
# Util class for hyperparams.
# ------------------------------------------------------------------------------
import json
class Params():
"""Class that loads hyperparameters from a json file."""
def __init__(self, json_path):
self.update(json_path)
def save(self, json_path):
"""Saves parameters to json file."""
with open(json_path, 'w') as f:
json.dump(self.__dict__, f, indent=4)
def update(self, json_path):
"""Loads parameters from json file."""
with open(json_path) as f:
params = json.load(f)
self.__dict__.update(params)
@property
def dict(self):
"""Gives dict-like access to Params instance."""
return self.__dict__
class ParamsDict():
"""Class that loads hyperparameters from a json file."""
def __init__(self, data):
self.update(data)
def __setitem__(self, key, item):
self.__dict__[key] = item
def __getitem__(self, key):
return self.__dict__[key]
def __repr__(self):
return repr(self.__dict__)
def __len__(self):
return len(self.__dict__)
def __delitem__(self, key):
del self.__dict__[key]
def save(self, json_path):
"""Saves parameters to json file."""
with open(json_path, 'w') as f:
json.dump(self.__dict__, f, indent=4)
def update(self, *args, **kwargs):
return self.__dict__.update(*args, **kwargs)
|