Base Kernel Classes¶
spectrans.kernels.base ¶
Base interfaces and classes for kernel functions and random feature maps.
This module defines the abstract base classes for kernel functions used in spectral attention mechanisms and other kernel-based methods. It provides interfaces for both explicit kernel evaluations and implicit feature map representations through random features.
The kernel framework supports various approximation techniques including Random Fourier Features (RFF), polynomial kernels, and spectral kernels, enabling computation of attention mechanisms with linear complexity.
Classes:
| Name | Description |
|---|---|
KernelFunction |
Abstract base class for kernel functions \(k(\mathbf{x}, \mathbf{y})\). |
RandomFeatureMap |
Abstract base class for random feature approximations. |
ShiftInvariantKernel |
Base class for shift-invariant (stationary) kernels. |
Examples:
Implementing a custom kernel:
>>> import torch
>>> from spectrans.kernels.base import KernelFunction
>>> class LinearKernel(KernelFunction):
... def compute(self, x, y):
... return torch.matmul(x, y.transpose(-2, -1))
Using a random feature map:
>>> from spectrans.kernels.base import RandomFeatureMap
>>> class CustomFeatureMap(RandomFeatureMap):
... def __init__(self, input_dim, num_features):
... super().__init__(input_dim, num_features)
... # Initialize random parameters
... def forward(self, x):
... # Return feature mapped tensor
... pass
Notes
For shift-invariant kernels, Bochner's theorem states that any positive definite shift-invariant kernel can be represented as the Fourier transform of a non-negative measure:
This representation enables Random Fourier Features approximation through Monte Carlo sampling, where the kernel is approximated by the inner product of explicit feature maps \(k(\mathbf{x}, \mathbf{y}) \approx \varphi(\mathbf{x})^T \varphi(\mathbf{y})\). The feature map takes the form \(\varphi(\mathbf{x}) = \sqrt{\frac{2}{D}} [\cos(\omega_1^T\mathbf{x} + b_1), \ldots, \cos(\omega_D^T\mathbf{x} + b_D)]\) where \(\omega_i\) are sampled from \(p(\omega)\) and \(b_i\) from \(\text{Uniform}[0, 2\pi]\).
The approximation error decreases with \(O(1/\sqrt{D})\) where \(D\) is the number of random features.
References
Ali Rahimi and Benjamin Recht. 2007. Random features for large-scale kernel machines. In Advances in Neural Information Processing Systems 20 (NeurIPS 2007), pages 1177-1184.
Krzysztof Choromanski, Valerii Likhosherstov, David Dohan, Xingyou Song, Andreea Gane, Tamas Sarlos, Peter Hawkins, Jared Davis, Afroz Mohiuddin, Lukasz Kaiser, David Belanger, Lucy Colwell, and Adrian Weller. 2021. Rethinking attention with performers. In Proceedings of the International Conference on Learning Representations (ICLR).
See Also
spectrans.kernels.rff : Random Fourier Features implementation. spectrans.kernels.spectral : Spectral kernel functions.
Classes¶
KernelFunction ¶
Bases: ABC
Abstract base class for kernel functions.
A kernel function \(k(\mathbf{x}, \mathbf{y})\) defines a similarity measure between inputs \(\mathbf{x}\) and \(\mathbf{y}\), satisfying positive semi-definiteness properties. This interface supports both explicit kernel evaluation and feature map representations.
Methods:
| Name | Description |
|---|---|
compute |
Compute kernel values between x and y. |
gram_matrix |
Compute Gram matrix \(K_{ij} = k(\mathbf{x}_i, \mathbf{x}_j)\). |
is_positive_definite |
Check if the kernel yields a positive definite Gram matrix. |
Functions¶
compute
abstractmethod
¶
Compute kernel values between x and y.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
First input tensor of shape (..., n, d). |
required |
y
|
Tensor
|
Second input tensor of shape (..., m, d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Kernel matrix of shape (..., n, m) where element \((i,j)\) contains \(k(\mathbf{x}_i, \mathbf{y}_j)\). |
Source code in spectrans/kernels/base.py
gram_matrix ¶
Compute Gram matrix \(K_{ij} = k(\mathbf{x}_i, \mathbf{x}_j)\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (..., n, d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Gram matrix of shape (..., n, n). |
Source code in spectrans/kernels/base.py
is_positive_definite ¶
Check if the kernel yields a positive definite Gram matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (..., n, d). |
required |
eps
|
float
|
Tolerance for eigenvalue positivity check. |
1e-6
|
Returns:
| Type | Description |
|---|---|
bool
|
True if all eigenvalues of Gram matrix are > eps. |
Source code in spectrans/kernels/base.py
RandomFeatureMap ¶
RandomFeatureMap(input_dim: int, num_features: int, kernel_scale: float = 1.0, seed: int | None = None)
Bases: Module, ABC
Abstract base class for random feature map approximations.
Random feature maps provide finite-dimensional approximations to kernel functions through the mapping:
.. math:: k(\mathbf{x}, \mathbf{y}) \approx \varphi(\mathbf{x})^T \varphi(\mathbf{y})
This enables linear-time computation of kernel operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Dimension of input vectors. |
required |
num_features
|
int
|
Number of random features (D). |
required |
kernel_scale
|
float
|
Scaling parameter for the kernel. |
1.0
|
seed
|
int | None
|
Random seed for reproducibility. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
input_dim |
int
|
Input dimension. |
num_features |
int
|
Number of random features. |
kernel_scale |
float
|
Kernel scaling parameter. |
Methods:
| Name | Description |
|---|---|
forward |
Apply feature map to input. |
kernel_approximation |
Approximate kernel matrix using feature maps. |
Source code in spectrans/kernels/base.py
Functions¶
forward
abstractmethod
¶
Apply feature map to input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (..., n, d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Feature mapped tensor of shape (..., n, D) where D is the number of random features. |
Source code in spectrans/kernels/base.py
kernel_approximation ¶
Approximate kernel matrix using feature maps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
First input of shape (..., n, d). |
required |
y
|
Tensor
|
Second input of shape (..., m, d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Approximated kernel matrix of shape (..., n, m). |
Source code in spectrans/kernels/base.py
ShiftInvariantKernel ¶
Bases: KernelFunction
Base class for shift-invariant (stationary) kernels.
Shift-invariant kernels depend only on the difference \(\mathbf{x} - \mathbf{y}\), i.e., \(k(\mathbf{x}, \mathbf{y}) = k(\mathbf{x} - \mathbf{y}, \mathbf{0})\) \(= \kappa(\mathbf{x} - \mathbf{y})\) for some function \(\kappa\).
These kernels admit Random Fourier Features approximation via Bochner's theorem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bandwidth
|
float
|
Kernel bandwidth parameter (inverse of length scale). |
1.0
|
Attributes:
| Name | Type | Description |
|---|---|---|
bandwidth |
float
|
The bandwidth parameter. |
Methods:
| Name | Description |
|---|---|
evaluate_difference |
Evaluate kernel on difference vectors. |
compute |
Compute kernel matrix for shift-invariant kernel. |
spectral_density |
Fourier transform of the kernel (spectral density). |
Source code in spectrans/kernels/base.py
Functions¶
evaluate_difference
abstractmethod
¶
Evaluate kernel on difference vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diff
|
Tensor
|
Difference vectors \(\mathbf{x} - \mathbf{y}\) of shape (..., d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Kernel values \(\kappa(\text{diff})\) of shape (...). |
Source code in spectrans/kernels/base.py
compute ¶
Compute kernel matrix for shift-invariant kernel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
First input of shape (..., n, d). |
required |
y
|
Tensor
|
Second input of shape (..., m, d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Kernel matrix of shape (..., n, m). |
Source code in spectrans/kernels/base.py
spectral_density
abstractmethod
¶
Fourier transform of the kernel (spectral density).
For shift-invariant kernels, this defines the sampling distribution for Random Fourier Features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
omega
|
Tensor
|
Frequency vectors of shape (..., d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Spectral density values of shape (...). |
Source code in spectrans/kernels/base.py
PolynomialKernel ¶
Bases: KernelFunction
Polynomial kernel.
The kernel function is: \(k(\mathbf{x}, \mathbf{y}) = (\alpha \langle \mathbf{x}, \mathbf{y} \rangle + c)^d\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
degree
|
int
|
Polynomial degree. |
2
|
alpha
|
float
|
Scaling of inner product. |
1.0
|
coef0
|
float
|
Constant term. |
0.0
|
Attributes:
| Name | Type | Description |
|---|---|---|
degree |
int
|
The polynomial degree. |
alpha |
float
|
Inner product scaling. |
coef0 |
float
|
Constant coefficient. |
Methods:
| Name | Description |
|---|---|
compute |
Compute polynomial kernel matrix. |
Source code in spectrans/kernels/base.py
Functions¶
compute ¶
Compute polynomial kernel matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
First input of shape (..., n, d). |
required |
y
|
Tensor
|
Second input of shape (..., m, d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Kernel matrix of shape (..., n, m). |
Source code in spectrans/kernels/base.py
CosineKernel ¶
Bases: KernelFunction
Cosine similarity kernel.
The kernel function is: \(k(\mathbf{x}, \mathbf{y}) =\) \(\frac{\langle \mathbf{x}, \mathbf{y} \rangle}{\|\mathbf{x}\| \|\mathbf{y}\|}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eps
|
float
|
Small value for numerical stability. |
1e-8
|
Attributes:
| Name | Type | Description |
|---|---|---|
eps |
float
|
Numerical stability parameter. |
Methods:
| Name | Description |
|---|---|
compute |
Compute cosine similarity kernel matrix. |
Source code in spectrans/kernels/base.py
Functions¶
compute ¶
Compute cosine similarity kernel matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
First input of shape (..., n, d). |
required |
y
|
Tensor
|
Second input of shape (..., m, d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Kernel matrix of shape (..., n, m). |