Module Orthogonal#
orthogonal
#
| CLASS | DESCRIPTION |
|---|---|
OrthogonalLayer |
Base class for orthogonal neural network layers. |
QRLayer |
QR decomposition-based orthogonal layer. |
SVDLayer |
SVD-based orthogonal layer. |
PCALayer |
PCA-based orthogonal layer. |
RunningQRLayer |
Running QR projection orthogonal layer with weighted decomposition. |
Classes#
OrthogonalLayer(in_features: int, out_features: int | None = None, **kwargs)
#
Bases: Module
Base class for orthogonal neural network layers.
Provides a common interface for layers that update_weights and store orthogonal transformations during training.
| PARAMETER | DESCRIPTION |
|---|---|
in_features
|
Number of input features.
TYPE:
|
out_features
|
Number of output features, if None uses in_features.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
update_weights |
Compute and store orthogonal transformation. |
forward |
Apply orthogonal transformation. |
Source code in spectre/module/orthogonal.py
Functions#
update_weights(X: torch.Tensor, weights: torch.Tensor | None = None) -> None
abstractmethod
#
forward(X: torch.Tensor, weights: torch.Tensor | None = None) -> torch.Tensor
#
Apply orthogonal transformation.
During training, attempts to update_weights and store orthogonal weights using
the specific method implemented in update_weights(). If the input has
sufficient dimensions and no linear algebra errors occur, weights are
updated. Returns input unchanged in all cases.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor of shape (n_samples, in_features).
TYPE:
|
weights
|
Weights tensor of shape (n_samples,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Input tensor orthogonalized if not training. If training, returns X unchanged. |
Source code in spectre/module/orthogonal.py
QRLayer(in_features: int, out_features: int | None = None, **kwargs)
#
Bases: OrthogonalLayer
QR decomposition-based orthogonal layer.
Computes QR decomposition (X=QR) during training and stores the inverse
of R matrix as orthogonal weights. Q matrix provides an orthogonal basis
while R contains the transformation coefficients.
| PARAMETER | DESCRIPTION |
|---|---|
in_features
|
Number of input features.
TYPE:
|
out_features
|
Number of output features, if None uses in_features.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
update_weights |
Compute and store QR-based orthogonal transformation. |
Source code in spectre/module/orthogonal.py
Functions#
update_weights(X: torch.Tensor, weights: torch.Tensor | None = None) -> None
#
Compute and store QR-based orthogonal transformation.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor of shape (n_samples, in_features).
TYPE:
|
weights
|
Weights tensor of shape (n_samples,).
TYPE:
|
Source code in spectre/module/orthogonal.py
SVDLayer(in_features: int, out_features: int | None = None, **kwargs)
#
Bases: OrthogonalLayer
SVD-based orthogonal layer.
Computes singular value decomposition during training and stores the right singular vectors as orthogonal weights. Provides optimal low-rank approximation through principal components.
| PARAMETER | DESCRIPTION |
|---|---|
in_features
|
Number of input features.
TYPE:
|
out_features
|
Number of output features, if None uses in_features.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
update_weights |
Compute and store SVD-based orthogonal transformation. |
Source code in spectre/module/orthogonal.py
Functions#
update_weights(X: torch.Tensor, weights: torch.Tensor | None = None) -> None
#
Compute and store SVD-based orthogonal transformation.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights.
TYPE:
|
Source code in spectre/module/orthogonal.py
PCALayer(in_features: int, out_features: int | None = None, standardize: bool = True, **kwargs)
#
Bases: OrthogonalLayer
PCA-based orthogonal layer.
Computes principal component analysis during training and stores the principal components as orthogonal weights. Provides dimensionality reduction through variance maximization with optional data standardization.
| PARAMETER | DESCRIPTION |
|---|---|
in_features
|
Number of input features.
TYPE:
|
out_features
|
Number of output features, if None uses in_features.
TYPE:
|
standardize
|
Whether to standardize input data before PCA.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
update_weights |
Compute and store PCA-based orthogonal transformation. |
Source code in spectre/module/orthogonal.py
Functions#
update_weights(X: torch.Tensor, weights: torch.Tensor | None = None) -> None
#
Compute and store PCA-based orthogonal transformation.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor of shape (n_samples, in_features).
TYPE:
|
weights
|
Weights tensor of shape (n_samples,).
TYPE:
|
Source code in spectre/module/orthogonal.py
RunningQRLayer(in_features: int, eps: float = torch.finfo(torch.float32).eps, **kwargs)
#
Bases: OrthogonalLayer
Running QR projection orthogonal layer with weighted decomposition.
Maintains a running average of \(R^{-1}\) across training batches and supports both QR decomposition mode and projection mode. During training with QR mode, update_weightss weighted QR decomposition and returns orthogonal Q matrix. In projection mode, applies the running average transformation.
| PARAMETER | DESCRIPTION |
|---|---|
in_features
|
Number of input features.
TYPE:
|
eps
|
Small value to avoid division by zero.
TYPE:
|
Notes
The weighted QR decomposition produces a Q matrix that is orthogonal with
respect to the weighted inner product, not the standard Euclidean inner
product. For data with sample weights w_i, Q satisfies:
\(Q^\top \mathrm{diag}(w) Q = I\)
but NOT necessarily \(Q^\top Q = I\) in the unweighted case.
The running average of \(R^{-1}\) across batches is a heuristic approximation that does not have strict mathematical guarantees for orthogonalization.
| METHOD | DESCRIPTION |
|---|---|
update_weights |
Compute weighted QR decomposition and update running average. |
Source code in spectre/module/orthogonal.py
Functions#
update_weights(X: torch.Tensor, weights: torch.Tensor | None = None) -> None
#
Compute weighted QR decomposition and update running average.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor of shape (n_samples, in_features).
TYPE:
|
weights
|
Sample weights of shape (n_samples, ).
TYPE:
|