geometric_kernels.frontends.gpytorch¶
GPyTorch kernel wrapper.
A tutorial on how to use this wrapper to run Gaussian process regression on a geometric space is available in the frontends/GPyTorch.ipynb notebook.
Module Contents¶
- class geometric_kernels.frontends.gpytorch.GPyTorchGeometricKernel(base_kernel, lengthscale=None, nu=None, trainable_nu=False, **kwargs)[source]¶
Bases:
gpytorch.kernels.Kernel
GPyTorch wrapper for
BaseGeometricKernel
.A tutorial on how to use this wrapper to run Gaussian process regression on a geometric space is available in the frontends/GPyTorch.ipynb notebook.
Note
Remember that the base_kernel itself does not store any of its hyperparameters (like lengthscale and nu). If you do not set them manually—when initializing the object or after, by setting the properties—this wrapper will use the values provided by base_kernel.init_params.
Note
As customary in GPyTorch, this wrapper does not maintain a variance (outputscale) parameter. To add it, use
gpytorch.kernels.ScaleKernel(GPyTorchGeometricKernel(...))
.- Parameters:
base_kernel (geometric_kernels.kernels.BaseGeometricKernel) – The kernel to wrap.
lengthscale (beartype.typing.Union[float, torch.Tensor, numpy.ndarray]) –
Initial value of the length scale.
If not given or set to None, uses the default value of the base_kernel, as provided by its init_params method.
nu (beartype.typing.Union[float, torch.Tensor, numpy.ndarray]) –
Initial value of the smoothness parameter nu.
If not given or set to None, uses the default value of the base_kernel, as provided by its init_params method.
trainable_nu (bool) –
Whether or not the parameter nu is to be optimized over.
Cannot be True if nu is equal to infinity. You cannot change this parameter after constructing the object. Defaults to False.
- Raises:
ValueError – If trying to set nu = infinity together with trainable_nu = True.
- __call__(x1, x2=None, diag=False, last_dim_is_batch=False, **params)¶
Computes the covariance between \(\mathbf x_1\) and \(\mathbf x_2\).
Note
Following PyTorch convention, all
GP
objects should use __call__ rather thanforward()
. The __call__ method applies additional pre- and post-processing to the forward method, and additionally employs a lazy evaluation scheme to reduce memory and computational costs.- Parameters:
x1 (torch.Tensor) – First set of data (… x N x D).
x2 (Optional[torch.Tensor]) – Second set of data (… x M x D). (If None, then x2 is set to x1.)
diag (bool) – Should the Kernel compute the whole kernel, or just the diag? If True, it must be the case that x1 == x2. (Default: False.)
last_dim_is_batch (bool) – If True, treat the last dimension of x1 and x2 as another batch dimension. (Useful for additive structure over the dimensions). (Default: False.)
- Returns:
An object that will lazily evaluate to the kernel matrix or vector. The shape depends on the kernel’s evaluation mode:
full_covar: … x N x M
full_covar with last_dim_is_batch=True: … x K x N x M
diag: … x N
diag with last_dim_is_batch=True: … x K x N
- Return type:
Union[gpytorch.lazy.LazyEvaluatedKernelTensor, linear_operator.operators.LinearOperator, torch.Tensor]
- __init__(base_kernel, lengthscale=None, nu=None, trainable_nu=False, **kwargs)[source]¶
Initializes internal Module state, shared by both nn.Module and ScriptModule.
- Parameters:
base_kernel (geometric_kernels.kernels.BaseGeometricKernel)
lengthscale (beartype.typing.Union[float, torch.Tensor, numpy.ndarray])
nu (beartype.typing.Union[float, torch.Tensor, numpy.ndarray])
trainable_nu (bool)
- forward(x1, x2, diag=False, last_dim_is_batch=False, **kwargs)[source]¶
Evaluate the covariance matrix K(x1, x2).
- Parameters:
x1 (torch.Tensor) – First batch of inputs.
x2 (torch.Tensor) – Second batch of inputs.
diag (bool) – If set to True, ignores x2 and returns the diagonal of K(x1, x1).
last_dim_is_batch (bool) – Ignored.
- Returns:
The covariance matrix K(x1, x2) or, if diag=True, the diagonal of the covariance matrix K(x1, x1).
- Return type:
torch.Tensor
- property nu: torch.Tensor¶
The smoothness parameter
- Return type:
torch.Tensor
- property space: beartype.typing.Union[geometric_kernels.spaces.Space, beartype.typing.List[geometric_kernels.spaces.Space]]¶
Alias to the base_kernels space property.
- Return type:
beartype.typing.Union[geometric_kernels.spaces.Space, beartype.typing.List[geometric_kernels.spaces.Space]]