-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
48 lines (39 loc) · 1.85 KB
/
graph.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
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import pickle
import os
from pprint import pprint
def parse_results(filename):
""" Reads the metric.txt file. Returns a dictionary of time sequenced metrics. """
ret = {"development_accuracy":[],
"development_similarity":[],
"training_accuracy":[],
"training_similarity":[]}
with open(filename) as file:
for l in file:
if not l[0].isdigit():
continue
batches, dataset, metric, value = l.split()
category = dataset + "_" + metric
ret[category].append((int(batches), float(value)))
for k in ret:
ret[k] = list(zip(*ret[k]))
return ret
def create_graph(results_dir="unsaved_model/results/"):
""" Draws a graph of accuracy and similarity, for dev and train. Also plots train loss. """
loss = pickle.load(open("{}loss_track.pkl".format(results_dir), "rb"))
plt.figure()
fig, loss_ax = plt.subplots()
loss_ax.plot(loss, "g", label="loss")
stats_ax = loss_ax.twinx()
results = parse_results("{}metrics.tsv".format(results_dir))
stats_ax.plot(results["development_accuracy"][0], results["development_accuracy"][1], "r", label="dev_acc")
stats_ax.plot(results["development_similarity"][0], results["development_similarity"][1], "r", label="dev_sim")
stats_ax.plot(results["training_accuracy"][0], results["training_accuracy"][1], "b", label="train_acc")
stats_ax.plot(results["training_similarity"][0], results["training_similarity"][1], "b", label="train_sim")
handles_loss, labels_loss = loss_ax.get_legend_handles_labels()
handles_stats, labels_stats = stats_ax.get_legend_handles_labels()
stats_ax.legend(handles_stats + handles_loss, labels_stats + labels_loss, loc=2)
plt.savefig("{}/graph".format(results_dir))
plt.close()