跳过内容

Matern 核

smac.model.gaussian_process.kernels.matern_kernel #

MaternKernel #

MaternKernel(
    length_scale: float | tuple[float, ...] | ndarray = 1.0,
    length_scale_bounds: tuple[float, float]
    | list[tuple[float, float]]
    | ndarray = (1e-05, 100000.0),
    nu: float = 1.5,
    operate_on: ndarray | None = None,
    has_conditions: bool = False,
    prior: AbstractPrior | None = None,
)

基类:AbstractKernel, Matern

Matern 核实现。

源代码位于 smac/model/gaussian_process/kernels/matern_kernel.py
def __init__(
    self,
    length_scale: float | tuple[float, ...] | np.ndarray = 1.0,
    length_scale_bounds: tuple[float, float] | list[tuple[float, float]] | np.ndarray = (1e-5, 1e5),
    nu: float = 1.5,
    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,
        length_scale=length_scale,
        length_scale_bounds=length_scale_bounds,
        nu=nu,
    )

hyperparameters property #

hyperparameters: list[Hyperparameter]

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

meta property #

meta: dict[str, Any]

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

n_dims property #

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