def test_interval(self): """Interval takes into account explicitly bounds.""" dim = Real('yolo', 'norm', 0, 3, low=-3, high=+3) assert dim.interval() == (-3, 3) dim = Real('yolo', 'alpha', 0.9, low=-3, high=+3) assert dim.interval() == (0, 3) dim = Real('yolo', 'uniform', -2, 4, low=-3, high=+3) assert dim.interval() == (-2.0, 2.0)
def test_interval(self): """Interval takes into account explicitly bounds.""" dim = Real("yolo", "norm", 0, 3, low=-3, high=+3) assert dim.interval() == (-3, 3) dim = Real("yolo", "alpha", 0.9, low=-3, high=+3) assert dim.interval() == (0, 3) dim = Real("yolo", "uniform", -2, 4, low=-3, high=+3) assert dim.interval() == (-2.0, 2.0)
def real_grid(dim: Real, num: int): """Build real grid""" if dim.prior_name.endswith("reciprocal"): a, b = dim.interval() return list(_log_grid(a, b, num)) elif dim.prior_name.endswith("uniform"): a, b = dim.interval() return list(_lin_grid(a, b, num)) else: raise TypeError( "Grid Search only supports `loguniform`, `uniform` and `choices`: " "`{}`".format(dim.prior_name))
def _sample_real_point( self, dimension: Real, below_points: numpy.ndarray | Sequence[numpy.ndarray], above_points: numpy.ndarray | Sequence[numpy.ndarray], is_log: bool = False, ) -> numpy.ndarray: """Sample one value for real dimension based on the observed good and bad points""" low, high = dimension.interval() below_points = numpy.array(below_points) above_points = numpy.array(above_points) if is_log: low = numpy.log(low) high = numpy.log(high) below_points = numpy.log(below_points) above_points = numpy.log(above_points) below_mus, below_sigmas, below_weights = adaptive_parzen_estimator( below_points, low, high, self.prior_weight, self.equal_weight, flat_num=self.full_weight_num, ) above_mus, above_sigmas, above_weights = adaptive_parzen_estimator( above_points, low, high, self.prior_weight, self.equal_weight, flat_num=self.full_weight_num, ) gmm_sampler_below = GMMSampler( self, mus=below_mus, sigmas=below_sigmas, low=low, high=high, weights=below_weights, ) gmm_sampler_above = GMMSampler( self, mus=above_mus, sigmas=above_sigmas, low=low, high=high, weights=above_weights, ) candidate_points = gmm_sampler_below.sample(self.n_ei_candidates) lik_blow = gmm_sampler_below.get_loglikelis(candidate_points) lik_above = gmm_sampler_above.get_loglikelis(candidate_points) new_point = compute_max_ei_point(candidate_points, lik_blow, lik_above) if is_log: new_point = numpy.exp(new_point) return new_point
def test_simple_instance(self, seed): """Test Real.__init__.""" dim = Real('yolo', 'norm', 0.9) samples = dim.sample(seed=seed) assert len(samples) == 1 assert dists.norm.rvs(0.9) == samples[0] assert dists.norm.interval(1.0, 0.9) == dim.interval() assert dists.norm.interval(0.5, 0.9) == dim.interval(0.5) assert 1.0 in dim assert str( dim ) == "Real(name=yolo, prior={norm: (0.9,), {}}, shape=(), default value=None)" assert dim.name == 'yolo' assert dim.type == 'real' assert dim.shape == ()
def _(dim: Real): if dim.shape: raise NotImplementedError( "Array with reciprocal prior cannot be converted.") lower, upper = dim.interval() return ng.p.Log(lower=lower, upper=upper, exponent=2)