-
Notifications
You must be signed in to change notification settings - Fork 3
/
plotting.py
66 lines (50 loc) · 1.83 KB
/
plotting.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
from visdom import Visdom
import numpy as np
class VisdomLinePlotter(object):
"""Plots to Visdom"""
def __init__(self, env_name='main', port=8050, disable=False):
self.disable = disable
if self.disable:
return
try:
self.viz = Visdom(port=port)
except (ConnectionError, ConnectionRefusedError) as e:
raise ConnectionError("Visdom Server not running, please launch it with `visdom` in the terminal")
self.env = env_name
self.plots = {}
def clear(self):
self.plots = {}
def imshow(self, var_name, images):
if self.disable:
return
if var_name not in self.plots:
self.plots[var_name] = self.viz.images(images)
else:
self.viz.images(images, win=self.plots[var_name], env=self.env)
def plot(self, window_id, variable, title, x, y, xlabel='epochs'):
if self.disable:
return
if window_id not in self.plots:
self.plots[window_id] = self.viz.line(X=np.array([x,x]), Y=np.array([y,y]), env=self.env, opts=dict(
legend=[variable],
title=title,
xlabel=xlabel,
ylabel=variable
))
else:
self.viz.line(X=np.array([x]), Y=np.array([y]), env=self.env, win=self.plots[window_id], name=variable, update = 'append')
class Logger:
def __init__(self, name):
self.name = plottern
self.value = 0
def increment(self, value, step=1):
self.value += value
self.step += step
def update(self, value, alpha=0.8):
self.value = alpha * self.value + (1 - alpha) * value
def set(self, value):
self.value = value
def reset(self):
self.value = 0
def value(self):
return self.value