-
Notifications
You must be signed in to change notification settings - Fork 0
/
framework.py
308 lines (258 loc) · 10.3 KB
/
framework.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
import argparse
import json
import os
import time
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import FunctionTransformer, OneHotEncoder
from benchmarks.lcbench import LCBench
from benchmarks.taskset import TaskSet
from benchmarks.hyperbo import PD1
from surrogate_models.power_law_surrogate import PowerLawSurrogate
from surrogate_models.asha import AHBOptimizer
from surrogate_models.dehb.interface import DEHBOptimizer
from surrogate_models.dragonfly import DragonFlyOptimizer
from surrogate_models.random_search import RandomOptimizer
class Framework:
def __init__(
self,
args: argparse.Namespace,
seed: int,
):
"""
Args:
args: Namespace
Includes all the arguments given as variables to the main_experiment
script.
seed: int
The seed for the experiment.
"""
if args.benchmark_name == 'lcbench':
benchmark_extension = os.path.join(
'lc_bench',
'results',
'data_2k.json',
)
elif args.benchmark_name == 'taskset':
benchmark_extension = os.path.join(
'data',
'taskset',
)
elif args.benchmark_name == 'pd1':
benchmark_extension = 'pd1'
else:
raise ValueError(f'Benchmark {args.benchmark_name} not supported')
benchmark_data_path = os.path.join(
args.project_dir,
benchmark_extension,
)
benchmark_types = {
'lcbench': LCBench,
'taskset': TaskSet,
'pd1': PD1,
}
surrogate_types = {
'power_law': PowerLawSurrogate,
'asha': AHBOptimizer,
'dehb': DEHBOptimizer,
'dragonfly': DragonFlyOptimizer,
'random': RandomOptimizer,
}
disable_preprocessing = {
'dehb',
}
self.benchmark = benchmark_types[args.benchmark_name](benchmark_data_path, args.dataset_name)
self.dataset_name = args.dataset_name
self.seed = seed
self.max_value = self.benchmark.max_value
self.min_value = self.benchmark.min_value
self.total_budget = args.budget_limit
self.fantasize_step = args.fantasize_step
self.categorical_indicator = self.benchmark.categorical_indicator
self.log_indicator = self.benchmark.log_indicator
self.hp_names = self.benchmark.hp_names
self.minimization_metric = self.benchmark.minimization_metric
self.info_dict = dict()
self.result_dir = os.path.join(
args.output_dir,
args.benchmark_name,
args.surrogate_name,
)
os.makedirs(self.result_dir, exist_ok=True)
self.result_file = os.path.join(
self.result_dir,
f'{self.dataset_name}_{self.seed}.json',
)
if args.surrogate_name not in disable_preprocessing:
self.hp_candidates = self.preprocess(self.benchmark.get_hyperparameter_candidates())
else:
self.hp_candidates = self.benchmark.get_hyperparameter_candidates()
if args.surrogate_name == 'power_law':
self.surrogate = surrogate_types[args.surrogate_name](
self.hp_candidates,
seed=seed,
max_benchmark_epochs=self.benchmark.max_budget,
ensemble_size=args.ensemble_size,
nr_epochs=args.nr_epochs,
fantasize_step=self.fantasize_step,
minimization=self.minimization_metric,
total_budget=args.budget_limit,
device='cpu',
dataset_name=args.dataset_name,
output_path=self.result_dir,
max_value=self.max_value,
min_value=self.min_value,
)
else:
self.surrogate = surrogate_types[args.surrogate_name](
hyperparameter_candidates=self.hp_candidates,
param_space=self.benchmark.param_space,
min_budget=self.benchmark.min_budget,
max_budget=self.benchmark.max_budget,
eta=3,
seed=seed,
max_nr_trials=args.budget_limit,
maximization=not self.benchmark.minimization_metric,
)
def run(self):
evaluated_configs = dict()
surrogate_budget = 0
if self.benchmark.minimization_metric:
best_value = np.inf
else:
best_value = 0
while surrogate_budget < self.total_budget:
start_time = time.time()
hp_index, budget = self.surrogate.suggest()
hp_curve = self.benchmark.get_curve(hp_index, budget)
self.surrogate.observe(hp_index, budget, hp_curve)
time_duration = time.time() - start_time
if hp_index in evaluated_configs:
previous_budget = evaluated_configs[hp_index]
else:
previous_budget = 0
budget_cost = budget - previous_budget
evaluated_configs[hp_index] = budget
step_time_duration = time_duration / budget_cost
for epoch in range(previous_budget + 1, budget + 1):
epoch_performance = float(hp_curve[epoch - 1])
if self.benchmark.minimization_metric:
if best_value > epoch_performance:
best_value = epoch_performance
else:
if best_value < epoch_performance:
best_value = epoch_performance
surrogate_budget += 1
if surrogate_budget > self.total_budget:
exit(0)
self.log_info(
int(hp_index),
epoch_performance,
epoch,
best_value,
step_time_duration,
)
exit(0)
def preprocess(self, hp_candidates: np.ndarray) -> np.ndarray:
"""Preprocess the hyperparameter candidates.
Performs min-max standardization for the numerical attributes and
additionally one-hot encoding for the categorical attributes.
Args:
hp_candidates: np.ndarray
The hyperparameter candidates in their raw form as taken
from the benchmark.
Returns:
preprocessed_candidates: np.ndarray
The transformed hyperparameter candidates after being
preprocessed.
"""
column_transformers = []
numerical_columns = [
col_index for col_index, category_indicator in enumerate(self.categorical_indicator)
if not category_indicator
]
categorical_columns = [
col_index for col_index, category_indicator in enumerate(self.categorical_indicator)
if category_indicator
]
general_transformers = []
if len(numerical_columns) > 0:
if self.log_indicator is not None and any(self.log_indicator):
log_columns = [col_index for col_index, log_indicator in enumerate(self.log_indicator) if log_indicator]
log_transformer = FunctionTransformer(np.log)
column_transformers.append(
(
'log_pre',
ColumnTransformer(
[('log', log_transformer, log_columns)],
remainder='passthrough'
)
)
)
general_transformers.append(('num', MinMaxScaler(), numerical_columns))
if len(categorical_columns) > 0:
general_transformers.append(
(
'cat',
OneHotEncoder(
categories=[self.hp_names] * hp_candidates.shape[1],
sparse=False,
),
categorical_columns,
)
)
column_transformers.append(('feature_types_pre', ColumnTransformer(general_transformers)))
preprocessor = Pipeline(
column_transformers
)
# TODO log preprocessing will push numerical columns to the right
# so a mapping has to happen for the feature_types_pre
preprocessed_candidates = preprocessor.fit_transform(hp_candidates)
return preprocessed_candidates
def log_info(
self,
hp_index: int,
performance: float,
budget: int,
best_value_observed: float,
time_duration: float,
):
"""Log information after every HPO iteration.
Args:
hp_index: int
The index of the suggested hyperparameter candidate.
performance: float
The performance of the hyperparameter candidate.
budget: int
The budget at which the hyperpararameter candidate has been evaluated so far.
best_value_observed: float
The incumbent value observed so far during the optimization.
time_duration: float
The time taken for the HPO iteration.
"""
if 'hp' in self.info_dict:
self.info_dict['hp'].append(hp_index)
else:
self.info_dict['hp'] = [hp_index]
accuracy_performance = performance
if 'scores' in self.info_dict:
self.info_dict['scores'].append(accuracy_performance)
else:
self.info_dict['scores'] = [accuracy_performance]
incumbent_acc_performance = best_value_observed
if 'curve' in self.info_dict:
self.info_dict['curve'].append(incumbent_acc_performance)
else:
self.info_dict['curve'] = [incumbent_acc_performance]
if 'epochs' in self.info_dict:
self.info_dict['epochs'].append(budget)
else:
self.info_dict['epochs'] = [budget]
if 'overhead' in self.info_dict:
self.info_dict['overhead'].append(time_duration)
else:
self.info_dict['overhead'] = [time_duration]
with open(self.result_file, 'w') as fp:
json.dump(self.info_dict, fp)