Metrics Reconstruction#
reconstruction
#
| FUNCTION | DESCRIPTION |
|---|---|
reconstruction_error |
Compute reconstruction error between original |
relative_reconstruction_error |
Compute relative reconstruction error normalized by data magnitude. |
procrustes_error |
Compute Procrustes error after optimal orthogonal alignment. |
Functions#
reconstruction_error(X: torch.Tensor, X_reconstructed: torch.Tensor, reduce: str = 'mse', per_sample: bool = False) -> torch.Tensor
#
Compute reconstruction error between original X and reconstructed data
X_reconstructed.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Original data, shape (n_samples, n_features).
TYPE:
|
X_reconstructed
|
Reconstructed data, shape (n_samples, n_features).
TYPE:
|
reduce
|
Reduction operation: "mse", "mae", "frobenius".
TYPE:
|
per_sample
|
If True, return per-sample errors.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Reconstruction error. |
Source code in spectre/metrics/reconstruction.py
relative_reconstruction_error(X: torch.Tensor, X_reconstructed: torch.Tensor, eps: float = torch.finfo(torch.float32).eps, per_sample: bool = False) -> torch.Tensor
#
Compute relative reconstruction error normalized by data magnitude.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Original data, shape (n_samples, n_features).
TYPE:
|
X_reconstructed
|
Reconstructed data, shape (n_samples, n_features).
TYPE:
|
eps
|
Small constant for numerical stability.
TYPE:
|
per_sample
|
If True, return per-sample errors.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Relative reconstruction error in [0, inf). Values closer to 0 are better. |
Source code in spectre/metrics/reconstruction.py
procrustes_error(X: torch.Tensor, Y: torch.Tensor, eps: float = torch.finfo(torch.float32).eps, scaling: bool = False) -> torch.Tensor
#
Compute Procrustes error after optimal orthogonal alignment.
Finds optimal orthogonal transformation (rotation and optional scaling) to align Y with X, then computes the residual error. Useful for comparing embeddings that may differ by rotation.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Target matrix, shape (n_samples, n_features).
TYPE:
|
Y
|
Source matrix to align, shape (n_samples, n_features).
TYPE:
|
scaling
|
If True, allow scaling in addition to rotation.
TYPE:
|
eps
|
Small value to avoid division by zero.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Procrustes error after optimal alignment. |
Examples:
>>> X = torch.randn(100, 3)
>>> # Rotate Y relative to X
>>> angle = torch.pi / 4
>>> R = torch.tensor(
... [
... [torch.cos(angle), -torch.sin(angle), 0],
... [torch.sin(angle), torch.cos(angle), 0],
... [0, 0, 1],
... ]
... )
>>> Y = X @ R.T
>>> procrustes_error(X, Y)
0.0...
With noise and scaling:
>>> Y = 1.5 * (X @ R.T) + 0.01 * torch.randn(100, 3)
>>> procrustes_error(X, Y, scaling=True)
0.01...
Source code in spectre/metrics/reconstruction.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |