-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhpob_handler.py
260 lines (198 loc) · 10.7 KB
/
hpob_handler.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
import numpy as np
try:
import ujson as json
except:
import json
import os
import xgboost as xgb
this_dir = os.path.abspath(os.path.dirname(__file__))
class HPOBHandler:
def __init__(self,
root_dir = os.path.join(this_dir, "hpob-data/"),
mode = "v3-test",
surrogates_dir="saved-surrogates/"):
"""
Constructor for the HPOBHandler.
Inputs:
* root_dir: path to directory with the benchmark data.
* mode: mode name indicating how to load the data. Options:
- v1: Loads HPO-B-v1
- v2: Loads HPO-B-v2
- v3: Loads HPO-B-v3
- v3-test: Loads only the meta-test split from HPO-B-v3
- v3-train-augmented: Loads all splits from HPO-B-v3, but augmenting the meta-train data with the less frequent search-spaces.
* surrogates_dir: path to directory with surrogates models.
"""
print("Loading HPO-B handler")
self.mode = mode
self.surrogates_dir = surrogates_dir
self.seeds = ["test0", "test1", "test2", "test3", "test4"]
if self.mode == "v3-test":
self.load_data(root_dir, only_test=True)
elif self.mode == "v3-train-augmented":
self.load_data(root_dir, only_test=False, augmented_train=True)
elif self.mode in ["v1", "v2", "v3"]:
self.load_data(root_dir, version = self.mode, only_test=False)
else:
raise ValueError("Provide a valid mode")
surrogates_file = surrogates_dir+"summary-stats.json"
if os.path.isfile(surrogates_file):
with open(surrogates_file) as f:
self.surrogates_stats = json.load(f)
def load_data(self, rootdir="", version = "v3", only_test = True, augmented_train = False):
"""
Loads data with some specifications.
Inputs:
* root_dir: path to directory with the benchmark data.
* version: name indicating what HPOB version to use. Options: v1, v2, v3).
* Only test: Whether to load only testing data (valid only for version v3). Options: True/False
* augmented_train: Whether to load the augmented train data (valid only for version v3). Options: True/False
"""
print("Loading data...")
meta_train_augmented_path = os.path.join(rootdir, "meta-train-dataset-augmented.json")
meta_train_path = os.path.join(rootdir, "meta-train-dataset.json")
meta_test_path = os.path.join(rootdir,"meta-test-dataset.json")
meta_validation_path = os.path.join(rootdir, "meta-validation-dataset.json")
bo_initializations_path = os.path.join(rootdir, "bo-initializations.json")
with open(meta_test_path, "rb") as f:
self.meta_test_data = json.load(f)
with open(bo_initializations_path, "rb") as f:
self.bo_initializations = json.load(f)
if not only_test:
if augmented_train or version=="v1":
with open(meta_train_augmented_path, "rb") as f:
self.meta_train_data = json.load(f)
else:
with open(meta_train_path, "rb") as f:
self.meta_train_data = json.load(f)
with open(meta_validation_path, "rb") as f:
self.meta_validation_data = json.load(f)
if version != "v3":
temp_data = {}
for search_space in self.meta_train_data.keys():
temp_data[search_space] = {}
for dataset in self.meta_train_data[search_space].keys():
temp_data[search_space][dataset] = self.meta_train_data[search_space][dataset]
if search_space in self.meta_test_data.keys():
for dataset in self.meta_test_data[search_space].keys():
temp_data[search_space][dataset] = self.meta_test_data[search_space][dataset]
for dataset in self.meta_validation_data[search_space].keys():
temp_data[search_space][dataset] = self.meta_validation_data[search_space][dataset]
self.meta_train_data = None
self.meta_validation_data = None
self.meta_test_data = temp_data
self.search_space_dims = {}
for search_space in self.meta_test_data.keys():
dataset = list(self.meta_test_data[search_space].keys())[0]
X = self.meta_test_data[search_space][dataset]["X"][0]
self.search_space_dims[search_space] = len(X)
def normalize(self, y, y_min = None, y_max=None):
if y_min is None:
return (y-np.min(y))/(np.max(y)-np.min(y))
else:
return(y-y_min)/(y_max-y_min)
def evaluate (self, bo_method = None, search_space_id = None, dataset_id = None, seed = None, n_trials = 10):
"""
Evaluates a method on the benchmark with discretized search-spaces.
Inputs:
* bo_method: object to evaluate. It should have a function (class method) named 'observe_and_suggest'.
* search_space_id: Identifier of the search spaces for the evaluation. Option: see original paper.
* dataset_id: Identifier of the dataset for the evaluation. Options: see original paper.
* seed: Identifier of the seed for the evaluation. Options: test0, test1, test2, test3, test4.
* trails: Number of trials (iterations on the opoitmization).
Ooutput:
* a list with the maximumu performance (incumbent) for every trial.
"""
assert bo_method!=None, "Provide a valid method object for evaluation."
assert hasattr(bo_method, "observe_and_suggest"), "The provided object does not have a method called ´observe_and_suggest´"
assert search_space_id!= None, "Provide a valid search space id. See documentatio for valid obptions."
assert dataset_id!= None, "Provide a valid dataset_id. See documentation for valid options."
assert seed!=None, "Provide a valid initialization. Valid options are: test0, test1, test2, test3, test4."
try:
X = np.array(self.meta_test_data[search_space_id][dataset_id]["X"])
y = np.array(self.meta_test_data[search_space_id][dataset_id]["y"])
except KeyError:
print(self.meta_test_data.keys())
raise
y = self.normalize(y)
data_size = len(X)
pending_evaluations = list(range(data_size))
current_evaluations = []
init_ids = self.bo_initializations[search_space_id][dataset_id][seed]
for i in range(len(init_ids)):
idx = init_ids[i]
pending_evaluations.remove(idx)
current_evaluations.append(idx)
max_accuracy_history = [np.max(y[current_evaluations])]
for i in range(n_trials):
idx = bo_method.observe_and_suggest(
X_obs=X[current_evaluations],
y_obs=y[current_evaluations],
X_pen=X[pending_evaluations],
)
idx = pending_evaluations[idx]
pending_evaluations.remove(idx)
current_evaluations.append(idx)
max_accuracy_history.append(np.max(y[current_evaluations]))
if max(y) in max_accuracy_history:
break
max_accuracy_history+=[max(y).item()]*(n_trials-i-1)
return max_accuracy_history
def evaluate_continuous(self, bo_method = None, search_space_id = None, dataset_id = None, seed = None, n_trials = 10):
"""
Evaluates a method on the benchmark with continuous search-spaces.
Inputs:
* bo_method: object to evaluate. It should have a function (class method) named 'observe_and_suggest'.
* search_space_id: Identifier of the search spaces for the evaluation. Option: see original paper.
* dataset_id: Identifier of the dataset for the evaluation. Options: see original paper.
* seed: Identifier of the seed for the evaluation. Options: test0, test1, test2, test3, test4.
* trails: Number of trials (iterations on the opoitmization).
Ooutput:
* a list with the maximum performance (incumbent) for every trial.
"""
assert bo_method!=None, "Provide a valid method object for evaluation."
assert hasattr(bo_method, "observe_and_suggest"), "The provided object does not have a method called ´observe_and_suggest´"
assert search_space_id!= None, "Provide a valid search space id. See documentatio for valid obptions."
assert dataset_id!= None, "Provide a valid dataset_id. See documentation for valid options."
assert seed!=None, "Provide a valid initialization. Valid options are: test0, test1, test2, test3, test4."
surrogate_name='surrogate-'+search_space_id+'-'+dataset_id
bst_surrogate = xgb.Booster()
bst_surrogate.load_model(self.surrogates_dir+surrogate_name+'.json')
X = np.array(self.meta_test_data[search_space_id][dataset_id]["X"])
y = np.array(self.meta_test_data[search_space_id][dataset_id]["y"])
y_min = self.surrogates_stats[surrogate_name]["y_min"]
y_max = self.surrogates_stats[surrogate_name]["y_max"]
dim = X.shape[1]
current_evaluations = []
init_ids = self.bo_initializations[search_space_id][dataset_id][seed]
for i in range(len(init_ids)):
idx = init_ids[i]
current_evaluations.append(idx)
x_observed = X[current_evaluations]
y_observed = y[current_evaluations]
max_accuracy_history = []
for i in range(n_trials):
y_tf_observed = self.normalize(y_observed, y_min, y_max)
y_tf_observed = np.clip(y_tf_observed, 0, 1)
best_f = np.max(y_tf_observed)
max_accuracy_history.append(best_f)
new_x = bo_method.observe_and_suggest(x_observed, y_tf_observed)
x_q = xgb.DMatrix(new_x.reshape(-1,dim))
new_y = bst_surrogate.predict(x_q)
y_observed = np.append(y_observed, new_y).reshape(-1,1)
x_observed = np.append(x_observed, new_x).reshape(-1,x_observed.shape[1])
if max(y_observed) >= y_max:
break
y_tf_observed = self.normalize(y_observed, y_min, y_max)
y_tf_observed = np.clip(y_tf_observed, 0, 1)
max_accuracy_history.append(best_f)
max_accuracy_history+=[1]*(n_trials-i-1)
return max_accuracy_history
def get_search_spaces(self):
return list(self.meta_test_data.keys())
def get_datasets(self, search_space):
return list(self.meta_test_data[search_space].keys())
def get_seeds(self):
return self.seeds
def get_search_space_dim(self, search_space):
return self.search_space_dims[search_space]