Kernel Functions¶
spectrans.kernels ¶
Kernel functions for spectral transformers.
This module provides kernel functions and feature maps used in spectral attention mechanisms and other kernel-based methods. It includes both explicit kernel evaluations and implicit representations through random feature maps.
The kernels approximate attention mechanisms with linear complexity through random feature expansions and spectral decompositions.
Modules:
| Name | Description |
|---|---|
base |
Base classes and interfaces for kernel functions. |
rff |
Random Fourier Features implementations. |
spectral |
Spectral kernel functions and decompositions. |
Classes:
| Name | Description |
|---|---|
CosineKernel |
Cosine similarity kernel. |
FourierKernel |
Kernel defined in Fourier domain. |
GaussianRFFKernel |
Gaussian kernel with RFF approximation. |
KernelFunction |
Abstract base class for kernel functions. |
KernelType |
Type literal for kernel selection. |
LaplacianRFFKernel |
Laplacian kernel with RFF approximation. |
LearnableSpectralKernel |
Spectral kernel with learnable parameters. |
OrthogonalRandomFeatures |
Orthogonal variant of random features. |
PolynomialKernel |
Polynomial kernel implementation. |
PolynomialSpectralKernel |
Polynomial kernel with spectral decomposition. |
RFFAttentionKernel |
RFF designed for attention mechanisms. |
RandomFeatureMap |
Abstract base class for random feature approximations. |
ShiftInvariantKernel |
Base class for shift-invariant kernels. |
SpectralKernel |
Base class for spectral kernels. |
TruncatedSVDKernel |
Kernel approximation via truncated SVD. |
Examples:
Using Gaussian RFF kernel:
>>> from spectrans.kernels import GaussianRFFKernel
>>> kernel = GaussianRFFKernel(input_dim=64, num_features=256, sigma=1.0)
>>> x = torch.randn(32, 100, 64)
>>> features = kernel(x)
>>> assert features.shape == (32, 100, 256)
Using learnable spectral kernel:
>>> from spectrans.kernels import LearnableSpectralKernel
>>> kernel = LearnableSpectralKernel(input_dim=64, rank=16)
>>> K = kernel.compute(x, x)
>>> assert K.shape == (32, 100, 100)
Notes
Kernel approximation achieves linear complexity attention mechanisms through random feature expansions and spectral decompositions. Random Fourier Features, based on Bochner's theorem, approximate shift-invariant kernels via the factorization \(k(\mathbf{x}, \mathbf{y}) \approx \varphi(\mathbf{x})^T \varphi(\mathbf{y})\) where \(\varphi\) maps inputs to a feature space.
Spectral decomposition methods leverage eigendecomposition for kernel computation through low-rank approximations, while orthogonal feature variants apply orthogonalized random projections to reduce approximation variance. 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.layers.attention : Attention layers using these kernels.
spectrans.kernels.base : Base kernel interfaces.
spectrans.kernels.rff : Random Fourier Features implementations.
Classes¶
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). |
Source code in spectrans/kernels/base.py
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
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
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
GaussianRFFKernel ¶
GaussianRFFKernel(input_dim: int, num_features: int, sigma: float = 1.0, use_cos_sin: bool = False, orthogonal: bool = False, trainable: bool = False, seed: int | None = None)
Bases: ShiftInvariantKernel, RandomFeatureMap
Gaussian (RBF) kernel with Random Fourier Features approximation.
Implements the Gaussian kernel using RFF.
The kernel function is: \(k(\mathbf{x}, \mathbf{y}) = \exp\left(-\frac{\|\mathbf{x} - \mathbf{y}\|^2}{2\sigma^2}\right)\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Dimension of input vectors. |
required |
num_features
|
int
|
Number of random Fourier features. |
required |
sigma
|
float
|
Kernel bandwidth (standard deviation). |
1.0
|
use_cos_sin
|
bool
|
If True, use both cos and sin features (doubles feature dimension). |
False
|
orthogonal
|
bool
|
If True, use orthogonal random features. |
False
|
trainable
|
bool
|
If True, make random parameters trainable. |
False
|
seed
|
int | None
|
Random seed for reproducibility. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
omega |
Parameter or Tensor
|
Random frequencies of shape (input_dim, num_features). |
bias |
Parameter or Tensor
|
Random phase shifts of shape (num_features,). |
Methods:
| Name | Description |
|---|---|
forward |
Apply random Fourier feature map. |
evaluate_difference |
Evaluate Gaussian kernel on difference vectors. |
spectral_density |
Spectral density for Gaussian kernel (Gaussian distribution). |
Source code in spectrans/kernels/rff.py
Functions¶
forward ¶
Apply random Fourier feature map.
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 self.output_features. |
Source code in spectrans/kernels/rff.py
evaluate_difference ¶
Evaluate Gaussian kernel on difference vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diff
|
Tensor
|
Difference vectors of shape (..., d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Kernel values of shape (...). |
Source code in spectrans/kernels/rff.py
spectral_density ¶
Spectral density for Gaussian kernel (Gaussian distribution).
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/rff.py
LaplacianRFFKernel ¶
LaplacianRFFKernel(input_dim: int, num_features: int, sigma: float = 1.0, use_cos_sin: bool = False, trainable: bool = False, seed: int | None = None)
Bases: ShiftInvariantKernel, RandomFeatureMap
Laplacian kernel with Random Fourier Features approximation.
Implements the Laplacian kernel using RFF with Cauchy distribution.
The kernel function is: \(k(\mathbf{x}, \mathbf{y}) = \exp\left(-\frac{\|\mathbf{x} - \mathbf{y}\|_1}{\sigma}\right)\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Dimension of input vectors. |
required |
num_features
|
int
|
Number of random Fourier features. |
required |
sigma
|
float
|
Kernel bandwidth parameter. |
1.0
|
use_cos_sin
|
bool
|
If True, use both cos and sin features. |
False
|
trainable
|
bool
|
If True, make random parameters trainable. |
False
|
seed
|
int | None
|
Random seed for reproducibility. |
None
|
Methods:
| Name | Description |
|---|---|
forward |
Apply random Fourier feature map. |
evaluate_difference |
Evaluate Laplacian kernel on difference vectors. |
spectral_density |
Spectral density for Laplacian kernel (Cauchy distribution). |
Source code in spectrans/kernels/rff.py
Functions¶
forward ¶
Apply random Fourier feature map.
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). |
Source code in spectrans/kernels/rff.py
evaluate_difference ¶
Evaluate Laplacian kernel on difference vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
diff
|
Tensor
|
Difference vectors of shape (..., d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Kernel values of shape (...). |
Source code in spectrans/kernels/rff.py
spectral_density ¶
Spectral density for Laplacian kernel (Cauchy distribution).
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/rff.py
OrthogonalRandomFeatures ¶
OrthogonalRandomFeatures(input_dim: int, num_features: int, kernel_type: Literal['gaussian', 'laplacian'] = 'gaussian', sigma: float = 1.0, use_hadamard: bool = False, trainable: bool = False, seed: int | None = None)
Bases: RandomFeatureMap
Orthogonal Random Features for kernel approximation.
Uses structured orthogonal matrices to reduce approximation variance compared to standard i.i.d. Gaussian features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Dimension of input vectors. |
required |
num_features
|
int
|
Number of random features. |
required |
kernel_type
|
Literal['gaussian', 'laplacian']
|
Type of kernel to approximate. |
"gaussian"
|
sigma
|
float
|
Kernel bandwidth parameter. |
1.0
|
use_hadamard
|
bool
|
If True, use fast Hadamard transform. |
False
|
trainable
|
bool
|
If True, make scaling parameters trainable. |
False
|
seed
|
int | None
|
Random seed. |
None
|
Methods:
| Name | Description |
|---|---|
forward |
Apply orthogonal random feature map. |
Source code in spectrans/kernels/rff.py
Functions¶
forward ¶
Apply orthogonal random feature map.
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). |
Source code in spectrans/kernels/rff.py
RFFAttentionKernel ¶
RFFAttentionKernel(input_dim: int, num_features: int, kernel_type: Literal['softmax', 'relu', 'elu'] = 'softmax', use_orthogonal: bool = True, redraw: bool = False, seed: int | None = None)
Bases: RandomFeatureMap
Random Fourier Features specifically designed for attention mechanisms.
Implements positive random features for use in linear attention, following the Performer architecture.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Dimension of input vectors (typically head_dim). |
required |
num_features
|
int
|
Number of random features. |
required |
kernel_type
|
Literal['softmax', 'relu', 'elu']
|
Type of kernel approximation. |
"softmax"
|
use_orthogonal
|
bool
|
If True, use orthogonal random features. |
True
|
redraw
|
bool
|
If True, redraw random features at each forward pass. |
False
|
seed
|
int | None
|
Random seed. |
None
|
Methods:
| Name | Description |
|---|---|
forward |
Apply random feature map for attention. |
Source code in spectrans/kernels/rff.py
Functions¶
forward ¶
Apply random feature map for attention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (..., n, d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Positive feature mapped tensor of shape (..., n, D). |
Source code in spectrans/kernels/rff.py
FourierKernel ¶
FourierKernel(rank: int, input_dim: int, learnable_filter: bool = True, filter_type: Literal['gaussian', 'butterworth', 'ideal'] = 'gaussian', cutoff_freq: float = 0.5)
Bases: Module, SpectralKernel
Kernel defined in Fourier domain.
Defines kernel through spectral filters in frequency space.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rank
|
int
|
Number of Fourier modes. |
required |
input_dim
|
int
|
Input dimension. |
required |
learnable_filter
|
bool
|
Whether filter is learnable. |
True
|
filter_type
|
Literal['gaussian', 'butterworth', 'ideal']
|
Type of spectral filter. |
"gaussian"
|
cutoff_freq
|
float
|
Normalized cutoff frequency. |
0.5
|
Attributes:
| Name | Type | Description |
|---|---|---|
filter |
Parameter or Tensor
|
Spectral filter of shape (rank,). |
Methods:
| Name | Description |
|---|---|
compute |
Compute Fourier kernel. |
Source code in spectrans/kernels/spectral.py
Functions¶
compute ¶
Compute Fourier 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/spectral.py
LearnableSpectralKernel ¶
LearnableSpectralKernel(input_dim: int, rank: int, init_scale: float = 1.0, trainable_eigenvectors: bool = True, normalize: bool = True)
Bases: Module, SpectralKernel
Spectral kernel with learnable eigenvalues and eigenfunctions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Input dimension. |
required |
rank
|
int
|
Number of spectral components. |
required |
init_scale
|
float
|
Initialization scale. |
1.0
|
trainable_eigenvectors
|
bool
|
Whether eigenvectors are trainable. |
True
|
normalize
|
bool
|
Whether to normalize. |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
eigenvectors |
Parameter
|
Learnable eigenvectors of shape (input_dim, rank). |
eigenvalues |
Parameter
|
Learnable eigenvalues of shape (rank,). |
Methods:
| Name | Description |
|---|---|
compute |
Compute learnable spectral kernel. |
extract_features |
Extract spectral features. |
forward |
Forward pass for nn.Module compatibility. |
orthogonalize_eigenvectors |
Orthogonalize eigenvectors via Gram-Schmidt. |
Source code in spectrans/kernels/spectral.py
Functions¶
compute ¶
Compute learnable spectral 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/spectral.py
extract_features ¶
Extract spectral features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input of shape (..., n, d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Spectral features of shape (..., n, r). |
Source code in spectrans/kernels/spectral.py
forward ¶
Forward pass for nn.Module compatibility.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
First input of shape (..., n, d). |
required |
y
|
Tensor | None
|
Second input. If None, returns features. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Kernel matrix or features. |
Source code in spectrans/kernels/spectral.py
orthogonalize_eigenvectors ¶
Orthogonalize eigenvectors via Gram-Schmidt.
PolynomialSpectralKernel ¶
PolynomialSpectralKernel(rank: int, degree: int = 2, coef0: float = 1.0, alpha: float = 1.0, normalize: bool = True)
Bases: SpectralKernel
Polynomial kernel with spectral decomposition.
Computes \((\mathbf{X}\mathbf{Y}^T + c)^d\) using eigendecomposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rank
|
int
|
Rank of spectral approximation. |
required |
degree
|
int
|
Polynomial degree. |
2
|
coef0
|
float
|
Constant coefficient. |
1.0
|
alpha
|
float
|
Scaling factor. |
1.0
|
normalize
|
bool
|
Whether to normalize. |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
degree |
int
|
Polynomial degree. |
coef0 |
float
|
Constant term. |
alpha |
float
|
Scale factor. |
Methods:
| Name | Description |
|---|---|
compute |
Compute polynomial spectral kernel. |
compute_attention |
Compute attention weights using spectral decomposition. |
Source code in spectrans/kernels/spectral.py
Functions¶
compute ¶
Compute polynomial spectral 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/spectral.py
compute_attention ¶
Compute attention weights using spectral decomposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
q
|
Tensor
|
Queries of shape (..., n, d). |
required |
k
|
Tensor
|
Keys of shape (..., m, d). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Attention weights of shape (..., n, m). |
Source code in spectrans/kernels/spectral.py
SpectralKernel ¶
Bases: KernelFunction
Base class for spectral kernel functions.
Spectral kernels use eigendecomposition or spectral analysis for efficient kernel computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rank
|
int
|
Rank of spectral approximation. |
required |
normalize
|
bool
|
Whether to normalize kernel values. |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
rank |
int
|
Approximation rank. |
normalize |
bool
|
Normalization flag. |
Methods:
| Name | Description |
|---|---|
spectral_decomposition |
Compute spectral decomposition of input. |
Source code in spectrans/kernels/spectral.py
Functions¶
spectral_decomposition ¶
Compute spectral decomposition of input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of shape (..., n, d). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
eigenvectors |
Tensor
|
Eigenvectors of shape (..., n, rank). |
eigenvalues |
Tensor
|
Eigenvalues of shape (..., rank). |
Source code in spectrans/kernels/spectral.py
TruncatedSVDKernel ¶
Bases: SpectralKernel
Kernel approximation via truncated SVD.
Uses SVD to compute low-rank approximation of kernel matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rank
|
int
|
Truncation rank. |
required |
normalize
|
bool
|
Whether to normalize. |
True
|
use_randomized
|
bool
|
Use randomized SVD for large matrices. |
False
|
Attributes:
| Name | Type | Description |
|---|---|---|
use_randomized |
bool
|
Whether to use randomized algorithms. |
Methods:
| Name | Description |
|---|---|
compute |
Compute kernel via truncated SVD. |
Source code in spectrans/kernels/spectral.py
Functions¶
compute ¶
Compute kernel via truncated SVD.
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
|
Approximate kernel matrix of shape (..., n, m). |