-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalanced_mini_plot.py
96 lines (81 loc) · 2.93 KB
/
balanced_mini_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
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
87
88
89
90
91
92
93
94
95
96
"""Plot evaluation scores"""
from collections import deque
from itertools import cycle
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np
def _main():
curdir = Path(__file__).parent.resolve()
resultsdir = curdir / 'balanced_mini_output' / '2096804712593481934'
label_to_dir = {
'train': resultsdir / 'train_eval',
'dev': resultsdir / 'dev_eval',
'test': resultsdir / 'test_eval',
}
plot_spearman(label_to_dir)
plot_mse(label_to_dir)
def plot_spearman(label_to_dir):
colorcycler = cycle(plt.rcParams['axes.prop_cycle'].by_key()['color'])
markercycler = cycle(['o', 's', '^'])
for label, evaldir in label_to_dir.items():
datapoints = read_record(evaldir)
win_avg = windowed_average(datapoints, window_size=10)
cur_color = next(colorcycler)
cur_marker = next(markercycler)
plt.scatter(np.arange(len(datapoints)),
datapoints,
label=label,
alpha=0.5,
marker=cur_marker,
color=cur_color)
plt.plot(win_avg, color=cur_color)
plt.legend()
plt.xlabel('Batch')
plt.ylim(bottom=0, top=0.5)
plt.ylabel('Spearman\'s Rank Correlation')
plt.title('Rank Correlation of Predictions to Labels')
plt.savefig('balanced_mini_eval_plot.svg')
plt.close()
def plot_mse(label_to_dir):
colorcycler = cycle(plt.rcParams['axes.prop_cycle'].by_key()['color'])
markercycler = cycle(['o', 's', '^'])
for label, evaldir in label_to_dir.items():
datapoints = read_mse(evaldir)
win_avg = windowed_average(datapoints, window_size=10)
cur_color = next(colorcycler)
cur_marker = next(markercycler)
plt.scatter(np.arange(len(datapoints)),
datapoints,
label=label,
alpha=0.5,
marker=cur_marker,
color=cur_color)
plt.plot(win_avg, color=cur_color)
plt.legend()
plt.xlabel('Batch')
# plt.ylim(top=0.05, bottom=0.025)
plt.ylabel('Mean Squared Error')
plt.title('Mean Squared Error of Predictions to Labels')
plt.savefig('balanced_mini_loss_plot.svg')
plt.close()
def read_record(evaldir):
recordpath = evaldir / 'record.txt'
with recordpath.open('r', encoding='utf-8') as ifh:
# skip header
next(ifh)
return [float(line.strip().split('\t')[2]) for line in ifh]
def read_mse(evaldir):
recordpath = evaldir / 'record.txt'
with recordpath.open('r', encoding='utf-8') as ifh:
# skip header
next(ifh)
return [float(line.strip().split('\t')[3]) for line in ifh]
def windowed_average(datapoints, window_size=5):
window = deque(maxlen=window_size)
result = []
for point in datapoints:
window.append(point)
result.append(np.mean(window))
return result
if __name__ == '__main__':
_main()