-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_results.py
213 lines (174 loc) · 6.49 KB
/
plot_results.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib import rc
from matplotlib.pyplot import figure
import json
import yaml
import matplotlib.ticker as ticker
def plot_(lbl: str, res_file: str, plt_type: str = 'epoch_loss',
x_axis='time', plot_freq=1, eval_freq=1,
line_width=4, marker=None, line_style=None,
optima: float = 0.0, color=None, smoothen=False,
batch_size=1):
with open(res_file, 'rb') as f:
result = json.load(f)
scores = []
if type(result) is not list:
result = [result]
for run in result:
best_acc = run["best_test_acc"]
res = run[plt_type]
# res -= optima * np.ones(len(res))
if plt_type == 'jacobian_residual':
res = np.ones(len(res)) - res
res = np.square(res)
res = res[::plot_freq]
if smoothen:
res = smooth(res, 3)
scores += [res]
scores = np.array(scores)
mean = np.mean(scores, axis=0)
std = np.std(scores, axis=0)
UB = mean + 1 * std
LB = mean - 1 * std
if plt_type == 'test_acc':
final_acc = mean[-1]
print("{} Final Test Accuracy : {}; Best Test Accuracy: {} +- {} "
.format(lbl, final_acc, best_acc, min(3 * std)))
elif plt_type == 'train_loss':
min_loss = min(mean)
final_loss = mean[-1]
print("{} Train Loss : {} +- {}".format(lbl, final_loss, min(3 * std)))
if x_axis == 'time':
tot_cost = 0
for res_i in result:
print(res_i["total_cost"])
tot_cost += res_i["total_cost"]
tot_cost /= len(result)
x_freq = int(tot_cost / len(mean))
x = np.arange(len(mean)) * x_freq
elif x_axis == 'steps':
x = np.arange(start=1, stop=len(result[0][plt_type])+1)[::plot_freq]
x *= eval_freq
else:
raise NotImplementedError
# mean = mean[::plot_freq]
plt.plot(x, mean, label=lbl, linewidth=line_width, marker=marker, linestyle=line_style, color=color)
# plt.fill_between(x, LB, UB, alpha=0.3, linewidth=0.5, color=color)
def smooth(y, box_pts):
box = np.ones(box_pts) / box_pts
y_smooth = np.convolve(y, box, mode='same')
y_smooth[-1] = y_smooth[-2]
return y_smooth
if __name__ == '__main__':
ax = plt.figure().gca()
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
# activate latex text rendering
rc('text', usetex=True)
plt_cfg = yaml.load(open('plt_cfg.yaml'), Loader=yaml.FullLoader)
# -------------------------------------------------------------------------------------------
# ------------------------------- Modify Here -----------------------------------------------
d = plt_cfg["dir"] if plt_cfg["dir"] else ""
pl_type = plt_cfg["plot_type"]
x_ax = plt_cfg["x_ax"]
plot_type = plt_cfg["plot_type"]
smoothen = plt_cfg["smoothen"]
ylim_b = plt_cfg["ylim_b"]
ylim_t = plt_cfg["ylim_t"]
xlim_l = plt_cfg["xlim_l"]
xlim_r = plt_cfg["xlim_r"]
for pl in plt_cfg["plots"]:
result_file = d + pl["file"]
lbl = pl["label"]
lw = pl['line_width']
ls = pl["line_style"]
mk = pl["marker"]
clr = pl["clr"]
bs = pl["batch_size"]
plt_freq = pl["plot_freq"]
eval_freq = pl["eval_freq"]
plot_(lbl=lbl,
res_file=result_file,
plt_type=pl_type,
x_axis=x_ax,
optima=0,
line_width=lw,
marker=mk,
line_style=ls,
color=clr,
plot_freq=plt_freq,
eval_freq=eval_freq,
smoothen=smoothen,
batch_size=bs)
# Fix the X Labels
if x_ax == 'time':
plt.xlabel(r'$\mathcal{O}$(Time)', fontsize=10)
elif x_ax == 'steps':
plt.xlabel(r'Number of passes over data', fontsize=10)
elif x_ax == 'samples':
plt.xlabel(r'Number of Samples Seen', fontsize=10)
else:
raise NotImplementedError
plt.ylim(ylim_b, ylim_t)
plt.xlim(xlim_l, xlim_r)
if plot_type == 'test_error':
plt.yscale("log")
ax.yaxis.set_minor_formatter(ticker.ScalarFormatter())
plt.ylabel('Test Error', fontsize=10)
elif plot_type == 'test_acc':
ax.yaxis.set_minor_formatter(ticker.ScalarFormatter())
# plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
plt.ylabel('Test Accuracy (%)', fontsize=10)
elif plot_type == 'train_acc':
plt.ylabel('Train Accuracy', fontsize=10)
elif plot_type == 'train_loss':
# ax.set_yscale('log')
ax.yaxis.set_major_formatter(ticker.ScalarFormatter())
plt.ticklabel_format(style='sci', axis='both', scilimits=(0, 0))
plt.ylabel('Training Loss', fontsize=10)
elif plot_type == 'train_error':
plt.ylabel('Train Error', fontsize=10)
elif plot_type == 'jacobian_residual':
plt.ylabel(r'1 - $\xi$')
plt.xlabel("Gradient Aggregation Steps", fontsize=10)
elif plot_type == 'test_loss':
# ax.set_yscale('log')
ax.yaxis.set_major_formatter(ticker.ScalarFormatter())
plt.ticklabel_format(style='sci', axis='both', scilimits=(0, 0))
plt.ylabel('Test Loss', fontsize=10)
else:
raise NotImplementedError
plt.legend()
plt.grid(True, which='both', linestyle='--')
plt.tick_params(labelsize=10)
figure(figsize=(1, 1))
plt.show()
# def plot_mass(masses):
# # x_labels = ['0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1']
# # legends = ['epoch 5', 'epoch 10', 'epoch 15', 'epoch 20']
# x_labels = ['$0\%$', '$10\%$', '$20\%$']
# legends = [r"\textsc{SGD}",
# r"\textsc{Gm-SGD}",
# r"\textsc{BGmD}"]
#
# x = np.arange(len(x_labels))
# fig, ax = plt.subplots()
#
# ax.set_xticks(x)
# ax.set_yticks(np.arange(start=0, stop=100, step=10))
# ax.set_xticklabels(x_labels)
#
# # with open(res_file, 'rb') as f:
# # res = json.load(f)
# # masses = res["frac_mass_retained"]
# width = 0.1
# offset = -3 / 2
# for frac_dist, leg in zip(masses, legends):
# # frac_dist = frac_dist[1:]
# # frac_dist[-1] = 1
# # plt.plot(x, frac_dist)
# plt.bar(height=frac_dist, x=x + offset * width, width=width, label=leg)
# offset += 1
# ax.legend(loc='lower left', bbox_to_anchor=(0.0, 1.01), ncol=3,
# borderaxespad=0, frameon=False, fontsize=11)