Decomposition Nystrom#
nystrom
#
| FUNCTION | DESCRIPTION |
|---|---|
nystrom_extension |
Compute Nyström extension for out-of-sample predictions. |
Classes#
Functions#
nystrom_extension(X: torch.Tensor, X_train: torch.Tensor, weights: torch.Tensor | None = None, weights_train: torch.Tensor | None = None, *, eigenvectors: torch.Tensor, eigenvalues: torch.Tensor, kernel_fn: Kernel, distance_fn: PairwiseDistance, out_features: int | None = None, scale_by_eigenvalues: bool = True) -> torch.Tensor
#
Compute Nyström extension for out-of-sample predictions.
Projects new data points onto the embedding learned from training data using the Nyström approximation method. This enables out-of-sample prediction for kernel-based dimensionality reduction methods.
The function computes the kernel matrix between test (:attr:X) and training
samples (:attr:X_train) to obtain :math:K_{\text{test,train}}, then projects
onto the training eigenvectors. For kernels with normalization, the normalization
effects from training are already captured in the eigenvectors, so no additional
normalization is applied to the non-square K_test_train matrix.
Mathematical formulation:
.. math::
\Phi_{\text{test}} = K_{\text{test,train}} \Psi_{\text{train}}
where :math:K_{\text{test,train}} \in \mathbb{R}^{n_{\text{test}}
\times n_{\text{train}}} is the kernel matrix between test and training
samples, and :math:\Psi_{\text{train}} \in \mathbb{R}^{n_{\text{train}}
\times d} are the eigenvectors from training.
For diffusion maps (when scale_by_eigenvalues=True), the embedding is scaled:
.. math::
\Phi_{\text{test}} = K_{\text{test,train}} \Psi_{\text{train}} \Lambda
where :math:\Lambda = \text{diag}(\lambda_1, \ldots, \lambda_d) contains
the eigenvalues from training.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
New data points of shape (n_samples, in_features).
TYPE:
|
X_train
|
Training data points of shape (n_train_samples, in_features).
TYPE:
|
weights
|
New sample weights of shape (n_samples, ). Currently not used because kernel normalization cannot be applied to non-square matrices. Reserved for future extensions.
TYPE:
|
weights_train
|
Training sample weights of shape (n_train_samples, ). Currently not used because kernel normalization cannot be applied to non-square matrices. The normalization effects from training are already captured in the eigenvectors. Reserved for future extensions.
TYPE:
|
eigenvectors
|
Eigenvectors from training of shape (n_train_samples, out_features).
TYPE:
|
eigenvalues
|
Eigenvalues from training of shape (out_features, ).
TYPE:
|
kernel_fn
|
Kernel function used during training. Must be a Kernel instance.
TYPE:
|
distance_fn
|
Pairwise distance function. Must be a PairwiseDistance instance.
TYPE:
|
out_features
|
Number of components to return. If None, returns all components.
TYPE:
|
scale_by_eigenvalues
|
Whether to scale projection by eigenvalues (for diffusion maps). Set to False for methods like Laplacian eigenmaps.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Embedded coordinates of shape (n_samples, out_features). |
Examples:
>>> import torch
>>> from spectre.decomposition.nystrom import nystrom_extension
>>> from spectre.kernel import GaussianKernel
>>> from spectre.pairwise_distance import PairwiseDistanceEuclidean
>>> X_train = torch.randn(100, 5)
>>> X = torch.randn(20, 5)
>>> # Assume eigenvectors and eigenvalues from training
>>> kernel_fn = GaussianKernel(bw_method=0.5)
>>> distance_fn = PairwiseDistanceEuclidean()
>>> coords = nystrom_extension(
... X,
... X_train,
... eigenvectors,
... eigenvalues,
... kernel_fn,
... distance_fn,
... out_features=3,
... )
>>> coords.shape
torch.Size([20, 3])
Source code in spectre/decomposition/nystrom.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |