Kernel Composite#
composite
#
| CLASS | DESCRIPTION |
|---|---|
CompositeKernel |
Abstract base class for composite kernels that combine multiple kernels. |
SumKernel |
Sum of multiple kernels. |
ProductKernel |
Product of multiple kernels. |
ScaledKernel |
Scaled version of a kernel. |
| FUNCTION | DESCRIPTION |
|---|---|
sum_kernels |
Create a sum of kernels (functional interface). |
product_kernels |
Create a product of kernels (functional interface). |
scale_kernel |
Create a scaled kernel (functional interface). |
Classes#
CompositeKernel(kernels: list[Kernel], learnable: bool = False, learnable_log: bool = True, normalization=None, dtype: torch.dtype = torch.float32, **kwargs)
#
Bases: Kernel, ABC
Abstract base class for composite kernels that combine multiple kernels.
Provides a framework for kernel arithmetic operations such as addition, multiplication, and weighted combinations of kernel functions.
| PARAMETER | DESCRIPTION |
|---|---|
kernels
|
List of kernel functions to combine.
TYPE:
|
learnable
|
Whether composite parameters should be learnable.
TYPE:
|
learnable_log
|
Whether to parameterize learnable parameters in log space.
TYPE:
|
normalization
|
Kernel normalization instance for the final combined kernel.
TYPE:
|
dtype
|
Data type for computations.
TYPE:
|
**kwargs
|
Additional arguments passed to Kernel base class.
DEFAULT:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
kernels |
List of kernel functions.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
forward_step |
Compute composite kernel matrix from distance matrix. |
Source code in spectre/kernel/composite.py
Functions#
forward_step(D: torch.Tensor, weights: torch.Tensor | None = None, **kwargs) -> torch.Tensor
#
Compute composite kernel matrix from distance matrix.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix of shape (n_samples, n_samples).
TYPE:
|
weights
|
Sample weights for normalization.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Combined kernel matrix. |
Source code in spectre/kernel/composite.py
SumKernel(kernels: list[Kernel], weights: list[float] | None = None, learnable: bool = False, learnable_log: bool = True, **kwargs)
#
Bases: CompositeKernel
Sum of multiple kernels.
Combines kernels by element-wise addition, optionally with learnable weights.
| PARAMETER | DESCRIPTION |
|---|---|
kernels
|
List of kernel functions to sum.
TYPE:
|
weights
|
Weights for each kernel. If None, uses uniform weights.
TYPE:
|
learnable
|
Whether weights should be learnable.
TYPE:
|
learnable_log
|
Whether to parameterize weights in log space.
TYPE:
|
normalize
|
Whether to apply normalization.
TYPE:
|
**kwargs
|
Additional arguments passed to CompositeKernel.
DEFAULT:
|
Examples:
>>> from spectre.kernel import GaussianKernel, PolynomialKernel
>>> k1 = GaussianKernel(eps=1.0)
>>> k2 = PolynomialKernel(degree=2)
>>> sum_kernel = SumKernel([k1, k2], weights=[0.7, 0.3])
>>> X = torch.randn(50, 3)
>>> K = sum_kernel(X)
| METHOD | DESCRIPTION |
|---|---|
get_kernel_params |
Return current kernel parameters. |
Source code in spectre/kernel/composite.py
ProductKernel(kernels: list[Kernel], exponents: list[float] | None = None, learnable: bool = False, learnable_log: bool = True, **kwargs)
#
Bases: CompositeKernel
Product of multiple kernels.
Combines kernels by element-wise multiplication, optionally with learnable exponents.
| PARAMETER | DESCRIPTION |
|---|---|
kernels
|
List of kernel functions to multiply.
TYPE:
|
exponents
|
Exponents for each kernel. If None, uses exponents of 1.0.
TYPE:
|
learnable
|
Whether exponents should be learnable.
TYPE:
|
learnable_log
|
Whether to parameterize exponents in log space.
TYPE:
|
normalize
|
Whether to apply normalization.
TYPE:
|
**kwargs
|
Additional arguments passed to CompositeKernel.
DEFAULT:
|
Examples:
>>> k1 = GaussianKernel(eps=1.0)
>>> k2 = PolynomialKernel(degree=2)
>>> prod_kernel = ProductKernel([k1, k2], exponents=[1.0, 0.5])
>>> X = torch.randn(50, 3)
>>> K = prod_kernel(X)
| METHOD | DESCRIPTION |
|---|---|
get_kernel_params |
Return current kernel parameters. |
Source code in spectre/kernel/composite.py
ScaledKernel(kernel: Kernel, scale: float = 1.0, learnable: bool = False, learnable_log: bool = True, **kwargs)
#
Bases: Kernel
Scaled version of a kernel.
Applies a learnable or fixed scaling factor to a kernel.
| PARAMETER | DESCRIPTION |
|---|---|
kernel
|
Base kernel to scale.
TYPE:
|
scale
|
Scaling factor.
TYPE:
|
learnable
|
Whether scale should be learnable.
TYPE:
|
learnable_log
|
Whether to parameterize scale in log space.
TYPE:
|
normalize
|
Whether to apply normalization.
TYPE:
|
**kwargs
|
Additional arguments passed to Kernel.
DEFAULT:
|
Examples:
>>> base_kernel = GaussianKernel(eps=1.0)
>>> scaled_kernel = ScaledKernel(base_kernel, scale=2.0, learnable=True)
>>> X = torch.randn(50, 3)
>>> K = scaled_kernel(X)
| METHOD | DESCRIPTION |
|---|---|
forward_step |
Compute scaled kernel matrix from distance matrix. |
get_kernel_params |
Return current kernel parameters. |
Source code in spectre/kernel/composite.py
Functions#
forward_step(D: torch.Tensor, weights: torch.Tensor | None = None, **kwargs) -> torch.Tensor
#
Compute scaled kernel matrix from distance matrix.
| PARAMETER | DESCRIPTION |
|---|---|
D
|
Pairwise distance matrix of shape (n_samples, n_samples).
TYPE:
|
weights
|
Sample weights for normalization.
TYPE:
|
**kwargs
|
Additional keyword arguments passed to the kernel.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scaled kernel matrix. |
Source code in spectre/kernel/composite.py
Functions#
sum_kernels(kernels: list[Kernel], weights: list[float] | None = None) -> SumKernel
#
Create a sum of kernels (functional interface).
| PARAMETER | DESCRIPTION |
|---|---|
kernels
|
List of kernels to sum.
TYPE:
|
weights
|
Weights for each kernel.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SumKernel
|
Sum kernel object. |
Examples:
>>> k1 = GaussianKernel(bw_method=torch.tensor(1.0))
>>> k2 = PolynomialKernel(degree=torch.tensor(2))
>>> sum_k = sum_kernels([k1, k2], weights=[0.6, 0.4])
Source code in spectre/kernel/composite.py
product_kernels(kernels: list[Kernel], exponents: list[float] | None = None) -> ProductKernel
#
Create a product of kernels (functional interface).
| PARAMETER | DESCRIPTION |
|---|---|
kernels
|
List of kernels to multiply.
TYPE:
|
exponents
|
Exponents for each kernel.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ProductKernel
|
Product kernel object. |
Examples:
>>> k1 = GaussianKernel(bw_method=torch.tensor(1.0))
>>> k2 = PolynomialKernel(degree=torch.tensor(2))
>>> prod_k = product_kernels([k1, k2], exponents=[1.0, 0.5])
Source code in spectre/kernel/composite.py
scale_kernel(kernel: Kernel, scale: float = 1.0) -> ScaledKernel
#
Create a scaled kernel (functional interface).
| PARAMETER | DESCRIPTION |
|---|---|
kernel
|
Base kernel to scale.
TYPE:
|
scale
|
Scaling factor.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ScaledKernel
|
Scaled kernel object. |
Examples:
>>> base_k = GaussianKernel(bw_method=torch.tensor(1.0))
>>> scaled_k = scale_kernel(base_k, scale=2.0)