Spectral Kernels¶
spectrans.kernels.spectral ¶
Spectral kernel functions for attention mechanisms.
This module implements kernel functions based on spectral decomposition and eigenfunction expansions. These kernels provide alternatives to RFF-based approximations for spectral attention mechanisms.
The implementations include polynomial spectral kernels, eigenvalue-based decompositions, and learnable spectral filters that can be optimized during training.
Classes:
| Name | Description |
|---|---|
SpectralKernel |
Base class for spectral kernel functions. |
PolynomialSpectralKernel |
Polynomial kernel with spectral decomposition. |
TruncatedSVDKernel |
Kernel approximation via truncated SVD. |
LearnableSpectralKernel |
Spectral kernel with learnable eigenvalues. |
FourierKernel |
Kernel defined in Fourier domain. |
Examples:
Using polynomial spectral kernel:
>>> import torch
>>> from spectrans.kernels.spectral import PolynomialSpectralKernel
>>> kernel = PolynomialSpectralKernel(rank=32, degree=3)
>>> Q, K = torch.randn(2, 100, 64), torch.randn(2, 100, 64)
>>> attention_weights = kernel.compute_attention(Q, K)
>>> assert attention_weights.shape == (2, 100, 100)
Learnable spectral kernel:
>>> from spectrans.kernels.spectral import LearnableSpectralKernel
>>> kernel = LearnableSpectralKernel(input_dim=64, rank=16)
>>> features = kernel.extract_features(Q)
>>> assert features.shape == (2, 100, 16)
Notes
Spectral kernels leverage eigendecomposition for kernel computation through the representation \(K(\mathbf{X}, \mathbf{Y}) = \Phi(\mathbf{X}) \mathbf{\Lambda} \Phi(\mathbf{Y})^T\) where \(\Phi\) are eigenfunctions and \(\mathbf{\Lambda}\) are eigenvalues.
This decomposition enables low-rank approximations via truncation of the eigenspectrum and learnable spectral filters through trainable eigenvalues. The rank parameter determines the number of eigenmodes retained in the approximation.
References
Yunyang Chen, Yingfeng Luo, and Liping Zhang. 2021. Scatterbrain: Unifying sparse and low-rank attention approximation. In Advances in Neural Information Processing Systems 34 (NeurIPS 2021).
Sinong Wang, Belinda Z. Li, Madian Khabsa, Han Fang, and Hao Ma. 2020. Linformer: Self-attention with linear complexity. arXiv preprint arXiv:2006.04768.
See Also
spectrans.kernels.base : Base kernel interfaces. spectrans.kernels.rff : Random Fourier Features.
Classes¶
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
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
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). |
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.
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). |