-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparison.py
207 lines (159 loc) · 6.32 KB
/
comparison.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
# os.environ['OPENBLAS_NUM_THREADS'] = '1'
# os.environ['MKL_NUM_THREADS'] = '1'
from multiprocessing.pool import Pool
import pickle
import time
import h5py
import numpy as np
from .util import get_digits
from .gibbs import BayesMixture, BernoulliMixtureEM
from .metrics import average_of_estimates, find_clusters, calculate_auroc
def threaded_estimates(data):
X_train = data[0]
K = data[1]
start_time = time.time()
mixture = BayesMixture(X_train, K=K, alpha=50, beta=0.5, delta=0.8, seed=int(time.time()))
z_new, a_k, gamma, mixing_props = mixture.gibbs_sample(max_iterations=30, hard_cluster=False, verbose=False)
duration = time.time() - start_time
return a_k[:, :, -1], duration
def threaded_estimates_EM(data):
X_train = data[0]
K = data[1]
start_time = time.time()
mixture = BernoulliMixtureEM(K=K, D=784, X=X_train)
a_k, pi = mixture.train(max_iterations=80, verbose=False, display_interval=50, save_interval=None)
duration = time.time() - start_time
return a_k[:, :, -1], duration
def get_data(X_train, n=1100, K=8):
for i in range(n):
yield (X_train, K)
def collect_estimates():
X_train, y_train, X_test, y_test = get_digits(0.5)
cores = 6
pool = Pool(processes=cores)
for k in [10]:
for digit_estimated in [3, 4, 5, 6, 7, 8, 9]:
condition = (y_train == digit_estimated)
selected_digits = X_train[np.where(condition)]
selected_labels = y_train[np.where(condition)]
h5 = h5py.File(f"./datasets/{digit_estimated}_estimates_{k}.h5", "a")
remaining_digits = 250 - len(h5.keys())
durations = []
idx = 0
for estimate, duration in pool.imap_unordered(threaded_estimates,
get_data(selected_digits, n=remaining_digits, K=k)):
h5.create_dataset(f"{idx}{time.time()}", data=estimate)
durations.append(duration)
if idx % 20 == 0:
print(
f"{digit_estimated} - Done with {idx}/{remaining_digits}. Avg {np.mean(durations) / cores:.3f}s per estimate.")
idx += 1
h5.close()
def collect_estimates_histo():
X_train, y_train, X_test, y_test = get_digits(0.5)
bayes_ests = []
em_ests = []
for i in range(1000):
condition = (y_train == 3)
selected_digits = X_train[np.where(condition)]
mixture = BayesMixture(selected_digits, K=20, alpha=50, beta=0.5, delta=0.8, seed=int(time.time()))
z_new, a_k, gamma, mixing_props, best_ak = mixture.gibbs_sample(max_iterations=50, hard_cluster=False,
verbose=False)
bayes_ests.append({"best_ak": best_ak,
"all_ak": a_k,
"all_z": z_new})
if i % 10 == 0:
print(f"Done {i} out of 1000 for Bayes")
if i % 100 == 0:
with open(f"D:\\bayes_ests_{time.time()}.bin", "wb") as f:
pickle.dump(bayes_ests, f)
exit()
print("Done histo")
def collect_estimates_k():
X_train, y_train, X_test, y_test = get_digits(0.5)
bayes_ests = {}
with open(f"ests_k_1565828825.666521.bin", "rb") as f:
bayes_ests = pickle.load(f)
for digit in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
print(f"Doing digit {digit}")
if digit not in bayes_ests:
bayes_ests[digit] = {}
for k in [10, 20, 30, 50]:
print(f"Doing k= {k}")
if k in bayes_ests[digit]:
continue
condition = (y_train == digit)
selected_digits = X_train[np.where(condition)]
local_est = []
for est in range(10):
mixture = BayesMixture(selected_digits, K=20, alpha=50, beta=0.5, delta=0.8, seed=int(time.time()))
z_new, a_k, gamma, mixing_props = mixture.gibbs_sample(max_iterations=50, hard_cluster=False,
verbose=False)
local_est.append(a_k[:, :, -1])
bayes_ests[digit][k] = average_of_estimates(local_est, 10)
with open(f"ests_k_{time.time()}.bin", "wb") as f:
pickle.dump(bayes_ests, f)
print(f"Done digit {digit}, K={k}")
def collect_estimates_EM():
X_train, y_train, X_test, y_test = get_digits(0.5)
h5 = h5py.File(f"./datasets/EM_estimates_{10}.h5", "a")
for digit in [0, 1, 2, 3]:
condition = (y_train == digit)
selected_digits = X_train[np.where(condition)]
mixture = BernoulliMixtureEM(K=10, D=784, X=selected_digits)
a_k, pi = mixture.train(max_iterations=100, display_interval=20, save_interval=None, verbose=False)
h5.create_dataset(f"{digit}", data=a_k.T)
h5.close()
def save_auroc(mode="bayes"):
X_train, y_train, X_test, y_test = get_digits(0.5)
cores = 4
pool = Pool(processes=cores)
estimates = {}
for k in [20, 30]:
estimates[k] = {}
for digit_estimated in [0, 1, 2, 3]:
condition = (y_train == digit_estimated)
selected_digits = X_train[np.where(condition)]
durations = []
local_estimates = []
if mode == "bayes":
est_fun = threaded_estimates
else:
est_fun = threaded_estimates_EM
for estimate, duration in pool.imap_unordered(est_fun, get_data(selected_digits, n=10, K=k)):
local_estimates.append(estimate)
estimates[k][digit_estimated] = average_of_estimates(local_estimates, 10)
print(f"Done k={k}, digit #{digit_estimated}")
with open(f"{mode}_auroc_allk.bin", "wb") as f:
pickle.dump(estimates, f)
def simple_compare():
X_train, y_train, X_test, y_test = get_digits(0.5)
with open(f"bayes_auroc_allk.bin", "rb") as f:
bayes_data = pickle.load(f)
for k in [20, 30]:
for digit_estimated in [0, 1, 2, 3]:
avg_est = bayes_data[k][digit_estimated][0]
predictions = find_clusters(avg_est, X_test, partial=True)
auroc = calculate_auroc(avg_est, X_test, predictions)
print(f"#{digit_estimated}:K={k}: {auroc:.4f}")
def final_compare():
X_train, y_train, X_test, y_test = get_digits(0.5)
with h5py.File(f"./datasets/EM_estimates_10.h5", "r") as h5:
bernoulli_estimates = {key: h5[key][()] for key in h5.keys()}
for digit in [0, 1, 2, 3]:
print(f"Doing digit {digit}")
with h5py.File(f"./datasets/{digit}_estimates_10.h5", "r") as h5:
bayes_estimates = [h5[key][()] for key in h5.keys()]
avg_bayes_set = average_of_estimates(bayes_estimates, size=10)[0:10]
bayes_auroc = []
for idx, estimate in enumerate(avg_bayes_set):
predictions = find_clusters(estimate, X_test, partial=True)
auroc = calculate_auroc(estimate, X_test, predictions)
bayes_auroc.append(auroc)
print("Done calculating Bayes AUROC")
predictions = find_clusters(bernoulli_estimates[f"{digit}"].T, X_test, partial=True)
auroc = calculate_auroc(bernoulli_estimates[f"{digit}"].T, X_test, predictions)
print(f"Bayes best estimate: {np.min(bayes_auroc):.4f}")
print(f"EM estimate: {auroc:.4f}")
if __name__ == "__main__":
collect_estimates_histo()