Exemple #1
0
 def prepareScoring(
     self, spaceSpec: typing.Tuple["skopt.space.space.Real",
                                   "skopt.space.space.Integer",
                                   "skopt.space.space.Categorical"]
 ) -> typing.Tuple[int, str, typing.Tuple["skopt.space.space.Real",
                                          "skopt.space.space.Integer",
                                          "skopt.space.space.Categorical"]]:
     from skopt.utils import cook_estimator, normalize_dimensions
     normalized = normalize_dimensions(spaceSpec)
     base_estimator = cook_estimator(self.__class__.skoptAlgo,
                                     space=normalized,
                                     random_state=None)
     optimizer = skopt.Optimizer(
         normalized,
         base_estimator,
         n_initial_points=0,
         acq_func=self.acquisitionType,
         acq_optimizer=self.acquisitionOptimizerType,
         acq_optimizer_kwargs={
             "n_points": self.iters,
             "n_restarts_optimizer": self.nRestartsOptimizer,
             "n_jobs": self.jobs
         },
         acq_func_kwargs={
             "xi": self.chi,
             "kappa": self.kappa
         })
     return (self.iters, "SKOpt (" + self.__class__.skoptAlgo + ")",
             optimizer)
Exemple #2
0
    def set_optimizer(self, n_iter, opt_seed, acq_func, gp_seed, **kwargs):
        self.logger.info('Retrieving model stored at: {}'.format(self.optimizer_path))
        try:
            optimizer = load(self.optimizer_path)
            self.logger.info('Loading model stored at: {}'.format(self.optimizer_path))
            finished_iter = np.array(optimizer.yi).shape[0]
            if finished_iter == 0:
                optimizer = None
                self.logger.info('Optimizer did not finish any iterations so setting optimizer to null')
        except KeyError:
            self.logger.error('Cannot open the file {}'.format(self.optimizer_path))
            optimizer = None

        except ValueError:
            self.logger.error('Cannot open the file {}'.format(self.optimizer_path))
            optimizer = None
        except FileNotFoundError:
            self.logger.error('No such file or directory: {}'.format(self.optimizer_path))
            optimizer = None

        if optimizer is not None:
            n_iter = n_iter - finished_iter
            if n_iter < 0:
                n_iter = 0
            self.logger.info('Iterations already done: {} and running iterations {}'.format(finished_iter, n_iter))
            self.opt = optimizer
            self.logger.debug('Setting the provided optimizer')
            self.log_best_params()
        else:
            transformed = []
            for param in self.parameter_ranges:
                transformed.append(check_dimension(param))
            self.logger.info("Parameter Space: {}".format(transformed))
            norm_space = normalize_dimensions(transformed)
            self.logger.info("Parameter Space after transformation: {}".format(norm_space))
            categorical_space = np.array([isinstance(s, Categorical) for s in norm_space])
            self.logger.info("categorical_space: {}".format(categorical_space))
            if np.all(categorical_space):
                base_estimator = cook_estimator("RF", space=norm_space, random_state=gp_seed)
            else:
                base_estimator = cook_estimator("GP", space=norm_space, random_state=gp_seed, noise="gaussian")

            self.opt = Optimizer(dimensions=self.parameter_ranges, random_state=opt_seed, base_estimator=base_estimator,
                                 acq_func=acq_func, **kwargs)

        return n_iter
Exemple #3
0
def test_acquisition_gradient_cookbook():
    rng = np.random.RandomState(0)
    X = rng.randn(20, 5)
    y = rng.randn(20)
    X_new = rng.randn(5)
    gpr = cook_estimator("GP", Space(((-5.0, 5.0), )), random_state=0)
    gpr.fit(X, y)

    for acq_func in ["LCB", "PI", "EI"]:
        check_gradient_correctness(X_new, gpr, acq_func, np.max(y))
def test_use_given_estimator():
    """ Test that gp_minimize does not use default estimator if one is passed
    in explicitly. """
    domain = [(1.0, 2.0), (3.0, 4.0)]
    noise_correct = 1e+5
    noise_fake = 1e-10
    estimator = cook_estimator("GP", domain, noise=noise_correct)
    res = gp_minimize(branin, domain, n_calls=1, n_random_starts=1,
                      base_estimator=estimator, noise=noise_fake)

    assert res['models'][-1].noise == noise_correct
def test_use_given_estimator():
    """ Test that gp_minimize does not use default estimator if one is passed
    in explicitly. """
    domain = [(1.0, 2.0), (3.0, 4.0)]
    noise_correct = 1e+5
    noise_fake = 1e-10
    estimator = cook_estimator("GP", domain, noise=noise_correct)
    res = gp_minimize(branin, domain, n_calls=1, n_random_starts=1,
                      base_estimator=estimator, noise=noise_fake)

    assert res['models'][-1].noise == noise_correct
def test_acquisition_per_second_gradient(acq_func):
    rng = np.random.RandomState(0)
    X = rng.randn(20, 10)
    # Make the second component large, so that mean_grad and std_grad
    # do not become zero.
    y = np.vstack((X[:, 0], np.abs(X[:, 0])**3)).T

    for X_new in [rng.randn(10), rng.randn(10)]:
        gpr = cook_estimator("GP", Space(((-5.0, 5.0),)), random_state=0)
        mor = MultiOutputRegressor(gpr)
        mor.fit(X, y)
        check_gradient_correctness(X_new, mor, acq_func, 1.5)
Exemple #7
0
def test_acquisition_per_second_gradient(acq_func):
    rng = np.random.RandomState(0)
    X = rng.randn(20, 10)
    # Make the second component large, so that mean_grad and std_grad
    # do not become zero.
    y = np.vstack((X[:, 0], np.abs(X[:, 0])**3)).T

    for X_new in [rng.randn(10), rng.randn(10)]:
        gpr = cook_estimator("GP", Space(((-5.0, 5.0), )), random_state=0)
        mor = MultiOutputRegressor(gpr)
        mor.fit(X, y)
        check_gradient_correctness(X_new, mor, acq_func, 1.5)
Exemple #8
0
 def fit(self, X, y):
     """
     The first estimator returns a constant value.
     The second estimator is a gaussian process regressor that
     models the logarithm of the time.
     """
     X = np.array(X)
     gpr = cook_estimator("GP", self.space, normalize_y=False)
     gpr.fit(X, np.log(np.ravel(X)))
     self.estimators_ = []
     self.estimators_.append(ConstSurrogate())
     self.estimators_.append(gpr)
     return self
def best_fit_curve(x, y):
    from skopt.utils import cook_estimator
    from skopt.space import Real

    # single feature dataset
    X = np.array(x)[:, np.newaxis]

    # fit gaussian process model
    model = cook_estimator('GP', [Real(0.1, 2.0)])
    model.fit(X, y)

    fnc = lambda V: model.predict(V[:, np.newaxis])
    return fnc
 def fit(self, X, y):
     """
     The first estimator returns a constant value.
     The second estimator is a gaussian process regressor that
     models the logarithm of the time.
     """
     X = np.array(X)
     y = np.array(y)
     gpr = cook_estimator("GP", self.space, random_state=0)
     gpr.fit(X, np.log(np.ravel(X)))
     self.estimators_ = []
     self.estimators_.append(ConstSurrogate())
     self.estimators_.append(gpr)
     return self
def test_use_given_estimator_with_max_model_size():
    """ Test that gp_minimize does not use default estimator if one is passed
    in explicitly. """
    domain = [(1.0, 2.0), (3.0, 4.0)]
    noise_correct = 1e+5
    noise_fake = 1e-10
    estimator = cook_estimator("GP", domain, noise=noise_correct)
    res = gp_minimize(branin,
                      domain,
                      n_calls=1,
                      n_initial_points=1,
                      base_estimator=estimator,
                      noise=noise_fake,
                      model_queue_size=1)
    assert len(res['models']) == 1
    assert res['models'][-1].noise == noise_correct
Exemple #12
0
def create_opt(lines, ranker_name):
    gp_seed, opt_seed = get_seed(lines)
    _ranker_class = object_rankers[ranker_name]
    _ranker_class._use_early_stopping = True
    param_ranges = _ranker_class.set_tunable_parameter_ranges({})
    transformed = []
    for param in param_ranges:
        transformed.append(check_dimension(param))
    space = normalize_dimensions(transformed)
    base_estimator = cook_estimator("GP",
                                    space=space,
                                    random_state=gp_seed,
                                    noise="gaussian")
    optimizer = Optimizer(dimensions=param_ranges,
                          random_state=opt_seed,
                          base_estimator=base_estimator)
    return optimizer
Exemple #13
0
def _proc_optimizer_opts(args_dict):
    """Process optimizer kwargs. 
    
    This is meainly used to set different defaults
    for skopt.learn - Tree regressors. 
    """
    from skopt.utils import cook_estimator
    if args_dict is None:
        return {}
    if "base_estimator" in args_dict:
        if args_dict["base_estimator"] == "ET":
            args = {
                "n_estimators": 100,
                "min_samples_leaf": 3,
                "max_depth": None,
                "bootstrap": False
            }
        elif args_dict["base_estimator"] == "ET2":
            args = {
                "n_estimators": 1000,
                "min_samples_leaf": 1,
                "max_depth": None,
                "bootstrap": False
            }
            args_dict["base_estimator"] = "ET"
        elif args_dict["base_estimator"] == "RF":
            args = {
                "n_estimators": 100,
                "min_samples_leaf": 1,
                "max_depth": None,
                "bootstrap": True
            }
        elif args_dict["base_estimator"] == "GBRT":
            args = None
        else:
            args = None
        if args:
            args_dict["base_estimator"] = cook_estimator(
                args_dict["base_estimator"], **args)
    return args_dict
    def __init__(self,
                 dimensions,
                 base_estimator='gp',
                 n_random_starts=None,
                 n_initial_points=10,
                 acq_func='gp_hedge',
                 acq_optimizer='auto',
                 random_state=None,
                 acq_func_kwargs=None,
                 acq_optimizer_kwargs=None):
        """This is nearly identical to :meth:`skopt.optimizer.optimizer.Optimizer.__init__`. It is recreated here to use the
        modified :class:`hyperparameter_hunter.space.Space`, rather than the original `skopt` version. This is not an ideal
        solution, and other options are being considered

        Parameters
        ----------
        dimensions: See :class:`skopt.optimizer.optimizer.Optimizer.__init__`
        base_estimator: See :class:`skopt.optimizer.optimizer.Optimizer.__init__`
        n_random_starts: See :class:`skopt.optimizer.optimizer.Optimizer.__init__`
        n_initial_points: See :class:`skopt.optimizer.optimizer.Optimizer.__init__`
        acq_func: See :class:`skopt.optimizer.optimizer.Optimizer.__init__`
        acq_optimizer: See :class:`skopt.optimizer.optimizer.Optimizer.__init__`
        random_state: See :class:`skopt.optimizer.optimizer.Optimizer.__init__`
        acq_func_kwargs: See :class:`skopt.optimizer.optimizer.Optimizer.__init__`
        acq_optimizer_kwargs: See :class:`skopt.optimizer.optimizer.Optimizer.__init__`"""
        # TODO: Figure out way to override skopt Optimizer's use of skopt Space without having to rewrite __init__
        self.__repeated_ask_kwargs = {}
        self.rng = check_random_state(random_state)

        # Configure acquisition function - Store and create acquisition function set
        self.acq_func = acq_func
        self.acq_func_kwargs = acq_func_kwargs

        allowed_acq_funcs = ['gp_hedge', 'EI', 'LCB', 'PI', 'EIps', 'PIps']
        if self.acq_func not in allowed_acq_funcs:
            raise ValueError(
                F'Expected `acq_func` to be in {allowed_acq_funcs}, got {self.acq_func}'
            )

        # Treat hedging method separately
        if self.acq_func == 'gp_hedge':
            self.cand_acq_funcs_ = ['EI', 'LCB', 'PI']
            self.gains_ = np.zeros(3)
        else:
            self.cand_acq_funcs_ = [self.acq_func]

        if acq_func_kwargs is None:
            acq_func_kwargs = dict()
        self.eta = acq_func_kwargs.get('eta', 1.0)

        # Configure counters of points - Check `n_random_starts` deprecation first
        if n_random_starts is not None:
            warnings.warn((
                '`n_random_starts` will be removed in favour of `n_initial_points`'
            ), DeprecationWarning)
            n_initial_points = n_random_starts
        if n_initial_points < 0:
            raise ValueError(
                F'Expected `n_initial_points` >= 0, got {n_initial_points}')
        self._n_initial_points = n_initial_points
        self.n_initial_points_ = n_initial_points

        # Configure estimator - Build `base_estimator` if doesn't exist
        if isinstance(base_estimator, str):
            base_estimator = cook_estimator(base_estimator,
                                            space=dimensions,
                                            random_state=self.rng.randint(
                                                0,
                                                np.iinfo(np.int32).max))

        # Check if regressor
        if not is_regressor(base_estimator) and base_estimator is not None:
            raise ValueError(
                F'`base_estimator`={base_estimator} must be a regressor')

        # Treat per second acquisition function specially
        is_multi_regressor = isinstance(base_estimator, MultiOutputRegressor)
        if 'ps' in self.acq_func and not is_multi_regressor:
            self.base_estimator_ = MultiOutputRegressor(base_estimator)
        else:
            self.base_estimator_ = base_estimator

        # Configure optimizer - Decide optimizer based on gradient information
        if acq_optimizer == 'auto':
            if has_gradients(self.base_estimator_):
                acq_optimizer = 'lbfgs'
            else:
                acq_optimizer = 'sampling'

        if acq_optimizer not in ['lbfgs', 'sampling']:
            raise ValueError(
                'Expected `acq_optimizer` to be "lbfgs" or "sampling", got {}'.
                format(acq_optimizer))
        if (not has_gradients(self.base_estimator_)
                and acq_optimizer != 'sampling'):
            raise ValueError(
                'The regressor {} should run with `acq_optimizer`="sampling"'.
                format(type(base_estimator)))
        self.acq_optimizer = acq_optimizer

        # Record other arguments
        if acq_optimizer_kwargs is None:
            acq_optimizer_kwargs = dict()

        self.n_points = acq_optimizer_kwargs.get('n_points', 10000)
        self.n_restarts_optimizer = acq_optimizer_kwargs.get(
            'n_restarts_optimizer', 5)
        n_jobs = acq_optimizer_kwargs.get('n_jobs', 1)
        self.n_jobs = n_jobs
        self.acq_optimizer_kwargs = acq_optimizer_kwargs

        # Configure search space - Normalize space if GP regressor
        if isinstance(self.base_estimator_, GaussianProcessRegressor):
            dimensions = normalize_dimensions(dimensions)
        self.space = Space(dimensions)

        # Record categorical and non-categorical indices
        self._cat_inds = []
        self._non_cat_inds = []
        for ind, dim in enumerate(self.space.dimensions):
            if isinstance(dim, Categorical):
                self._cat_inds.append(ind)
            else:
                self._non_cat_inds.append(ind)

        # Initialize storage for optimization
        self.models = []
        self.Xi = []
        self.yi = []

        # Initialize cache for `ask` method responses
        # This ensures that multiple calls to `ask` with n_points set return same sets of points. Reset to {} at call to `tell`
        self.cache_ = {}
 def _prepare_estimator(self):
     """Initialize :attr:`base_estimator` with :attr:`space` via `skopt.utils.cook_estimator`"""
     self.base_estimator = cook_estimator(
         self.base_estimator, space=self.space, **self.base_estimator_kwargs
     )
Exemple #16
0
def test_has_gradients(estimator, gradients):
    space = Space([(-2.0, 2.0)])

    assert has_gradients(cook_estimator(estimator, space=space)) == gradients
Exemple #17
0
    def fit(self,
            X,
            Y,
            total_duration=6e7,
            n_iter=100,
            cv_iter=None,
            optimizer=None,
            acq_func='gp_hedge',
            **kwargs):
        start = datetime.now()

        def splitter(itr):
            for train_idx, test_idx in itr:
                yield X[train_idx], Y[train_idx], X[test_idx], Y[test_idx]

        def splitter_dict(itr_dict):

            n_splits = len(list(itr_dict.values())[0])
            for i in range(n_splits):
                X_train = dict()
                Y_train = dict()
                X_test = dict()
                Y_test = dict()
                for n_obj, itr in itr_dict.items():
                    train_idx = itr[i][0]
                    test_idx = itr[i][1]
                    X_train[n_obj] = np.copy(X[n_obj][train_idx])
                    X_test[n_obj] = np.copy(X[n_obj][test_idx])
                    Y_train[n_obj] = np.copy(Y[n_obj][train_idx])
                    Y_test[n_obj] = np.copy(Y[n_obj][test_idx])
                yield X_train, Y_train, X_test, Y_test

        if cv_iter is None:
            cv_iter = ShuffleSplit(n_splits=3,
                                   test_size=0.1,
                                   random_state=self.random_state)
        if isinstance(X, dict):
            splits = dict()
            for n_obj, arr in X.items():
                if arr.shape[0] == 1:
                    splits[n_obj] = [([0], [0])
                                     for i in range(cv_iter.n_splits)]
                else:
                    splits[n_obj] = list(cv_iter.split(arr))
        else:
            splits = list(cv_iter.split(X))
        # Pre-compute splits for reuse
        # Here we fix a random seed for all simulations to correlate the random
        # streams:

        seed = self.random_state.randint(2**32, dtype='uint32')
        self.logger.debug(
            'Random seed for the ranking algorithm: {}'.format(seed))
        opt_seed = self.random_state.randint(2**32, dtype='uint32')
        self.logger.debug('Random seed for the optimizer: {}'.format(opt_seed))
        gp_seed = self.random_state.randint(2**32, dtype='uint32')
        self.logger.debug(
            'Random seed for the GP surrogate: {}'.format(gp_seed))

        if optimizer is not None:
            opt = optimizer
            self.logger.debug('Setting the provided optimizer')
            self.log_best_params(opt)
        else:
            transformed = []
            for param in self.parameter_ranges:
                transformed.append(check_dimension(param))
            self.logger.info("Parameter Space: {}".format(transformed))
            space = normalize_dimensions(transformed)
            self.logger.info(
                "Parameter Space after transformation: {}".format(space))

            # Todo: Make this passable
            base_estimator = cook_estimator("GP",
                                            space=space,
                                            random_state=gp_seed,
                                            noise="gaussian")
            opt = Optimizer(dimensions=self.parameter_ranges,
                            random_state=opt_seed,
                            base_estimator=base_estimator,
                            acq_func=acq_func,
                            **kwargs)
        self._callbacks_set_optimizer(opt)
        self._callbacks_on_optimization_begin()
        time_taken = duration_tillnow(start)
        total_duration -= time_taken
        max_fit_duration = -10000
        self.logger.info('Time left for {} iterations is {}'.format(
            n_iter, microsec_to_time(total_duration)))

        try:
            for t in range(n_iter):
                start = datetime.now()
                self._callbacks_on_iteration_begin(t)
                self.logger.info(
                    'Starting optimization iteration: {}'.format(t))
                if t > 0:
                    self.log_best_params(opt)

                next_point = opt.ask()
                self.logger.info('Next parameters:\n{}'.format(next_point))
                results = []
                running_times = []
                if isinstance(X, dict):
                    for X_train, Y_train, X_test, Y_test in splitter_dict(
                            splits):
                        result, time_taken = self._fit_ranker(
                            X_train, Y_train, X_test, Y_test, next_point)
                        running_times.append(time_taken)
                        results.append(result)
                else:
                    for X_train, Y_train, X_test, Y_test in splitter(splits):
                        result, time_taken = self._fit_ranker(
                            X_train, Y_train, X_test, Y_test, next_point)
                        running_times.append(time_taken)
                        results.append(result)

                results = np.array(results)
                running_times = np.array(running_times)
                mean_result = np.mean(results)
                mean_fitting_duration = np.mean(running_times)

                # Storing the maximum time to run the splitting model and adding the time for out of sample evaluation
                if max_fit_duration < np.sum(running_times):
                    max_fit_duration = np.sum(running_times)

                self.logger.info(
                    'Validation error for the parameters is {:.4f}'.format(
                        mean_result))
                self.logger.info('Time taken for the parameters is {}'.format(
                    microsec_to_time(np.sum(running_times))))
                if "ps" in opt.acq_func:
                    opt.tell(next_point, [mean_result, mean_fitting_duration])
                else:
                    opt.tell(next_point, mean_result)
                self._callbacks_on_iteration_end(t)

                self.logger.info(
                    "Main optimizer iterations done {} and saving the model".
                    format(np.array(opt.yi).shape[0]))
                dump(opt, self.optimizer_path)

                time_taken = duration_tillnow(start)
                total_duration -= time_taken
                self.logger.info('Time left for simulations is {} '.format(
                    microsec_to_time(total_duration)))

                if (total_duration - max_fit_duration) < 0:
                    self.logger.info(
                        'At iteration {} maximum time required by model to validate a parameter values'
                        .format(microsec_to_time(max_fit_duration)))
                    self.logger.info(
                        'At iteration {} simulation stops, due to time deficiency'
                        .format(t))
                    break

        except KeyboardInterrupt:
            self.logger.debug(
                'Optimizer interrupted saving the model at {}'.format(
                    self.optimizer_path))
            self.log_best_params(opt)
        else:
            self.logger.debug(
                'Finally, fit a model on the complete training set and storing the model at {}'
                .format(self.optimizer_path))
            self._fit_params["epochs"] = self._fit_params.get("epochs", 1000)
            if "ps" in opt.acq_func:
                best_point = opt.Xi[np.argmin(np.array(opt.yi)[:, 0])]
            else:
                best_point = opt.Xi[np.argmin(opt.yi)]
            self._set_new_parameters(best_point)
            self.model = copy.copy(self.ranker)
            self.model.fit(X, Y, **self._fit_params)

        finally:
            self._callbacks_on_optimization_end()
            self.optimizer = opt
            if np.array(opt.yi).shape[0] != 0:
                dump(opt, self.optimizer_path)
Exemple #18
0
    def run(self):
        """start the tuning process"""
        def objective(var):
            """objective method receive the benchmark result and send the next parameters"""
            iter_result = {}
            option = []
            for i, knob in enumerate(self.knobs):
                params[knob['name']] = var[i]
                if knob['dtype'] == 'string':
                    option.append(knob['options'].index(var[i]))
                else:
                    option.append(var[i])

            iter_result["param"] = params
            self.child_conn.send(iter_result)
            result = self.child_conn.recv()
            x_num = 0.0
            eval_list = result.split(',')
            for value in eval_list:
                num = float(value)
                x_num = x_num + num
            options.append(option)
            performance.append(x_num)
            return x_num

        params = {}
        options = []
        performance = []
        labels = []
        estimator = None

        try:
            if self.engine == 'random' or self.engine == 'forest' or \
                    self.engine == 'gbrt' or self.engine == 'bayes' or self.engine == 'extraTrees':
                params_space = self.build_space()
                ref_x, ref_y = self.transfer()
                if len(ref_x) == 0:
                    if len(self.ref) == 0:
                        ref_x = None
                    else:
                        ref_x = self.ref
                    ref_y = None
                if ref_x is not None and not isinstance(
                        ref_x[0], (list, tuple)):
                    ref_x = [ref_x]
                LOGGER.info('x0: %s', ref_x)
                LOGGER.info('y0: %s', ref_y)

                if ref_x is not None and isinstance(ref_x[0], (list, tuple)):
                    self._n_random_starts = 0 if len(ref_x) >= self._n_random_starts \
                        else self._n_random_starts - len(ref_x) + 1

                LOGGER.info('n_random_starts parameter is: %d',
                            self._n_random_starts)
                LOGGER.info("Running performance evaluation.......")
                if self.engine == 'random':
                    estimator = 'dummy'
                elif self.engine == 'forest':
                    estimator = 'RF'
                elif self.engine == 'extraTrees':
                    estimator = 'ET'
                elif self.engine == 'gbrt':
                    estimator = 'GBRT'
                elif self.engine == 'bayes':
                    params_space = normalize_dimensions(params_space)
                    estimator = cook_estimator("GP",
                                               space=params_space,
                                               noise=self.noise)

                LOGGER.info("base_estimator is: %s", estimator)
                optimizer = baseOpt(dimensions=params_space,
                                    n_random_starts=self._n_random_starts,
                                    random_state=1,
                                    base_estimator=estimator)
                n_calls = self.max_eval
                # User suggested points at which to evaluate the objective first
                if ref_x and ref_y is None:
                    ref_y = list(map(objective, ref_x))
                    LOGGER.info("ref_y is: %s", ref_y)

                # Pass user suggested initialisation points to the optimizer
                if ref_x:
                    if not isinstance(ref_y,
                                      (collections.Iterable, numbers.Number)):
                        raise ValueError(
                            "`ref_y` should be an iterable or a scalar, "
                            "got %s" % type(ref_y))
                    if len(ref_x) != len(ref_y):
                        raise ValueError("`ref_x` and `ref_y` should "
                                         "have the same length")
                    LOGGER.info("ref_x: %s", ref_x)
                    LOGGER.info("ref_y: %s", ref_y)
                    n_calls -= len(ref_y)
                    ret = optimizer.tell(ref_x, ref_y)

                for i in range(n_calls):
                    next_x = optimizer.ask()
                    LOGGER.info("next_x: %s", next_x)
                    LOGGER.info("Running performance evaluation.......")
                    next_y = objective(next_x)
                    LOGGER.info("next_y: %s", next_y)
                    ret = optimizer.tell(next_x, next_y)
                    LOGGER.info("finish (ref_x, ref_y) tell")

            elif self.engine == 'abtest':
                abtuning_manager = ABtestTuningManager(self.knobs,
                                                       self.child_conn,
                                                       self.split_count)
                options, performance = abtuning_manager.do_abtest_tuning_abtest(
                )
                params = abtuning_manager.get_best_params()
                # convert string option into index
                options = abtuning_manager.get_options_index(options)
            elif self.engine == 'gridsearch':
                num_done = 0
                if self.y_ref is not None:
                    num_done = len(self.y_ref)
                gstuning_manager = GridSearchTuningManager(
                    self.knobs, self.child_conn)
                options, performance = gstuning_manager.do_gridsearch(num_done)
                params, labels = gstuning_manager.get_best_params()
                # convert string option into index
                options = gstuning_manager.get_options_index(options)
            elif self.engine == 'lhs':
                from analysis.optimizer.knob_sampling_manager import KnobSamplingManager
                knobsampling_manager = KnobSamplingManager(
                    self.knobs, self.child_conn, self.max_eval,
                    self.split_count)
                options = knobsampling_manager.get_knob_samples()
                performance = knobsampling_manager.do_knob_sampling_test(
                    options)
                params = knobsampling_manager.get_best_params(
                    options, performance)
                options = knobsampling_manager.get_options_index(options)
            elif self.engine == 'tpe':
                from analysis.optimizer.tpe_optimizer import TPEOptimizer
                tpe_opt = TPEOptimizer(self.knobs, self.child_conn,
                                       self.max_eval)
                best_params = tpe_opt.tpe_minimize_tuning()
                final_param = {}
                final_param["finished"] = True
                final_param["param"] = best_params
                self.child_conn.send(final_param)
                return best_params
            elif self.engine == 'traverse':
                from analysis.optimizer.knob_traverse_manager import KnobTraverseManager
                default_values = [
                    p_nob['ref'] for _, p_nob in enumerate(self.knobs)
                ]
                knobtraverse_manager = KnobTraverseManager(
                    self.knobs, self.child_conn, default_values)
                traverse_list = knobtraverse_manager.get_traverse_list()
                performance = knobtraverse_manager.get_traverse_performance(
                    traverse_list)
                rank = knobtraverse_manager.get_traverse_rank(performance)
                final_param = {
                    "rank": rank,
                    "param": knobtraverse_manager.get_default_values(),
                    "finished": True
                }
                self.child_conn.send(final_param)
                return final_param["param"]

            LOGGER.info("Minimization procedure has been completed.")
        except ValueError as value_error:
            LOGGER.error('Value Error: %s', repr(value_error))
            self.child_conn.send(value_error)
            return None
        except RuntimeError as runtime_error:
            LOGGER.error('Runtime Error: %s', repr(runtime_error))
            self.child_conn.send(runtime_error)
            return None
        except Exception as err:
            LOGGER.error('Unexpected Error: %s', repr(err))
            self.child_conn.send(Exception("Unexpected Error:", repr(err)))
            return None

        for i, knob in enumerate(self.knobs):
            if estimator is not None:
                params[knob['name']] = ret.x[i]
            if self.engine != 'gridsearch':
                labels.append(knob['name'])

        LOGGER.info("Optimized result: %s", params)
        LOGGER.info("The optimized profile has been generated.")
        final_param = {}
        if self.sel_feature is True:
            if self.feature_selector == "wefs":
                wefs = WeightedEnsembleFeatureSelector()
                rank = wefs.get_ensemble_feature_importance(
                    options, performance, labels)
            elif self.feature_selector == "vrfs":
                vrfs = VarianceReductionFeatureSelector()
                rank = vrfs.get_ensemble_feature_importance(
                    options, performance, labels)
            final_param["rank"] = rank
            LOGGER.info(
                "The feature importances of current evaluation are: %s", rank)

        final_param["param"] = params
        final_param["finished"] = True
        self.child_conn.send(final_param)
        return params
 def _prepare_estimator(self):
     """Initialize :attr:`base_estimator` with :attr:`hyperparameter_space` and any other kwargs, using
     `skopt.utils.cook_estimator`"""
     self.base_estimator = cook_estimator(self.base_estimator,
                                          space=self.hyperparameter_space,
                                          **self.base_estimator_kwargs)
Exemple #20
0
def test_has_gradients(estimator, gradients):
    space = Space([(-2.0, 2.0)])

    assert has_gradients(cook_estimator(estimator, space=space)) == gradients
Exemple #21
0
def test_categorical_gp_has_gradients():
    space = Space([('a', 'b')])

    assert not has_gradients(cook_estimator('GP', space=space))
Exemple #22
0
def test_categorical_gp_has_gradients():
    space = Space([('a', 'b')])

    assert not has_gradients(cook_estimator('GP', space=space))