-
Notifications
You must be signed in to change notification settings - Fork 1
/
optimizer.py
215 lines (193 loc) · 7.02 KB
/
optimizer.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
from copy import deepcopy
import numpy as np
from multiprocessing import Process, Queue
from tqdm import tqdm
from .exceptions import TimelimitError
from .samplers.utils import SAMPLERS_MAP
import warnings
warnings.filterwarnings("ignore")
class Optimizer(object):
"""Black-box Optimizer
Black box optimization is a method to find optimal parameteres by
observing the response of an objective function wrt each parameters
without defining models, e.g., hyperparameter optimization.
Parameters
----------
score_func : function
Takes dictionary as input and returns scalar score.
space : list(dict)
Each element define name search space as a dictionary.
sampler: str
The name of sample to use: 'grid', 'random', and 'bayes'
init_X: array-like(float), shape=(n_samples, n_dim)
The list of parameters to initizlie sampler
init_y: array-like(float), shape(n_samples,)
The list of score of init_X
timeout: int, optional
If specified, it terminates score evaluation after
timeout seconds has passed.
kwargs:
These parameteres are sent to sampler object
Here is the samples of how to define score_func and space:
from sklearn.svm import SVC
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score
from sklearn.model_selection import StratifiedShuffleSplit
from sklearn.preprocessing import StandardScaler
data, target = make_classification(n_samples=2500,
n_features=45,
n_informative=5,
n_redundant=5)
space = [
{'name': 'C', 'domain': (1e-8, 1e5), 'type': 'continuous', 'scale': 'log'},
{'name': 'gamma', 'domain': (1e-8, 1e5), 'type': 'continuous', 'scale': 'log'},
{'name': 'kernel', 'domain': 'rbf', 'type': 'fixed'}
]
def score_func(params):
splitter = StratifiedShuffleSplit(n_splits=1, test_size=0.2)
train_idx, test_idx = list(splitter.split(data, target))[0]
train_data = data[train_idx]
train_target = target[train_idx]
clf = SVC(**params)
clf.fit(train_data, train_target)
pred = clf.predict(data[test_idx])
true_y = target[test_idx]
score = accuracy_score(true_y, pred)
return -score
"""
def __init__(self, score_func, space,
sampler="random", init_X=None, init_y=None,
maximize=False, timeout=None, **kwargs):
self._score_func = score_func
self._space_conf = space
self._maximize = maximize
self._timeout = timeout
# Separate fixed params
self.fixed_params = dict()
self._nonfixed_conf = []
if isinstance(self._space_conf, list):
for conf in self._space_conf:
if conf["type"] == "fixed":
self.fixed_params[conf["name"]] = conf["domain"]
else:
self._nonfixed_conf.append(conf)
else:
self._nonfixed_conf = self._space_conf
# Sampler cares only about non fixed parameters
if isinstance(init_X, list):
for param in init_X:
for fixed_name in self.fixed_params.keys():
del param[fixed_name]
self.sampler = SAMPLERS_MAP[sampler](self._nonfixed_conf,
init_X, init_y, **kwargs)
def search(self, return_full=False, num_iter=10, is_display=True,
*args, **kwargs):
"""Find optimal set of parameters
Parameters
----------
return_full: bool (default False)
If True, return all of search results
If False, return only optimal set of paramters and its score
num_iter: int (default 10)
How many time to try
is_display: bool (default)
If True, show the progress bar
Returns
-------
If return_full == True:
Xs: list(dict)
ys: list(float)
If return_fulll == False
best_X: dict
ys: float
"""
if is_display:
iteration = tqdm(range(num_iter))
else:
iteration = range(num_iter)
for i in iteration:
Xs = self.sampler.sample(*args, **kwargs)
sucess_Xs = []
ys = []
for X in Xs:
try:
y = self.score_func(X)
ys.append(y)
sucess_Xs.append(X)
except TimelimitError as e:
print(e)
print("Try different configuration")
continue
self.sampler.update(sucess_Xs, ys)
Xs, ys = self.sampler.data
best_idx = np.argmin(ys)
# Default is minimization
if self._maximize:
ys = -ys
# Update with fixed parameters
fixed_params = deepcopy(self.fixed_params)
for X in Xs:
X.update(fixed_params)
if return_full:
return Xs, ys
else:
best_X = Xs[best_idx]
best_y = ys[best_idx]
return best_X, best_y
def score_func(self, X, *args, **kwargs):
fixed_params = deepcopy(self.fixed_params)
X = deepcopy(X)
X.update(fixed_params)
if self._timeout is not None:
def record(que):
try:
score = self._score_func(X, *args, **kwargs)
# Default is minimization
if self._maximize:
score = -score
except Exception as e:
score = e
que.put(score)
que = Queue()
proc = Process(target=record, args=(que,))
proc.start()
proc.join(self.timeout)
if proc.is_alive():
proc.terminate()
proc.join()
raise TimelimitError()
else:
response = que.get()
if isinstance(response, Exception):
print("Error at score_func", response)
raise response
else:
score = response
else:
score = self._score_func(X, *args, **kwargs)
# Default is minimization
if self._maximize:
score = -score
return score
@property
def results(self):
best_X = []
best_y = []
X, y = self.sampler.data
for i in range(len(y)):
idx = np.argmin(y[:i + 1])
best_X.append(X[idx])
best_y.append(y[idx])
best_y = np.array(best_y)
if self._maximize:
best_y = -best_y
return best_X, best_y
@property
def best_results(self):
X, y = self.sampler.data
idx = np.argmin(y)
best_X = X[idx]
best_y = y[idx]
if self._maximize:
best_y = -best_y
return best_X, best_y