pairwise_distances#
- torchdr.pairwise_distances(X: Tensor, Y: Tensor | None = None, metric: str = 'euclidean', backend: str | None = None, exclude_diag: bool = False, k: int | None = None, compile: bool = False)[source]#
Compute pairwise distances between two tensors.
- Parameters:
X (torch.Tensor of shape (n_samples, n_features)) – Input data.
Y (torch.Tensor of shape (m_samples, n_features), optional) – Input data. If None, Y is set to X.
metric (str, optional) – Metric to use. Default is “euclidean”.
backend ({'keops', 'faiss', None}, optional) – Backend to use for computation. If None, use standard torch operations.
exclude_diag (bool, optional) – Whether to exclude the diagonal from the distance matrix. Only used when k is not None. Default is False.
k (int, optional) – If not None, return only the k-nearest neighbors.
compile (bool, default=False) – Whether to use torch.compile for faster computation.
- Returns:
C (torch.Tensor) – Pairwise distances.
indices (torch.Tensor, optional) – Indices of the k-nearest neighbors. Only returned if k is not None.