Ejemplo n.º 1
0
def make_features(
    values,
    params,
    fallback_func=param_processor.choose_default_placeholder,
    converters={},
    convert_fallback=True,
):
    """Return an iterator of the values for the parameters in sorted order with the given fallback function.
    If passed, converters must map funcs to functions from (value, *args) -> new_value which will be run
    on the resulting value for that func (but only on fallbacks if convert_fallback)."""
    for name, (func, args, kwargs) in sorted_items(params):
        # determine feature
        fallback = False
        _coconut_match_to = values
        _coconut_match_check = False
        if _coconut.isinstance(_coconut_match_to, _coconut.abc.Mapping):
            _coconut_match_temp_0 = _coconut_match_to.get(
                name, _coconut_sentinel)
            if _coconut_match_temp_0 is not _coconut_sentinel:
                feature = _coconut_match_temp_0
                _coconut_match_check = True
        if _coconut_match_check:
            pass
        else:
            _coconut_match_to = kwargs
            _coconut_match_check = False
            if _coconut.isinstance(_coconut_match_to, _coconut.abc.Mapping):
                _coconut_match_temp_0 = _coconut_match_to.get(
                    "placeholder_when_missing", _coconut_sentinel)
                if _coconut_match_temp_0 is not _coconut_sentinel:
                    placeholder_value = _coconut_match_temp_0
                    _coconut_match_check = True
            if _coconut_match_check:
                feature = placeholder_value
            else:
                fallback = True
                feature = fallback_func(name, func, *args, **kwargs)


# run converters
        if not fallback or convert_fallback:
            _coconut_match_to = converters
            _coconut_match_check = False
            if _coconut.isinstance(_coconut_match_to, _coconut.abc.Mapping):
                _coconut_match_temp_0 = _coconut_match_to.get(
                    func, _coconut_sentinel)
                if _coconut_match_temp_0 is not _coconut_sentinel:
                    converter_func = _coconut_match_temp_0
                    _coconut_match_check = True
            if _coconut_match_check:
                feature = converter_func(feature, *args)

        yield feature
Ejemplo n.º 2
0
    def __init__(self, examples, params, base_estimator="gp", **options):
        self.init_fallback_backend()

        if not examples:
            self.current_values = {}
            return

        data_points, losses = split_examples(examples, params)
        dimensions = [
            create_dimension(name, func, *args)
            for name, (func, args, kwargs) in sorted_items(params)
        ]

        if isinstance(base_estimator, str):
            base_estimator = py_str(base_estimator)

        optimizer = Optimizer(dimensions, base_estimator, **options)
        optimizer.tell(data_points, losses)
        current_point = optimizer.ask()

        self.current_values = make_values(params, current_point)
Ejemplo n.º 3
0
    def __init__(self,
                 examples,
                 params,
                 algo=tpe.suggest,
                 rstate=np.random.RandomState(),
                 show_progressbar=False,
                 **options):
        self.init_fallback_backend()

        if not examples:
            self.current_values = {}
            return

        space = (as_apply)(dict(
            ((name), (create_space(name, func, *args)))
            for name, (func, args, kwargs) in sorted_items(params)))

        domain = Domain(self.set_current_values, space)

        trial_list = examples_to_trials(examples, params)

        trials = Trials()
        trials.insert_trial_docs(trial_list)

        self.fmin_iter = FMinIter(algo,
                                  domain,
                                  trials,
                                  rstate,
                                  show_progressbar=show_progressbar,
                                  **options)

        # run one iteration of hyperparameter optimization, with values saved
        #  to the self.set_current_values callback passed to Domain
        next(self.fmin_iter)

        assert self.current_values is not None, self.current_values
        assert set(
            self.current_values.keys()) == set(params), self.current_values
Ejemplo n.º 4
0
    def setup_backend(self,
                      params,
                      algo=tpe.suggest,
                      rstate=np.random.RandomState(),
                      show_progressbar=False,
                      **options):
        """Special method to initialize the backend from params."""
        self.params = params

        space = (as_apply)(dict(
            ((name), (create_space(name, func, *args)))
            for name, (func, args, kwargs) in sorted_items(params)))

        domain = Domain(self.set_current_values, space)

        self.trials = Trials()

        self.fmin_iter = FMinIter(algo,
                                  domain,
                                  self.trials,
                                  rstate,
                                  show_progressbar=show_progressbar,
                                  **options)
Ejemplo n.º 5
0
def create_dimensions(params):
    """Construct the full optimization space for the given parameters."""
    return [
        create_space(name, func, *args)
        for name, (func, args, kwargs) in sorted_items(params)
    ]
Ejemplo n.º 6
0
def get_names_and_features(
    values,
    params,
    fallback_func=param_processor.choose_default_placeholder,
    converters={},
    convert_fallback=True,
):
    """Return an iterator of (name, feature) for the parameters in sorted order with the given fallback function.
    If passed, converters must map funcs to functions from (value, *args) -> new_value which will be run
    on the resulting value for that func (but only on fallbacks if convert_fallback)."""
    for name, (func, args, kwargs) in sorted_items(params):
        # determine feature
        fallback = False
        _coconut_match_to_1 = values
        _coconut_match_check_3 = False
        _coconut_match_set_name_feature = _coconut_sentinel
        if _coconut.isinstance(_coconut_match_to_1, _coconut.abc.Mapping):
            _coconut_match_temp_12 = _coconut_match_to_1.get(
                name, _coconut_sentinel)
            if _coconut_match_temp_12 is not _coconut_sentinel:
                _coconut_match_set_name_feature = _coconut_match_temp_12
                _coconut_match_check_3 = True
        if _coconut_match_check_3:
            if _coconut_match_set_name_feature is not _coconut_sentinel:
                feature = _coconut_match_set_name_feature
        if _coconut_match_check_3:
            pass
        else:
            _coconut_match_to_0 = kwargs
            _coconut_match_check_2 = False
            _coconut_match_set_name_placeholder_value = _coconut_sentinel
            if _coconut.isinstance(_coconut_match_to_0, _coconut.abc.Mapping):
                _coconut_match_temp_11 = _coconut_match_to_0.get(
                    "placeholder_when_missing", _coconut_sentinel)
                if _coconut_match_temp_11 is not _coconut_sentinel:
                    _coconut_match_set_name_placeholder_value = _coconut_match_temp_11
                    _coconut_match_check_2 = True
            if _coconut_match_check_2:
                if _coconut_match_set_name_placeholder_value is not _coconut_sentinel:
                    placeholder_value = _coconut_match_set_name_placeholder_value
            if _coconut_match_check_2:
                feature = placeholder_value
            else:
                fallback = True
                feature = fallback_func(name, func, *args, **kwargs)


# run converters
        if not fallback or convert_fallback:
            _coconut_match_to_2 = converters
            _coconut_match_check_4 = False
            _coconut_match_set_name_converter_func = _coconut_sentinel
            if _coconut.isinstance(_coconut_match_to_2, _coconut.abc.Mapping):
                _coconut_match_temp_13 = _coconut_match_to_2.get(
                    func, _coconut_sentinel)
                if _coconut_match_temp_13 is not _coconut_sentinel:
                    _coconut_match_set_name_converter_func = _coconut_match_temp_13
                    _coconut_match_check_4 = True
            if _coconut_match_check_4:
                if _coconut_match_set_name_converter_func is not _coconut_sentinel:
                    converter_func = _coconut_match_set_name_converter_func
            if _coconut_match_check_4:
                feature = converter_func(feature, *args)

        yield name, feature
Ejemplo n.º 7
0
Archivo: pysot.py Proyecto: evhub/bbopt
    def __init__(self, params):
        self.params = params
        self.dim = len(params)
        self.got_values = None

        # lower and upper bounds; len must match self.dim
        self.lb = []
        self.ub = []

        # lists of indices that are discrete vs. continuous
        self.int_var = []
        self.cont_var = []

        self.names = []
        self.choices = {}
        for i, (name, (func, args, kwargs)) in enumerate(sorted_items(params)):
            self.names.append(name)
            _coconut_case_match_to_0 = func, args
            _coconut_case_match_check_0 = False
            _coconut_match_set_name_choices = _coconut_sentinel
            if (_coconut.isinstance(
                    _coconut_case_match_to_0, _coconut.abc.Sequence)) and (
                        _coconut.len(_coconut_case_match_to_0) == 2) and (
                            _coconut_case_match_to_0[0]
                            == "choice") and (_coconut.isinstance(
                                _coconut_case_match_to_0[1],
                                _coconut.abc.Sequence)) and (_coconut.len(
                                    _coconut_case_match_to_0[1]) == 1):
                _coconut_match_set_name_choices = _coconut_case_match_to_0[1][
                    0]
                _coconut_case_match_check_0 = True
            if _coconut_case_match_check_0:
                if _coconut_match_set_name_choices is not _coconut_sentinel:
                    choices = _coconut_case_match_to_0[1][0]
            if _coconut_case_match_check_0:
                self.lb.append(0)
                self.ub.append(len(choices) - 1)
                self.int_var.append(i)
                self.choices[name] = choices
            if not _coconut_case_match_check_0:
                _coconut_match_set_name_start = _coconut_sentinel
                _coconut_match_set_name_stop = _coconut_sentinel
                _coconut_match_set_name_step = _coconut_sentinel
                if (_coconut.isinstance(
                        _coconut_case_match_to_0, _coconut.abc.Sequence)) and (
                            _coconut.len(_coconut_case_match_to_0) == 2) and (
                                _coconut_case_match_to_0[0]
                                == "randrange") and (_coconut.isinstance(
                                    _coconut_case_match_to_0[1],
                                    _coconut.abc.Sequence)) and (_coconut.len(
                                        _coconut_case_match_to_0[1]) == 3):
                    _coconut_match_set_name_start = _coconut_case_match_to_0[
                        1][0]
                    _coconut_match_set_name_stop = _coconut_case_match_to_0[1][
                        1]
                    _coconut_match_set_name_step = _coconut_case_match_to_0[1][
                        2]
                    _coconut_case_match_check_0 = True
                if _coconut_case_match_check_0:
                    if _coconut_match_set_name_start is not _coconut_sentinel:
                        start = _coconut_case_match_to_0[1][0]
                    if _coconut_match_set_name_stop is not _coconut_sentinel:
                        stop = _coconut_case_match_to_0[1][1]
                    if _coconut_match_set_name_step is not _coconut_sentinel:
                        step = _coconut_case_match_to_0[1][2]
                if _coconut_case_match_check_0:
                    rng = range(start, stop, step)
                    self.lb.append(0)
                    self.ub.append(len(rng) - 1)
                    self.int_var.append(i)
                    self.choices[name] = rng
            if not _coconut_case_match_check_0:
                _coconut_match_set_name_start = _coconut_sentinel
                _coconut_match_set_name_stop = _coconut_sentinel
                if (_coconut.isinstance(
                        _coconut_case_match_to_0, _coconut.abc.Sequence)) and (
                            _coconut.len(_coconut_case_match_to_0) == 2) and (
                                _coconut_case_match_to_0[0]
                                == "uniform") and (_coconut.isinstance(
                                    _coconut_case_match_to_0[1],
                                    _coconut.abc.Sequence)) and (_coconut.len(
                                        _coconut_case_match_to_0[1]) == 2):
                    _coconut_match_set_name_start = _coconut_case_match_to_0[
                        1][0]
                    _coconut_match_set_name_stop = _coconut_case_match_to_0[1][
                        1]
                    _coconut_case_match_check_0 = True
                if _coconut_case_match_check_0:
                    if _coconut_match_set_name_start is not _coconut_sentinel:
                        start = _coconut_case_match_to_0[1][0]
                    if _coconut_match_set_name_stop is not _coconut_sentinel:
                        stop = _coconut_case_match_to_0[1][1]
                if _coconut_case_match_check_0:
                    self.lb.append(start)
                    self.ub.append(stop)
                    self.cont_var.append(i)
            if not _coconut_case_match_check_0:
                raise TypeError(
                    "insufficiently specified parameter {_coconut_format_0}".
                    format(_coconut_format_0=(name)))

        self.lb = (np.array)(self.lb)
        self.ub = (np.array)(self.ub)
        self.int_var = (np.array)(self.int_var)
        self.cont_var = (np.array)(self.cont_var)