Skip to content

Tophat prior

smac.model.gaussian_process.priors.tophat_prior #

SoftTopHatPrior #

SoftTopHatPrior(
    lower_bound: float,
    upper_bound: float,
    exponent: float,
    seed: int = 0,
)

Bases: AbstractPrior

Soft Tophat prior as it used in the original spearmint code.

Parameters#

lower_bound : float Lower bound of the prior. In original scale. upper_bound : float Upper bound of the prior. In original scale. exponent : float Exponent of the prior. seed : int, defaults to 0

Source code in smac/model/gaussian_process/priors/tophat_prior.py
def __init__(
    self,
    lower_bound: float,
    upper_bound: float,
    exponent: float,
    seed: int = 0,
) -> None:
    super().__init__(seed=seed)

    with warnings.catch_warnings():
        warnings.simplefilter("error")
        self._lower_bound = lower_bound
        try:
            self._log_lower_bound = np.log(lower_bound)
        except RuntimeWarning as w:
            if "invalid value encountered in log" in w.args[0]:
                raise ValueError("Invalid lower bound %f (cannot compute log)" % lower_bound)

            raise w
        self._upper_bound = upper_bound
        try:
            self._log_upper_bound = np.log(upper_bound)
        except RuntimeWarning as w:
            if "invalid value encountered in log" in w.args[0]:
                raise ValueError("Invalid lower bound %f (cannot compute log)" % lower_bound)

            raise w

    if exponent <= 0:
        raise ValueError("Exponent cannot be less or equal than zero (but is %f)" % exponent)

    self._exponent = exponent

sample_from_prior #

sample_from_prior(n_samples: int) -> ndarray

Returns n_samples from the prior. All samples are on a log scale. This method calls self._sample_from_prior and applies a log transformation to the obtained values.

Parameters#

n_samples : int The number of samples that will be drawn.

Returns#

samples : np.ndarray

Source code in smac/model/gaussian_process/priors/abstract_prior.py
def sample_from_prior(self, n_samples: int) -> np.ndarray:
    """Returns `n_samples` from the prior. All samples are on a log scale. This method calls
    `self._sample_from_prior` and applies a log transformation to the obtained values.

    Parameters
    ----------
    n_samples : int
        The number of samples that will be drawn.

    Returns
    -------
    samples : np.ndarray
    """
    if np.ndim(n_samples) != 0:
        raise ValueError("argument n_samples needs to be a scalar (is %s)" % n_samples)

    if n_samples <= 0:
        raise ValueError("argument n_samples needs to be positive (is %d)" % n_samples)

    sample = np.log(self._sample_from_prior(n_samples=n_samples))

    if np.any(~np.isfinite(sample)):
        raise ValueError("Sample %s from prior %s contains infinite values!" % (sample, self))

    return sample

TophatPrior #

TophatPrior(
    lower_bound: float, upper_bound: float, seed: int = 0
)

Bases: AbstractPrior

Tophat prior as it used in the original spearmint code.

Parameters#

lower_bound : float Lower bound of the prior. In original scale. upper_bound : float Upper bound of the prior. In original scale. seed : int, defaults to 0

Source code in smac/model/gaussian_process/priors/tophat_prior.py
def __init__(
    self,
    lower_bound: float,
    upper_bound: float,
    seed: int = 0,
):
    super().__init__(seed=seed)
    self._min = lower_bound
    self._log_min = np.log(lower_bound)
    self._max = upper_bound
    self._log_max = np.log(upper_bound)
    self._prob = 1 / (self._max - self._min)
    self._log_prob = np.log(self._prob)

    if not (self._max > self._min):
        raise Exception("Upper bound of Tophat prior must be greater than the lower bound.")

get_log_probability #

get_log_probability(theta: float) -> float

Returns the log probability of theta. This method exponentiates theta and calls self._get_log_probability.

Warning#

Theta must be on a log scale!

Parameters#

theta : float Hyperparameter configuration in log space.

Returns#

float The log probability of theta

Source code in smac/model/gaussian_process/priors/abstract_prior.py
def get_log_probability(self, theta: float) -> float:
    """Returns the log probability of theta. This method exponentiates theta and calls `self._get_log_probability`.

    Warning
    -------
    Theta must be on a log scale!

    Parameters
    ----------
    theta : float
        Hyperparameter configuration in log space.

    Returns
    -------
    float
        The log probability of theta
    """
    return self._get_log_probability(np.exp(theta))

sample_from_prior #

sample_from_prior(n_samples: int) -> ndarray

Returns n_samples from the prior. All samples are on a log scale. This method calls self._sample_from_prior and applies a log transformation to the obtained values.

Parameters#

n_samples : int The number of samples that will be drawn.

Returns#

samples : np.ndarray

Source code in smac/model/gaussian_process/priors/abstract_prior.py
def sample_from_prior(self, n_samples: int) -> np.ndarray:
    """Returns `n_samples` from the prior. All samples are on a log scale. This method calls
    `self._sample_from_prior` and applies a log transformation to the obtained values.

    Parameters
    ----------
    n_samples : int
        The number of samples that will be drawn.

    Returns
    -------
    samples : np.ndarray
    """
    if np.ndim(n_samples) != 0:
        raise ValueError("argument n_samples needs to be a scalar (is %s)" % n_samples)

    if n_samples <= 0:
        raise ValueError("argument n_samples needs to be positive (is %d)" % n_samples)

    sample = np.log(self._sample_from_prior(n_samples=n_samples))

    if np.any(~np.isfinite(sample)):
        raise ValueError("Sample %s from prior %s contains infinite values!" % (sample, self))

    return sample