-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathlogger.py
74 lines (57 loc) · 2.19 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
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
"""
Defines a Log class, which is used for outputting statistics while a model is training, which
can then be visualized using vis/visualize_training.html.
The code below is slight modification of https://github.com/ivendrov/torch-logger, see that for detailed documentation.
"""
import json
import os
import os.path as osp
def write_json(file, d):
name = file + '.json'
with open(name, 'w') as f:
json.dump(d, f)
def load_json(file):
name = file + '.json'
if not osp.exists(name):
return None
with open(name, 'r') as f:
return json.load(f)
class Log:
def __init__(self, name, hyperparams, saveDir, xLabel = "Iterations", saveFrequency = 0):
self.name = name
self.hyperparams = hyperparams
self.xLabel = xLabel
self.saveLoc = osp.join(saveDir, name)
self.saveFrequency = saveFrequency
self.data = {}
self.updatesCounter = 0
if not osp.exists(self.saveLoc + '.json'):
write_json(self.saveLoc, [])
# update index file
indexLoc = osp.join(saveDir, 'index')
models = [filename[:-5] for filename in os.listdir(saveDir)
if filename.endswith('json') and filename != "index.json"]
write_json(indexLoc, models)
def update(self, ys, x=None):
""" adds the data point (x, ys), where ys is a dictionary of different statistics to keep track of """
if x is None:
x = self.updatesCounter
for name, y in ys.iteritems():
point = {'x': x, 'y': y}
# if dataset doesn't exist, create it
if name not in self.data:
self.data[name] = []
# add the point
self.data[name].append(point)
self.updatesCounter += 1
if self.saveFrequency > 0 and self.updatesCounter % self.saveFrequency == 0:
self.save()
def save(self, stats=None):
""" Saves all the data as saveDir/name.json, along with the given statistics """
write_json(self.saveLoc, {
'name': self.name,
'xLabel': self.xLabel,
'hyperparams': self.hyperparams,
'data': self.data,
'stats': stats
})