Skip to content

Matern kernel

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,
)

Bases: AbstractKernel, Matern

Matern kernel implementation.

Source code in 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]

Returns a list of all hyperparameter specifications.

meta property #

meta: dict[str, Any]

Returns the meta data of the created object. This method calls the get_params method to collect the parameters of the kernel.

n_dims property #

n_dims: int

Returns the number of non-fixed hyperparameters of the kernel.

__call__ #

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

Call the kernel function. Internally, self._call is called, which must be specified by a subclass.

Source code in 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]

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.

Source code in 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