1
|
|
|
"""Base class for optimizer.""" |
2
|
|
|
|
3
|
|
|
from skbase.base import BaseObject |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class BaseOptimizer(BaseObject): |
7
|
|
|
"""Base class for optimizer.""" |
8
|
|
|
|
9
|
|
|
_search_config_names = [] |
10
|
|
|
|
11
|
|
|
def __init__(self): |
12
|
|
|
super().__init__() |
13
|
|
|
assert hasattr(self, "experiment"), "Optimizer must have an experiment." |
14
|
|
|
search_config = self.get_params() |
15
|
|
|
self._experiment = search_config.pop("experiment", None) |
16
|
|
|
|
17
|
|
|
def add_search(self, experiment, **search_config): |
18
|
|
|
"""Add a new optimization search process with specified parameters. |
19
|
|
|
|
20
|
|
|
Parameters |
21
|
|
|
---------- |
22
|
|
|
experiment : BaseExperiment |
23
|
|
|
The experiment to optimize parameters for. |
24
|
|
|
search_config : dict with str keys |
25
|
|
|
Key/value pairs may be any subset of the parameters of the class. |
26
|
|
|
""" |
27
|
|
|
self._experiment = experiment |
28
|
|
|
if not hasattr(self, "_search_config_update"): |
29
|
|
|
self._search_config_update = search_config |
30
|
|
|
else: |
31
|
|
|
self._search_config_update.update(search_config) |
32
|
|
|
|
33
|
|
|
def get_search_config(self): |
34
|
|
|
"""Get the search configuration. |
35
|
|
|
|
36
|
|
|
Returns |
37
|
|
|
------- |
38
|
|
|
dict with str keys |
39
|
|
|
The search configuration dictionary. |
40
|
|
|
""" |
41
|
|
|
search_config = self.get_params(deep=False) |
42
|
|
|
search_config.pop("experiment", None) |
43
|
|
|
if hasattr(self, "_search_config_update"): |
44
|
|
|
search_config.update(self._search_config_update) |
45
|
|
|
return search_config |
46
|
|
|
|
47
|
|
|
def get_experiment(self): |
48
|
|
|
"""Get the experiment. |
49
|
|
|
|
50
|
|
|
Returns |
51
|
|
|
------- |
52
|
|
|
BaseExperiment |
53
|
|
|
The experiment to optimize parameters for. |
54
|
|
|
""" |
55
|
|
|
return self._experiment |
56
|
|
|
|
57
|
|
|
def run(self): |
58
|
|
|
"""Run the optimization search process. |
59
|
|
|
|
60
|
|
|
Returns |
61
|
|
|
------- |
62
|
|
|
best_params : dict |
63
|
|
|
The best parameters found during the optimization process. |
64
|
|
|
""" |
65
|
|
|
experiment = self.get_experiment() |
66
|
|
|
search_config = self.get_search_config() |
67
|
|
|
|
68
|
|
|
return self._run(experiment, **search_config) |
69
|
|
|
|