-
Notifications
You must be signed in to change notification settings - Fork 2
/
nkutil.py
53 lines (47 loc) · 1.77 KB
/
nkutil.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
class HParams():
_skip_keys = ['populate_arguments', 'set_from_args', 'print', 'to_dict']
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
def __getitem__(self, item):
return getattr(self, item)
def __setitem__(self, item, value):
if not hasattr(self, item):
raise KeyError(f"Hyperparameter {item} has not been declared yet")
setattr(self, item, value)
def to_dict(self):
res = {}
for k in dir(self):
if k.startswith('_') or k in self._skip_keys:
continue
res[k] = self[k]
return res
def populate_arguments(self, parser):
for k in dir(self):
if k.startswith('_') or k in self._skip_keys:
continue
v = self[k]
k = k.replace('_', '-')
if type(v) in (int, float, str):
parser.add_argument(f'--{k}', type=type(v), default=v)
elif isinstance(v, bool):
if not v:
parser.add_argument(f'--{k}', action='store_true')
else:
parser.add_argument(f'--no-{k}', action='store_false')
def set_from_args(self, args):
for k in dir(self):
if k.startswith('_') or k in self._skip_keys:
continue
if hasattr(args, k):
self[k] = getattr(args, k)
elif hasattr(args, f'no_{k}'):
self[k] = getattr(args, f'no_{k}')
def print(self):
p_str = ''
for k in dir(self):
if k.startswith('_') or k in self._skip_keys:
continue
# print(k, repr(self[k]))
p_str += str(k) + '\t' + str(repr(self[k])) + '\n'
return p_str