forked from ariseff/overcoming-catastrophic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5_plotter_revised_updated.py
193 lines (165 loc) · 10.1 KB
/
h5_plotter_revised_updated.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 20 11:18:02 2017
@author: andrew
"""
import h5py
import matplotlib.pyplot as plt
import os
import numpy as np
from scipy.stats.kde import gaussian_kde
def main():
fignum = [1]
for filename in os.listdir('/home/andrew/REU/overcoming-catastrophic-master/091717'):
if filename.endswith('.h5'):
try:
f = h5py.File(filename, 'r')
runs = int(f['params'][len(f['params']) - 1])
d = {}
run_dict = {}
task_counter_dict = {}
for runNum in range(runs):
run_dict['{}'.format(runNum + 1)] = 0
task_counter_dict['{}'.format(runNum + 1)] = 0
for key in f.keys():
if key[0:5] == 'count':
if run_dict[str(int(key[-2:]))] == 0:
run_dict[str(int(key[-2:]))] += 1
else:
run_dict[str(int(key[-2:]))] += .5
for dict_key in run_dict.keys():
while run_dict[dict_key] != 0:
task_counter_dict[dict_key] += 1
run_dict[dict_key] -= task_counter_dict[dict_key]
complete_cycle = True
for i in range(1, runs + 1):
if task_counter_dict[str(i)] == 0:
print(filename + " terminated early")
complete_cycle = False
if complete_cycle == True:
"""
change to range(runs) for complete looping
"""
for run in range(runs):
EWC = 'run {} lambda {}'.format(str(run + 1), str(float(f['params'][2])))
SGD = 'run {} lambda {}'.format(str(run + 1), str(0))
d[EWC] = np.zeros((task_counter_dict[str(run + 1)], task_counter_dict[str(run + 1)]))
d[SGD] = np.zeros((task_counter_dict[str(run + 1)], task_counter_dict[str(run + 1)]))
for count in range(task_counter_dict[str(run + 1)]):
acc_EWC = []
acc_SGD = []
for task in range(count + 1):
if count == 0:
acc_EWC = np.append(acc_EWC, f['count {} task {} lambda {} run {}'.format(str(count + 1), str(task + 1), str(0), str(run + 1))][len(f['count {} task {} lambda {} run {}'.format(str(count + 1), str(task + 1), str(0), str(run + 1))]) - 1])
acc_SGD = np.append(acc_SGD, f['count {} task {} lambda {} run {}'.format(str(count + 1), str(task + 1), str(0), str(run + 1))][len(f['count {} task {} lambda {} run {}'.format(str(count + 1), str(task + 1), str(0), str(run + 1))]) - 1])
else:
acc_EWC = np.append(acc_EWC, f['count {} task {} lambda {} run {}'.format(str(count + 1), str(task + 1), str(float(f['params'][2])), str(run + 1))][len(f['count {} task {} lambda {} run {}'.format(str(count + 1), str(task + 1), str(float(f['params'][2])), str(run + 1))]) - 1])
acc_SGD = np.append(acc_SGD, f['count {} task {} lambda {} run {}'.format(str(count + 1), str(task + 1), str(0), str(run + 1))][len(f['count {} task {} lambda {} run {}'.format(str(count + 1), str(task + 1), str(0), str(run + 1))]) - 1])
while len(acc_EWC) < task_counter_dict[str(run + 1)]:
acc_EWC = np.append(acc_EWC, 0)
while len(acc_SGD) < task_counter_dict[str(run + 1)]:
acc_SGD = np.append(acc_SGD, 0)
d[EWC][count] = acc_EWC
d[SGD][count] = acc_SGD
old_EWC = [0, d[EWC][0,0]]
latest_EWC = [0]
old_SGD = [0, d[SGD][0,0]]
latest_SGD = [0]
for row in range(1, len(d[EWC])):
old_acc_EWC_sum = 0
old_acc_SGD_sum = 0
for col in range(row):
old_acc_EWC_sum += d[EWC][row, col]
old_acc_SGD_sum += d[SGD][row, col]
old_EWC.append(old_acc_EWC_sum / float(row))
old_SGD.append(old_acc_SGD_sum / float(row))
for row in range(0, len(d[EWC])):
latest_EWC.append(d[EWC][row, row])
latest_SGD.append(d[SGD][row, row])
plt.figure(num=fignum[0], figsize=(20,10))
plot1 = plt.subplot(111)
plot1.set_title(filename + " run: " + str(run + 1))
plt.plot(old_EWC, label="EWC old", marker = '^')
plt.plot(latest_EWC, label="EWC latest", marker = 'o')
"""
plt.plot(old_SGD, label="SGD old", marker = 'D')
plt.plot(latest_SGD, label="SGD latest", marker = 's')
"""
plt.xlabel('tasks')
plt.ylabel('accuracy')
plt.axis([1, 25, 0.5, 0.95])
plt.legend(loc=3)
plt.savefig(filename + "_run_ " + str(run + 1) + ".png")
plt.close()
"""
plt.figure(num=fignum[0] + 1, figsize=(20, 10))
plot2 = plt.subplot(111)
plot2.set_title(filename + "Error Sum Terms run {}".format(str(run + 1)))
plt.plot(f['Error Sum Terms run {}'.format(str(run + 1))], marker = 'o')
plt.xlabel('tasks')
plt.ylabel('Summed Term in EWC Error')
plt.axis([1, 25, 0.0, 1])
plt.savefig(filename + "_error_sums_run_" + str(run + 1) + ".png")
fignum[0] += 2
plt.close()
"""
plt.figure(num=fignum[0] + 1, figsize=(20, 10))
plot2 = plt.subplot(111)
plot2.set_title(filename + "Penalty Terms run {}".format(str(run + 1)))
plt.plot(f['penalties run {}'.format(str(run + 1))], marker = 'o')
plt.xlabel('tasks')
plt.ylabel('EWC Penalty')
plt.axis([1, 25, 0.0, 2])
plt.savefig(filename + "_penalties_run_" + str(run + 1) + ".png")
fignum[0] += 2
plt.close()
"""
plot2.set_title(filename + "sum Fisher run {}".format(str(run + 1)))
plt.plot(f['fisher sum run {}'.format(str(run + 1))], marker = 'o')
plt.xlabel('tasks')
plt.ylabel('sum of average Fisher diagonal means')
plt.axis([1, 25, 0.0, 0.11])
plt.savefig(filename + "_fisher_sum_run_ " + str(run + 1) + ".png")
fignum[0] += 2
plt.close()
"""
"""
pdf_failure = []
for r in range(runs):
pdf_failure.append(task_counter_dict['{}'.format(str(r + 1))])
print(pdf_failure)
pdf_fisher = []
for r2 in range(runs):
pdf_fisher.append(f['fisher sum run {}'.format(str(r2 + 1))][len(f['fisher sum run {}'.format(str(r2 + 1))]) - 1])
print(pdf_fisher)
for index_fisher in range(len(pdf_fisher)):
if index_fisher == 'nan':
del pdf_fisher[index_fisher]
del pdf_failure[index_fisher]
fail_gauss = gaussian_kde(pdf_failure)
fisher_gauss = gaussian_kde(pdf_fisher)
dist_space_fail = np.linspace(min(pdf_failure), max(pdf_failure), 100)
dist_space_fisher = np.linspace(min(pdf_fisher), max(pdf_fisher), 100)
plt.figure(num=fignum[0], figsize=(20, 10))
fail = plt.subplot(111)
fail.set_title("PDF of Network Failure Task Count")
plt.plot(dist_space_fail, fail_gauss(dist_space_fail))
plt.xlabel("task count at failure")
plt.ylabel("probability density")
plt.savefig(filename + "failure_prob_dens.png")
plt.close()
plt.figure(num=fignum[0] + 1, figsize=(20, 10))
fisher = plt.subplot(111)
fisher.set_title("PDF of Fisher Sum Just Prior to Failure")
plt.plot(dist_space_fisher, fisher_gauss(dist_space_fisher))
plt.xlabel("fisher sum")
plt.ylabel("probability density")
plt.savefig(filename + "fisher_prob_dens.png")
plt.close()
fignum[0] += 2
"""
except IOError:
print(filename + " unable to be opened")
if __name__ == "__main__":
main()