-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_experiment.py
370 lines (301 loc) · 13.6 KB
/
run_experiment.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
from Dataset import Dataset
from Model import Model
from Experiment import Experiment
from itertools import product
from configs.hyperparameters import get_hyperparameters
from configs.constants import SPLIT_DEFAULT, SEEDS_DEFAULT, CALIB_FRACS_DEFAULT
from configs.constants import SEEDS_REDUCED, CALIB_FRACS_REDUCED
from configs.constants import MCB_DEFAULT
# helper function for obtaining save directory names
def _save_dir(dataset, model, calib_frac, val_split_seed):
return 'models/saved_models/{0}/{1}/calib={2}_val_seed={3}/'.format(
dataset, model, calib_frac, val_split_seed
)
### Experiment primitives ###
def SimpleModel_train_eval(model_name, dataset, calib_fracs, seeds=SEEDS_DEFAULT, wdb_project_name=None, groups_collection='default', wandb=True):
'''
Train and evaulate a simple model on several mcb algorithms.
'''
if wdb_project_name is None:
wdb_project = f'{dataset}_{model_name}_eval'
else:
wdb_project = wdb_project_name
for cf in calib_fracs:
for seed in seeds:
hp = get_hyperparameters(model_name, dataset, cf)
config = {
'model': model_name,
'dataset': dataset,
'groups_collection': groups_collection,
'calib_frac': cf,
'val_split_seed': seed,
'split': SPLIT_DEFAULT,
'mcb': MCB_DEFAULT,
'save_dir': _save_dir(dataset, model_name, cf, seed),
**hp
}
scale_data = True if 'scale_data' in hp and hp['scale_data'] else False
dataset_obj = Dataset(dataset, val_split_seed=config['val_split_seed'],
scale=scale_data, groups=groups_collection)
# init model
model = Model(model_name, config=config, SAVE_DIR=config['save_dir'])
experiment = Experiment(dataset_obj, model, calib_frac=config['calib_frac'])
# init logger
if wandb: experiment.init_logger(config, project=wdb_project)
# train and postprocess
experiment.train_model()
if config['calib_frac'] > 0:
experiment.multicalibrate_multiple(config['mcb'])
# evaluate
experiment.evaluate_val()
experiment.evaluate_test()
# close logger
if wandb: experiment.init_logger(finish=True)
def NN_pretrain(model_name, dataset, calib_fracs, seeds, wandb=True):
'''
Pretrain model, and evaluate on validation / test sets.
No multicalibration post-processing.
'''
wdb_project = f'{dataset}_{model_name}_eval_pretrain'
for cf in calib_fracs:
hp = get_hyperparameters(model_name, dataset, cf)
for seed in seeds:
config = {
# data
'dataset': dataset,
'val_split_seed': seed,
'split': SPLIT_DEFAULT,
'calib_frac': cf,
# NN
'model': model_name,
'save_dir': _save_dir(dataset, model_name, cf, seed),
# evaluation
'val_save_epoch': 0,
'val_eval_epoch': 1,
# mcb
'mcb': [],
# hyperparameters
**hp
}
dataset_obj = Dataset(dataset, val_split_seed=config['val_split_seed'])
# init model
model = Model(model_name, config=config, SAVE_DIR=config['save_dir'], dataset_obj=dataset_obj)
experiment = Experiment(dataset_obj, model, calib_frac=config['calib_frac'])
# init logger
if wandb: experiment.init_logger(config, project=wdb_project)
# train and postprocess
experiment.train_model()
if config['calib_frac'] > 0:
experiment.multicalibrate_multiple(config['mcb'])
# evaluate
experiment.evaluate_val()
experiment.evaluate_test()
# close logger
if wandb: experiment.init_logger(finish=True)
def NN_train_eval(model_name, dataset, calib_fracs, seeds=SEEDS_DEFAULT, wdb_project_name=None, groups_collection='default', wandb=True):
'''
Train and evaulate model on validation and test sets.
Use multicalibration post-processing.
'''
if wdb_project_name is None:
wdb_project = f'{dataset}_{model_name}_eval'
else:
wdb_project = wdb_project_name
for cf in calib_fracs:
hp = get_hyperparameters(model_name, dataset, cf)
for seed in seeds:
config = {
# data
'dataset': dataset,
'groups_collection': 'default',
'val_split_seed': seed,
'split': SPLIT_DEFAULT,
'calib_frac': cf,
# NN
'model': model_name,
'save_dir': _save_dir(dataset, model_name, cf, seed),
# evaluation
'val_save_epoch': 0,
'val_eval_epoch': 1,
# mcb
'mcb': MCB_DEFAULT,
# hyperparameters
**hp
}
dataset_obj = Dataset(dataset, val_split_seed=config['val_split_seed'], groups=groups_collection)
# init model
model = Model(model_name, config=config, dataset_obj=dataset_obj, SAVE_DIR=config['save_dir'])
experiment = Experiment(dataset_obj, model, calib_frac=config['calib_frac'])
# init logger
run_name = f'cf={cf}_seed={seed}_epoch={hp["epochs"]-1}'
if wandb: experiment.init_logger(config, project=wdb_project, run_name=run_name)
# train and postprocess
experiment.train_model()
if config['calib_frac'] > 0:
experiment.multicalibrate_multiple(config['mcb'])
# evaluate
experiment.evaluate_val()
experiment.evaluate_test()
# close logger
if wandb: experiment.init_logger(finish=True)
def NN_eval(model_name, dataset, calib_fracs, seeds, eval_epochs=None, no_mcb=False, wandb=True):
'''
No training. Evaluate a pretrained model with several mcb algorithms.
We use this function to evaluate larger models, such as DistilBERT and ViT
'''
wdb_project = f'{dataset}_{model_name}_eval'
for cf in calib_fracs:
hp = get_hyperparameters(model_name, dataset, cf)
num_epochs = hp['epochs']
epochs = [num_epochs - 1]
if eval_epochs is not None:
epochs = eval_epochs
# if image resnet, can only evaluate at last epoch
if model_name in ['ImageResNet', 'MLP']:
err_msg = f'{model_name} can only evaluate at last epoch; all-epoch saving is not supported.'
assert epochs == [num_epochs - 1], err_msg
for seed in seeds:
for e in epochs:
print(f'********** {dataset} {model_name} cf={cf} seed={seed} epoch={e} **********')
config = {
# evaluation
'eval_epoch': e,
# data
'dataset': dataset,
'val_split_seed': seed,
'split': SPLIT_DEFAULT,
'calib_frac': cf,
# NN
'model': model_name,
'save_dir': _save_dir(dataset, model_name, cf, seed),
# evaluation
'val_save_epoch': 0,
'val_eval_epoch': 1,
# mcb
'mcb': [] if no_mcb else MCB_DEFAULT,
# hyperparameters
**hp
}
# create duplicate config, and alter only batch size
# this allows for evaluation with less memory
config_low_bs = config.copy()
config_low_bs['batch_size'] = 8
dataset_obj = Dataset(dataset, val_split_seed=config['val_split_seed'])
# init model
model = Model(model_name, config=config_low_bs, SAVE_DIR=config['save_dir'],
dataset_obj=dataset_obj, from_saved=True, saved_epoch=e)
experiment = Experiment(dataset_obj, model, calib_frac=config['calib_frac'])
# init logger
run_name = f'cf={cf}_seed={seed}_epoch={e}'
if wandb: experiment.init_logger(config, project=wdb_project, run_name=run_name)
# train and postprocess
if config['calib_frac'] > 0:
experiment.multicalibrate_multiple(config['mcb'])
# evaluate
experiment.evaluate_val()
experiment.evaluate_test()
# close logger
if wandb: experiment.init_logger(finish=True)
def data_reuse_experiment(model_name, dataset, seed, wandb=True):
# set constants for the experiment
mcb_params = MCB_DEFAULT
calib_frac = 0
calib_train_overlap = 1.0
groups_collection = 'default'
# set the save directory and wandb project
save_dir = 'models/saved_models/{dataset}/{model_name}/calib={calib_frac}_val_seed={seed}/'
wdb_project = f'{dataset}_{model_name}_reuse-train-data'
# define config for experiment
hyp = get_hyperparameters(model_name, dataset, calib_frac)
config = {
'model': model_name, # model name
'dataset': dataset, # dataset name
'group_collection': groups_collection, # group collection
'calib_frac': calib_frac, # calibration fraction
'calib_train_overlap': calib_train_overlap, # calibration train overlap
'val_split_seed': seed, # seed for validation split
'split': SPLIT_DEFAULT, # default split
'mcb': MCB_DEFAULT, # just to keep track of mcb algorithm
'save_dir': save_dir, # save directory
'val_save_epoch': 0, # save model every epoch
'val_eval_epoch': 1, # evaluate model every epoch
**hyp
}
dataset_obj = Dataset(dataset, val_split_seed=config['val_split_seed'], groups=groups_collection)
model = Model(model_name, config=config, SAVE_DIR=config['save_dir'])
experiment = Experiment(dataset_obj, model, calib_frac=config['calib_frac'],
calib_train_overlap=calib_train_overlap)
# init logger; this saves metrics to wandb
if wandb: experiment.init_logger(config, project=wdb_project)
# train and postprocess
experiment.train_model()
if config['calib_frac'] > 0 or config['calib_train_overlap'] > 0:
experiment.multicalibrate_multiple(mcb_params)
# evaluate splits
# experiment.evaluate_train()
# experiment.evaluate_calib()
experiment.evaluate_val()
experiment.evaluate_test()
# close logger
if wandb: experiment.init_logger(finish=True)
### Multi-experiment functions ###
def eval_all_SimpleModel(datasets, calib_fracs, models, seeds=SEEDS_DEFAULT):
'''
Helper function for evaluating SimpleModels with optimal
hyperparameters on all datasets.
'''
for dataset in datasets:
for model in models:
print(f'********** {dataset} {model} **********')
SimpleModel_train_eval(model, dataset, calib_fracs, seeds)
def eval_all_MLP():
'''
Helper function for evaluating MLPs with optimal
hyperparameters on all datasets.
'''
for dataset in ['ACSIncome', 'BankMarketing', 'CreditDefault', 'MEPS', 'HMDA']:
NN_train_eval('MLP', dataset, CALIB_FRACS_DEFAULT)
def data_reuse_experiments_all():
models = [
'SVM',
'LogisticRegression',
'NaiveBayes',
'DecisionTree',
'RandomForest',
'MLP'
]
datasets = ['ACSIncome', 'BankMarketing', 'CreditDefault', 'MEPS', 'HMDA']
seeds = SEEDS_DEFAULT
# create all combinations of datasets, models, and seeds
combs = product(datasets, models, seeds)
for dataset, model, seed in combs:
print(f'********** {dataset} {model} seed={seed} **********')
data_reuse_experiment(model, dataset, seed)
if __name__ == '__main__':
'''
One may call experiments from here. All experiments are logged to wandb,
and each project has a name of the form '{dataset}_{model}_eval'. This is
to differentiate from the projects titled '{dataset}_{model}_search',
which are used for hyperparameter search.
'''
# example usage #1:
# SimpleModel_train_eval('SVM', 'ACSIncome', [0.4], seeds=[55, 45])
# example usage #2:
# NN_train_eval('MLP', 'HMDA',
# [0, 0.01, 0.05, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0],
# seeds=[15, 25, 35, 45, 55],
# wdb_project_name='HMDA_MLP_alternate_eval', # custom project name
# groups_collection='alternate',
# wandb=True)
# example usage #3:
# data_reuse_experiments_all()
# example usage #4:
# for dataset in ['ACSIncome']:
# for model in ['NaiveBayes']:
# SimpleModel_train_eval(model, dataset,
# calib_fracs=[0, 0.01, 0.05, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0],
# seeds=[15, 25, 35, 45, 55],
# wdb_project_name=f'{dataset}_{model}_alternate_eval',
# groups_collection='alternate',
# wandb=True)
pass