-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathbayesian_optimization.py
More file actions
317 lines (257 loc) · 11.7 KB
/
Copy pathbayesian_optimization.py
File metadata and controls
317 lines (257 loc) · 11.7 KB
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
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import division
import json
import random
import numpy as np
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import Matern
from scipy.stats import norm
from scipy.optimize import minimize
from suggestion.models import Study
from suggestion.models import Trial
from suggestion.algorithm.base_algorithm import BaseSuggestionAlgorithm
from suggestion.algorithm.random_search import RandomSearchAlgorithm
class BayesianOptimizationDemo(object):
def find_closest_value_in_list(self, the_list, objective_value):
closest_value = the_list[0]
for current_value in the_list:
if abs(current_value - objective_value) < abs(closest_value -
objective_value):
closest_value = current_value
return closest_value
def test_function2(self, x, y):
return -x**2 - (y - 1)**2 + 1
def test_function(self, x, y):
# The best x is 2
return np.exp(-(x - 2)**2) + np.exp(-(x - 6)**2 / 10) + 1 / (x**2 + 1)
def test_bayes_optimizaion(self):
print("Start bayesian optimization")
# 1. Initialize parameters
acquisition_fucntion_kappa = 5
init_point_number = 3
iteration_number = 3
iteration_index = 0
train_features = []
train_labels = []
gp = GaussianProcessRegressor(
kernel=Matern(nu=2.5),
n_restarts_optimizer=25, )
bound_dict = {'x': (-4, 4), 'y': (-3, 3)}
# Example: [[-4, 4], [-3, 3]]
bounds = []
for key in bound_dict.keys():
bounds.append(bound_dict[key])
# Example: ndarray([[-4, 4], [-3, 3]])
bounds = np.asarray(bounds)
# 2. Get init random samples
# Example: array([-3.66909025, -1.93270006, 1.36095631])
init_xs = np.random.uniform(-4, 4, size=init_point_number)
# Example: array([-0.84486644, -0.95367483, 0.61358525])
init_ys = np.random.uniform(-3, 3, size=init_point_number)
# Example: [[-3.66909025, -0.84486644], [-1.93270006, -0.95367483], [1.36095631, 0.61358525]]
init_points = []
for i in range(init_point_number):
init_points.append([init_xs[i], init_ys[i]])
# Example: [-4.4555402320291684, -7.9016857176523114]
init_labels = []
for point in init_points:
init_labels.append(self.test_function(point[0], point[1]))
# 3. GP compute the prior
train_features = np.asarray(init_points)
train_labels = np.asarray(init_labels)
current_max_label = train_labels.max()
gp.fit(train_features, train_labels)
# 4. Acquision function computes the max value
# Example: [[-3.66909025, -0.84486644], [-1.93270006, -0.95367483], [1.36095631, 0.61358525], ...], shape is [100000, 2]
x_tries = np.random.uniform(
bounds[:, 0], bounds[:, 1], size=(100000, bounds.shape[0]))
mean, std = gp.predict(x_tries, return_std=True)
# Confidence bound criteria
acquisition_fucntion_values = mean + acquisition_fucntion_kappa * std
x_max = x_tries[acquisition_fucntion_values.argmax()]
max_acquision_fucntion_value = acquisition_fucntion_values.max()
x_max = np.clip(x_max, bounds[:, 0], bounds[:, 1])
print("Current max acquision function choose: {}".format(x_max))
for i in range(iteration_number):
iteration_index += 1
# 5. Choose the best and compute to add in train dataset
train_features = np.vstack((train_features, x_max.reshape((1, -1))))
train_labels = np.append(train_labels,
self.test_function(x_max[0], x_max[1]))
# 6. Re-compute gaussian process and acquistion function
gp.fit(train_features, train_labels)
# Update maximum value
if train_labels[-1] > current_max_label:
current_max_label = train_labels[-1]
print("Get the better parameters!")
x_tries = np.random.uniform(
bounds[:, 0], bounds[:, 1], size=(100000, bounds.shape[0]))
mean, std = gp.predict(x_tries, return_std=True)
acquisition_fucntion_values = mean + acquisition_fucntion_kappa * std
x_max = x_tries[acquisition_fucntion_values.argmax()]
max_acquision_fucntion_value = acquisition_fucntion_values.max()
x_max = np.clip(x_max, bounds[:, 0], bounds[:, 1])
print("Max label: {}, current label: {}, acquision function choose: {}".
format(current_max_label, train_labels[-1], x_max))
class BayesianOptimization(BaseSuggestionAlgorithm):
def get_random_value(self, min_value, max_value):
return random.uniform(min_value, max_value)
def get_new_suggestions(self, study_id, trials, number=1):
study = Study.objects.get(id=study_id)
completed_trials = Trial.objects.filter(
study_id=study_id, status="Completed")
study_configuration_json = json.loads(study.study_configuration)
random_init_trials = study_configuration_json.get("randomInitTrials", 3)
# a list: [{}, {}, ...]
params = study_configuration_json["params"]
# Use random search if it has less dataset
if len(completed_trials) < random_init_trials:
randomSearchAlgorithm = RandomSearchAlgorithm()
return_trials = randomSearchAlgorithm.get_new_suggestions(
study_id, trials, number=random_init_trials - len(completed_trials))
return return_trials
else:
return_trials = []
acquisition_fucntion_kappa = 5
# iterate over dict can be dangerous, so abandon bound_dict
bounds = []
for param in params:
if param["type"] == "DOUBLE" or param["type"] == "INTEGER":
min_value = param["minValue"]
max_value = param["maxValue"]
bounds.append((min_value, max_value))
elif param["type"] == "DISCRETE":
feasible_points_string = param["feasiblePoints"]
feasible_points = [
float(value.strip())
for value in feasible_points_string.split(",")
]
feasible_points.sort()
min_value = feasible_points[0]
max_value = feasible_points[-1]
bounds.append((min_value, max_value))
elif param["type"] == "CATEGORICAL":
feasible_points_string = param["feasiblePoints"]
feasible_points = [
value.strip() for value in feasible_points_string.split(",")
]
for feasible_point in feasible_points:
parameter_name = "{}_{}".format(param["parameterName"],
feasible_point)
bounds.append((0, 1))
bounds = np.asarray(bounds)
gp = GaussianProcessRegressor(
kernel=Matern(nu=2.5),
n_restarts_optimizer=25, )
init_points = []
init_labels = []
"""
parametername_type_map = {}
for param in params:
parametername_type_map[param["parameterName"]] = param["type"]
"""
for trial in completed_trials:
# Example: {"learning_rate": 0.01, "optimizer": "ftrl"}
parameter_values_json = json.loads(trial.parameter_values)
# Example: [0.01]
instance_features = []
instance_label = trial.objective_value
for param in params:
if param["type"] == "DOUBLE" or param["type"] == "INTEGER" or param["type"] == "DISCRETE":
instance_feature = parameter_values_json[param["parameterName"]]
instance_features.append(instance_feature)
elif param["type"] == "CATEGORICAL":
feasible_points_string = param["feasiblePoints"]
# Example: ["sgd", "adagrad", "adam", "ftrl"]
feasible_points = [
value.strip() for value in feasible_points_string.split(",")
]
# Example: "ftrl"
parameter_value = parameter_values_json[param["parameterName"]]
for feasible_point in feasible_points:
if feasible_point == parameter_value:
instance_features.append(1)
else:
instance_features.append(0)
init_points.append(instance_features)
init_labels.append(instance_label)
#import ipdb;ipdb.set_trace()
train_features = np.asarray(init_points)
train_labels = np.asarray(init_labels)
current_max_label = train_labels.max()
gp.fit(train_features, train_labels)
# Example: [[-3.66909025, -0.84486644], [-1.93270006, -0.95367483], [1.36095631, 0.61358525], ...], shape is [100000, 2]
# shape: [10000, n_params]
x_tries = np.random.uniform(
bounds[:, 0], bounds[:, 1], size=(100000, bounds.shape[0]))
mean, std = gp.predict(x_tries, return_std=True)
# Confidence bound criteria
# shape: [100000]
acquisition_fucntion_values = mean + acquisition_fucntion_kappa * std
# Topn max ac function values
# shape: [number]
topn_indices = acquisition_fucntion_values.argsort()[-number:][::-1]
# shape: [number, n_params]
x_max_topn = x_tries[topn_indices]
max_acquision_fucntion_value = acquisition_fucntion_values.max()
# Example: [[3993.864683994805, 44.15441513231316], ...]
# shape: [number, n_params]
x_max_topn = np.clip(x_max_topn, bounds[:, 0], bounds[:, 1])
print("Current topn max acquision function choose: {}".format(x_max_topn))
# Example: [{"hidden2": 3993.864683994805, "hidden1": 44.15441513231316}, ...]
suggested_parameter_values_jsons = []
"""
# Example: [0.1, 0.5, 0.3, 0.9]
# Example: {"learning_rate": (0.01, 0.5), "hidden1": (40, 400), "optimizer_sgd": (0, 1), "optimizer_ftrl": (0, 1)}
for key in bound_dict.keys():
parameter_values_json[key] = x_max[index]
index += 1
"""
for x_max in x_max_topn:
return_trial = Trial.create(study.id, "BayesianOptimizationTrial")
suggested_parameter_values_json = {}
index = 0
for param in params:
if param["type"] == "DOUBLE" or param["type"] == "DISCRETE":
suggested_parameter_values_json[param["parameterName"]] = x_max[
index]
index += 1
elif param["type"] == "INTEGER":
suggested_parameter_values_json[param["parameterName"]] = int(
round(x_max[index]))
index += 1
elif param["type"] == "DISCRETE":
feasible_points_string = param["feasiblePoints"]
feasible_points = [
float(value.strip())
for value in feasible_points_string.split(",")
]
feasible_points.sort()
selected_value = self.find_closest_value_in_list(
feasible_points, x_max[index])
suggested_parameter_values_json[param[
"parameterName"]] = selected_value
index += 1
elif param["type"] == "CATEGORICAL":
feasible_points_string = param["feasiblePoints"]
# Example: ["sgd", "adagrad", "adam", "ftrl"]
feasible_points = [
value.strip() for value in feasible_points_string.split(",")
]
# 记录这4个值中数最大的,然后取到对应的字符串
current_max = x_max[index]
suggested_parameter_value = feasible_points[0]
for feasible_point in feasible_points:
if x_max[index] > current_max:
current_max = x_max[index]
suggested_parameter_value = feasible_point
index += 1
suggested_parameter_values_json[param[
"parameterName"]] = suggested_parameter_value
suggested_parameter_values_jsons.append(suggested_parameter_values_json)
return_trial.parameter_values = json.dumps(
suggested_parameter_values_json)
return_trial.save()
return_trials.append(return_trial)
return return_trials