-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_plot.py
56 lines (47 loc) · 1.59 KB
/
gen_plot.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
import time
import select
import subprocess
import sys
import math
import matplotlib.pyplot as plt
loss_path = "loss.log" if len(sys.argv) <= 1 else sys.argv[1]
HISTORY = 200
LOSS_SMOOTHING = 0
with open(loss_path, "r") as f:
data = list(map(lambda s: tuple(s[:-1].split(": ")), f.readlines()[-HISTORY:]))
data = sorted(list(filter(lambda tup: len(tup)==2, data)))
x_loss = list(map(lambda d: int(d[0]), data))
y_loss = list(map(lambda d: float(d[1]), data))
if LOSS_SMOOTHING > 0:
print("Using loss smoothing by running average of width %d." % (2*LOSS_SMOOTHING+1))
y_loss_smoothed = []
for i in range(len(y_loss)):
ynew = 0.0
count = 0
for k in range(i-LOSS_SMOOTHING,i+LOSS_SMOOTHING+1):
if 0 <= k < len(y_loss):
ynew += y_loss[k]
count += 1
y_loss_smoothed.append(ynew/float(count))
y_loss = y_loss_smoothed
plt.title("(loss smoothing width %d)" % (2*LOSS_SMOOTHING+1))
plt.plot(x_loss, y_loss, 'm-')
plt.xlabel("Number of Steps")
plt.ylabel("Loss")
plt.title("Training curve of " + loss_path)
plt.pause(1)
filename = loss_path
f = subprocess.Popen(['tail','-F','-n 0',filename],\
stdout=subprocess.PIPE,stderr=subprocess.PIPE)
p = select.poll()
p.register(f.stdout)
while True:
if p.poll(1):
line = f.stdout.readline()[:-1].decode("utf-8").split(": ")
print(line)
x_loss += [int(line[0])]
y_loss += [float(line[1])]
plt.plot(x_loss, y_loss, 'm-')
plt.pause(0.1)
time.sleep(1)
plt.show()