-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistics.py
68 lines (54 loc) · 2.18 KB
/
statistics.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
import json
import os
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
def animate(i):
# Create arrays for all different statistical data
learning_rate = []
train_index = []
train_accuracy = []
train_data_loss = []
train_reg_loss = []
valid_index = []
valid_accuracy = []
valid_data_loss = []
# Open statistics.json file in read mode
with open(os.path.join(os.getcwd(), "statistics.json"), "r") as f:
# Converts json string to python dict
try:
stat_json = json.loads(f.read())
except:
return
# Iterate over all data points in key "train"
for data_points in stat_json["train"]:
# Extract all information and save them in the arrays
train_index.append(data_points["index"])
learning_rate.append(data_points["learning_rate"])
train_accuracy.append(data_points["accuracy"])
train_data_loss.append(data_points["data_loss"])
train_reg_loss.append(data_points["reg_loss"])
# Iterate over all data points in key "valid"
for data_points in stat_json["valid"]:
# Extract all information and save them in the arrays
valid_index.append(data_points["index"])
valid_accuracy.append(data_points["accuracy"])
valid_data_loss.append(data_points["data_loss"])
# Update the graphs
plt.cla()
# Plot all the training data
plt.plot(train_index, train_accuracy, label="training accuracy")
plt.plot(train_index, np.array(train_data_loss), label="training loss")
# Plot all the validation data
plt.plot(valid_index, valid_accuracy, label="validation accuracy")
plt.plot(valid_index, valid_data_loss, label="validation loss")
plt.ylim(0, 1)
# Add legend to graph
plt.legend(loc="upper left")
# Close statistics.json file
f.close()
# Animation function
# Calls animate() function every 1000 milliseconds
ani = FuncAnimation(plt.gcf(), animate, 1000)
# Show the graph
plt.show()