def sample(self):
        mtype = str_to_types[self["type"]]
        if mtype not in [int, float]:
            err = "Invalid type: {} for UniformNumber"
            raise ParamValueExcept(err.format(mtype))

        mmin = mtype(self["lower"])
        mmax = mtype(self["upper"])
        if self["log_scale"]:
            if mmin < 0. or mmax < 0.:
                raise ParamValueExcept(
                    "log_scale only allowed for positive ranges")
            mmin = np.log(np.maximum(mmin, 1e-7))
            mmax = np.log(mmax)

        nr = np.random.uniform(mmin, mmax)
        if self['log_scale']:
            nr = np.exp(nr)
        return mtype(nr)
 def sample(self):
     mtype = str_to_types[self["type"]]
     mu = self["mu"]
     sigma = self["sigma"]
     if not (type == float):
         raise ParamValueExcept("Parameter with normal distribution"
                                " must be float!")
     if self["log_scale"]:
         return np.random.lognormal(mtype(mu), mtype(sigma))
     else:
         return np.random.normal(mtype(mu), mtype(sigma))
 def __or__(self, other):
     if not isinstance(other, Condition):
         err = "or operator | requires Condition() instance on right side"
         raise ParamValueExcept(err)
     if other["uid"] == self["uid"]:
         err = "Parameter {} cannot be conditioned on itself"
         raise ParamInconsistent(err.format(self["uid"]))
     if isinstance(self, ConditionResult):
         err = "Parameter {} appears to be a nested condition " \
               "specified via the | operator which is not supported"
         raise ParamInconsistent(err.format(self["uid"]))
     return ConditionResult(self, other)
 def decode(cls, storage):
     uid = storage["uid"]
     choices = []
     for choice in storage["choices"]:
         if isinstance(choice, dict):
             p = decode_param_or_op(choice)
         else:
             if not isinstance(choice, basic_types):
                 err = "Choice parameter {} is not " \
                       "a base type or Constant!"
                 raise ParamValueExcept(err.format(choice))
             choices.append(choice)
     return cls(choices, uid=uid)
 def __init__(self, lower, upper, type, default=None, log_scale=False,
              uid=None):
     if default is None:
         if log_scale:
             default = np.exp((np.log(lower) + np.log(upper)) / 2.)
         else:
             default = (lower + upper) / 2.
     fixed = {
         "lower": type(lower),
         "upper": type(upper),
         "type": types_to_str[type],
         "default": type(default),
         "log_scale": log_scale
     }
     super(UniformNumber, self).__init__(uid=uid, fixed=fixed)
     if not (self["lower"] <= self["default"] <= self["upper"]):
         err = "Default for {} is not between min and max".format(
             self["uid"])
         raise ParamValueExcept(err)
     if self["upper"] <= self["lower"]:
         err = "Upper bound {} is larger than lower bound {} for {} ".format(
             self["upper"], self["lower"], self["uid"])
         raise ParamValueExcept(err)
 def __init__(self, choices_in, uid=None):
     choices = []
     for choice in choices_in:
         if isinstance(choice, Constant):
             choices.append(choice)
         else:
             if not isinstance(choice, basic_types):
                 err = "Choice parameter {} is not " \
                       "a base type or Constant!"
                 raise ParamValueExcept(err.format(choice))
             choices.append(choice)
     fixed = {
         "choices": choices
     }
     super(Categorical, self).__init__(uid=uid, fixed=fixed)
Example #7
0
 def sample(self, max_iters_till_cycle=50, strategy="random"):
     if strategy not in ["random", "default"]:
         raise ParamValueExcept(
             "Unknown sampling strategy {}".format(strategy))
     # allocate result dict
     res = {}
     # first add all fixed parameters
     # for pname in self.fixed:
     #    res[pname] = self[pname]
     # second sample all non conditions
     considered_params = set()
     for pname in self.non_conditions:
         if strategy == "random":
             res[pname] = self.parameters[pname].sample()
         else:
             res[pname] = self.parameters[pname].default()
         considered_params.add(pname)
     # then the conditional parameters
     remaining_params = set(self.conditions)
     i = 0
     while remaining_params:
         for pname in self.conditions:
             if pname in remaining_params:
                 cparam = self.parameters[pname]
                 conditioned_on = self.uids_to_names[cparam["condition"]
                                                     ["uid"]]
                 if conditioned_on in res.keys():
                     if strategy == "random":
                         cres = self.parameters[pname].sample(
                             res[conditioned_on])
                     else:
                         cres = self.parameters[pname].default(
                             res[conditioned_on])
                     if cres:
                         res[pname] = cres
                     considered_params.add(pname)
                     remaining_params.remove(pname)
                 elif conditioned_on in considered_params:
                     remaining_params.remove(pname)
                 else:
                     continue
         i += 1
         if i > max_iters_till_cycle:
             err = "Cannot satisfy conditionals involving " \
                   "parameters {} probably a loop! If you are sure " \
                   "no loop exists increase max_iters_till_cycle"
             raise InconsistentSpace(err.format(remaining_params))
     return res
def convert_simple_param(name, param):
    """
    Convert a simple labwatch parameter to a ConfigSpace parameter.

    Parameters
    ----------
    name: str
        The name of the parameter.

    param: dict
        Dictionary describing the parameter.

    Returns
    -------
    ConfigSpace.hyperparameters.Hyperparameter:
        The converted hyperparameter.
    """
    if param["_class"] == 'Constant':
        return csh.Constant(name, param["value"])
    elif param["_class"] == 'Categorical':
        # convert the choices to only contain
        # basic types (they might contain Constant parameters
        basic_choices = []
        for choice in param["choices"]:
            if isinstance(choice, dict):
                basic_choices.append(choice["default"])
            elif not isinstance(choice, basic_types):
                err = "Choice parameter {} is not " \
                      "a base type or Constant!"
                raise ParamValueExcept(err.format(choice))
            else:
                basic_choices.append(choice)
        return csh.CategoricalHyperparameter(name=name,
                                             choices=basic_choices,
                                             default_value=basic_choices[0])
    elif param["_class"] == 'UniformFloat':
        return csh.UniformFloatHyperparameter(name=name,
                                              lower=param["lower"],
                                              upper=param["upper"],
                                              default_value=param["default"],
                                              log=param["log_scale"])
    elif param["_class"] == 'UniformInt':
        return csh.UniformIntegerHyperparameter(name=name,
                                                lower=param["lower"],
                                                upper=param["upper"],
                                                default_value=param["default"],
                                                log=param["log_scale"])
    elif param["_class"] == 'UniformNumber':
        ptype = str_to_types[param["type"]]
        if ptype == float:
            return csh.UniformFloatHyperparameter(
                name=name,
                lower=param["lower"],
                upper=param["upper"],
                default_value=param["default"],
                log=param["log_scale"])
        elif ptype == int:
            return csh.UniformIntegerHyperparameter(
                name=name,
                lower=param["lower"],
                upper=param["upper"],
                default_value=param["default"],
                log=param["log_scale"])
        else:
            raise ValueError("Don't know how to represent UniformNumber with "
                             "type: {} in ConfigSpace".format(param["type"]))
    elif param["_class"] == 'Gaussian':
        return csh.NormalFloatHyperparameter(name=name,
                                             mu=param["mu"],
                                             sigma=param["sigma"],
                                             log=param["log_scale"])

    else:
        raise ValueError("Don't know how to represent {} in ConfigSpace "
                         "notation.".format(param))