-
Notifications
You must be signed in to change notification settings - Fork 27
/
tabzilla_experiment.py
304 lines (263 loc) · 10.2 KB
/
tabzilla_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
# experiment script for tabzilla
#
# this script runs an experiment specified by a config file
import argparse
import logging
import sys
import traceback
from collections import namedtuple
from pathlib import Path
from typing import NamedTuple
import optuna
optuna.logging.set_verbosity(optuna.logging.ERROR)
from models.basemodel import BaseModel
from tabzilla_alg_handler import ALL_MODELS, get_model
from tabzilla_datasets import TabularDataset
from tabzilla_utils import (
ExperimentResult,
cross_validation,
get_experiment_parser,
get_scorer,
)
class TabZillaObjective(object):
"""
adapted from TabSurvey.train.Objective.
this saves output from each trial.
"""
def __init__(
self,
model_handle: BaseModel,
dataset: TabularDataset,
experiment_args: NamedTuple,
hparam_seed: int,
random_parameters: bool,
time_limit: int,
):
# BaseModel handle that will be initialized and trained
self.model_handle = model_handle
self.dataset = dataset
self.experiment_args = experiment_args
self.dataset.subset_random_seed = self.experiment_args.subset_random_seed
# directory where results will be written
self.output_path = Path(self.experiment_args.output_dir).resolve()
# create the scorer, and get the direction of optimization from the scorer object
sc_tmp = get_scorer(dataset.target_type)
self.direction = sc_tmp.direction
# if True, sample random hyperparameters. if False, sample using the optuna sampler object
self.random_parameters = random_parameters
# if random_parameters = True, then this is used to generate random hyperparameters
self.hparam_seed = hparam_seed
# time limit for any cross-validation cycle (seconds)
self.time_limit = time_limit
def __call__(self, trial):
if self.random_parameters:
# first trial is always default params. after that, sample using either random or optuna suggested hparams
if trial.number == 0:
trial_params = self.model_handle.default_parameters()
hparam_source = "default"
else:
trial_params = self.model_handle.get_random_parameters(
trial.number + self.hparam_seed * 999
)
hparam_source = f"random_{trial.number}_s{self.hparam_seed}"
else:
trial_params = self.model_handle.define_trial_parameters(
trial, None
) # the second arg was "args", and is not used by the function. so we will pass None instead
hparam_source = f"sampler_{trial.number}"
# Create model
# pass a namespace "args" that contains all information needed to initialize the model.
# this is a combination of dataset args and parameter search args
# in TabSurvey, these were passed through an argparse args object
arg_namespace = namedtuple(
"args",
[
"model_name",
"batch_size",
"scale_numerical_features",
"val_batch_size",
"objective",
"gpu_ids",
"use_gpu",
"epochs",
"data_parallel",
"early_stopping_rounds",
"dataset",
"cat_idx",
"num_features",
"subset_features",
"subset_rows",
"subset_features_method",
"subset_rows_method",
"cat_dims",
"num_classes",
"logging_period",
],
)
# if model class has epochs defined, use this number. otherwise, use the num epochs passed in args.
if hasattr(self.model_handle, "default_epochs"):
max_epochs = self.model_handle.default_epochs
else:
max_epochs = self.experiment_args.epochs
args = arg_namespace(
model_name=self.model_handle.__name__,
batch_size=self.experiment_args.batch_size,
val_batch_size=self.experiment_args.val_batch_size,
scale_numerical_features=self.experiment_args.scale_numerical_features,
epochs=max_epochs,
gpu_ids=self.experiment_args.gpu_ids,
use_gpu=self.experiment_args.use_gpu,
data_parallel=self.experiment_args.data_parallel,
early_stopping_rounds=self.experiment_args.early_stopping_rounds,
logging_period=self.experiment_args.logging_period,
objective=self.dataset.target_type,
dataset=self.dataset.name,
cat_idx=self.dataset.cat_idx,
num_features=self.dataset.num_features,
subset_features=self.experiment_args.subset_features,
subset_rows=self.experiment_args.subset_rows,
subset_features_method=self.experiment_args.subset_features_method,
subset_rows_method=self.experiment_args.subset_rows_method,
cat_dims=self.dataset.cat_dims,
num_classes=self.dataset.num_classes,
)
# parameterized model
model = self.model_handle(trial_params, args)
# Cross validate the chosen hyperparameters
try:
result = cross_validation(
model,
self.dataset,
self.time_limit,
scaler=args.scale_numerical_features,
args=args,
)
obj_val = result.scorers["val"].get_objective_result()
except Exception as e:
print(f"caught exception during cross-validation...")
tb = traceback.format_exc()
result = ExperimentResult(
dataset=self.dataset,
scaler=args.scale_numerical_features,
model=model,
timers={},
scorers={},
predictions=None,
probabilities=None,
ground_truth=None,
)
result.exception = tb
obj_val = None
print(tb)
# add info about the hyperparams and trial number
result.hparam_source = hparam_source
result.trial_number = trial.number
result.experiment_args = vars(self.experiment_args)
# write results to file
result_file_base = self.output_path.joinpath(
f"{hparam_source}_trial{trial.number}"
)
result.write(
result_file_base,
write_predictions=self.experiment_args.write_predictions,
compress=False,
)
return obj_val
def iteration_callback(study, trial):
print(f"Trial {trial.number + 1} complete")
def main(experiment_args, model_name, dataset_dir):
# read dataset from folder
dataset = TabularDataset.read(Path(dataset_dir).resolve())
model_handle = get_model(model_name)
# create results directory if it doesn't already exist
output_path = Path(experiment_args.output_dir).resolve()
output_path.mkdir(parents=True, exist_ok=True)
optuna.logging.get_logger("optuna").addHandler(logging.StreamHandler(sys.stdout))
if experiment_args.n_random_trials > 0:
objective = TabZillaObjective(
model_handle=model_handle,
dataset=dataset,
experiment_args=experiment_args,
hparam_seed=experiment_args.hparam_seed,
random_parameters=True,
time_limit=experiment_args.trial_time_limit,
)
print(
f"evaluating {experiment_args.n_random_trials} random hyperparameter samples..."
)
study = optuna.create_study(
direction=objective.direction,
study_name=None,
storage=None,
load_if_exists=False,
)
study.optimize(
objective,
n_trials=experiment_args.n_random_trials,
timeout=experiment_args.experiment_time_limit,
callbacks=[iteration_callback],
)
previous_trials = study.trials
else:
previous_trials = None
if experiment_args.n_opt_trials:
# TODO: this needs to be tested
objective = TabZillaObjective(
model_handle=model_handle,
dataset=dataset,
experiment_args=experiment_args,
hparam_seed=experiment_args.hparam_seed,
random_parameters=False,
time_limit=experiment_args.trial_time_limit,
)
print(
f"running {experiment_args.n_opt_trials} steps of hyperparameter optimization..."
)
study = optuna.create_study(
direction=objective.direction,
study_name=None,
storage=None,
load_if_exists=False,
)
# if random search was run, add these trials
if previous_trials is not None:
print(
f"adding {experiment_args.n_random_trials} random trials to warm-start HPO"
)
study.add_trials(previous_trials)
study.optimize(
objective,
n_trials=experiment_args.n_opt_trials,
timeout=experiment_args.experiment_time_limit,
)
print(f"trials complete. results written to {output_path}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="parser for tabzilla experiments")
parser.add_argument(
"--experiment_config",
required=True,
type=str,
help="config file for parameter experiment args",
)
parser.add_argument(
"--dataset_dir",
required=True,
type=str,
help="directory containing pre-processed dataset.",
)
parser.add_argument(
"--model_name",
required=True,
type=str,
choices=ALL_MODELS,
help="name of the algorithm",
)
args = parser.parse_args()
print(f"ARGS: {args}")
# now parse the dataset and search config files
experiment_parser = get_experiment_parser()
experiment_args = experiment_parser.parse_args(
args="-experiment_config " + args.experiment_config
)
print(f"EXPERIMENT ARGS: {experiment_args}")
main(experiment_args, args.model_name, args.dataset_dir)