Repository navigation
Expand file tree
/
Copy pathrandom_search.py
More file actions
246 lines (203 loc) · 8.7 KB
/
Copy pathrandom_search.py
File metadata and controls
246 lines (203 loc) · 8.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
"""Random search optimizer."""
# copyright: hyperactive developers, MIT License (see LICENSE file)
from collections.abc import Sequence
import numpy as np
from sklearn.model_selection import ParameterSampler
from hyperactive.base import BaseOptimizer
from hyperactive.opt._common import _score_params
from hyperactive.utils.parallel import parallelize
class RandomSearchSk(BaseOptimizer):
"""Random search optimizer leveraging sklearn's ``ParameterSampler``.
Parameters
----------
param_distributions : dict[str, list | scipy.stats.rv_frozen]
Search space specification. Discrete lists are sampled uniformly;
scipy distribution objects are sampled via their ``rvs`` method.
n_iter : int, default=10
Number of parameter sets to evaluate.
random_state : int | np.random.RandomState | None, default=None
Controls the pseudo-random generator for reproducibility.
error_score : float, default=np.nan
Score assigned when the experiment raises an exception.
backend : {"dask", "loky", "multiprocessing", "threading", "ray"}, default = "None".
Parallelization backend to use in the search process.
- "None": executes loop sequentally, simple list comprehension
- "loky", "multiprocessing" and "threading": uses ``joblib.Parallel`` loops
- "joblib": custom and 3rd party ``joblib`` backends, e.g., ``spark``
- "dask": uses ``dask``, requires ``dask`` package in environment
- "ray": uses ``ray``, requires ``ray`` package in environment
backend_params : dict, optional
additional parameters passed to the backend as config.
Directly passed to ``utils.parallel.parallelize``.
Valid keys depend on the value of ``backend``:
- "None": no additional parameters, ``backend_params`` is ignored
- "loky", "multiprocessing" and "threading": default ``joblib`` backends
any valid keys for ``joblib.Parallel`` can be passed here, e.g., ``n_jobs``,
with the exception of ``backend`` which is directly controlled by ``backend``.
If ``n_jobs`` is not passed, it will default to ``-1``, other parameters
will default to ``joblib`` defaults.
- "joblib": custom and 3rd party ``joblib`` backends, e.g., ``spark``.
any valid keys for ``joblib.Parallel`` can be passed here, e.g., ``n_jobs``,
``backend`` must be passed as a key of ``backend_params`` in this case.
If ``n_jobs`` is not passed, it will default to ``-1``, other parameters
will default to ``joblib`` defaults.
- "dask": any valid keys for ``dask.compute`` can be passed, e.g., ``scheduler``
- "ray": The following keys can be passed:
- "ray_remote_args": dictionary of valid keys for ``ray.init``
- "shutdown_ray": bool, default=True; False prevents ``ray`` from shutting
down after parallelization.
- "logger_name": str, default="ray"; name of the logger to use.
- "mute_warnings": bool, default=False; if True, suppresses warnings
experiment : BaseExperiment, optional
Callable returning a scalar score when invoked with keyword
arguments matching a parameter set.
Example
-------
Random search with different backend configurations:
>>> from hyperactive.opt import RandomSearchSk
>>> from scipy.stats import uniform
>>> param_distributions = {
... "C": uniform(loc=0.1, scale=10),
... "gamma": ["scale", "auto", 0.001, 0.01, 0.1, 1],
... }
>>>
>>> # Sequential execution
>>> random_search = RandomSearchSk(
... param_distributions=param_distributions,
... n_iter=20,
... backend="None",
... )
>>>
>>> # Parallel execution with threading backend
>>> random_search_parallel = RandomSearchSk(
... param_distributions=param_distributions,
... n_iter=20,
... backend="threading",
... backend_params={"n_jobs": 2},
... )
Attributes
----------
best_params_ : dict[str, Any]
Hyper-parameter configuration with the best (lowest) score.
best_score_ : float
Score achieved by ``best_params_``.
best_index_ : int
Index of ``best_params_`` in the sampled sequence.
"""
def __init__(
self,
param_distributions=None,
n_iter=10,
random_state=None,
error_score=np.nan,
backend="None",
backend_params=None,
experiment=None,
):
self.experiment = experiment
self.param_distributions = param_distributions
self.n_iter = n_iter
self.random_state = random_state
self.error_score = error_score
self.backend = backend
self.backend_params = backend_params
super().__init__()
@staticmethod
def _is_distribution(obj) -> bool:
"""Return True if *obj* looks like a scipy frozen distribution."""
return callable(getattr(obj, "rvs", None))
def _check_param_distributions(self, param_distributions):
"""Validate ``param_distributions`` similar to sklearn ≤1.0.x."""
if hasattr(param_distributions, "items"):
param_distributions = [param_distributions]
for p in param_distributions:
for name, v in p.items():
if self._is_distribution(v):
# Assume scipy frozen distribution: nothing to check
continue
if isinstance(v, np.ndarray) and v.ndim > 1:
raise ValueError("Parameter array should be one-dimensional.")
if isinstance(v, str) or not isinstance(v, (np.ndarray, Sequence)):
raise ValueError(
f"Parameter distribution for ({name}) must be a list, numpy "
f"array, or scipy.stats ``rv_frozen``, but got ({type(v)})."
" Single values need to be wrapped in a sequence."
)
if len(v) == 0:
raise ValueError(
f"Parameter values for ({name}) need to be a "
"non-empty sequence."
)
def _solve(
self,
experiment,
param_distributions,
n_iter,
random_state,
error_score,
backend,
backend_params,
):
"""Sample ``n_iter`` points and return the best parameter set."""
self._check_param_distributions(param_distributions)
sampler = ParameterSampler(
param_distributions=param_distributions,
n_iter=n_iter,
random_state=random_state,
)
candidate_params = list(sampler)
meta = {
"experiment": experiment,
"error_score": error_score,
}
scores = parallelize(
fun=_score_params,
iter=candidate_params,
meta=meta,
backend=backend,
backend_params=backend_params,
)
best_index = int(np.argmin(scores)) # lower-is-better convention
best_params = candidate_params[best_index]
# public attributes for external consumers
self.best_index_ = best_index
self.best_score_ = float(scores[best_index])
self.best_params_ = best_params
return best_params
@classmethod
def get_test_params(cls, parameter_set: str = "default"):
"""Provide deterministic benchmark configurations for unit tests."""
from hyperactive.experiment.bench import Ackley
from hyperactive.experiment.integrations import SklearnCvExperiment
# 1) ML example (Iris + SVC)
sklearn_exp = SklearnCvExperiment.create_test_instance()
param_dist_1 = {
"C": [0.01, 0.1, 1, 10],
"gamma": np.logspace(-4, 1, 6),
}
params_sklearn = {
"experiment": sklearn_exp,
"param_distributions": param_dist_1,
"n_iter": 5,
"random_state": 42,
}
# 2) continuous optimisation example (Ackley)
ackley_exp = Ackley.create_test_instance()
param_dist_2 = {
"x0": np.linspace(-5, 5, 50),
"x1": np.linspace(-5, 5, 50),
}
params_ackley = {
"experiment": ackley_exp,
"param_distributions": param_dist_2,
"n_iter": 20,
"random_state": 0,
}
params = [params_sklearn, params_ackley]
from hyperactive.utils.parallel import _get_parallel_test_fixtures
parallel_fixtures = _get_parallel_test_fixtures()
for x in parallel_fixtures:
new_ackley = params_ackley.copy()
new_ackley.update(x)
params.append(new_ackley)
return params