-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
68 lines (60 loc) · 2.12 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
from matplotlib import pyplot as plt
SPLITS = ["Train", "Validation"]
METRICS = ["Accuracy", "Loss"]
def config(attr):
"""
Retrieves quries from config file.
"""
if not hasattr(config, "config"):
with open("config.json") as f:
config.config = eval(f.read())
node = config.config
for part in attr.split("."):
node = node[part]
return node
class TwitterPlotter():
#Class that manages my matplotlib implementation
def __init__(self,name):
plt.ion()
fig, self.axes = plt.subplots(1, 2, figsize=(20, 5))
plt.suptitle(name)
self.axes[0].set_xlabel("Epoch")
self.axes[0].set_ylabel("Accuracy")
self.axes[1].set_xlabel("Epoch")
self.axes[1].set_ylabel("Loss")
self.name = name
def update(self, epoch, stats):
colors = ["r", "b"]
for i in range(len(METRICS)):
for j in range(len(SPLITS)):
idx = len(METRICS) * j + i
if idx >= len(stats[-1]):
print('skipping')
continue
self.axes[i].plot(
range(epoch - len(stats) + 1, epoch + 1),
[stat[idx] for stat in stats],
linestyle="--",
marker="o",
color=colors[j],
)
self.axes[i].legend(SPLITS[: int(len(stats[-1]) / len(METRICS))])
plt.pause(0.00001)
def save(self):
plt.savefig("{}.png".format(self.name), dpi=200)
def hold(self):
plt.ioff()
plt.show()
def logger(epoch, stats):
"""Print the train, validation, test accuracy/loss/auroc.
Each epoch in `stats` should have order
[val_acc, val_loss, val_auc, train_acc, ...]
Test accuracy is optional and will only be logged if stats is length 9.
"""
print("Epoch {}".format(epoch))
for j, split in enumerate(SPLITS):
for i, metric in enumerate(METRICS):
idx = len(METRICS) * j + i
if idx >= len(stats[-1]):
continue
print(f"\t{split} {metric}:{round(stats[-1][idx],4)}")