def _suggest(self, name: str, distribution: BaseDistribution) -> Any: storage = self.storage trial_id = self._trial_id trial = storage.get_trial(trial_id) if name in trial.distributions: # No need to sample if already suggested. distributions.check_distribution_compatibility( trial.distributions[name], distribution) param_value = distribution.to_external_repr( storage.get_trial_param(trial_id, name)) else: if self._is_fixed_param(name, distribution): param_value = storage.get_trial_system_attrs( trial_id)["fixed_params"][name] elif distribution.single(): param_value = distributions._get_single_value(distribution) elif self._is_relative_param(name, distribution): param_value = self.relative_params[name] else: study = pruners._filter_study(self.study, trial) param_value = self.study.sampler.sample_independent( study, trial, name, distribution) param_value_in_internal_repr = distribution.to_internal_repr( param_value) storage.set_trial_param(trial_id, name, param_value_in_internal_repr, distribution) return param_value
def sample_independent( self, study: Study, trial: FrozenTrial, param_name: str, param_distribution: BaseDistribution, ) -> Any: if param_name not in self._search_space: message = "The parameter name, {}, is not found in the given grid.".format(param_name) raise ValueError(message) # TODO(c-bata): Reduce the number of duplicated evaluations on multiple workers. # Current selection logic may evaluate the same parameters multiple times. # See https://gist.github.com/c-bata/f759f64becb24eea2040f4b2e3afce8f for details. grid_id = trial.system_attrs["grid_id"] param_value = self._all_grids[grid_id][self._param_names.index(param_name)] contains = param_distribution._contains(param_distribution.to_internal_repr(param_value)) if not contains: raise ValueError( "The value `{}` is out of range of the parameter `{}`. Please make " "sure the search space of the `GridSampler` only contains values " "consistent with the distribution specified in the objective " "function. The distribution is: `{}`.".format( param_value, param_name, param_distribution ) ) return param_value
def sample_independent( self, study: Study, trial: FrozenTrial, param_name: str, param_distribution: BaseDistribution, ) -> Any: if "grid_id" not in trial.system_attrs: message = "All parameters must be specified when using GridSampler with enqueue_trial." raise ValueError(message) if param_name not in self._search_space: message = "The parameter name, {}, is not found in the given grid.".format( param_name) raise ValueError(message) # TODO(c-bata): Reduce the number of duplicated evaluations on multiple workers. # Current selection logic may evaluate the same parameters multiple times. # See https://gist.github.com/c-bata/f759f64becb24eea2040f4b2e3afce8f for details. grid_id = trial.system_attrs["grid_id"] param_value = self._all_grids[grid_id][self._param_names.index( param_name)] contains = param_distribution._contains( param_distribution.to_internal_repr(param_value)) if not contains: warnings.warn( f"The value `{param_value}` is out of range of the parameter `{param_name}`. " f"The value will be used but the actual distribution is: `{param_distribution}`." ) return param_value
def sample_independent( self, study: Study, trial: FrozenTrial, param_name: str, param_distribution: BaseDistribution, ) -> Any: # If param_name isn't in self._fixed_params.keys(), param_value is set to None. param_value = self._fixed_params.get(param_name) if param_value is None: # Unfixed params are sampled here. return self._base_sampler.sample_independent( study, trial, param_name, param_distribution) else: # Fixed params are sampled here. # Check if a parameter value is contained in the range of this distribution. param_value_in_internal_repr = param_distribution.to_internal_repr( param_value) contained = param_distribution._contains( param_value_in_internal_repr) if not contained: warnings.warn( f"Fixed parameter '{param_name}' with value {param_value} is out of range " f"for distribution {param_distribution}.") return param_value
def sample_independent( self, study: "optuna.study.Study", trial: "optuna.trial.FrozenTrial", param_name: str, param_distribution: BaseDistribution, ) -> Any: param_value = self.params[param_name] assert param_distribution._contains( param_distribution.to_internal_repr(param_value)) return param_value
def _is_relative_param(self, name: str, distribution: BaseDistribution) -> bool: if name not in self.relative_params: return False if name not in self.relative_search_space: raise ValueError( "The parameter '{}' was sampled by `sample_relative` method " "but it is not contained in the relative search space.".format(name) ) relative_distribution = self.relative_search_space[name] distributions.check_distribution_compatibility(relative_distribution, distribution) param_value = self.relative_params[name] param_value_in_internal_repr = distribution.to_internal_repr(param_value) return distribution._contains(param_value_in_internal_repr)
def _is_fixed_param(self, name: str, distribution: BaseDistribution) -> bool: system_attrs = self.storage.get_trial_system_attrs(self._trial_id) if "fixed_params" not in system_attrs: return False if name not in system_attrs["fixed_params"]: return False param_value = system_attrs["fixed_params"][name] param_value_in_internal_repr = distribution.to_internal_repr(param_value) contained = distribution._contains(param_value_in_internal_repr) if not contained: warnings.warn( "Fixed parameter '{}' with value {} is out of range " "for distribution {}.".format(name, param_value, distribution) ) return True
def _suggest(self, name: str, distribution: BaseDistribution) -> Any: if name not in self._params: raise ValueError( "The value of the parameter '{}' is not found. Please set it at " "the construction of the FrozenTrial object.".format(name)) value = self._params[name] param_value_in_internal_repr = distribution.to_internal_repr(value) if not distribution._contains(param_value_in_internal_repr): raise ValueError("The value {} of the parameter '{}' is out of " "the range of the distribution {}.".format( value, name, distribution)) if name in self._distributions: distributions.check_distribution_compatibility( self._distributions[name], distribution) self._distributions[name] = distribution return value
def test_sample_independent(sampler_class: Callable[[], BaseMultiObjectiveSampler], distribution: BaseDistribution) -> None: study = optuna.multi_objective.study.create_study(["minimize", "maximize"], sampler=sampler_class()) for i in range(100): value = study.sampler.sample_independent(study, _create_new_trial(study), "x", distribution) assert distribution._contains(distribution.to_internal_repr(value)) if not isinstance(distribution, CategoricalDistribution): # Please see https://github.com/optuna/optuna/pull/393 why this assertion is needed. assert not isinstance(value, np.floating) if isinstance(distribution, FloatDistribution): if distribution.step is not None: # Check the value is a multiple of `distribution.q` which is # the quantization interval of the distribution. value -= distribution.low value /= distribution.step round_value = np.round(value) np.testing.assert_almost_equal(round_value, value)