-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvisualiser.py
executable file
·78 lines (63 loc) · 2.53 KB
/
visualiser.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
74
75
76
77
78
from collections import defaultdict
import visdom
# Do not print error messages if visdom client throws error
IGNORE_VISDOM_ERROR = 10
class VisdomWriter:
"""Class to publish data to Visdom"""
def __init__(self, enabled=True, logger=None):
"""
Initialise writer
Parameters
----------
enabled (bool): Whether or not to publish data to visdom
"""
self.vis = visdom.Visdom()
self.enabled = enabled
self.logger = logger
self.xdict = defaultdict(list)
self.ydict = defaultdict(list)
self.visdom_error_count = 0
def text(self, data, title="default"):
"""
Publish text data to visdom
Parameters
----------
data (string): text data to publish
title (string): visdom window title
"""
if self.enabled:
try:
self.vis.text(data, win=title, opts=dict(title=title))
self.visdom_error_count = 0
except Exception as e:
self.visdom_error_count += 1
if (self.visdom_error_count < IGNORE_VISDOM_ERROR):
if self.logger:
self.logger.error('Failed to publish text to visdom server: {}'.format(e))
else:
print('Failed to publish text to visdom server: {}'.format(e))
if self.logger:
self.logger.debug('{}: {}'.format(title, data))
def push(self, item, key="default"):
"""
Publish data required to plot line chart, useful for plotting losses
Parameters
----------
item (object): number or tensor
key (string): unique name of item being plotted (e.g "actor")
"""
if self.enabled:
self.ydict[key].append(item)
self.xdict[key].append(len(self.ydict[key]))
try:
self.vis.line(Y=self.ydict[key], X=self.xdict[key], win=key, opts=dict(title=key))
self.visdom_error_count = 0
except Exception as e:
self.visdom_error_count += 1
if self.visdom_error_count < IGNORE_VISDOM_ERROR:
if self.logger:
self.logger.error('Failed to publish data to visdom server: {}'.format(e))
else:
print('Failed to publish data to visdom server: {}'.format(e))
if self.logger:
self.logger.debug('{}: {}'.format(key, item))