2021-02-05 21:41:14 -08:00
|
|
|
'''!
|
|
|
|
* Copyright (c) 2020-2021 Microsoft Corporation. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See LICENSE file in the
|
|
|
|
* project root for license information.
|
|
|
|
'''
|
2021-08-12 02:02:22 -04:00
|
|
|
from typing import Dict, Optional, Tuple
|
2021-02-05 21:41:14 -08:00
|
|
|
import numpy as np
|
|
|
|
try:
|
2021-08-12 02:02:22 -04:00
|
|
|
from ray import __version__ as ray_version
|
|
|
|
assert ray_version >= '1.0.0'
|
2021-02-05 21:41:14 -08:00
|
|
|
from ray.tune.suggest import Searcher
|
|
|
|
from ray.tune.suggest.variant_generator import generate_variants
|
|
|
|
from ray.tune import sample
|
2021-02-28 12:43:43 -08:00
|
|
|
from ray.tune.utils.util import flatten_dict, unflatten_dict
|
2021-08-12 02:02:22 -04:00
|
|
|
except (ImportError, AssertionError):
|
2021-02-05 21:41:14 -08:00
|
|
|
from .suggestion import Searcher
|
2021-02-28 12:43:43 -08:00
|
|
|
from .variant_generator import generate_variants, flatten_dict, unflatten_dict
|
2021-02-05 21:41:14 -08:00
|
|
|
from ..tune import sample
|
2021-08-12 02:02:22 -04:00
|
|
|
from ..tune.space import complete_config, denormalize, normalize
|
2021-02-05 21:41:14 -08:00
|
|
|
|
|
|
|
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class FLOW2(Searcher):
|
|
|
|
'''Local search algorithm FLOW2, with adaptive step size
|
|
|
|
'''
|
|
|
|
|
|
|
|
STEPSIZE = 0.1
|
|
|
|
STEP_LOWER_BOUND = 0.0001
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
init_config: dict,
|
|
|
|
metric: Optional[str] = None,
|
|
|
|
mode: Optional[str] = None,
|
|
|
|
space: Optional[dict] = None,
|
|
|
|
prune_attr: Optional[str] = None,
|
|
|
|
min_resource: Optional[float] = None,
|
|
|
|
max_resource: Optional[float] = None,
|
|
|
|
resource_multiple_factor: Optional[float] = 4,
|
2021-07-06 11:32:20 -04:00
|
|
|
cost_attr: Optional[str] = 'time_total_s',
|
2021-02-05 21:41:14 -08:00
|
|
|
seed: Optional[int] = 20):
|
|
|
|
'''Constructor
|
|
|
|
|
|
|
|
Args:
|
2021-04-06 11:37:52 -07:00
|
|
|
init_config: a dictionary of a partial or full initial config,
|
|
|
|
e.g. from a subset of controlled dimensions
|
2021-04-08 09:29:55 -07:00
|
|
|
to the initial low-cost values.
|
|
|
|
e.g. {'epochs': 1}
|
2021-02-05 21:41:14 -08:00
|
|
|
metric: A string of the metric name to optimize for.
|
|
|
|
mode: A string in ['min', 'max'] to specify the objective as
|
2021-06-02 22:08:24 -04:00
|
|
|
minimization or maximization.
|
2021-02-05 21:41:14 -08:00
|
|
|
cat_hp_cost: A dictionary from a subset of categorical dimensions
|
2021-04-08 09:29:55 -07:00
|
|
|
to the relative cost of each choice.
|
2021-02-05 21:41:14 -08:00
|
|
|
e.g.,
|
2021-04-08 09:29:55 -07:00
|
|
|
|
2021-02-05 21:41:14 -08:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
{'tree_method': [1, 1, 2]}
|
2021-04-08 09:29:55 -07:00
|
|
|
|
|
|
|
i.e., the relative cost of the
|
2021-02-05 21:41:14 -08:00
|
|
|
three choices of 'tree_method' is 1, 1 and 2 respectively.
|
|
|
|
space: A dictionary to specify the search space.
|
2021-04-08 09:29:55 -07:00
|
|
|
prune_attr: A string of the attribute used for pruning.
|
2021-02-05 21:41:14 -08:00
|
|
|
Not necessarily in space.
|
2021-04-08 09:29:55 -07:00
|
|
|
When prune_attr is in space, it is a hyperparameter, e.g.,
|
2021-02-05 21:41:14 -08:00
|
|
|
'n_iters', and the best value is unknown.
|
2021-04-08 09:29:55 -07:00
|
|
|
When prune_attr is not in space, it is a resource dimension,
|
2021-02-05 21:41:14 -08:00
|
|
|
e.g., 'sample_size', and the peak performance is assumed
|
|
|
|
to be at the max_resource.
|
2021-04-08 09:29:55 -07:00
|
|
|
min_resource: A float of the minimal resource to use for the
|
2021-02-05 21:41:14 -08:00
|
|
|
prune_attr; only valid if prune_attr is not in space.
|
2021-04-08 09:29:55 -07:00
|
|
|
max_resource: A float of the maximal resource to use for the
|
2021-02-05 21:41:14 -08:00
|
|
|
prune_attr; only valid if prune_attr is not in space.
|
|
|
|
resource_multiple_factor: A float of the multiplicative factor
|
|
|
|
used for increasing resource.
|
2021-07-06 11:32:20 -04:00
|
|
|
cost_attr: A string of the attribute used for cost.
|
2021-02-05 21:41:14 -08:00
|
|
|
seed: An integer of the random seed.
|
|
|
|
'''
|
|
|
|
if mode:
|
|
|
|
assert mode in ["min", "max"], "`mode` must be 'min' or 'max'."
|
|
|
|
else:
|
|
|
|
mode = "min"
|
|
|
|
|
|
|
|
super(FLOW2, self).__init__(
|
|
|
|
metric=metric,
|
|
|
|
mode=mode)
|
|
|
|
# internally minimizes, so "max" => -1
|
|
|
|
if mode == "max":
|
|
|
|
self.metric_op = -1.
|
|
|
|
elif mode == "min":
|
|
|
|
self.metric_op = 1.
|
|
|
|
self.space = space or {}
|
2021-08-12 02:02:22 -04:00
|
|
|
self._space = flatten_dict(self.space, prevent_delimiter=True)
|
2021-02-05 21:41:14 -08:00
|
|
|
self._random = np.random.RandomState(seed)
|
|
|
|
self._seed = seed
|
2021-02-28 12:43:43 -08:00
|
|
|
self.init_config = init_config
|
|
|
|
self.best_config = flatten_dict(init_config)
|
2021-02-05 21:41:14 -08:00
|
|
|
self.prune_attr = prune_attr
|
|
|
|
self.min_resource = min_resource
|
|
|
|
self.resource_multiple_factor = resource_multiple_factor or 4
|
2021-07-06 11:32:20 -04:00
|
|
|
self.cost_attr = cost_attr
|
2021-02-05 21:41:14 -08:00
|
|
|
self.max_resource = max_resource
|
|
|
|
self._resource = None
|
|
|
|
self._step_lb = np.Inf
|
|
|
|
if space:
|
|
|
|
self._init_search()
|
|
|
|
|
|
|
|
def _init_search(self):
|
|
|
|
self._tunable_keys = []
|
|
|
|
self._bounded_keys = []
|
|
|
|
self._unordered_cat_hp = {}
|
2021-08-12 02:02:22 -04:00
|
|
|
hier = False
|
|
|
|
for key, domain in self._space.items():
|
2021-04-08 09:29:55 -07:00
|
|
|
assert not (isinstance(domain, dict) and 'grid_search' in domain), \
|
|
|
|
f"{key}'s domain is grid search, not supported in FLOW^2."
|
2021-02-05 21:41:14 -08:00
|
|
|
if callable(getattr(domain, 'get_sampler', None)):
|
|
|
|
self._tunable_keys.append(key)
|
|
|
|
sampler = domain.get_sampler()
|
2021-06-04 10:31:33 -07:00
|
|
|
# the step size lower bound for uniform variables doesn't depend
|
|
|
|
# on the current config
|
|
|
|
if isinstance(sampler, sample.Quantized):
|
2021-08-12 02:02:22 -04:00
|
|
|
q = sampler.q
|
|
|
|
sampler = sampler.get_sampler()
|
|
|
|
if str(sampler) == 'Uniform':
|
2021-06-04 10:31:33 -07:00
|
|
|
self._step_lb = min(
|
2021-08-12 02:02:22 -04:00
|
|
|
self._step_lb, q / (domain.upper - domain.lower))
|
2021-06-04 10:31:33 -07:00
|
|
|
elif isinstance(domain, sample.Integer) and str(sampler) == 'Uniform':
|
|
|
|
self._step_lb = min(
|
2021-08-12 02:02:22 -04:00
|
|
|
self._step_lb, 1.0 / (domain.upper - 1 - domain.lower))
|
2021-03-05 23:39:14 -08:00
|
|
|
if isinstance(domain, sample.Categorical):
|
2021-08-12 02:02:22 -04:00
|
|
|
if not domain.ordered:
|
2021-04-08 09:29:55 -07:00
|
|
|
self._unordered_cat_hp[key] = len(domain.categories)
|
2021-08-12 02:02:22 -04:00
|
|
|
if not hier:
|
|
|
|
for cat in domain.categories:
|
|
|
|
if isinstance(cat, dict):
|
|
|
|
hier = True
|
|
|
|
break
|
2021-02-05 21:41:14 -08:00
|
|
|
if str(sampler) != 'Normal':
|
|
|
|
self._bounded_keys.append(key)
|
2021-08-12 02:02:22 -04:00
|
|
|
if not hier:
|
|
|
|
self._space_keys = sorted(self._space.keys())
|
|
|
|
self._hierarchical = hier
|
|
|
|
if (self.prune_attr and self.prune_attr not in self._space
|
2021-04-08 09:29:55 -07:00
|
|
|
and self.max_resource):
|
2021-02-05 21:41:14 -08:00
|
|
|
self.min_resource = self.min_resource or self._min_resource()
|
|
|
|
self._resource = self._round(self.min_resource)
|
2021-08-12 02:02:22 -04:00
|
|
|
if not hier:
|
|
|
|
self._space_keys.append(self.prune_attr)
|
2021-04-08 09:29:55 -07:00
|
|
|
else:
|
|
|
|
self._resource = None
|
2021-02-05 21:41:14 -08:00
|
|
|
self.incumbent = {}
|
2021-04-08 09:29:55 -07:00
|
|
|
self.incumbent = self.normalize(self.best_config) # flattened
|
2021-02-05 21:41:14 -08:00
|
|
|
self.best_obj = self.cost_incumbent = None
|
|
|
|
self.dim = len(self._tunable_keys) # total # tunable dimensions
|
2021-04-08 09:29:55 -07:00
|
|
|
self._direction_tried = None
|
2021-02-05 21:41:14 -08:00
|
|
|
self._num_complete4incumbent = self._cost_complete4incumbent = 0
|
|
|
|
self._num_allowed4incumbent = 2 * self.dim
|
|
|
|
self._proposed_by = {} # trial_id: int -> incumbent: Dict
|
2021-07-05 21:17:26 -04:00
|
|
|
self.step_ub = np.sqrt(self.dim)
|
|
|
|
self.step = self.STEPSIZE * self.step_ub
|
2021-02-05 21:41:14 -08:00
|
|
|
lb = self.step_lower_bound
|
2021-04-08 09:29:55 -07:00
|
|
|
if lb > self.step:
|
|
|
|
self.step = lb * 2
|
2021-02-05 21:41:14 -08:00
|
|
|
# upper bound
|
2021-04-08 09:29:55 -07:00
|
|
|
if self.step > self.step_ub:
|
|
|
|
self.step = self.step_ub
|
2021-02-05 21:41:14 -08:00
|
|
|
# maximal # consecutive no improvements
|
2021-06-25 14:24:46 -07:00
|
|
|
self.dir = 2**(min(9, self.dim))
|
2021-05-07 04:29:38 +00:00
|
|
|
self._configs = {} # dict from trial_id to (config, stepsize)
|
2021-02-05 21:41:14 -08:00
|
|
|
self._K = 0
|
2021-05-07 04:29:38 +00:00
|
|
|
self._iter_best_config = self.trial_count_proposed = self.trial_count_complete = 1
|
|
|
|
self._num_proposedby_incumbent = 0
|
2021-02-05 21:41:14 -08:00
|
|
|
self._reset_times = 0
|
2021-02-22 22:10:41 -08:00
|
|
|
# record intermediate trial cost
|
|
|
|
self._trial_cost = {}
|
2021-07-05 21:17:26 -04:00
|
|
|
self._same = False # whether the proposed config is the same as best_config
|
|
|
|
self._init_phase = True # initial phase to increase initial stepsize
|
|
|
|
self._trunc = 0
|
|
|
|
# no truncation by default. when > 0, it means how many
|
|
|
|
# non-zero dimensions to keep in the random unit vector
|
2021-02-05 21:41:14 -08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def step_lower_bound(self) -> float:
|
|
|
|
step_lb = self._step_lb
|
|
|
|
for key in self._tunable_keys:
|
2021-04-08 09:29:55 -07:00
|
|
|
if key not in self.best_config:
|
|
|
|
continue
|
2021-08-12 02:02:22 -04:00
|
|
|
domain = self._space[key]
|
2021-02-05 21:41:14 -08:00
|
|
|
sampler = domain.get_sampler()
|
2021-06-04 10:31:33 -07:00
|
|
|
# the stepsize lower bound for log uniform variables depends on the
|
|
|
|
# current config
|
2021-02-05 21:41:14 -08:00
|
|
|
if isinstance(sampler, sample.Quantized):
|
2021-08-12 02:02:22 -04:00
|
|
|
q = sampler.q
|
2021-02-05 21:41:14 -08:00
|
|
|
sampler_inner = sampler.get_sampler()
|
|
|
|
if str(sampler_inner) == 'LogUniform':
|
2021-04-08 09:29:55 -07:00
|
|
|
step_lb = min(
|
2021-08-12 02:02:22 -04:00
|
|
|
step_lb, np.log(1.0 + q / self.best_config[key])
|
2021-04-08 09:29:55 -07:00
|
|
|
/ np.log(domain.upper / domain.lower))
|
|
|
|
elif isinstance(domain, sample.Integer) and str(sampler) == 'LogUniform':
|
|
|
|
step_lb = min(
|
|
|
|
step_lb, np.log(1.0 + 1.0 / self.best_config[key])
|
2021-08-12 02:02:22 -04:00
|
|
|
/ np.log((domain.upper - 1) / domain.lower))
|
2021-04-08 09:29:55 -07:00
|
|
|
if np.isinf(step_lb):
|
|
|
|
step_lb = self.STEP_LOWER_BOUND
|
|
|
|
else:
|
2021-07-05 21:17:26 -04:00
|
|
|
step_lb *= self.step_ub
|
2021-02-05 21:41:14 -08:00
|
|
|
return step_lb
|
2021-04-08 09:29:55 -07:00
|
|
|
|
2021-02-05 21:41:14 -08:00
|
|
|
@property
|
|
|
|
def resource(self) -> float:
|
|
|
|
return self._resource
|
|
|
|
|
|
|
|
def _min_resource(self) -> float:
|
|
|
|
''' automatically decide minimal resource
|
|
|
|
'''
|
|
|
|
return self.max_resource / np.pow(self.resource_multiple_factor, 5)
|
|
|
|
|
|
|
|
def _round(self, resource) -> float:
|
|
|
|
''' round the resource to self.max_resource if close to it
|
|
|
|
'''
|
|
|
|
if resource * self.resource_multiple_factor > self.max_resource:
|
|
|
|
return self.max_resource
|
|
|
|
return resource
|
|
|
|
|
2021-04-08 09:29:55 -07:00
|
|
|
def rand_vector_gaussian(self, dim, std=1.0):
|
2021-02-05 21:41:14 -08:00
|
|
|
vec = self._random.normal(0, std, dim)
|
|
|
|
return vec
|
2021-04-08 09:29:55 -07:00
|
|
|
|
|
|
|
def complete_config(
|
|
|
|
self, partial_config: Dict,
|
|
|
|
lower: Optional[Dict] = None, upper: Optional[Dict] = None
|
2021-08-12 02:02:22 -04:00
|
|
|
) -> Tuple[Dict, Dict]:
|
2021-02-05 21:41:14 -08:00
|
|
|
''' generate a complete config from the partial config input
|
|
|
|
add minimal resource to config if available
|
|
|
|
'''
|
2021-08-12 02:02:22 -04:00
|
|
|
disturb = self._reset_times and partial_config == self.init_config
|
|
|
|
# if not the first time to complete init_config, use random gaussian
|
|
|
|
config, space = complete_config(
|
|
|
|
partial_config, self.space, self, disturb, lower, upper)
|
2021-04-08 09:29:55 -07:00
|
|
|
if partial_config == self.init_config:
|
|
|
|
self._reset_times += 1
|
2021-02-05 21:41:14 -08:00
|
|
|
if self._resource:
|
|
|
|
config[self.prune_attr] = self.min_resource
|
2021-08-12 02:02:22 -04:00
|
|
|
return config, space
|
2021-02-05 21:41:14 -08:00
|
|
|
|
2021-08-12 02:02:22 -04:00
|
|
|
def create(self, init_config: Dict, obj: float, cost: float, space: Dict
|
|
|
|
) -> Searcher:
|
|
|
|
# space is the subspace where the init_config is located
|
2021-07-05 21:17:26 -04:00
|
|
|
flow2 = self.__class__(
|
2021-08-12 02:02:22 -04:00
|
|
|
init_config, self.metric, self.mode,
|
|
|
|
space, self.prune_attr,
|
2021-07-05 21:17:26 -04:00
|
|
|
self.min_resource, self.max_resource,
|
2021-07-06 11:32:20 -04:00
|
|
|
self.resource_multiple_factor, self.cost_attr, self._seed + 1)
|
2021-02-05 21:41:14 -08:00
|
|
|
flow2.best_obj = obj * self.metric_op # minimize internally
|
|
|
|
flow2.cost_incumbent = cost
|
2021-07-05 21:17:26 -04:00
|
|
|
self._seed += 1
|
2021-02-05 21:41:14 -08:00
|
|
|
return flow2
|
|
|
|
|
2021-08-12 02:02:22 -04:00
|
|
|
def normalize(self, config, recursive=False) -> Dict:
|
2021-02-05 21:41:14 -08:00
|
|
|
''' normalize each dimension in config to [0,1]
|
|
|
|
'''
|
2021-08-12 02:02:22 -04:00
|
|
|
return normalize(
|
|
|
|
config, self._space, self.best_config, self.incumbent, recursive)
|
2021-02-05 21:41:14 -08:00
|
|
|
|
|
|
|
def denormalize(self, config):
|
|
|
|
''' denormalize each dimension in config from [0,1]
|
|
|
|
'''
|
2021-08-12 02:02:22 -04:00
|
|
|
return denormalize(
|
|
|
|
config, self._space, self.best_config, self.incumbent, self._random)
|
2021-02-05 21:41:14 -08:00
|
|
|
|
|
|
|
def set_search_properties(self,
|
|
|
|
metric: Optional[str] = None,
|
|
|
|
mode: Optional[str] = None,
|
|
|
|
config: Optional[Dict] = None) -> bool:
|
|
|
|
if metric:
|
|
|
|
self._metric = metric
|
|
|
|
if mode:
|
|
|
|
assert mode in ["min", "max"], "`mode` must be 'min' or 'max'."
|
2021-03-05 23:39:14 -08:00
|
|
|
self._mode = mode
|
2021-02-05 21:41:14 -08:00
|
|
|
if mode == "max":
|
|
|
|
self.metric_op = -1.
|
|
|
|
elif mode == "min":
|
|
|
|
self.metric_op = 1.
|
|
|
|
if config:
|
|
|
|
self.space = config
|
2021-08-12 02:02:22 -04:00
|
|
|
self._space = flatten_dict(self.space)
|
2021-02-05 21:41:14 -08:00
|
|
|
self._init_search()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def on_trial_complete(self, trial_id: str, result: Optional[Dict] = None,
|
|
|
|
error: bool = False):
|
|
|
|
''' compare with incumbent
|
|
|
|
'''
|
|
|
|
# if better, move, reset num_complete and num_proposed
|
|
|
|
# if not better and num_complete >= 2*dim, num_allowed += 2
|
2021-05-07 04:29:38 +00:00
|
|
|
self.trial_count_complete += 1
|
2021-02-05 21:41:14 -08:00
|
|
|
if not error and result:
|
|
|
|
obj = result.get(self._metric)
|
2021-04-08 09:29:55 -07:00
|
|
|
if obj:
|
2021-02-05 21:41:14 -08:00
|
|
|
obj *= self.metric_op
|
2021-02-28 12:43:43 -08:00
|
|
|
if self.best_obj is None or obj < self.best_obj:
|
2021-05-07 04:29:38 +00:00
|
|
|
self.best_obj = obj
|
|
|
|
self.best_config, self.step = self._configs[trial_id]
|
2021-02-05 21:41:14 -08:00
|
|
|
self.incumbent = self.normalize(self.best_config)
|
|
|
|
self.cost_incumbent = result.get(self.cost_attr)
|
|
|
|
if self._resource:
|
|
|
|
self._resource = self.best_config[self.prune_attr]
|
|
|
|
self._num_complete4incumbent = 0
|
|
|
|
self._cost_complete4incumbent = 0
|
2021-05-07 04:29:38 +00:00
|
|
|
self._num_proposedby_incumbent = 0
|
2021-02-05 21:41:14 -08:00
|
|
|
self._num_allowed4incumbent = 2 * self.dim
|
|
|
|
self._proposed_by.clear()
|
2021-04-08 09:29:55 -07:00
|
|
|
if self._K > 0:
|
2021-02-28 12:43:43 -08:00
|
|
|
# self._oldK must have been set when self._K>0
|
2021-04-08 09:29:55 -07:00
|
|
|
self.step *= np.sqrt(self._K / self._oldK)
|
|
|
|
if self.step > self.step_ub:
|
|
|
|
self.step = self.step_ub
|
2021-05-07 04:29:38 +00:00
|
|
|
self._iter_best_config = self.trial_count_complete
|
2021-07-05 21:17:26 -04:00
|
|
|
if self._trunc:
|
|
|
|
self._trunc = min(self._trunc + 1, self.dim)
|
2021-02-05 21:41:14 -08:00
|
|
|
return
|
2021-07-05 21:17:26 -04:00
|
|
|
elif self._trunc:
|
|
|
|
self._trunc = max(self._trunc >> 1, 1)
|
2021-02-05 21:41:14 -08:00
|
|
|
proposed_by = self._proposed_by.get(trial_id)
|
|
|
|
if proposed_by == self.incumbent:
|
|
|
|
# proposed by current incumbent and no better
|
|
|
|
self._num_complete4incumbent += 1
|
2021-02-22 22:10:41 -08:00
|
|
|
cost = result.get(
|
|
|
|
self.cost_attr) if result else self._trial_cost.get(trial_id)
|
2021-04-08 09:29:55 -07:00
|
|
|
if cost:
|
|
|
|
self._cost_complete4incumbent += cost
|
|
|
|
if self._num_complete4incumbent >= 2 * self.dim and \
|
|
|
|
self._num_allowed4incumbent == 0:
|
2021-02-05 21:41:14 -08:00
|
|
|
self._num_allowed4incumbent = 2
|
2021-04-08 09:29:55 -07:00
|
|
|
if self._num_complete4incumbent == self.dir and (
|
|
|
|
not self._resource or self._resource == self.max_resource):
|
|
|
|
# check stuck condition if using max resource
|
2021-02-05 21:41:14 -08:00
|
|
|
self._num_complete4incumbent -= 2
|
|
|
|
if self._num_allowed4incumbent < 2:
|
|
|
|
self._num_allowed4incumbent = 2
|
2021-04-08 09:29:55 -07:00
|
|
|
# elif proposed_by: del self._proposed_by[trial_id]
|
|
|
|
|
2021-02-05 21:41:14 -08:00
|
|
|
def on_trial_result(self, trial_id: str, result: Dict):
|
|
|
|
''' early update of incumbent
|
|
|
|
'''
|
|
|
|
if result:
|
|
|
|
obj = result.get(self._metric)
|
2021-04-08 09:29:55 -07:00
|
|
|
if obj:
|
2021-02-05 21:41:14 -08:00
|
|
|
obj *= self.metric_op
|
2021-02-28 12:43:43 -08:00
|
|
|
if self.best_obj is None or obj < self.best_obj:
|
2021-02-05 21:41:14 -08:00
|
|
|
self.best_obj = obj
|
2021-05-07 04:29:38 +00:00
|
|
|
config = self._configs[trial_id][0]
|
2021-02-05 21:41:14 -08:00
|
|
|
if self.best_config != config:
|
|
|
|
self.best_config = config
|
|
|
|
if self._resource:
|
|
|
|
self._resource = config[self.prune_attr]
|
|
|
|
self.incumbent = self.normalize(self.best_config)
|
|
|
|
self.cost_incumbent = result.get(self.cost_attr)
|
|
|
|
self._cost_complete4incumbent = 0
|
|
|
|
self._num_complete4incumbent = 0
|
2021-05-07 04:29:38 +00:00
|
|
|
self._num_proposedby_incumbent = 0
|
2021-02-05 21:41:14 -08:00
|
|
|
self._num_allowed4incumbent = 2 * self.dim
|
|
|
|
self._proposed_by.clear()
|
2021-05-07 04:29:38 +00:00
|
|
|
self._iter_best_config = self.trial_count_complete
|
2021-02-22 22:10:41 -08:00
|
|
|
cost = result.get(self.cost_attr)
|
|
|
|
# record the cost in case it is pruned and cost info is lost
|
|
|
|
self._trial_cost[trial_id] = cost
|
2021-02-05 21:41:14 -08:00
|
|
|
|
2021-07-05 21:17:26 -04:00
|
|
|
def rand_vector_unit_sphere(self, dim, trunc=0) -> np.ndarray:
|
2021-02-05 21:41:14 -08:00
|
|
|
vec = self._random.normal(0, 1, dim)
|
2021-07-05 21:17:26 -04:00
|
|
|
if 0 < trunc < dim:
|
|
|
|
vec[np.abs(vec).argsort()[:dim - trunc]] = 0
|
2021-03-28 17:54:25 -07:00
|
|
|
mag = np.linalg.norm(vec)
|
2021-04-08 09:29:55 -07:00
|
|
|
return vec / mag
|
2021-02-05 21:41:14 -08:00
|
|
|
|
|
|
|
def suggest(self, trial_id: str) -> Optional[Dict]:
|
|
|
|
''' suggest a new config, one of the following cases:
|
|
|
|
1. same incumbent, increase resource
|
|
|
|
2. same resource, move from the incumbent to a random direction
|
|
|
|
3. same resource, move from the incumbent to the opposite direction
|
2021-06-02 22:08:24 -04:00
|
|
|
#TODO: better decouple FLOW2 config suggestion and stepsize update
|
2021-02-05 21:41:14 -08:00
|
|
|
'''
|
2021-05-07 04:29:38 +00:00
|
|
|
self.trial_count_proposed += 1
|
2021-02-05 21:41:14 -08:00
|
|
|
if self._num_complete4incumbent > 0 and self.cost_incumbent and \
|
|
|
|
self._resource and self._resource < self.max_resource and (
|
2021-04-08 09:29:55 -07:00
|
|
|
self._cost_complete4incumbent
|
|
|
|
>= self.cost_incumbent * self.resource_multiple_factor):
|
2021-02-05 21:41:14 -08:00
|
|
|
# consider increasing resource using sum eval cost of complete
|
|
|
|
# configs
|
2021-05-07 04:29:38 +00:00
|
|
|
old_resource = self._resource
|
2021-02-05 21:41:14 -08:00
|
|
|
self._resource = self._round(
|
|
|
|
self._resource * self.resource_multiple_factor)
|
2021-05-07 04:29:38 +00:00
|
|
|
self.cost_incumbent *= self._resource / old_resource
|
2021-02-05 21:41:14 -08:00
|
|
|
config = self.best_config.copy()
|
|
|
|
config[self.prune_attr] = self._resource
|
|
|
|
self._direction_tried = None
|
2021-05-07 04:29:38 +00:00
|
|
|
self._configs[trial_id] = (config, self.step)
|
2021-07-06 11:32:20 -04:00
|
|
|
return unflatten_dict(config)
|
2021-02-05 21:41:14 -08:00
|
|
|
self._num_allowed4incumbent -= 1
|
|
|
|
move = self.incumbent.copy()
|
|
|
|
if self._direction_tried is not None:
|
|
|
|
# return negative direction
|
|
|
|
for i, key in enumerate(self._tunable_keys):
|
2021-04-08 09:29:55 -07:00
|
|
|
move[key] -= self._direction_tried[i]
|
2021-02-05 21:41:14 -08:00
|
|
|
self._direction_tried = None
|
2021-05-01 00:19:41 +00:00
|
|
|
else:
|
|
|
|
# propose a new direction
|
|
|
|
self._direction_tried = self.rand_vector_unit_sphere(
|
2021-07-05 21:17:26 -04:00
|
|
|
self.dim, self._trunc) * self.step
|
2021-05-01 00:19:41 +00:00
|
|
|
for i, key in enumerate(self._tunable_keys):
|
|
|
|
move[key] += self._direction_tried[i]
|
2021-02-05 21:41:14 -08:00
|
|
|
self._project(move)
|
|
|
|
config = self.denormalize(move)
|
|
|
|
self._proposed_by[trial_id] = self.incumbent
|
2021-05-07 04:29:38 +00:00
|
|
|
self._configs[trial_id] = (config, self.step)
|
|
|
|
self._num_proposedby_incumbent += 1
|
2021-07-06 11:32:20 -04:00
|
|
|
best_config = self.best_config
|
2021-07-05 21:17:26 -04:00
|
|
|
if self._init_phase:
|
2021-05-18 15:57:42 -07:00
|
|
|
if self._direction_tried is None:
|
2021-05-07 04:29:38 +00:00
|
|
|
if self._same:
|
2021-07-05 21:17:26 -04:00
|
|
|
# check if the new config is different from best_config
|
2021-05-07 04:29:38 +00:00
|
|
|
same = True
|
|
|
|
for key, value in config.items():
|
2021-07-05 21:17:26 -04:00
|
|
|
if key not in best_config or value != best_config[key]:
|
2021-05-07 04:29:38 +00:00
|
|
|
same = False
|
|
|
|
break
|
|
|
|
if same:
|
|
|
|
# increase step size
|
|
|
|
self.step += self.STEPSIZE
|
|
|
|
if self.step > self.step_ub:
|
|
|
|
self.step = self.step_ub
|
|
|
|
else:
|
2021-07-05 21:17:26 -04:00
|
|
|
# check if the new config is different from best_config
|
2021-05-07 04:29:38 +00:00
|
|
|
same = True
|
|
|
|
for key, value in config.items():
|
2021-07-05 21:17:26 -04:00
|
|
|
if key not in best_config or value != best_config[key]:
|
2021-05-07 04:29:38 +00:00
|
|
|
same = False
|
|
|
|
break
|
|
|
|
self._same = same
|
|
|
|
if self._num_proposedby_incumbent == self.dir and (
|
2021-05-18 15:57:42 -07:00
|
|
|
not self._resource or self._resource == self.max_resource):
|
|
|
|
# check stuck condition if using max resource
|
|
|
|
self._num_proposedby_incumbent -= 2
|
2021-07-05 21:17:26 -04:00
|
|
|
self._init_phase = False
|
2021-05-18 15:57:42 -07:00
|
|
|
if self.step >= self.step_lower_bound:
|
|
|
|
# decrease step size
|
|
|
|
self._oldK = self._K if self._K else self._iter_best_config
|
|
|
|
self._K = self.trial_count_proposed + 1
|
|
|
|
self.step *= np.sqrt(self._oldK / self._K)
|
|
|
|
else:
|
|
|
|
return None
|
2021-07-05 21:17:26 -04:00
|
|
|
if self._init_phase:
|
|
|
|
return unflatten_dict(config)
|
|
|
|
if self._trunc == 1 and self._direction_tried is not None:
|
|
|
|
# random
|
|
|
|
for i, key in enumerate(self._tunable_keys):
|
|
|
|
if self._direction_tried[i] != 0:
|
|
|
|
for _, generated in generate_variants({'config': {
|
2021-08-12 02:02:22 -04:00
|
|
|
key: self._space[key]
|
2021-07-05 21:17:26 -04:00
|
|
|
}}):
|
|
|
|
if generated['config'][key] != best_config[key]:
|
|
|
|
config[key] = generated['config'][key]
|
|
|
|
return unflatten_dict(config)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# check if config == best_config
|
|
|
|
if len(config) == len(best_config):
|
|
|
|
for key, value in best_config.items():
|
|
|
|
if value != config[key]:
|
|
|
|
return unflatten_dict(config)
|
|
|
|
# print('move to', move)
|
|
|
|
self.incumbent = move
|
2021-02-28 12:43:43 -08:00
|
|
|
return unflatten_dict(config)
|
2021-02-05 21:41:14 -08:00
|
|
|
|
|
|
|
def _project(self, config):
|
|
|
|
''' project normalized config in the feasible region and set prune_attr
|
|
|
|
'''
|
|
|
|
for key in self._bounded_keys:
|
|
|
|
value = config[key]
|
|
|
|
config[key] = max(0, min(1, value))
|
2021-04-08 09:29:55 -07:00
|
|
|
if self._resource:
|
|
|
|
config[self.prune_attr] = self._resource
|
2021-02-05 21:41:14 -08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def can_suggest(self) -> bool:
|
|
|
|
''' can't suggest if 2*dim configs have been proposed for the incumbent
|
|
|
|
while fewer are completed
|
|
|
|
'''
|
|
|
|
return self._num_allowed4incumbent > 0
|
|
|
|
|
2021-08-12 02:02:22 -04:00
|
|
|
def config_signature(self, config, space: Dict = None) -> tuple:
|
2021-02-05 21:41:14 -08:00
|
|
|
''' return the signature tuple of a config
|
|
|
|
'''
|
2021-02-28 12:43:43 -08:00
|
|
|
config = flatten_dict(config)
|
2021-08-12 02:02:22 -04:00
|
|
|
if space:
|
|
|
|
space = flatten_dict(space)
|
|
|
|
else:
|
|
|
|
space = self._space
|
2021-02-05 21:41:14 -08:00
|
|
|
value_list = []
|
2021-08-12 02:02:22 -04:00
|
|
|
keys = sorted(config.keys()) if self._hierarchical else self._space_keys
|
|
|
|
for key in keys:
|
|
|
|
value = config[key]
|
|
|
|
if key == self.prune_attr:
|
|
|
|
value_list.append(value)
|
|
|
|
# else key must be in self.space
|
|
|
|
# get rid of list type or constant,
|
|
|
|
# e.g., "eval_metric": ["logloss", "error"]
|
|
|
|
elif isinstance(space[key], sample.Integer):
|
|
|
|
value_list.append(int(round(value)))
|
2021-02-05 21:41:14 -08:00
|
|
|
else:
|
2021-08-12 02:02:22 -04:00
|
|
|
value_list.append(value)
|
2021-02-05 21:41:14 -08:00
|
|
|
return tuple(value_list)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def converged(self) -> bool:
|
|
|
|
''' return whether the local search has converged
|
|
|
|
'''
|
2021-04-08 09:29:55 -07:00
|
|
|
if self._num_complete4incumbent < self.dir - 2:
|
|
|
|
return False
|
2021-02-05 21:41:14 -08:00
|
|
|
# check stepsize after enough configs are completed
|
|
|
|
return self.step < self.step_lower_bound
|
|
|
|
|
|
|
|
def reach(self, other: Searcher) -> bool:
|
|
|
|
''' whether the incumbent can reach the incumbent of other
|
|
|
|
'''
|
2021-04-08 09:29:55 -07:00
|
|
|
config1, config2 = self.best_config, other.best_config
|
2021-02-05 21:41:14 -08:00
|
|
|
incumbent1, incumbent2 = self.incumbent, other.incumbent
|
2021-04-08 09:29:55 -07:00
|
|
|
if self._resource and config1[self.prune_attr] > config2[self.prune_attr]:
|
2021-02-05 21:41:14 -08:00
|
|
|
# resource will not decrease
|
|
|
|
return False
|
|
|
|
for key in self._unordered_cat_hp:
|
|
|
|
# unordered cat choice is hard to reach by chance
|
2021-04-08 09:29:55 -07:00
|
|
|
if config1[key] != config2[key]:
|
|
|
|
return False
|
|
|
|
delta = np.array(
|
|
|
|
[incumbent1[key] - incumbent2[key] for key in self._tunable_keys])
|
2021-02-05 21:41:14 -08:00
|
|
|
return np.linalg.norm(delta) <= self.step
|