API Reference#
SPD Learn: Deep Learning on Riemannian Manifolds.
A pure PyTorch library for Symmetric Positive Definite (SPD) matrix learning.
SPD Learn follows a functional-first design philosophy: low-level operations
are implemented as pure functions in spd_learn.functional, which are
then wrapped into stateful layers in spd_learn.modules, and finally
composed into complete architectures in spd_learn.models.
Functional#
Functional API for SPD matrix operations.
This module provides differentiable operations for Symmetric Positive Definite (SPD) matrices, organized into:
Core operations: Matrix logarithm, exponential, power, square root
Metrics: Riemannian metrics (AIRM, Log-Euclidean, Bures-Wasserstein, Log-Cholesky)
Transport: Parallel transport operations
Covariance: Covariance estimation functions
Regularization: Covariance regularization utilities
Vectorization: Batch vectorization and (un)vectorization helpers
Numerical: Numerical stability configuration
This module provides low-level functions for operations on tensors, particularly
those representing SPD matrices or elements in related manifolds. These functions
form the core computational backend for the layers in spd_learn.modules
and models in spd_learn.models.
Core Matrix Operations#
Basic matrix operations commonly used in Riemannian geometry, such as matrix logarithm, exponential, power functions, and utilities for ensuring symmetry or clamping eigenvalues.
|
Matrix logarithm of a symmetric matrix. |
|
Matrix exponential of a symmetric matrix. |
|
Matrix (scaled) SoftPlus of a symmetric matrix. |
|
Matrix inverse (scaled) SoftPlus of a symmetric matrix. |
|
Scaled SoftPlus function. |
|
Inverse of the scaled SoftPlus function |
|
Computes the matrix power. |
|
Matrix square root. |
|
Inverse matrix square root. |
|
Matrix square root and inverse matrix square root. |
|
Rectification of the eigenvalues of a symmetric matrix. |
|
Absolute value of the eigenvalues of a symmetric matrix. |
|
Ensures that a matrix is symmetric. |
Compute the orthogonal polar factor of a matrix. |
Covariance Estimation#
Functions for computing various types of covariance matrices from input data.
|
Computes the covariance matrix of multivariate data. |
|
Computes the sample covariance matrix of multivariate data. |
Computes the real-valued covariance matrix of time series data. |
|
Computes the real-valued cross-frequency covariance matrix. |
Regularization#
Functional regularization utilities for covariance matrices.
|
Performs trace normalization on a batch of covariance matrices. |
|
Applies Ledoit-Wolf shrinkage to a batch of covariance matrices. |
|
Apply shrinkage regularization to covariance matrices. |
Riemannian Metrics#
SPD Learn implements four Riemannian metrics for SPD manifolds. Each metric provides distance computation, geodesic interpolation, and exponential/logarithmic maps. Choose based on your application’s needs:
Metric |
Properties |
Best For |
|---|---|---|
AIRM |
Affine-invariant, curvature-aware |
Theoretical correctness, domain adaptation |
Log-Euclidean |
Bi-invariant, closed-form mean |
Fast computation, deep learning |
Log-Cholesky |
Avoids eigendecomposition |
Numerical stability, large matrices |
Bures-Wasserstein |
Optimal transport connection |
Covariance interpolation, statistics |
AIRM (Affine-Invariant Riemannian Metric)#
The AIRM is the canonical Riemannian metric on SPD manifolds with affine invariance properties. It provides geodesic distances and interpolations that are invariant under congruence transformations.
|
Compute the geodesic distance under the Affine-Invariant Riemannian Metric (AIRM). |
|
Geodesic interpolation on the SPD manifold under the Affine-Invariant Riemannian Metric (AIRM). |
|
Riemannian exponential map under the Affine-Invariant metric. |
|
Riemannian logarithmic map under the Affine-Invariant metric. |
Log-Euclidean Metric#
The Log-Euclidean Metric (LEM) maps SPD matrices to a flat (Euclidean) space via matrix logarithm, enabling efficient closed-form computations.
|
Computes the Log-Euclidean distance between SPD matrices. |
|
Geodesic interpolation under the Log-Euclidean metric. |
|
Computes the weighted Log-Euclidean mean of a batch of SPD matrices. |
|
Logarithmic multiplication of SPD matrices under the Log-Euclidean metric. |
|
Logarithmic scalar multiplication of an SPD matrix. |
|
Riemannian exponential map under the Log-Euclidean metric. |
|
Riemannian logarithmic map under the Log-Euclidean metric. |
Log-Cholesky Metric#
The Log-Cholesky metric uses the Cholesky decomposition to parameterize SPD matrices, avoiding expensive eigendecompositions while maintaining numerical stability.
|
Matrix logarithm via Cholesky decomposition (Log-Cholesky map). |
|
Inverse of the Log-Cholesky map (Cholesky exponential). |
|
Compute the distance in the Log-Cholesky metric. |
|
Compute the weighted mean in the Log-Cholesky space. |
|
Geodesic interpolation in the Log-Cholesky metric. |
Bures-Wasserstein Metric#
The Bures-Wasserstein (or Procrustes) metric has connections to optimal transport theory and is particularly useful for covariance interpolation.
Compute the Bures-Wasserstein distance between SPD matrices. |
|
|
Compute the geodesic interpolation under the Bures-Wasserstein metric. |
|
Compute the Bures-Wasserstein barycenter of SPD matrices. |
|
Compute the optimal transport map from A to B applied to X. |
Parallel Transport#
Functions for parallel transport of tangent vectors along geodesics on the SPD manifold, essential for operations like domain adaptation.
|
Parallel transport of tangent vector under the Affine-Invariant metric. |
|
Parallel transport of tangent vector under the Log-Euclidean metric. |
|
Parallel transport of tangent vector under the Log-Cholesky metric. |
|
Parallel transport via Schild's ladder approximation. |
|
Parallel transport via pole ladder approximation. |
|
Parallel transport of tangent vector with metric selection. |
Vectorization Utilities#
Vectorization helpers for batching, (un)vectorizing matrices, and symmetric matrix encodings.
|
Vectorizes a batch of tensors along the last two dimensions. |
|
Unvectorizes a batch of tensors along the last dimension. |
|
Vectorizes symmetric matrices by extracting triangular elements. |
|
Reconstructs symmetric matrices from vectorization. |
Dropout#
Functional implementation of dropout specifically designed for SPD tensors.
|
Applies dropout to a batch of SPD matrices. |
Autograd Helpers#
Custom forward and backward functions for operations like matrix eigen-decomposition, enabling gradient computation through these potentially complex steps.
|
Backward pass for the modified eigenvalue of a symmetric matrix. |
|
Forward pass for the modified eigenvalue of a symmetric matrix. |
Batch Normalization Operations#
Functions for Riemannian batch normalization computations on SPD manifolds.
|
Perform one iteration of the Karcher mean algorithm. |
|
Center SPD matrices around a mean via congruence transformation. |
|
Apply learnable rebiasing to centered SPD matrices. |
|
Compute scalar dispersion in the tangent space. |
Bilinear Operations#
Bilinear transformations that preserve SPD properties.
|
Apply bilinear transformation to SPD matrices. |
|
Increase the dimension of SPD matrices via embedding. |
Wavelet Operations#
Time-frequency analysis using Gabor wavelets.
|
Compute a complex Gabor (Morlet) wavelet filterbank. |
Numerical Stability#
Utilities for ensuring numerical stability when working with SPD matrices, including epsilon handling and eigenvalue clamping. See Numerical Stability for detailed guidance.
Global configuration for numerical stability thresholds. |
|
|
Global configuration for numerical stability thresholds. |
|
Context manager for temporarily modifying numerical configuration. |
|
Get a dtype-aware epsilon value for numerical stability. |
|
Get a dtype-aware epsilon value as a tensor. |
|
Get threshold for detecting equal eigenvalues in Loewner matrix. |
|
Safely clamp eigenvalues with dtype-aware threshold. |
|
Check if eigenvalues satisfy SPD requirements. |
|
Check if dtype is half precision (float16 or bfloat16). |
|
Recommend a dtype based on expected matrix condition number. |
Modules#
This module provides neural network layers specifically designed for deep learning
on Riemannian manifolds, particularly SPD matrices. These layers wrap the
functional operations from spd_learn.functional into stateful
torch.nn.Module components.
Covariance Layer#
Modules for computing covariance matrices, often used as the first step in SPD-based pipelines.
|
Covariance Estimation Layer for Neuroimaging Data. |
Matrix Eigen-Operations#
Modules performing operations based on matrix eigenvalue decomposition (LogEig, ReEig, ExpEig), essential for mapping between the SPD manifold and Euclidean/tangent spaces or applying non-linearities.
Manifold Parametrization#
Modules for parametrizing learnable SPD matrices, ensuring parameters remain on the SPD manifold during optimization. Supports both matrix exponential and softplus mappings from symmetric matrices to SPD matrices.
|
Symmetric Positive Definite Manifold parametrization. |
|
Positive definite scalars parametrization. |
Bilinear Mappings#
Layers implementing learnable bilinear transformations suitable for SPD matrices, acting as analogous operations to linear layers in Euclidean space.
|
Bilinear Mapping Layer for SPD Matrices. |
|
Bilinear Mapping Layer for SPD Matrix Dimensionality Expansion. |
Batch Normalization#
Batch normalization layers specifically adapted for data on the SPD manifold or related representations.
|
Riemannian Batch Normalization for SPD Matrices (Mean-only). |
|
SPD Batch Normalization (Mean and Variance). |
|
Batch Re-Normalization. |
Regularization#
Modules implementing regularization covariance methods, such as Ledoit-Wolf shrinkage for covariance estimation or trace normalization.
Dropout#
Dropout mechanisms designed for SPD matrix inputs or features derived from them.
|
Structured Dropout for SPD Matrices. |
Residual Connections#
Modules for residual/skip connections on Riemannian manifolds, enabling deeper SPD networks with improved gradient flow.
|
Residual/skip connection for SPD networks using the Log-Euclidean metric. |
Signal Processing#
Layers for signal processing operations, including learnable wavelet convolutions for time-frequency feature extraction.
|
Parametrized Complex Gabor Wavelet Convolution Layer. |
Utilities#
Utility layers for preprocessing, feature extraction, or other auxiliary tasks within Riemannian deep learning models.
|
Patch Embedding Layer. |
|
Vectorization Layer. |
|
Vectorize Triangular Part Layer. |
Models#
This module offers pre-built models for working with SPD matrices, using the
building blocks from spd_learn.modules.
|
EE(G) SPDNet. |
|
Gabor Riemann EEGNet. |
|
Manifold Attention Network for EEG Decoding (MAtt). |
|
Phase SPDNet. |
|
Symmetric Positive Definite Neural Network (SPDNet). |
|
Tensor-CSPNet. |
|
Tangent Space Mapping Network (TSMNet). |
Model Selection Guide#
Use this table to choose the right model for your application:
Model |
Best For |
Input Type |
Key Feature |
Complexity |
|---|---|---|---|---|
General SPD learning |
Covariance matrices |
Foundational architecture |
Low |
|
Multi-band EEG |
Filter bank data |
Temporal-spectral-spatial |
Medium |
|
Domain adaptation, Interpretation |
Raw EEG |
SPDBatchNorm |
Medium |
|
Channel-specific EEG |
Raw EEG |
Grouped convolution |
Medium |
|
Attention-based |
Raw EEG |
Manifold attention |
High |
|
Interpretable features |
Raw EEG |
Learnable wavelets |
Medium |
|
Nonlinear dynamics |
Raw EEG |
Phase-space embedding |
Low |
Decision Flowchart#
START
│
▼
┌─────────────────────────────────┐
│ What is your input data type? │
└─────────────────────────────────┘
│
├─── Pre-computed covariance matrices ──► SPDNet
│
├─── Filter bank EEG (multiple bands) ──► TensorCSPNet
│
└─── Raw time series
│
▼
┌─────────────────────────────────┐
│ Do you need domain adaptation? │
└─────────────────────────────────┘
│
├─── Yes ──► TSMNet (with domain adaptation)
│
└─── No
│
▼
┌─────────────────────────────────┐
│ What is your priority? │
└─────────────────────────────────┘
│
├─── Interpretability ──► TSMNet, Green
│
├─── Attention mechanism ──► MAtt
│
├─── Conv feature extraction ──► TSMNet, EEGSPDNet
│
└─── Nonlinear dynamics ──► PhaseSPDNet
Model Architectures#
SPDNet - Foundational architecture for SPD learning:
[CovLayer] → BiMap → ReEig → LogEig → Linear
TensorCSPNet - Multi-frequency EEG with temporal-spectral-spatial features:
Tensor Stacking → BiMap blocks → SPDBatchNormMean → LogEig → TCN → Linear
TSMNet - Domain adaptation with SPD batch normalization:
Conv2d → CovLayer → BiMap → ReEig → SPDBatchNormMeanVar → LogEig → Linear
EEGSPDNet - Channel-specific temporal filtering:
GroupedConv1d → CovLayer → BiMap/SPDDropout/ReEig blocks → LogEig → Linear
MAtt - Attention-based temporal weighting:
Conv2d → PatchCov → AttentionManifold → ReEig → LogEig → Linear
Green - Interpretable wavelet features:
WaveletConv → CovLayer → Shrinkage → BiMap → LogEig → BatchReNorm → MLP
PhaseSPDNet - Phase-space embedding for nonlinear dynamics:
PhaseDelay → SPDNet
Performance Comparison#
Based on motor imagery classification benchmarks (BNCI2014-001):
Model |
Accuracy (%) |
Parameters |
Training Time |
GPU Memory |
|---|---|---|---|---|
SPDNet |
70-75 |
~10K |
Fast |
Low |
TensorCSPNet |
75-82 |
~50K |
Medium |
Medium |
TSMNet |
72-78 |
~30K |
Medium |
Medium |
EEGSPDNet |
73-79 |
~40K |
Medium |
Medium |
MAtt |
74-80 |
~60K |
Slow |
High |
Green |
72-78 |
~20K |
Medium |
Low |
Note: Performance varies significantly across subjects and datasets.
To reproduce these results and run your own benchmarks, please refer to the Benchmarking SPD Learn Models with MOABB and Hydra example.
Initialization#
Initialization functions for SPD-aware neural networks.
This module provides functions to initialize tensors with SPD-specific methods, following PyTorch’s torch.nn.init pattern.
All functions operate in-place and return the modified tensor for convenience.
Functions#
stiefel_Initialize tensor on the Stiefel manifold (orthonormal columns).
spd_identity_Initialize tensor as identity matrix.
Examples
>>> import torch
>>> from spd_learn import init as spd_init
>>> W = torch.empty(8, 4)
>>> spd_init.stiefel_(W, seed=42)
>>> # W is now on the Stiefel manifold: W^T @ W = I
>>> torch.allclose(W.T @ W, torch.eye(4), atol=1e-5)
True
This module provides functions to initialize tensors with SPD-specific methods,
following PyTorch’s torch.nn.init pattern. All functions operate in-place
and return the modified tensor for convenience.
|
Initialize tensor on the Stiefel manifold (in-place). |
|
Initialize tensor as identity matrix (in-place). |