def SearchSpaceNumberToSMAC(key: str, hp: SearchSpaceNumber) -> Hyperparameter: """Returns either a list of values intended to be sampled uniformly or a frozen scipy.stats distribution""" dist = "uniform" if hp.distribution: dist = hp.distribution if hp.maximum is None: raise ValueError( f"maximum not specified for a number with distribution {dist} for {key}" ) max = hp.getInclusiveMax() if hp.minimum is None: raise ValueError( f"minimum not specified for a number with distribution {dist} for {key}" ) min = hp.getInclusiveMin() log: bool if dist == "uniform" or dist == "integer": log = False elif dist == "loguniform": log = True else: raise ValueError(f"unknown/unsupported distribution {dist} for {key}") if hp.discrete: return UniformIntegerHyperparameter(key, min, max, log=log) else: return UniformFloatHyperparameter(key, min, max, log=log)
def visitSearchSpaceNumber(self, space: SearchSpaceNumber, path: str, counter=None): label = self.mk_label(path, counter) if space.pgo is not None: return scope.pgo_sample( space.pgo, hp.quniform(label, 0, len(space.pgo) - 1, 1) ) dist = "uniform" if space.distribution: dist = space.distribution if space.maximum is None: raise SearchSpaceError( path, f"maximum not specified for a number with distribution {dist}" ) max = space.getInclusiveMax() # if the maximum is not None, the inclusive maximum should not be none assert max is not None # These distributions need only a maximum if dist == "integer": if not space.discrete: raise SearchSpaceError( path, "integer distribution specified for a non discrete numeric type", ) return hp.randint(label, max) if space.minimum is None: raise SearchSpaceError( path, f"minimum not specified for a number with distribution {dist}" ) min = space.getInclusiveMin() # if the minimum is not None, the inclusive minimum should not be none assert min is not None if dist == "uniform": if space.discrete: return scope.int(hp.quniform(label, min, max, 1)) else: return hp.uniform(label, min, max) elif dist == "loguniform": # for log distributions, hyperopt requires that we provide the log of the min/max if min <= 0: raise SearchSpaceError( path, f"minimum of 0 specified with a {dist} distribution. This is not allowed; please set it (possibly using minimumForOptimizer) to be positive", ) if min > 0: min = math.log(min) if max > 0: max = math.log(max) if space.discrete: return scope.int(hp.qloguniform(label, min, max, 1)) else: return hp.loguniform(label, min, max) else: raise SearchSpaceError(path, f"Unknown distribution type: {dist}")
def visitSearchSpaceNumber(self, space: SearchSpaceNumber, path: str, counter=None, useCounter=True): label = self.mk_label(path, counter, useCounter=useCounter) if space.pgo is not None: self.pgo_dict[label] = space.pgo return f"scope.pgo_sample(pgo_{label}, hp.quniform('{label}', {0}, {len(space.pgo)-1}, 1))" dist = "uniform" if space.distribution: dist = space.distribution if space.maximum is None: SearchSpaceError( path, f"maximum not specified for a number with distribution {dist}") max = space.getInclusiveMax() # These distributions need only a maximum if dist == "integer": if not space.discrete: raise SearchSpaceError( path, "integer distribution specified for a non discrete numeric type....", ) return f"hp.randint('{label}', {max})" if space.minimum is None: raise SearchSpaceError( path, f"minimum not specified for a number with distribution {dist}") min = space.getInclusiveMin() if dist == "uniform": if space.discrete: return f"hp.quniform('{label}', {min}, {max}, 1)" else: return f"hp.uniform('{label}', {min}, {max})" elif dist == "loguniform": # for log distributions, hyperopt requires that we provide the log of the min/max if min <= 0: raise SearchSpaceError( path, f"minimum of 0 specified with a {dist} distribution. This is not allowed; please set it (possibly using minimumForOptimizer) to be positive", ) if min > 0: min = math.log(min) if max > 0: max = math.log(max) if space.discrete: return f"hp.qloguniform('{label}', {min}, {max}, 1)" else: return f"hp.loguniform('{label}', {min}, {max})" else: raise SearchSpaceError(path, f"Unknown distribution type: {dist}")
def SearchSpaceNumberToGSValues( key: str, hp: SearchSpaceNumber, num_samples: Optional[int] = None) -> List[GSValue]: """Returns either a list of values intended to be sampled uniformly""" samples: int if num_samples is None: samples = DEFAULT_SAMPLES_PER_DISTRIBUTION else: samples = num_samples # Add preliminary support for PGO if hp.pgo is not None: ret = list(hp.pgo.samples(samples)) return ret # if we are not doing PGO dist = "uniform" if hp.distribution: dist = hp.distribution if hp.maximum is None: raise ValueError( f"maximum not specified for a number with distribution {dist} for {key}" ) max = hp.getInclusiveMax() assert max is not None if hp.minimum is None: raise ValueError( f"minimum not specified for a number with distribution {dist} for {key}" ) min = hp.getInclusiveMin() assert min is not None dt: np.dtype if hp.discrete: dt = np.dtype(int) else: dt = np.dtype(float) default = hp.default() if default is not None: # always use the default as one of the samples # TODO: ensure that the default is valid according to the schema if samples <= 1: return [default] samples = samples - 1 if dist == "uniform" or dist == "integer": ret = np.linspace(min, max, num=samples, dtype=dt).tolist() elif dist == "loguniform": ret = np.logspace(min, max, num=samples, dtype=dt).tolist() else: raise ValueError(f"unknown/unsupported distribution {dist} for {key}") if default is not None: ret.append(default) return ret