跳过内容

对数正态先验

smac.model.gaussian_process.priors.log_normal_prior #

LogNormalPrior #

LogNormalPrior(
    sigma: float, mean: float = 0, seed: int = 0
)

继承自: AbstractPrior

实现了对数正态先验。

参数#

sigma : float 指定正态分布的标准差。 mean : float 指定正态分布的均值。 seed : int, 默认为 0

源代码位于 smac/model/gaussian_process/priors/log_normal_prior.py
def __init__(
    self,
    sigma: float,
    mean: float = 0,
    seed: int = 0,
):
    super().__init__(seed=seed)

    if mean != 0:
        raise NotImplementedError(mean)

    self._sigma = sigma
    self._sigma_square = sigma**2
    self._mean = mean
    self._sqrt_2_pi = np.sqrt(2 * np.pi)

get_gradient #

get_gradient(theta: float) -> float

计算先验关于 theta 的梯度。此方法内部调用 self._get_gradient

警告#

Theta 必须是原始尺度。

参数#

theta : float 对数空间中的超参数配置

返回值#

gradient : float 在 theta 处的先验梯度。

源代码位于 smac/model/gaussian_process/priors/abstract_prior.py
def get_gradient(self, theta: float) -> float:
    """Computes the gradient of the prior with respect to theta. Internally, his method calls `self._get_gradient`.

    Warning
    -------
    Theta must be on the original scale.

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

    Returns
    -------
    gradient : float
        The gradient of the prior at theta.
    """
    return self._get_gradient(np.exp(theta))

get_log_probability #

get_log_probability(theta: float) -> float

返回 theta 的对数概率。此方法对 theta 取指数并调用 self._get_log_probability

警告#

Theta 必须是对数尺度!

参数#

theta : float 对数空间中的超参数配置。

返回值#

float theta 的对数概率

源代码位于 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

返回从先验中抽取的 n_samples 个样本。所有样本都处于对数尺度。此方法调用 self._sample_from_prior 并对获得的值应用对数变换。

参数#

n_samples : int 将要抽取的样本数量。

返回值#

samples : np.ndarray

源代码位于 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