Pairwise Distance Covariance#
covariance
#
| CLASS | DESCRIPTION |
|---|---|
PairwiseDistanceCov |
PyTorch module for computing covariance-based pairwise distances. |
| FUNCTION | DESCRIPTION |
|---|---|
pairwise_distance_cov |
Compute covariance-based pairwise distances between data samples. |
Classes#
PairwiseDistanceCov(cov_k_neighbor: int, cov_mode: Literal['batchwise', 'sequential'] = 'batchwise', reduce: Literal['mean', 'sum', 'none', None] = None, compute_mode: Literal['donot_use_mm_for_euclid_dist', 'use_mm_for_euclid_dist', 'use_mm_for_euclid_dist_if_necessary'] = 'donot_use_mm_for_euclid_dist', normalize: bool = False)
#
Bases: PairwiseDistance
PyTorch module for computing covariance-based pairwise distances.
This module computes pairwise distances using local covariance matrices estimated from nearest neighbors, implementing a heterogeneous Gaussian distance metric.
| PARAMETER | DESCRIPTION |
|---|---|
cov_k_neighbor
|
Number of nearest neighbors to use for covariance estimation.
TYPE:
|
cov_mode
|
Computation mode for covariance calculation.
TYPE:
|
reduce
|
Reduction method for the computed distances.
TYPE:
|
compute_mode
|
Computation strategy for Euclidean distances.
TYPE:
|
normalize
|
Whether to normalize distances to unit scale.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
forward_step |
Compute covariance-based pairwise distances. |
Source code in spectre/pairwise_distance/covariance.py
Functions#
forward_step(X: torch.Tensor, **kwargs) -> torch.Tensor
#
Compute covariance-based pairwise distances.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor with shape (n_samples, n_features).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Pairwise distance matrix. |
Source code in spectre/pairwise_distance/covariance.py
Functions#
pairwise_distance_cov(X: torch.Tensor, cov_k_neighbor: int = 64, cov_mode: Literal['batchwise', 'sequential'] = 'batchwise', reduce: Literal['mean', 'sum', 'none', None] = None, compute_mode: Literal['donot_use_mm_for_euclid_dist', 'use_mm_for_euclid_dist', 'use_mm_for_euclid_dist_if_necessary'] = 'donot_use_mm_for_euclid_dist', normalize: bool = False, eps: float = torch.finfo(torch.float32).eps) -> torch.Tensor
#
Compute covariance-based pairwise distances between data samples.
This function implements a heterogeneous Gaussian distance metric using local covariance matrices estimated from nearest neighbors. The distance between two samples is computed using their local covariance structure.
The heterogeneous Gaussian distance is computed as: \(d(x_k, x_l) = \sqrt{(x_k - x_l)^\top (C_{k}^{-1} + C_{l}^{-1}) (x_k - x_l)}\), where \(C_k\) and \(C_l\) are the local covariance matrices at samples \(x_k\) and \(x_l\).
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor with shape (n_samples, in_features).
TYPE:
|
cov_k_neighbor
|
Number of nearest neighbors to use for covariance estimation.
TYPE:
|
cov_mode
|
Computation mode for covariance calculation.
TYPE:
|
reduce
|
Reduction method for the computed distances.
TYPE:
|
compute_mode
|
Computation strategy for Euclidean distances.
TYPE:
|
normalize
|
Whether to normalize distances to unit scale.
TYPE:
|
eps
|
Small value added for numerical stability.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Pairwise distance matrix.
|
Examples:
>>> import torch
>>> X = torch.randn(100, 5)
>>> distances = pairwise_distance_cov(X, cov_k_neighbor=20)
>>> distances.shape
torch.Size([100, 100])
Source code in spectre/pairwise_distance/covariance.py
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 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 | |