Hadamard Transforms¶
spectrans.transforms.hadamard ¶
Hadamard transform implementations for spectral neural networks.
This module provides fast implementations of the Walsh-Hadamard Transform and related orthogonal transforms that use only +1 and -1 basis functions. These transforms provide alternatives to Fourier transforms, requiring only addition and subtraction operations without multiplications.
Hadamard transforms provide simplicity, orthogonality, and fast mixing operations for spectral neural networks.
Classes:
| Name | Description |
|---|---|
HadamardTransform |
Fast Walsh-Hadamard Transform for efficient orthogonal mixing. |
HadamardTransform2D |
2D Hadamard Transform for image-like data processing. |
SequencyHadamardTransform |
Sequency-ordered Hadamard Transform with frequency-like interpretation. |
SlantTransform |
Slant transform with sawtooth basis functions. |
Examples:
Basic Hadamard Transform:
>>> import torch
>>> from spectrans.transforms.hadamard import HadamardTransform
>>> hadamard = HadamardTransform(normalized=True)
>>> # Input size must be power of 2
>>> signal = torch.randn(32, 512) # 512 = 2^9
>>> transformed = hadamard.transform(signal, dim=-1)
>>> reconstructed = hadamard.inverse_transform(transformed, dim=-1)
2D Hadamard for image processing:
>>> from spectrans.transforms.hadamard import HadamardTransform2D
>>> hadamard2d = HadamardTransform2D(normalized=True)
>>> # Both dimensions must be powers of 2
>>> image = torch.randn(32, 64, 64) # 64 = 2^6
>>> transformed_image = hadamard2d.transform(image, dim=(-2, -1))
Sequency-ordered transform:
>>> from spectrans.transforms.hadamard import SequencyHadamardTransform
>>> seq_hadamard = SequencyHadamardTransform(normalized=True)
>>> seq_coeffs = seq_hadamard.transform(signal, dim=-1)
>>> # Coefficients ordered by sequency (analog of frequency)
Slant transform for edge detection:
>>> from spectrans.transforms.hadamard import SlantTransform
>>> slant = SlantTransform(normalized=True)
>>> slant_coeffs = slant.transform(signal, dim=-1)
Notes
Mathematical Properties:
Walsh-Hadamard Transform:
The Hadamard matrix \(\mathbf{H}_n\) for size \(n=2^k\) is defined recursively:
Orthogonality:
- \(\mathbf{H}_n \cdot \mathbf{H}_n^T = n \cdot \mathbf{I}\) (unnormalized)
- \(\mathbf{H}_n \cdot \mathbf{H}_n^T = \mathbf{I}\) (normalized by \(\frac{1}{\sqrt{n}}\))
- Perfect reconstruction: \(\mathbf{H}^{-1} = \mathbf{H}^T / n\)
Computational Advantages:
- Only requires +1 and -1 multiplications
- Fast \(O(n \log n)\) algorithm similar to FFT
- Memory efficient: can be computed in-place
- Highly parallel: suitable for vector operations
Sequency Ordering: Standard Hadamard ordering is not frequency-like. Sequency ordering rearranges coefficients by their "sequency" (number of sign changes), providing a more intuitive frequency-domain interpretation.
Applications in Spectral Transformers:
-
Efficient Mixing: Hadamard transforms provide orthogonal mixing with minimal computational cost (only additions/subtractions)
-
Binary Neural Networks: Natural fit for binary/quantized networks due to {+1, -1} basis functions
-
Compressive Sensing: Hadamard matrices provide good measurement matrices for sparse signal recovery
-
Pattern Recognition: Walsh functions capture different pattern frequencies useful for classification tasks
Implementation Details:
- Fast Algorithm: Uses recursive butterfly structure similar to FFT
- Power-of-2 Constraint: Input size must be \(2^k\) for fast algorithm
- Bit-Reversal: Efficient implementation uses bit-reversed indexing
- Normalization: Supports both normalized and unnormalized variants
- Gradient Support: Full autodiff compatibility for neural networks
Performance Characteristics:
- Time Complexity: \(O(n \log n)\) for fast algorithm
- Space Complexity: \(O(1)\) additional memory (algorithm supports in-place computation)
- Operations: Only additions and subtractions (no multiplications)
- Memory Bandwidth: Regular access patterns
Limitations:
- Input size must be power of 2 for fast algorithm
- Less frequency selectivity compared to Fourier transforms
- Binary nature may not suit all signal types
References
Jacques Hadamard. 1893. Résolution d'une question relative aux déterminants. Bulletin des Sciences Mathématiques, 17:240-246.
Joseph L. Walsh. 1923. A closed set of normal orthogonal functions. American Journal of Mathematics, 45(1):5-24.
K. R. Rao and P. Yip. 1990. Discrete Cosine Transform: Algorithms, Advantages, Applications. Academic Press, Boston.
See Also
spectrans.transforms.base : Base classes for orthogonal transforms spectrans.transforms.fourier : Fourier transforms for comparison spectrans.layers.mixing : Neural layers using Hadamard transforms
Classes¶
HadamardTransform ¶
Bases: OrthogonalTransform
Fast Walsh-Hadamard Transform.
The Hadamard transform is an orthogonal transform using only +1 and -1 values. The transform size must be a power of 2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
Whether to normalize by 1/sqrt(n) for orthogonality. |
True
|
Methods:
| Name | Description |
|---|---|
transform |
Apply Fast Walsh-Hadamard Transform. |
inverse_transform |
Apply inverse Hadamard transform. |
Source code in spectrans/transforms/hadamard.py
Functions¶
transform ¶
Apply Fast Walsh-Hadamard Transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Size along dim must be power of 2. |
required |
dim
|
int
|
Dimension along which to apply transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Hadamard transformed tensor. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If size along dim is not a power of 2. |
Source code in spectrans/transforms/hadamard.py
inverse_transform ¶
Apply inverse Hadamard transform.
The Hadamard transform is self-inverse (up to normalization).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Hadamard coefficients. |
required |
dim
|
int
|
Dimension along which to apply inverse transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Inverse transformed tensor. |
Source code in spectrans/transforms/hadamard.py
HadamardTransform2D ¶
Bases: SpectralTransform2D
2D Fast Walsh-Hadamard Transform.
Applies Hadamard transform along two dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
Whether to normalize for orthogonality. |
True
|
Methods:
| Name | Description |
|---|---|
transform |
Apply 2D Hadamard transform. |
inverse_transform |
Apply inverse 2D Hadamard transform. |
Source code in spectrans/transforms/hadamard.py
Functions¶
transform ¶
Apply 2D Hadamard transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Sizes along both dims must be powers of 2. |
required |
dim
|
tuple[int, int]
|
Dimensions along which to apply transform. |
(-2, -1)
|
Returns:
| Type | Description |
|---|---|
Tensor
|
2D Hadamard transformed tensor. |
Source code in spectrans/transforms/hadamard.py
inverse_transform ¶
Apply inverse 2D Hadamard transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Hadamard coefficients. |
required |
dim
|
tuple[int, int]
|
Dimensions along which to apply inverse transform. |
(-2, -1)
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Inverse transformed tensor. |
Source code in spectrans/transforms/hadamard.py
SequencyHadamardTransform ¶
Bases: OrthogonalTransform
Sequency-ordered Hadamard Transform.
The sequency ordering arranges basis functions by number of zero-crossings, similar to frequency ordering in Fourier transforms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
Whether to normalize for orthogonality. |
True
|
Methods:
| Name | Description |
|---|---|
transform |
Apply sequency-ordered Hadamard transform. |
inverse_transform |
Apply inverse sequency-ordered Hadamard transform. |
Source code in spectrans/transforms/hadamard.py
Functions¶
transform ¶
Apply sequency-ordered Hadamard transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Size along dim must be power of 2. |
required |
dim
|
int
|
Dimension along which to apply transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Sequency-ordered Hadamard coefficients. |
Source code in spectrans/transforms/hadamard.py
inverse_transform ¶
Apply inverse sequency-ordered Hadamard transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Sequency-ordered Hadamard coefficients. |
required |
dim
|
int
|
Dimension along which to apply inverse transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Inverse transformed tensor. |
Source code in spectrans/transforms/hadamard.py
SlantTransform ¶
Bases: OrthogonalTransform
Slant Transform.
The Slant transform is similar to Hadamard but with varying basis function slopes, providing better energy compaction for certain signals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
Whether to normalize for orthogonality. |
True
|
Methods:
| Name | Description |
|---|---|
transform |
Apply Slant transform. |
inverse_transform |
Apply inverse Slant transform. |
Source code in spectrans/transforms/hadamard.py
Functions¶
transform ¶
Apply Slant transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Size along dim should be power of 2. |
required |
dim
|
int
|
Dimension along which to apply transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Slant transformed tensor. |
Source code in spectrans/transforms/hadamard.py
inverse_transform ¶
Apply inverse Slant transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Slant coefficients. |
required |
dim
|
int
|
Dimension along which to apply inverse transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Inverse transformed tensor. |