-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
86 lines (78 loc) · 2.79 KB
/
utils.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
79
80
81
82
83
84
85
86
from matplotlib.animation import FuncAnimation
import matplotlib.pyplot as plt
import csv
import os
import pandas as pd
class Logger():
def __init__(self,loss_list):
try:
os.mkdir('Results')
except FileExistsError:
pass
self.loss_list = loss_list
with open('Results\loss_log.csv','w') as csv_file:
csv_writer = csv.DictWriter(csv_file,fieldnames=loss_list)
csv_writer.writeheader()
def lossLog(self,loss_list):
with open('Results\loss_log.csv','a') as csv_file:
csv_writer = csv.DictWriter(csv_file,fieldnames=self.loss_list)
csv_writer.writerow(loss_list)
class Plotter():
def __init__(self):
try:
os.mkdir('Results')
except FileExistsError:
pass
self.count=0
def loss_plotter(self):
data =pd.read_csv('Results/loss_log.csv')
plt.figure(figsize=(10,5))
plt.title("Loss")
for losses in list(data.columns.values.tolist()):
plt.plot(data[losses],label=str(losses))
plt.xlabel("iterations")
plt.ylabel("Loss")
plt.legend()
plt.savefig('Results/Loss')
# def loss_live_plotter(self):
# def _animation(i):
# data=pd.read_csv('Results/loss_log.csv')
# plt.cla()
# plt.figure(figsize=(10,5))
# plt.subplot(1,2,1)
# plt.title("last 100 iter Loss")
# for losses in list(data.columns.values.tolist()):
# # plot only latest 100 iterations
# plt.plot(data[losses][-100:],label=str(losses))
# plt.xlabel("iterations")
# plt.ylabel("Complete Loss")
# plt.legend()
# plt.subplot(1,2,2)
# plt.title("Loss")
# for losses in list(data.columns.values.tolist()):
# plt.plot(data[losses],label=str(losses))
# plt.xlabel("iterations")
# plt.ylabel("Loss")
# plt.legend()
# #plt.tight_layout()
# ani=FuncAnimation(plt.gcf(),_animation)
# plt.show()
def loss_live_plotter(self):
def animate(i):
data = pd.read_csv('Results/loss_log.csv')
n = len(data)
last_n = 100
plt.clf()
plt.subplot(1,2,1)
plt.plot(range(max(0, n-last_n), n), data['Exploitability'].tail(last_n))
plt.title('Last 100 values')
plt.xlabel('Iters')
plt.ylabel('Loss')
plt.subplot(1,2,2)
plt.plot(data['Exploitability'])
plt.title('All values')
plt.xlabel('Iters')
plt.ylabel('Loss')
plt.tight_layout()
ani = FuncAnimation(plt.gcf(), animate, interval=1000)
plt.show()