Transform Projection#
projection
#
| CLASS | DESCRIPTION |
|---|---|
ProjectionTransformer |
Linear projection using a pre-computed projection matrix. |
Classes#
ProjectionTransformer(projection_matrix: torch.Tensor, center: torch.Tensor | None = None)
#
Bases: Transformer
Linear projection using a pre-computed projection matrix.
Applies matrix multiplication X @ P where P is the projection matrix.
Useful for applying PCA projections, random projections, or other linear
transformations with fixed projection matrices.
| PARAMETER | DESCRIPTION |
|---|---|
projection_matrix
|
Projection matrix of shape (in_features, out_features).
TYPE:
|
center
|
Optional centering vector of shape (in_features,). If provided,
data is centered before projection:
TYPE:
|
| ATTRIBUTE | DESCRIPTION |
|---|---|
projection_matrix |
The projection matrix (registered as buffer).
TYPE:
|
center |
The centering vector if provided (registered as buffer).
TYPE:
|
in_features |
Number of input features.
TYPE:
|
out_features |
Number of output features after projection.
TYPE:
|
Examples:
Basic projection without centering
>>> import torch
>>> from spectre.transform import ProjectionTransformer
>>> P = torch.randn(10, 3)
>>> projector = ProjectionTransformer(P)
>>> X = torch.randn(100, 10)
>>> X_proj = projector.transform(X)
>>> X_proj.shape
torch.Size([100, 3])
Projection with centering (like PCA)
>>> mean = torch.randn(10)
>>> projector = ProjectionTransformer(P, center=mean)
>>> X_proj = projector.transform(X)
>>> X_proj.shape
torch.Size([100, 3])
Inverse projection (approximate reconstruction)
>>> X_reconstructed = projector.inverse_transform(X_proj)
>>> X_reconstructed.shape
torch.Size([100, 10])
| METHOD | DESCRIPTION |
|---|---|
transform |
Project input data using the projection matrix. |
inverse_transform |
Approximate inverse projection back to original space. |
Source code in spectre/transform/projection.py
Attributes#
in_features: int
property
#
Number of input features.
out_features: int
property
#
Number of output features.
Functions#
transform(X: torch.Tensor) -> torch.Tensor
#
Project input data using the projection matrix.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data of shape (n_samples, in_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Projected data of shape (n_samples, out_features). |
Source code in spectre/transform/projection.py
inverse_transform(X: torch.Tensor) -> torch.Tensor
#
Approximate inverse projection back to original space.
Uses the transpose of the projection matrix for reconstruction. This is exact for orthogonal projections and approximate otherwise.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Projected data of shape (n_samples, out_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reconstructed data of shape (n_samples, in_features). |
Notes
For orthogonal projection matrices (like PCA components), this provides exact reconstruction if all components are retained. Otherwise, it provides an approximate reconstruction.