Pairwise Distance Mahalanobis#
mahalanobis
#
| CLASS | DESCRIPTION |
|---|---|
PairwiseDistanceMahalanobis |
PyTorch module for computing Mahalanobis pairwise distances. |
| FUNCTION | DESCRIPTION |
|---|---|
pairwise_distance_mahalanobis |
Compute Mahalanobis pairwise distances between data samples. |
Classes#
PairwiseDistanceMahalanobis(reduce: Literal['mean', 'sum', 'none', None] = None, normalize: bool = False)
#
Bases: PairwiseDistance
PyTorch module for computing Mahalanobis pairwise distances.
This module computes pairwise Mahalanobis distances using provided inverse covariance matrices for each data sample. Supports computing distances within a single dataset (X vs X) or between two datasets (X vs Y).
| PARAMETER | DESCRIPTION |
|---|---|
reduce
|
Reduction method for the computed distances.
TYPE:
|
normalize
|
Whether to normalize distances to unit scale.
TYPE:
|
Examples:
Computing distances within a single dataset:
>>> import torch
>>> from spectre.pairwise_distance import PairwiseDistanceMahalanobis
>>> X = torch.randn(50, 3)
>>> metric_inv = torch.eye(3).unsqueeze(0).repeat(50, 1, 1)
>>> mahal_dist = PairwiseDistanceMahalanobis()
>>> distances = mahal_dist(X, metric_inv=metric_inv)
>>> distances.shape
... torch.Size([50, 50])
Computing cross-distances between two datasets:
>>> Y = torch.randn(30, 3)
>>> metric_inv_y = torch.eye(3).unsqueeze(0).repeat(30, 1, 1)
>>> distances = mahal_dist(X, metric_inv=metric_inv, Y=Y, metric_inv_y=metric_inv_y)
>>> distances.shape
... torch.Size([50, 30])
| METHOD | DESCRIPTION |
|---|---|
forward_step |
Compute Mahalanobis pairwise distances. |
Source code in spectre/pairwise_distance/mahalanobis.py
Functions#
forward_step(X: torch.Tensor, **kwargs) -> torch.Tensor
#
Compute Mahalanobis pairwise distances.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor with shape (n_samples, in_features).
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Pairwise distance matrix. |
Source code in spectre/pairwise_distance/mahalanobis.py
Functions#
pairwise_distance_mahalanobis(X: torch.Tensor, metric_inv: torch.Tensor, Y: torch.Tensor | None = None, metric_inv_y: torch.Tensor | None = None, normalize: bool = False, reduce: Literal['mean', 'sum', 'none', None] = None, eps: float = torch.finfo(torch.float32).eps) -> torch.Tensor
#
Compute Mahalanobis pairwise distances between data samples.
The Mahalanobis distance between two samples \(x_k\) and \(y_l\) is computed as: \(d(x_k, y_l) = \sqrt{(x_k - y_l)^\top M_{kl}^{-1} (x_k - y_l)}\), where \(M_{kl}^{-1}\) is the symmetrized inverse covariance or diffusion matrix: \(M_{kl}^{-1} = (M_{X_k}^{-1} + M_{Y_l}^{-1}) / 2\).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor with shape (n_samples_x, in_features).
TYPE:
|
metric_inv
|
Inverse covariance matrices for each sample in X with shape (n_samples_x, in_features, in_features).
TYPE:
|
Y
|
Second tensor with shape (n_samples_y, in_features). If None, computes distances within X. If provided, metric_inv_y must also be provided.
TYPE:
|
metric_inv_y
|
Inverse covariance matrices for each sample in Y with shape (n_samples_y, in_features, in_features). Must be provided if Y is provided.
TYPE:
|
normalize
|
Whether to normalize distances to unit scale.
TYPE:
|
reduce
|
Reduction method for the computed distances.
TYPE:
|
eps
|
Small value added for numerical stability.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Pairwise distance matrix. Shape depends on Y and reduce parameters:
|
Examples:
Computing distances within a single dataset:
>>> import torch
>>> X = torch.randn(50, 3)
>>> metric_inv = torch.eye(3).unsqueeze(0).repeat(50, 1, 1) # Identity matrices
>>> distances = pairwise_distance_mahalanobis(X, metric_inv)
>>> distances.shape
... torch.Size([50, 50])
Computing cross-distances between two datasets:
>>> X = torch.randn(50, 3)
>>> Y = torch.randn(30, 3)
>>> metric_inv_X = torch.eye(3).unsqueeze(0).repeat(50, 1, 1)
>>> metric_inv_y = torch.eye(3).unsqueeze(0).repeat(30, 1, 1)
>>> distances = pairwise_distance_mahalanobis(
... X, metric_inv_X, Y=Y, metric_inv_y=metric_inv_y
... )
>>> distances.shape
... torch.Size([50, 30])
Source code in spectre/pairwise_distance/mahalanobis.py
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 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 216 217 218 219 220 221 222 223 224 | |