Parametric Spectral Net#
spectral_net
#
| CLASS | DESCRIPTION |
|---|---|
SpectralNet |
SpectralNet for spectral clustering via deep neural networks. |
Classes#
SpectralNet(*, model: ModelType | 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: str = 'laplacian', 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, device: Literal['cuda', 'cpu'] = 'cuda', store_loss_context: bool = False)
#
Bases: Parametric
SpectralNet for spectral clustering via deep neural networks.
This is a simplified version of SpectraNet. See [1] for the original method.
Learns embeddings that approximate the eigenvectors of the graph Laplacian by optimizing a combination of orthogonality constraints and Rayleigh quotient minimization. This enables learning of spectral clustering without explicit eigendecomposition.
The method trains a neural network to map input data to low-dimensional embeddings \(Y\) such that:
- \(\text{Tr}(Y^\top L Y)\) is minimized (Rayleigh quotient),
- \(Y\) is regularized to be orthogonal,
where \(L\) is the graph Laplacian constructed from the input data.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Neural network architecture for learning spectral embeddings.
TYPE:
|
kernel_fn
|
Kernel function specification for constructing affinity matrix.
TYPE:
|
kernel_kwargs
|
Keyword arguments for kernel instantiation when
TYPE:
|
distance_fn
|
Distance metric specification for computing pairwise distances.
TYPE:
|
distance_kwargs
|
Keyword arguments for distance instantiation when
TYPE:
|
loss_weights
|
Weights for built-in loss components. Keys must match names in
For
Example:
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:
|
device
|
Computation device.
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
DEFAULT_LOSS_WEIGHTS |
Default weights for built-in loss components:
TYPE:
|
kernel_fn |
Kernel function instance.
TYPE:
|
distance_fn |
Distance function instance.
TYPE:
|
loss_fn |
Composite loss function containing spectral (Laplacian) and orthogonality losses.
TYPE:
|
Examples:
String-based configuration:
>>> from spectre.parametric import SpectralNet
>>> from spectre.core import Model
>>> import torch
>>>
>>> model = Model(in_features=10, out_features=3, multipliers=[2.0])
>>>
>>> snet = SpectralNet(
... model=model,
... kernel_fn="gaussian",
... kernel_kwargs={"bw_method": "median"},
... distance_fn="euclidean",
... loss_weights={"loss_eigenvalue": 1.0, "loss_orthogonal": 0.1},
... )
>>>
>>> X = torch.randn(100, 10)
>>> loss = snet.loss(X)
Object-based configuration:
>>> from spectre.kernel import GaussianKernel
>>> from spectre.pairwise_distance import PairwiseDistanceEuclidean
>>>
>>> snet = SpectralNet(
... model=model,
... kernel_fn=GaussianKernel(bw_method="median"),
... distance_fn=PairwiseDistanceEuclidean(),
... )
| METHOD | DESCRIPTION |
|---|---|
forward_step |
Compute forward pass through the network. |
loss |
Compute loss combining orthogonality and Laplacian terms. |
training_step |
PyTorch Lightning training step. |
score_step |
Compute loss for evaluation without training status checks. |
predict_states |
Predict cluster assignments from embeddings. |
Source code in spectre/parametric/spectral_net.py
Functions#
forward_step(X: torch.Tensor) -> torch.Tensor
#
Compute forward pass through the network.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
torch.Tensor of shape (n_samples, n_states)
|
Embedding vectors (approximate eigenvectors). |
Source code in spectre/parametric/spectral_net.py
loss(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]
#
Compute loss combining orthogonality and Laplacian terms.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data.
TYPE:
|
weights
|
Sample weights for weighted loss computation.
TYPE:
|
target
|
Not used (unsupervised method).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor or tuple[Tensor, Tensor]
|
Scalar loss value or tuple of scalar loss values. |
Source code in spectre/parametric/spectral_net.py
training_step(batch: DataBatch, batch_idx: int) -> torch.Tensor
#
PyTorch Lightning training step.
| PARAMETER | DESCRIPTION |
|---|---|
batch
|
Training batch containing data and optional weights.
TYPE:
|
batch_idx
|
Index of current batch within the epoch.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Loss value. |
Source code in spectre/parametric/spectral_net.py
score_step(X: torch.Tensor, weights: torch.Tensor | None = None, target: torch.Tensor | None = None) -> torch.Tensor
#
Compute loss for evaluation without training status checks.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data for evaluation.
TYPE:
|
weights
|
by default None Sample weights.
TYPE:
|
target
|
Not used (unsupervised method).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Scalar loss value. |
Source code in spectre/parametric/spectral_net.py
predict_states(X: torch.Tensor, method: str = 'argmax') -> torch.Tensor
#
Predict cluster assignments from embeddings.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data.
TYPE:
|
method
|
Clustering method to apply to embeddings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
torch.Tensor of shape (n_samples,)
|
Cluster assignments. |