-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpretabnet_census.py
545 lines (463 loc) · 20.6 KB
/
interpretabnet_census.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
from matplotlib import pyplot as plt
from pytorch_tabnet.tab_model import TabNetClassifier
import torch
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score, roc_auc_score
import pandas as pd
import numpy as np
import os
import wget
from pathlib import Path
import math
# CODE WITH INDIVIDUAL HYP TUNING
def main():
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data"
dataset = 'census-income'
dataset_name = 'census-income'
out = Path(os.getcwd() + '/data/' + dataset_name + '.csv')
out.parent.mkdir(parents=True, exist_ok=True)
if out.exists():
print("File already exists.")
else:
print("Downloading file...")
wget.download(url, out.as_posix())
train = pd.read_csv(out)
target = ' <=50K'
if "Set" not in train.columns:
train["Set"] = np.random.choice(["train", "valid", "test"], p=[.8, .1, .1], size=(train.shape[0],))
train_indices = train[train.Set == "train"].index
valid_indices = train[train.Set == "valid"].index
test_indices = train[train.Set == "test"].index
nunique = train.nunique()
types = train.dtypes
categorical_columns = []
categorical_dims = {}
for col in train.columns:
if types[col] == 'object' or nunique[col] < 200:
# print(col, train[col].nunique())
l_enc = LabelEncoder()
train[col] = train[col].fillna("VV_likely")
train[col] = l_enc.fit_transform(train[col].values)
categorical_columns.append(col)
categorical_dims[col] = len(l_enc.classes_)
else:
train.fillna(train.loc[train_indices, col].mean(), inplace=True)
train.loc[train[target] == 0, target] = "wealthy"
train.loc[train[target] == 1, target] = "not_wealthy"
unused_feat = ['Set']
features = [col for col in train.columns if col not in unused_feat + [target]]
cat_idxs = [i for i, f in enumerate(features) if f in categorical_columns]
cat_dims = [categorical_dims[f] for i, f in enumerate(features) if f in categorical_columns]
X_train = train[features].values[train_indices]
y_train = train[target].values[train_indices]
X_valid = train[features].values[valid_indices]
y_valid = train[target].values[valid_indices]
X_test = train[features].values[test_indices]
y_test = train[target].values[test_indices]
# TUNING HYPERPARAMETERS ###############################################################################################
# nd_na = [16, 32, 128]
# n_steps = [3, 4, 5]
# # gammas = [1.0, 1.2, 1.5, 2.0]
# lambda_sparses = [0.001, 0.01, 0.1, 0.3]
# learn_r = [0.005, 0.01, 0.02, 0.025]
# # reg_w = [0.001, 0.01, 0.05, 0.1]
# reg_m = [0.001, 0.01, 0.1, 0.3]
# reg_pq = [0.001, 0.01, 0.1, 0.3]
# opt_ndna = 32
# opt_nsteps = 3
# opt_gamma = 1.5
# opt_lambda = 0.001
# opt_lr = 0.025
# # opt_reg_w = 0
# opt_reg_m = 0
# opt_reg_pq = 0
# ndna_test_acc = 0
# for ndna in nd_na:
# clf = TabNetClassifier(
# n_d=ndna,
# n_a=ndna,
# n_steps=n_steps[0],
# gamma=gammas[0],
# lambda_sparse=lambda_sparses[0],
# cat_idxs=cat_idxs,
# cat_dims=cat_dims,
# optimizer_params=dict(lr=learn_r[0]),
# reg_m=reg_m[0],
# reg_pq=reg_pq[0],
# mask_type = 'relu'
# )
# clf.fit(
# X_train=X_train, y_train=y_train,
# eval_set=[(X_train, y_train), (X_valid, y_valid)],
# eval_name=['train', 'valid'], batch_size=256,
# virtual_batch_size=256,
# max_epochs=10, eval_metric=['accuracy']
# )
# y_pred = clf.predict(X_test)
# test_acc = accuracy_score(y_pred=y_pred, y_true=y_test)
# if test_acc > ndna_test_acc:
# opt_ndna = ndna
# ndna_test_acc = test_acc
# print("Optimum Hyperparameters Training", [opt_ndna, opt_nsteps, opt_gamma, opt_lambda, opt_lr, opt_reg_m, opt_reg_pq])
# nstep_test_acc = 0
# for nstep in n_steps:
# clf = TabNetClassifier(
# n_d=opt_ndna,
# n_a=opt_ndna,
# n_steps=nstep,
# gamma=gammas[0],
# lambda_sparse=lambda_sparses[0],
# cat_idxs=cat_idxs,
# cat_dims=cat_dims,
# optimizer_params=dict(lr=learn_r[0]),
# reg_m=reg_m[0],
# reg_pq=reg_pq[0],
# mask_type = 'relu'
# )
# clf.fit(
# X_train=X_train, y_train=y_train,
# eval_set=[(X_train, y_train), (X_valid, y_valid)],
# eval_name=['train', 'valid'], batch_size=256,
# virtual_batch_size=256,
# max_epochs=10, eval_metric=['accuracy']
# )
# y_pred = clf.predict(X_test)
# test_acc = accuracy_score(y_pred=y_pred, y_true=y_test)
# if test_acc > nstep_test_acc:
# opt_nsteps = nstep
# nstep_test_acc = test_acc
# print("Optimum Hyperparameters Training", [opt_ndna, opt_nsteps, opt_gamma, opt_lambda, opt_lr, opt_reg_m, opt_reg_pq])
# gams_test_acc = 0
# for gams in gammas:
# clf = TabNetClassifier(
# n_d=opt_ndna,
# n_a=opt_ndna,
# n_steps=opt_nsteps,
# gamma=gams,
# lambda_sparse=lambda_sparses[0],
# cat_idxs=cat_idxs,
# cat_dims=cat_dims,
# optimizer_params=dict(lr=learn_r[0]),
# reg_m=reg_m[0],
# reg_pq=reg_pq[0],
# mask_type = 'relu'
# )
# clf.fit(
# X_train=X_train, y_train=y_train,
# eval_set=[(X_train, y_train), (X_valid, y_valid)],
# eval_name=['train', 'valid'], batch_size=256,
# virtual_batch_size=256,
# max_epochs=10, eval_metric=['accuracy']
# )
# y_pred = clf.predict(X_test)
# test_acc = accuracy_score(y_pred=y_pred, y_true=y_test)
# if test_acc > gams_test_acc:
# opt_gamma = gams
# gams_test_acc = test_acc
# print("Optimum Hyperparameters Training", [opt_ndna, opt_nsteps, opt_gamma, opt_lambda, opt_lr, opt_reg_m, opt_reg_pq])
# lamb_test_acc = 0
# for lambs in lambda_sparses:
# clf = TabNetClassifier(
# n_d=opt_ndna,
# n_a=opt_ndna,
# n_steps=opt_nsteps,
# gamma=opt_gamma,
# lambda_sparse=lambs,
# cat_idxs=cat_idxs,
# cat_dims=cat_dims,
# optimizer_params=dict(lr=learn_r[0]),
# reg_m=reg_m[0],
# reg_pq=reg_pq[0],
# mask_type = 'relu'
# )
# clf.fit(
# X_train=X_train, y_train=y_train,
# eval_set=[(X_train, y_train), (X_valid, y_valid)],
# eval_name=['train', 'valid'], batch_size=256,
# virtual_batch_size=256,
# max_epochs=10, eval_metric=['accuracy']
# )
# y_pred = clf.predict(X_test)
# test_acc = accuracy_score(y_pred=y_pred, y_true=y_test)
# if test_acc > lamb_test_acc:
# opt_lambda = lambs
# lamb_test_acc = test_acc
# print("Optimum Hyperparameters Training", [opt_ndna, opt_nsteps, opt_gamma, opt_lambda, opt_lr, opt_reg_m, opt_reg_pq])
# lr_test_accuracy = 0
# for lr in learn_r:
# clf = TabNetClassifier(
# n_d=opt_ndna,
# n_a=opt_ndna,
# n_steps=opt_nsteps,
# gamma=opt_gamma,
# lambda_sparse=opt_lambda,
# cat_idxs=cat_idxs,
# cat_dims=cat_dims,
# optimizer_params=dict(lr=lr),
# reg_m=reg_m[0],
# reg_pq=reg_pq[0],
# mask_type = 'relu'
# )
# clf.fit(
# X_train=X_train, y_train=y_train,
# eval_set=[(X_train, y_train), (X_valid, y_valid)],
# eval_name=['train', 'valid'], batch_size=256,
# virtual_batch_size=256,
# max_epochs=10, eval_metric=['accuracy']
# )
# y_pred = clf.predict(X_test)
# test_acc = accuracy_score(y_pred=y_pred, y_true=y_test)
# if test_acc > lr_test_accuracy:
# opt_lr = lr
# lr_test_accuracy = test_acc
# print("Optimum Hyperparameters Training", [opt_ndna, opt_nsteps, opt_gamma, opt_lambda, opt_lr, opt_reg_m, opt_reg_pq])
# reg_m_test_accuracy = 0
# for r_m in reg_m:
# clf = TabNetClassifier(
# n_d=opt_ndna,
# n_a=opt_ndna,
# n_steps=opt_nsteps,
# gamma=opt_gamma,
# lambda_sparse=opt_lambda,
# cat_idxs=cat_idxs,
# cat_dims=cat_dims,
# optimizer_params=dict(lr=opt_lr),
# reg_m=r_m,
# reg_pq=reg_pq[0],
# mask_type = 'relu'
# )
# clf.fit(
# X_train=X_train, y_train=y_train,
# eval_set=[(X_train, y_train), (X_valid, y_valid)],
# eval_name=['train', 'valid'], batch_size=256,
# virtual_batch_size=256,
# max_epochs=10, eval_metric=['accuracy']
# )
# y_pred = clf.predict(X_test)
# test_acc = accuracy_score(y_pred=y_pred, y_true=y_test)
# if test_acc > reg_m_test_accuracy:
# opt_reg_m = r_m
# reg_m_test_accuracy = test_acc
# print("Optimum Hyperparameters Training", [opt_ndna, opt_nsteps, opt_gamma, opt_lambda, opt_lr, opt_reg_m, opt_reg_pq])
# reg_pq_test_accuracy = 0
# for r_pq in reg_pq:
# clf = TabNetClassifier(
# n_d=opt_ndna,
# n_a=opt_ndna,
# n_steps=opt_nsteps,
# gamma=opt_gamma,
# lambda_sparse=opt_lambda,
# cat_idxs=cat_idxs,
# cat_dims=cat_dims,
# optimizer_params=dict(lr=opt_lr),
# reg_m=opt_reg_m,
# reg_pq=r_pq,
# mask_type = 'relu'
# )
# clf.fit(
# X_train=X_train, y_train=y_train,
# eval_set=[(X_train, y_train), (X_valid, y_valid)],
# eval_name=['train', 'valid'], batch_size=256,
# virtual_batch_size=256,
# max_epochs=10, eval_metric=['accuracy']
# )
# y_pred = clf.predict(X_test)
# test_acc = accuracy_score(y_pred=y_pred, y_true=y_test)
# if test_acc > reg_pq_test_accuracy:
# opt_reg_pq = r_pq
# reg_pq_test_accuracy = test_acc
# print("Optimum Hyperparameters", [opt_ndna, opt_nsteps, opt_gamma, opt_lambda, opt_lr, opt_reg_m, opt_reg_pq])
# Tuning for Mask #######################################################################################################################
# mask_count = 1
# for l in lambda_sparses:
# for m in reg_m:
# for pq in reg_pq:
# clf = TabNetClassifier(
# n_d=32,
# n_a=32,
# n_steps=3,
# gamma=1.0,
# lambda_sparse=l,
# cat_idxs=cat_idxs,
# cat_dims=cat_dims,
# optimizer_params=dict(lr=0.025),
# reg_m = m,
# reg_pq = pq,
# mask_type = 'relu'
# )
# # max epoch 50
# clf.fit(
# X_train=X_train, y_train=y_train,
# eval_set=[(X_train, y_train), (X_valid, y_valid)],
# eval_name=['train', 'valid'], batch_size=256,
# virtual_batch_size=256,
# max_epochs=16, eval_metric=['accuracy']
# )
# y_pred = clf.predict(X_test)
# test_acc = accuracy_score(y_pred=y_pred, y_true=y_test)
# print(f"Mask : {mask_count}, Hyperparameters : {[l, m, pq]}, Accuracy : {test_acc}")
# # print(f"FINAL TEST SCORE FOR {dataset_name} : {test_acc}")
# explain_matrix, masks = clf.explain(X_test)
# fig, axs = plt.subplots(1, 3, figsize=(20, 20))
# for i in range(3):
# axs[i].imshow(masks[i][:50])
# axs[i].set_title(f"mask {i}")
# axs[i].set_ylabel("Test Samples")
# axs[i].set_xlabel("Features")
# # ticks = np.arange(1, 15)
# # labels = ["age", "workclass", "fnlwgt (the number of people the census believes the entry represents)",
# # "education", "education-num", "marital-status", "occupation", "relationship", "race",
# # "sex", "capital-gain", "capital-loss", "hours-per-week", "native-country"]
# # axs[i].set_xticks(ticks)
# # axs[i].set_xticklabels(labels)
# # plt.show()
# plt.savefig(f"Mask : {mask_count}, Hyperparameters : {[l, m, pq]}, Accuracy : {test_acc} cVAE.png")
# mask_count += 1
# Optimized Run #######################################################################################################################
# Finished tuning: Optimum Hyperparameters Training [32, 4, 1.2, 0.001, 0.005, 256, 256]
n_steps = 4
opt_ndna = 32
opt_gamma = 1.0
opt_lambda = 1.0
opt_lr = 0.025
def search_best_reg_m(start=0, end=1000000000, col_threshold_val=0.20, col_threshold=3, all_mask_pass=None, all_mask_pass_thresh=3, step_size=None, best_reg_m=None, reg_m_acc_dict=None, is_recursive=False):
if reg_m_acc_dict is None:
reg_m_acc_dict = {}
if all_mask_pass == all_mask_pass_thresh:
print(reg_m_acc_dict)
final_reg_m = max(reg_m_acc_dict, key=reg_m_acc_dict.get)
return final_reg_m
if all_mask_pass is None:
all_mask_pass = 0
# Fine-tuning around the best found value
best_reg_m = None
break_outer_loop = False
# Determining Magnitude for reg_m
diff = end - start
magnitude = int(math.log10(diff))
reg_m = start
while reg_m <= end and all_mask_pass < all_mask_pass_thresh: #do i need all_mask_pass threshold here?
print("reg_m", reg_m)
if reg_m in reg_m_acc_dict:
reg_m += step_size
continue
clf = TabNetClassifier(
n_d=opt_ndna,
n_a=opt_ndna,
n_steps=4,
gamma=opt_gamma,
lambda_sparse=opt_lambda,
cat_idxs=cat_idxs,
cat_dims=cat_dims,
optimizer_params=dict(lr=opt_lr),
mask_type = 'softmax',
reg_m=reg_m
)
# max epoch 50
clf.fit(
X_train=X_train, y_train=y_train,
eval_set=[(X_train, y_train), (X_valid, y_valid)],
eval_name=['train', 'valid'],
max_epochs=30, eval_metric=['accuracy']
)
y_pred = clf.predict(X_test)
test_acc = accuracy_score(y_pred=y_pred, y_true=y_test)
print(f"FINAL TEST SCORE FOR {dataset} : {test_acc}")
explain_matrix, masks = clf.explain(X_test)
# Extract the first 50 samples from each matrix
masks_dict = {}
for key, value in masks.items():
masks_dict[key] = value[:50]
# Normalize each extracted matrix so that its sum is 1
for key, value in masks_dict.items():
total_sum = value.sum()
# Avoid division by zero
if total_sum == 0:
continue
masks_dict[key] = value / total_sum
mask_threshold = n_steps // 2 + 1
mask_pass_count = 0
for key, value in masks_dict.items():
column_sums = value.sum(axis=0)
# print(f"Sum of columns for matrix with key {key}: {column_sums}")
# Check which columns are greater than col_threshold_val
cols_above_threshold = [i for i, col_sum in enumerate(column_sums) if col_sum > col_threshold_val]
print(f"Columns in matrix with key {key} that are greater than the threshold value: {cols_above_threshold}")
if col_threshold-1 <= len(cols_above_threshold) <= col_threshold+1:
mask_pass_count += 1
print("Num Mask Pass Threshold:", mask_pass_count)
if mask_pass_count >= mask_threshold:
if len(reg_m_acc_dict) == 0:
all_mask_pass += 1
best_reg_m = reg_m
reg_m_acc_dict[reg_m] = test_acc
break_outer_loop = True
break
elif test_acc > max(reg_m_acc_dict.values()):
all_mask_pass += 1
reg_m_acc_dict[reg_m] = test_acc
best_reg_m = reg_m
break
else:
print("Lesser Acc, Break")
break
if break_outer_loop:
break
if is_recursive:
reg_m += step_size
elif reg_m == 0:
reg_m = 10
else:
reg_m *= 10
# Check conditions after looping over all possible reg_m values
if best_reg_m is not None and len(reg_m_acc_dict) == 1: # i need to add the condition where i hit all mask pass and return the funct directly
print('Breaked')
magnitude = math.floor(math.log10(best_reg_m))
if magnitude >= 1:
step_size = 10**(magnitude-1)
else:
step_size = 10**(magnitude)
# Recursively refine the search with updated boundaries and reduced depth
new_start = int(max(start, best_reg_m - step_size))
new_end = int(min(end, best_reg_m + step_size))
return search_best_reg_m(new_start, new_end, col_threshold, col_threshold_val, all_mask_pass, all_mask_pass_thresh, step_size, best_reg_m, reg_m_acc_dict, is_recursive=True)
elif len(reg_m_acc_dict)==0:
return "Did not pass! Lower threshold!"
else:
final_reg_m = max(reg_m_acc_dict, key=reg_m_acc_dict.get)
return final_reg_m
opt_reg_m = search_best_reg_m()
print("opt_reg_m for best mask", opt_reg_m)
clf = TabNetClassifier(
n_d=opt_ndna,
n_a=opt_ndna,
n_steps=4,
gamma=opt_gamma,
lambda_sparse=opt_lambda,
cat_idxs=cat_idxs,
cat_dims=cat_dims,
optimizer_params=dict(lr=opt_lr),
mask_type = 'softmax',
reg_m=opt_reg_m
)
# max epoch 50
clf.fit(
X_train=X_train, y_train=y_train,
eval_set=[(X_train, y_train), (X_valid, y_valid)],
eval_name=['train', 'valid'],
max_epochs=100, eval_metric=['accuracy']
)
y_pred = clf.predict(X_test)
test_acc = accuracy_score(y_pred=y_pred, y_true=y_test)
print(f"FINAL TEST SCORE FOR {dataset} : {test_acc}")
explain_matrix, masks = clf.explain(X_test)
fig, axs = plt.subplots(1, n_steps, figsize=(20, 20))
for i in range(n_steps):
axs[i].imshow(masks[i][:50])
axs[i].set_title(f"mask {i}")
axs[i].set_ylabel("Test Samples")
axs[i].set_xlabel("Features")
plt.savefig(f"{dataset}_feature_mask_kld_{opt_reg_m}_accuracy_{test_acc}.png")
if __name__ == "__main__":
np.random.seed(0)
main()