forked from AhmedHossam23/ml-assisted-binary-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybrid.py
323 lines (242 loc) · 9.36 KB
/
hybrid.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import os
import numpy as np
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from tqdm import tqdm
import pandas as pd
def add_uniform_noise(probas, noise_level=0.1):
"""
Adds random uniform noise to a list of probabilities.
Parameters:
probas (list of float): List of probabilities to add noise to.
noise_level (float): Maximum amount of noise to add. Noise will be in the range [-noise_level, noise_level].
Returns:
list of float: Probabilities with added noise.
"""
noise = np.random.uniform(-noise_level, noise_level, len(probas))
probas += noise
probas = (probas - min(probas)) / (max(probas) - min(probas))
return probas
def get_value(y, index):
x = y[index]
return x
# loss calculation
def wBCE_loss(y, y_hat):
# Calculate the lambda value
N_bad = np.sum(y)
N = len(y)
N_good = N - N_bad
lam = N_good / N
# Ensure y_hat values are in (0, 1)
epsilon = 1e-8
y_hat = np.clip(y_hat, epsilon, 1 - epsilon)
# Calculate the weighted BCE loss
wBCE_loss = (-2 / N) * np.sum(
lam * y * np.log(y_hat) + (1 - lam) * (1 - y) * np.log(1 - y_hat)
)
return wBCE_loss
def diffs2(probas):
probas = np.array(probas)
left_sums = np.cumsum(probas)[:-1]
total_sum = np.sum(probas)
right_sums = total_sum - left_sums
differences = np.abs(left_sums - right_sums)
return differences.tolist()
# Binary search algorithm
def binary_search(commits):
num_of_runs = 0
len_commits = len(commits)
low, high = 0, len_commits - 1
state_of_first = get_value(commits, 0)
state_of_last = get_value(commits, len_commits - 1)
if state_of_first == 0 and state_of_last == 0:
return -1, num_of_runs
if CONFIG["print_detailed_search"]:
print(f"low: {low}, high: {high}")
prev_mid = -1
while low < high:
mid = (low + high) // 2
num_of_runs += 1
if commits[mid] == 1:
high = mid
elif commits[mid] == 0:
low = mid + 1
if CONFIG["print_detailed_search"]:
print(f"runs: {num_of_runs}, low: {low}, mid: {prev_mid}, high: {high}")
prev_mid = mid
return num_of_runs, high
# Hybrid search algorithm
def hybrid_search(commits, probabilities, alpha):
num_of_runs = 0
len_commits = len(commits)
low, high = 0, len_commits - 1
state_of_first = get_value(commits, 0)
state_of_last = get_value(commits, len_commits - 1)
if state_of_first == 0 and state_of_last == 0:
return -1, num_of_runs
if alpha == 0:
return binary_search(commits)
while low < high:
binary_mid = (low + high) // 2
diffs_arr = diffs2(probabilities[low : high + 1])
prob_mid = np.argmin(diffs_arr) + low
new_mid = round(((alpha * prob_mid) + ((1 - alpha) * binary_mid)))
num_of_runs += 1
if commits[new_mid] == 1:
high = new_mid
elif commits[new_mid] == 0:
low = new_mid + 1
if CONFIG["print_detailed_search"]:
print(f"runs: {num_of_runs}, low: {low}, high: {high}")
return num_of_runs, high
# data generation
def get_preds_per_depth(samples, features, n_informative, min_max_depth, max_max_depth):
samples = CONFIG["samples"]
features = CONFIG["features"]
min_max_depth = CONFIG["min_max_depth"]
max_max_depth = CONFIG["max_max_depth"]
n_informative = CONFIG["n_informative"]
name = f"data__samples_{samples}__features_{features}__n_informative_{n_informative}__min_max_depth_{min_max_depth}__max_max_depth_{max_max_depth}.pkl"
# check if the data is already generated
try:
data = pd.read_pickle(os.path.join("data", name))
print("you saved the time of data regeneration")
return data
except:
pass
X, y = make_classification(
n_samples=samples,
n_features=features,
n_redundant=10,
n_informative=n_informative,
n_clusters_per_class=1,
n_classes=2,
weights=[0.997, 0.003],
flip_y=0.01, # rolling back to default
random_state=42,
)
data = {}
for max_depth in tqdm(
range(CONFIG["min_max_depth"], CONFIG["max_max_depth"] + 1), desc="Max Depth Progress", colour="green"
):
model = RandomForestClassifier(
n_estimators=100,
max_depth=max_depth,
min_samples_split=2,
min_samples_leaf=1,
random_state=42,
n_jobs=-1,
)
model.fit(X, y)
probas = model.predict_proba(X)[:, 1]
data[max_depth] = {"preds": probas, "commits": y}
pd.DataFrame(data).to_pickle(os.path.join("data", name))
return data
def transform_data(y, predictions):
min_chunk_size = 2 ** (CONFIG["min_log_size"] - 1) + 1
max_chunk_size = 2 ** CONFIG["max_log_size"]
print(f"min_chunk_size: {min_chunk_size}, max_chunk_size: {max_chunk_size}")
index = 0
chunks = []
while True:
size = np.random.randint(min_chunk_size, max_chunk_size)
if index + size >= len(y):
break
y_chunk = y[index : index + size]
preds_chunk = predictions[index : index + size]
if sum(y_chunk) == 0 or y_chunk[0] == 1 :
# Do not append!
index += size
elif sum(y_chunk) == 1:
# Safely append
index += size
chunks.append({"y_chunk": y_chunk, "preds_chunk": preds_chunk})
else:
one_indices = [i for i, x in enumerate(y_chunk) if x == 1]
# Make n_bad deep copies of the chunk
y_copied_chunks = []
pred_copied_chunks = []
for one_index in one_indices:
curr_chunk = y_chunk.copy()
# set the current one_index to 1, and the rest to 0
curr_chunk[:one_index] = 0
curr_chunk[one_index + 1 :] = 0
y_copied_chunks.append(curr_chunk)
pred_copied_chunks.append(preds_chunk)
# Append the copied chunks
for y_copied_chunk, pred_copied_chunk in zip(
y_copied_chunks, pred_copied_chunks
):
chunks.append(
{"y_chunk": y_copied_chunk, "preds_chunk": pred_copied_chunk}
)
index += size
for chunk in chunks:
y_chunk = chunk["y_chunk"]
preds_chunk = chunk["preds_chunk"]
# Find the index of the single 1 in the chunk
y_transformed = y_chunk.copy()
y_transformed[np.argmax(y_chunk) + 1 :] = 1
chunk["y_transformed"] = y_transformed
assert len(y_chunk) == len(preds_chunk)
assert sum(y_chunk) == 1
return chunks
def main():
samples, features, n_informative, min_max_depth, max_max_depth = (
CONFIG["samples"],
CONFIG["features"],
CONFIG["n_informative"],
CONFIG["min_max_depth"],
CONFIG["max_max_depth"],
)
data = get_preds_per_depth(
samples, features, n_informative, min_max_depth, max_max_depth)
for _, data_entries in data.items():
preds = data_entries["preds"]
commits = data_entries["commits"]
chunks = transform_data(commits, preds)
results = []
for alpha in np.arange(0, 1.05, 0.05): # avoid floating point errors
for max_depth, preds_and_commits in data.items():
preds = preds_and_commits["preds"]
commits = preds_and_commits["commits"]
chunks = transform_data(commits, preds)
scores_mean = []
saved_runs_mean = []
for chunk in chunks:
y_transformed = chunk["y_transformed"]
# binary runs can be calculated only using the length of the array
binary_runs, binary_index = binary_search(y_transformed)
# binary_runs = np.ceil(np.log(len(y_transformed)))
print("---"*20)
print(f"alpha = {alpha}")
print("---"*20)
# no need to add noise to the predictions
hybrid_runs, hybrid_index = hybrid_search(y_transformed, chunk["preds_chunk"], alpha)
# check index similarity
assert binary_index == hybrid_index
score = wBCE_loss(chunk["y_chunk"], chunk["preds_chunk"])
saved_runs_mean.append((binary_runs - hybrid_runs) / (binary_runs - 2))
scores_mean.append(score)
if CONFIG["print_detailed_search"]:
print(f"Size: {np.ceil(np.log(len(y_transformed)))}")
print(f"saved runs: {(binary_runs - hybrid_runs)/(binary_runs - 2)}")
print("*" * 100 + "\n\n")
results.append({"alpha": alpha,
"saved_runs": np.mean(saved_runs_mean),
"loss": np.mean(scores_mean),})
print('-'*60)
print(f"Max Depth: {max_depth}")
print('-'*60)
pd.DataFrame(results).to_pickle("hybrid_vfinal.pkl")
CONFIG = {
"samples": 100000,
"features": 100,
"n_informative": 40, # changed from 90 to 40 as it gives the most stable results
"min_max_depth": 1,
"max_max_depth": 40,
"min_log_size": 8,
"max_log_size": 10,
"print_detailed_search": True,
}
main()