Skip to content

kernels: Kernel Functions

This module contains kernel functions for the Gaussian Process models. This includes all kernel functions in gpflow, and advanced, non-stationary kernels. Use the get_kernel method to retrieve a kernel class by its string name.

  • Attentive: A non-stationary kernel that uses a neural network to learn attention weights for a mixture of RBF kernels. This allows the model to adapt its assumptions about the data's correlation structure across the input space.

  • NeuralSpectral: Another non-stationary kernel that employs Multilayer perceptrons (MLPs) to learn the frequency, lengthscale, and variance of a spectral mixture. This provides a flexible way to model complex, non-stationary data.

  • gpflow.kernels: All available kernels in gpflow's kernels module.

sgptools.kernels.get_kernel(kernel)

Retrieves a Kernel class from the KERNELS dictionary based on its string name.

Parameters:

Name Type Description Default
kernel str

The name of the kernel to retrieve. The name must be a key in the KERNELS dictionary. Includes all available kernels in gpflow. e.g., 'NeuralSpectralKernel', 'AttentiveKernel', 'RBF'.

required

Returns:

Type Description
Type[Kernel]

Type[Kernel]: The kernel class corresponding to the provided kernel name. This class can then be instantiated to create a kernel object.

Raises:

Type Description
KeyError

If the provided kernel name does not exist in the KERNELS dictionary.

Usage
from sgptools.kernels import get_kernel
import gpflow
import numpy as np

# --- Select and instantiate a kernel ---
# 1. Get the RBF kernel class
RBFKernelClass = get_kernel('RBF')
# 2. Instantiate the kernel with specific parameters
rbf_kernel = RBFKernelClass(lengthscales=1.2)

# --- Or for a more complex custom kernel ---
NeuralKernelClass = get_kernel('NeuralSpectral')
neural_kernel = NeuralKernelClass(input_dim=2, Q=3, hidden_sizes=[32, 32])

# --- Example of using the kernel in a GPR model ---
# Dummy data
X = np.random.rand(10, 1)
Y = np.sin(X) + np.random.randn(*X.shape)*0.1
# Create a model with the selected RBF kernel
model = gpflow.models.GPR(data=(X, Y), kernel=rbf_kernel)
Source code in sgptools/kernels/__init__.py
def get_kernel(kernel: str) -> Type[gpflow.kernels.Kernel]:
    """
    Retrieves a Kernel class from the `KERNELS` dictionary based on its string name.

    Args:
        kernel (str): The name of the kernel to retrieve. The name must be a key
                      in the `KERNELS` dictionary. Includes all available kernels in gpflow.
                      e.g., 'NeuralSpectralKernel', 'AttentiveKernel', 'RBF'.

    Returns:
        Type[Kernel]: The kernel class corresponding to the provided kernel name.
                      This class can then be instantiated to create a kernel object.

    Raises:
        KeyError: If the provided `kernel` name does not exist in the `KERNELS` dictionary.

    Usage:
        ```python
        from sgptools.kernels import get_kernel
        import gpflow
        import numpy as np

        # --- Select and instantiate a kernel ---
        # 1. Get the RBF kernel class
        RBFKernelClass = get_kernel('RBF')
        # 2. Instantiate the kernel with specific parameters
        rbf_kernel = RBFKernelClass(lengthscales=1.2)

        # --- Or for a more complex custom kernel ---
        NeuralKernelClass = get_kernel('NeuralSpectral')
        neural_kernel = NeuralKernelClass(input_dim=2, Q=3, hidden_sizes=[32, 32])

        # --- Example of using the kernel in a GPR model ---
        # Dummy data
        X = np.random.rand(10, 1)
        Y = np.sin(X) + np.random.randn(*X.shape)*0.1
        # Create a model with the selected RBF kernel
        model = gpflow.models.GPR(data=(X, Y), kernel=rbf_kernel)
        ```
    """
    if kernel not in KERNELS:
        raise KeyError(f"Kernel '{kernel}' not found. Available options: {list(KERNELS.keys())}")
    return KERNELS[kernel]