跳过内容

白核

smac.model.gaussian_process.kernels.white_kernel #

WhiteKernel #

WhiteKernel(
    noise_level: float | tuple[float, ...] = 1.0,
    noise_level_bounds: tuple[float, float]
    | list[tuple[float, float]] = (1e-05, 100000.0),
    operate_on: ndarray | None = None,
    has_conditions: bool = False,
    prior: AbstractPrior | None = None,
)

Bases: AbstractKernel, WhiteKernel

白核实现。

源代码位于 smac/model/gaussian_process/kernels/white_kernel.py
def __init__(
    self,
    noise_level: float | tuple[float, ...] = 1.0,
    noise_level_bounds: tuple[float, float] | list[tuple[float, float]] = (1e-5, 1e5),
    operate_on: np.ndarray | None = None,
    has_conditions: bool = False,
    prior: AbstractPrior | None = None,
) -> None:
    super().__init__(
        operate_on=operate_on,
        has_conditions=has_conditions,
        prior=prior,
        noise_level=noise_level,
        noise_level_bounds=noise_level_bounds,
    )

hyperparameters 属性 #

hyperparameters: list[Hyperparameter]

返回所有超参数规范的列表。

meta 属性 #

meta: dict[str, Any]

返回创建对象的元数据。此方法调用 get_params 方法来收集核函数的参数。

n_dims 属性 #

n_dims: int

返回核函数的非固定超参数数量。

__call__ #

__call__(
    X: ndarray,
    Y: ndarray | None = None,
    eval_gradient: bool = False,
    active: ndarray | None = None,
) -> ndarray | tuple[ndarray, ndarray]

调用核函数。内部调用 self._call 方法,该方法必须由子类指定。

源代码位于 smac/model/gaussian_process/kernels/base_kernels.py
def __call__(
    self,
    X: np.ndarray,
    Y: np.ndarray | None = None,
    eval_gradient: bool = False,
    active: np.ndarray | None = None,
) -> np.ndarray | tuple[np.ndarray, np.ndarray]:
    """Call the kernel function. Internally, `self._call` is called, which must be specified by a subclass."""
    if active is None and self.has_conditions:
        if self.operate_on is None:
            active = get_conditional_hyperparameters(X, Y)
        else:
            if Y is None:
                active = get_conditional_hyperparameters(X[:, self.operate_on], None)
            else:
                active = get_conditional_hyperparameters(X[:, self.operate_on], Y[:, self.operate_on])

    if self.operate_on is None:
        rval = self._call(X, Y, eval_gradient, active)
    else:
        if self._len_active is None:
            raise RuntimeError("The internal variable `_len_active` is not set.")

        if Y is None:
            rval = self._call(
                X=X[:, self.operate_on].reshape([-1, self._len_active]),
                Y=None,
                eval_gradient=eval_gradient,
                active=active,
            )
            X = X[:, self.operate_on].reshape((-1, self._len_active))
        else:
            rval = self._call(
                X=X[:, self.operate_on].reshape([-1, self._len_active]),
                Y=Y[:, self.operate_on].reshape([-1, self._len_active]),
                eval_gradient=eval_gradient,
                active=active,
            )
            X = X[:, self.operate_on].reshape((-1, self._len_active))
            Y = Y[:, self.operate_on].reshape((-1, self._len_active))

    return rval

get_params #

get_params(deep: bool = True) -> dict[str, Any]

获取此核函数的参数。

参数#

deep : bool, 默认为 True。如果为 True,将返回此估计器和包含的子对象(也是估计器)的参数。

返回值#

params : dict[str, Any]。参数名称映射到它们的值。

源代码位于 smac/model/gaussian_process/kernels/base_kernels.py
def get_params(self, deep: bool = True) -> dict[str, Any]:
    """Get parameters of this kernel.

    Parameters
    ----------
    deep : bool, defaults to True
        If True, will return the parameters for this estimator and
        contained subobjects that are estimators.

    Returns
    -------
    params : dict[str, Any]
        Parameter names mapped to their values.
    """
    params = {}

    # ignore[misc] looks like it catches all kinds of errors, but misc is actually a category from mypy:
    # https://mypy.readthedocs.io/en/latest/error_code_list.html#miscellaneous-checks-misc
    tmp = super().get_params(deep)  # type: ignore[misc] # noqa F821
    args = list(tmp.keys())

    # Sum and Product do not clone the 'has_conditions' attribute by default. Instead of changing their
    # get_params() method, we simply add the attribute here!
    if "has_conditions" not in args:
        args.append("has_conditions")

    for arg in args:
        params[arg] = getattr(self, arg, None)

    return params