Pairwise Distance Euclidean#
euclidean
#
| CLASS | DESCRIPTION |
|---|---|
PairwiseDistanceEuclidean |
PyTorch module for computing Euclidean pairwise distances. |
| FUNCTION | DESCRIPTION |
|---|---|
pairwise_distance_euclidean |
Compute Euclidean pairwise distances between tensors. |
Classes#
PairwiseDistanceEuclidean(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 Euclidean pairwise distances.
This module wraps the pairwise_distance_euclidean function as a PyTorch
module for integration into neural network architectures.
| PARAMETER | DESCRIPTION |
|---|---|
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 Euclidean pairwise distances. |
Source code in spectre/pairwise_distance/euclidean.py
Functions#
forward_step(X: torch.Tensor, **kwargs) -> torch.Tensor
#
Compute Euclidean pairwise distances.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor with shape (n_samples, n_features).
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Pairwise distance matrix. |
Source code in spectre/pairwise_distance/euclidean.py
Functions#
pairwise_distance_euclidean(X: torch.Tensor, Y: torch.Tensor | None = None, 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) -> torch.Tensor
#
Compute Euclidean pairwise distances between tensors.
This function uses torch.cdist to efficiently compute pairwise Euclidean
distances with various computation modes and optional normalization.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input tensor with shape (n_samples, n_features).
TYPE:
|
Y
|
Second tensor with same feature dimension. If None, computes distances within X.
TYPE:
|
reduce
|
Reduction method to apply to distance matrix.
TYPE:
|
compute_mode
|
Computation mode for
TYPE:
|
normalize
|
Whether to L2-normalize the distance matrix along dimension 1.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Pairwise distance matrix.
|
References
Examples:
>>> import torch
>>> X = torch.randn(50, 10)
>>> distances = pairwise_distance_euclidean(X)
>>> distances.shape
torch.Size([50, 50])
>>> # With reduction
>>> mean_distances = pairwise_distance_euclidean(X, reduce="mean")
>>> mean_distances.shape
torch.Size([50])
Source code in spectre/pairwise_distance/euclidean.py
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |