-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
47 lines (37 loc) · 1.68 KB
/
logger.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
import os
import inspect
import json
from typing import Optional
class Logger:
def __init__(self, args, results_path: str, log_to_file: bool):
self.results_path = results_path
self.log_to_file = log_to_file
self.file_log_path = os.path.join(self.results_path, "log.txt")
if self.log_to_file:
os.makedirs(self.results_path, exist_ok=True)
def log_hyperparams(self, config_object):
attributes = inspect.getmembers(config_object, lambda a: not (inspect.isroutine(a)))
attributes = [a for a in attributes if not (a[0].startswith('__') and a[0].endswith('__'))]
attribute_dict = {}
def add_to_attribute_dict(a):
for key, value in a:
if isinstance(value, dict):
add_to_attribute_dict([(f"{key}.{k}", v) for k, v in value.items()])
else:
if key not in ["devices_for_eval_workers"] and len(str(value)) <= 500:
attribute_dict[key] = value
add_to_attribute_dict(attributes)
if self.log_to_file:
with open(self.file_log_path, "a+") as f:
f.write(json.dumps({"hyperparameters": attribute_dict}))
f.write("\n")
def log_metrics(self, metrics: dict, step: Optional[int] = None, step_desc: Optional[str] = "epoch"):
if self.log_to_file:
if step is not None:
metrics[step_desc] = step
with open(self.file_log_path, "a+") as f:
f.write(json.dumps(metrics))
f.write("\n")
def text_artifact(self, dest_path: str, obj):
with open(dest_path, "w") as f:
f.write(str(obj))