Kernel¶
-
class
probnum.kernels.Kernel(input_dim, output_dim=1)¶ Bases:
typing.Generic,abc.ABCKernel / covariance function.
Abstract base class for kernels / covariance functions. Kernels are a generalization of a positive-definite function or matrix. They typically define the covariance function of a random process and thus describe its spatial or temporal variation. If evaluated at two sets of points a kernel gives the covariance of the random process at these locations.
- Parameters
Examples
Kernels are implemented by subclassing this abstract base class.
>>> from probnum.kernels import Kernel ... >>> class CustomLinearKernel(Kernel): ... ... def __init__(self, constant=0.0): ... self.constant = constant ... super().__init__(input_dim=1, output_dim=1) ... ... def __call__(self, x0, x1=None): ... # Check and reshape inputs ... x0, x1, kernshape = self._check_and_reshape_inputs(x0, x1) ... ... # Compute kernel matrix ... if x1 is None: ... x1 = x0 ... kernmat = x0 @ x1.T + self.constant ... ... return Kernel._reshape_kernelmatrix(kernmat, newshape=kernshape)
We can now evaluate the kernel like so.
>>> import numpy as np >>> k = CustomLinearKernel(constant=1.0) >>> k(np.linspace(0, 1, 4)[:, None]) array([[1. , 1. , 1. , 1. ], [1. , 1.11111111, 1.22222222, 1.33333333], [1. , 1.22222222, 1.44444444, 1.66666667], [1. , 1.33333333, 1.66666667, 2. ]])
Attributes Summary
Dimension of arguments of the covariance function.
Dimension of the evaluated covariance function.
Methods Summary
__call__(x0[, x1])Evaluate the kernel.
Attributes Documentation
-
input_dim¶ Dimension of arguments of the covariance function.
The dimension of inputs to the covariance function \(k : \mathbb{R}^{ d_{in}} \times \mathbb{R}^{d_{in}} \rightarrow \mathbb{R}^{d_{out} \times d_{out}}\).
- Return type
-
output_dim¶ Dimension of the evaluated covariance function.
The resulting evaluated kernel \(k(x_0, x_1) \in \mathbb{R}^{d_{out} \times d_{out}}\) has shape=(output_dim, output_dim).
- Return type
Methods Documentation
-
abstract
__call__(x0, x1=None)[source]¶ Evaluate the kernel.
Computes the covariance function at
x0andx1. If the inputs have more than one dimension the covariance function is evaluated pairwise for all observations determined by the first dimension ofx0andx1. If onlyx0is given the kernel matrix \(K=k(X_0, X_0)\) is computed.- Parameters
x0 (~InputType) – shape=(input_dim,) or (n0, input_dim) – First input.
x1 (
Optional[~InputType]) – shape=(input_dim,) or (n1, input_dim) – Second input.
- Returns
shape=(), (output_dim, output_dim) or (n0, n1) or (n0, n1, output_dim, output_dim) – Kernel evaluated at
x0andx1or kernel matrix containing pairwise evaluations for all observations inx0(andx1).- Return type
cov