Skip to content

Prior acquisition function

smac.acquisition.function.prior_acquisition_function #

PriorAcquisitionFunction #

PriorAcquisitionFunction(
    acquisition_function: AbstractAcquisitionFunction,
    decay_beta: float,
    prior_floor: float = 1e-12,
    discretize: bool = False,
    discrete_bins_factor: float = 10.0,
)

Bases: AbstractAcquisitionFunction

Weight the acquisition function with a user-defined prior over the optimum.

See "piBO: Augmenting Acquisition Functions with User Beliefs for Bayesian Optimization" by Carl Hvarfner et al. [HSSL22] for further details.

Parameters#

decay_beta: float Decay factor on the user prior. A solid default value for decay_beta (empirically founded) is scenario.n_trials / 10. prior_floor : float, defaults to 1e-12 Lowest possible value of the prior to ensure non-negativity for all values in the search space. discretize : bool, defaults to False Whether to discretize (bin) the densities for continous parameters. Triggered for Random Forest models and continous hyperparameters to avoid a pathological case where all Random Forest randomness is removed (RF surrogates require piecewise constant acquisition functions to be well-behaved). discrete_bins_factor : float, defaults to 10.0 If discretizing, the multiple on the number of allowed bins for each parameter.

Source code in smac/acquisition/function/prior_acquisition_function.py
def __init__(
    self,
    acquisition_function: AbstractAcquisitionFunction,
    decay_beta: float,
    prior_floor: float = 1e-12,
    discretize: bool = False,
    discrete_bins_factor: float = 10.0,
):
    super().__init__()
    self._acquisition_function: AbstractAcquisitionFunction = acquisition_function
    self._functions: list[AbstractAcquisitionFunction] = []
    self._eta: float | None = None

    self._hyperparameters: dict[Any, Configuration] | None = None
    self._decay_beta = decay_beta
    self._prior_floor = prior_floor
    self._discretize = discretize
    self._discrete_bins_factor = discrete_bins_factor

    # check if the acquisition function is LCB or TS - then the acquisition function values
    # need to be rescaled to assure positiveness & correct magnitude
    if isinstance(self._acquisition_function, IntegratedAcquisitionFunction):
        acquisition_type = self._acquisition_function._acquisition_function
    else:
        acquisition_type = self._acquisition_function

    self._rescale = isinstance(acquisition_type, (LCB, TS))

    # Variables needed to adapt the weighting of the prior
    self._initial_design_size = None
    self._iteration_number = 0

__call__ #

__call__(configurations: list[Configuration]) -> ndarray

Compute the acquisition value for a given configuration.

Parameters#

configurations : list[Configuration] The configurations where the acquisition function should be evaluated.

Returns#

np.ndarray [N, 1] Acquisition values for X

Source code in smac/acquisition/function/abstract_acquisition_function.py
def __call__(self, configurations: list[Configuration]) -> np.ndarray:
    """Compute the acquisition value for a given configuration.

    Parameters
    ----------
    configurations : list[Configuration]
        The configurations where the acquisition function should be evaluated.

    Returns
    -------
    np.ndarray [N, 1]
        Acquisition values for X
    """
    X = convert_configurations_to_array(configurations)
    if len(X.shape) == 1:
        X = X[np.newaxis, :]

    acq = self._compute(X)
    if np.any(np.isnan(acq)):
        idx = np.where(np.isnan(acq))[0]
        acq[idx, :] = -np.finfo(float).max

    return acq

update #

update(model: AbstractModel, **kwargs: Any) -> None

Update the acquisition function attributes required for calculation.

This method will be called after fitting the model, but before maximizing the acquisition function. As an examples, EI uses it to update the current fmin. The default implementation only updates the attributes of the acquisition function which are already present.

Calls _update to update the acquisition function attributes.

Parameters#

model : AbstractModel The model which was used to fit the data. kwargs : Any Additional arguments to update the specific acquisition function.

Source code in smac/acquisition/function/abstract_acquisition_function.py
def update(self, model: AbstractModel, **kwargs: Any) -> None:
    """Update the acquisition function attributes required for calculation.

    This method will be called after fitting the model, but before maximizing the acquisition
    function. As an examples, EI uses it to update the current fmin. The default implementation only updates the
    attributes of the acquisition function which are already present.

    Calls `_update` to update the acquisition function attributes.

    Parameters
    ----------
    model : AbstractModel
        The model which was used to fit the data.
    kwargs : Any
        Additional arguments to update the specific acquisition function.
    """
    self.model = model
    self._update(**kwargs)