Base Mixing Classes¶
spectrans.layers.mixing.base ¶
Base classes for spectral mixing layers.
Provides base classes for spectral mixing layers, extending a base MixingLayer with domain-specific functionality for token mixing operations using spectral transforms. These classes define mathematical interfaces and computational requirements for spectral transformers.
Mixing layers implement core token mixing operations that replace traditional attention mechanisms in spectral transformers, providing linear or log-linear computational complexity for sequence modeling tasks.
Classes:
| Name | Description |
|---|---|
MixingLayer |
Base class for spectral mixing operations. |
UnitaryMixingLayer |
Base class for mixing layers that preserve energy (unitary operations). |
FilterMixingLayer |
Base class for frequency-domain filtering operations. |
Examples:
Implementing a custom spectral mixing layer:
>>> from spectrans.layers.mixing.base import MixingLayer
>>> class CustomMixing(MixingLayer):
... def forward(self, x):
... # Custom spectral mixing implementation
... return self.apply_spectral_operation(x)
Creating a unitary mixing layer:
>>> from spectrans.layers.mixing.base import UnitaryMixingLayer
>>> class OrthogonalMixing(UnitaryMixingLayer):
... def forward(self, x):
... return self.apply_unitary_transform(x)
... def verify_unitarity(self, x):
... # Custom verification logic
... return True
Notes
Mathematical Properties:
All spectral mixing layers preserve shape where output equals input shape for sequence modeling, support batched processing with consistent behavior, and maintain full gradient flow for end-to-end training.
Unitary mixing layers additionally satisfy energy preservation \(||f(\mathbf{x})||^2 = ||\mathbf{x}||^2\) following Parseval's theorem and preserve inner products through orthogonality.
Filter mixing layers operate in frequency domain, applying learned filters to frequency components with localized operations in frequency space.
See Also
spectrans.core.base : Core base classes for all spectral components spectrans.transforms : Spectral transform implementations used by mixing layers
Classes¶
MixingLayer ¶
Bases: SpectralComponent
Base class for spectral mixing operations.
Mixing layers perform token mixing operations using various spectral transforms instead of traditional attention mechanisms. This class provides spectral-specific functionality including mathematical property verification and standardized interfaces for spectral transform operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension of the model. |
required |
dropout
|
float
|
Dropout probability for regularization. |
0.0
|
norm_eps
|
float
|
Epsilon for numerical stability in normalization. |
1e-5
|
Attributes:
| Name | Type | Description |
|---|---|---|
hidden_dim |
int
|
Hidden dimension of the model. |
dropout |
Module
|
Dropout layer for regularization. |
norm_eps |
float
|
Epsilon for numerical stability. |
Methods:
| Name | Description |
|---|---|
get_spectral_properties |
Get mathematical properties of the spectral operation. |
verify_shape_consistency |
Verify that input and output shapes are consistent. |
compute_spectral_norm |
Compute spectral norm for analysis and regularization. |
Source code in spectrans/layers/mixing/base.py
Functions¶
get_spectral_properties
abstractmethod
¶
Get mathematical properties of the spectral operation.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing mathematical properties such as: - 'unitary': bool, whether the transform is unitary - 'real_output': bool, whether output is guaranteed real - 'frequency_domain': bool, whether operation occurs in frequency domain - 'energy_preserving': bool, whether energy is preserved |
Source code in spectrans/layers/mixing/base.py
verify_shape_consistency ¶
Verify that input and output shapes are consistent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_tensor
|
Tensor
|
Input tensor to the mixing layer. |
required |
output_tensor
|
Tensor
|
Output tensor from the mixing layer. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if shapes are consistent, False otherwise. |
Source code in spectrans/layers/mixing/base.py
compute_spectral_norm ¶
Compute spectral norm for analysis and regularization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Input tensor to compute spectral norm for. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Spectral norm of the input tensor. |
Source code in spectrans/layers/mixing/base.py
UnitaryMixingLayer ¶
UnitaryMixingLayer(hidden_dim: int, dropout: float = 0.0, norm_eps: float = 1e-05, energy_tolerance: float = 0.0001)
Bases: MixingLayer
Base class for unitary mixing operations.
Unitary mixing layers preserve energy and inner products, maintaining mathematical properties essential for stable training and theoretical guarantees in spectral transformers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension of the model. |
required |
dropout
|
float
|
Dropout probability for regularization. |
0.0
|
norm_eps
|
float
|
Epsilon for numerical stability. |
1e-5
|
energy_tolerance
|
float
|
Tolerance for energy preservation verification. |
1e-4
|
Attributes:
| Name | Type | Description |
|---|---|---|
energy_tolerance |
float
|
Tolerance for energy preservation checks. |
Methods:
| Name | Description |
|---|---|
get_spectral_properties |
Get properties specific to unitary transforms. |
verify_energy_preservation |
Verify energy preservation (Parseval's theorem). |
verify_orthogonality |
Verify orthogonality of the transform matrix. |
Source code in spectrans/layers/mixing/base.py
Functions¶
get_spectral_properties ¶
Get properties specific to unitary transforms.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing unitary transform properties. |
Source code in spectrans/layers/mixing/base.py
verify_energy_preservation ¶
Verify energy preservation (Parseval's theorem).
Checks that \(||\mathbf{output}||^2 \approx ||\mathbf{input}||^2\) within tolerance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_tensor
|
Tensor
|
Input tensor before transformation. |
required |
output_tensor
|
Tensor
|
Output tensor after transformation. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if energy is preserved within tolerance. |
Source code in spectrans/layers/mixing/base.py
verify_orthogonality ¶
Verify orthogonality of the transform matrix.
Checks that \(\mathbf{T} \mathbf{T}^H \approx \mathbf{I}\) (identity matrix).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transform_matrix
|
Tensor
|
Transform matrix to verify. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if matrix is orthogonal within tolerance. |
Source code in spectrans/layers/mixing/base.py
FilterMixingLayer ¶
FilterMixingLayer(hidden_dim: int, sequence_length: int, dropout: float = 0.0, norm_eps: float = 1e-05, learnable_filters: bool = True)
Bases: MixingLayer
Base class for frequency-domain filtering operations.
Filter mixing layers apply learnable filters in the frequency domain, enabling selective emphasis or suppression of frequency components for improved sequence modeling capabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_dim
|
int
|
Hidden dimension of the model. |
required |
sequence_length
|
int
|
Expected sequence length for filter initialization. |
required |
dropout
|
float
|
Dropout probability for regularization. |
0.0
|
norm_eps
|
float
|
Epsilon for numerical stability. |
1e-5
|
learnable_filters
|
bool
|
Whether filters are learnable parameters. |
True
|
Attributes:
| Name | Type | Description |
|---|---|---|
sequence_length |
int
|
Expected sequence length. |
learnable_filters |
bool
|
Whether filters are learnable. |
Methods:
| Name | Description |
|---|---|
get_spectral_properties |
Get properties specific to filtering operations. |
get_filter_response |
Get the frequency response of the current filters. |
analyze_frequency_response |
Analyze the frequency response characteristics. |
Source code in spectrans/layers/mixing/base.py
Functions¶
get_spectral_properties ¶
Get properties specific to filtering operations.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing filter-specific properties. |
Source code in spectrans/layers/mixing/base.py
get_filter_response
abstractmethod
¶
Get the frequency response of the current filters.
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex-valued frequency response of shape matching the filter parameters. |
Source code in spectrans/layers/mixing/base.py
analyze_frequency_response ¶
Analyze the frequency response characteristics.
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dictionary containing: - 'magnitude': Magnitude response - 'phase': Phase response - 'group_delay': Group delay response - 'passband_energy': Energy in different frequency bands |