Parametric Spectral Autoencoder#
spectral_autoencoder
#
| CLASS | DESCRIPTION |
|---|---|
SpectralAutoencoder |
Spectral autoencoder combining eigenvalue optimization with reconstruction. |
Classes#
SpectralAutoencoder(*, model: dict[str, ModelType], kernel_fn: Kernel | str = 'gaussian', kernel_kwargs: dict | None = None, distance_fn: PairwiseDistance | str = 'euclidean', distance_kwargs: dict | None = None, loss_fn: Literal['eigenvalue', 'adaptive_eigenvalue'] = 'eigenvalue', loss_kwargs: dict | None = None, loss_weights: dict[str, float] | None = None, additional_loss: list | dict | None = None, additional_loss_weights: list[float] | dict[str, float] | None = None, optimizer_fn: type[torch.optim.Optimizer] | str | None = None, optimizer_kwargs: dict | None = None, lr_scheduler_fn: Any | str | None = None, lr_scheduler_kwargs: dict | None = None, n_eigen: int = 10, symmetric_eigendecomposition: bool = True, device: Literal['cuda', 'cpu'] = 'cuda', store_loss_context: bool = False)
#
Bases: SpectralMap
Spectral autoencoder combining eigenvalue optimization with reconstruction.
Learns low-dimensional representations by jointly optimizing spectral properties of kernel matrices in the latent space and reconstruction quality. Inherits from SpectralMap and extends it with a decoder network for reconstruction, combining spectral gap maximization with the reconstruction objective of Autoencoder.
The model consists of an encoder network that maps input data to a latent space and a decoder network that reconstructs the original input. The total loss is a combination of spectral loss and reconstruction loss \(L_{\lambda} + L_{\mathrm{MSE}}\), where \(L_{\lambda}\) optimizes eigenvalue properties (e.g., spectral gap) and \(L_{\mathrm{MSE}}\) measures reconstruction error.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Dictionary containing 'encoder' and 'decoder' model specifications. Both networks are required for spectral autoencoder functionality:
Example:
TYPE:
|
kernel_fn
|
Kernel function specification for latent space.
Available string options:
TYPE:
|
kernel_kwargs
|
Keyword arguments for kernel instantiation when See kernels for available options.
TYPE:
|
distance_fn
|
Distance metric specification for latent space.
Available string options:
TYPE:
|
distance_kwargs
|
Keyword arguments for distance instantiation when
TYPE:
|
loss_fn
|
Loss function specification. Only EigenvalueLoss (
TYPE:
|
loss_kwargs
|
Additional keyword arguments for the spectral loss function.
TYPE:
|
loss_weights
|
Weights for built-in loss components. Keys must match names in
For
Example:
TYPE:
|
additional_loss
|
Additional loss functions to combine with spectral and reconstruction losses.
The total loss becomes a weighted sum of spectral, MSE, and additional losses. Useful for adding regularization (e.g., OrthogonalLoss, DecorrelationLoss).
TYPE:
|
additional_loss_weights
|
Weights for additional loss functions.
TYPE:
|
optimizer_fn
|
Optimizer specification for training.
TYPE:
|
optimizer_kwargs
|
Keyword arguments for optimizer instantiation
(e.g.,
TYPE:
|
lr_scheduler_fn
|
Learning rate scheduler specification.
TYPE:
|
lr_scheduler_kwargs
|
Keyword arguments for scheduler instantiation.
TYPE:
|
n_eigen
|
Number of eigenvalues to compute.
TYPE:
|
symmetric_eigendecomposition
|
Whether to use symmetric eigendecomposition for spectral loss.
TYPE:
|
device
|
Computation device. Falls back to "cpu" if CUDA unavailable.
TYPE:
|
store_loss_context
|
Whether to store the loss context dictionary for metric extraction in callbacks. When enabled, the
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
DEFAULT_LOSS_WEIGHTS |
Default weights for built-in loss components:
TYPE:
|
decoder |
Decoder network on the specified device. Also available as
TYPE:
|
kernel_fn |
Kernel function instance for latent space.
TYPE:
|
distance_fn |
Distance function instance for latent space.
TYPE:
|
loss_fn |
Composite loss function containing spectral, reconstruction, and any additional losses.
TYPE:
|
eigenvalues |
Most recently computed eigenvalues from forward pass, shape
(
TYPE:
|
Examples:
String-based configuration:
>>> import torch
>>> from spectre.parametric import SpectralAutoencoder
>>> from spectre.core import Model
>>>
>>> # Create encoder (input -> latent)
>>> encoder = Model(in_features=784, out_features=64, multipliers=[0.5])
>>> # Create decoder (latent -> output)
>>> decoder = Model(in_features=64, out_features=784, multipliers=[2.0])
>>>
>>> sae = SpectralAutoencoder(
... model={"encoder": encoder, "decoder": decoder},
... kernel_fn="gaussian",
... kernel_kwargs={"bw_method": "median"},
... distance_fn="euclidean",
... loss_fn="eigenvalue",
... loss_kwargs={"n_eigen": 2, "reduce": "gap"},
... device="cpu",
... )
>>>
>>> X = torch.randn(100, 784)
>>> loss = sae.loss(X) # Combined spectral + MSE loss
With custom loss weights:
>>> sae = SpectralAutoencoder(
... model={"encoder": encoder, "decoder": decoder},
... loss_kwargs={"n_eigen": 2, "reduce": "gap"},
... loss_weights={"loss_eigenvalue": 0.5, "loss_reconstruction": 2.0},
... device="cpu",
... )
>>>
>>> # Total 0.5 * spectral + 2.0 * MSE
>>> X = torch.randn(100, 784)
>>> loss = sae.loss(X)
With additional loss functions for regularization:
>>> from spectre.loss import OrthogonalLoss, DecorrelationLoss
>>>
>>> sae = SpectralAutoencoder(
... model={"encoder": encoder, "decoder": decoder},
... kernel_fn="gaussian",
... loss_fn="eigenvalue",
... loss_kwargs={"n_eigen": 2, "reduce": "gap"},
... loss_weights={"loss_eigenvalue": 0.5},
... additional_loss={
... "orthogonal": OrthogonalLoss(),
... "decorrelation": DecorrelationLoss(method="correlation"),
... },
... additional_loss_weights={
... "orthogonal": 0.1,
... "decorrelation": 0.05,
... },
... device="cpu",
... )
>>>
>>> # Total 0.5 * spectral + 1.0 * MSE + 0.1 * orthogonal + 0.05 * decorrelation
>>> X = torch.randn(100, 784)
>>> loss = sae.loss(X) # Composite loss value
| METHOD | DESCRIPTION |
|---|---|
loss |
Compute combined spectral and reconstruction losses. |
Source code in spectre/parametric/spectral_autoencoder.py
Functions#
loss(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Compute combined spectral and reconstruction losses.
Performs encoding and decoding operations, computes kernel matrix eigenvalues in the latent space for spectral loss, and measures reconstruction error between input and decoded output. When additional losses are provided, computes a composite loss combining spectral, MSE, and regularization terms.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data tensor to encode, evaluate spectrally, and reconstruct.
TYPE:
|
weights
|
Optional sample weights for weighted kernel computation and weighted MSE loss.
TYPE:
|
target
|
Not used for loss computation (unsupervised method).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar loss value. If additional losses are provided, returns weighted sum of all loss components (spectral + MSE + additional). |