-
Notifications
You must be signed in to change notification settings - Fork 5
/
Params.py
52 lines (44 loc) · 1.38 KB
/
Params.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
"""
Codes from:
https://github.com/cs230-stanford/cs230-code-examples/blob/master/pytorch/nlp/utils.py
"""
import json
class Params():
"""Class that loads hyperparameters from a json file.
Example:
```
params = Params(json_path)
print(params.learning_rate)
params.learning_rate = 0.5 # change the value of learning_rate in params
```
"""
def __init__(self, json_path):
self.path = json_path
with open(json_path) as f:
params = json.load(f)
self.__dict__.update(params)
def save(self, json_path):
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)
def __str__(self):
# return string representation of 'Parameters' class
# print(Parameters) or str(Parameters)
ret = '======== [Config] ========\n'
for k in self.__dict__:
ret += '%s: %s\n' % (str(k), str(self.__dict__[k]))
ret += '\n'
return ret
@property
def dict(self):
"""
Gives dict-like access to params instance by
`params.dict['learning_rate']
"""
return self.__dict__
def update_dict(self, k, v):
self.__dict__[k] = v