SafeOps: Distance

SafeOps: Distance#

Functions for calculating the cartesian distance of two vectors.

tad_mctc.storch.distance.cdist(x, y=None, p=2)[source]#

Wrapper for cartesian distance computation.

This currently replaces the use of torch.cdist, which does not handle zeros well and produces nan’s in the backward pass.

Additionally, torch.cdist does not return zero for distances between same vectors (see here).

Parameters:
  • x (Tensor) – First tensor.

  • y (Tensor | None, optional) – Second tensor. If no second tensor is given (default), the first tensor is used as the second tensor, too.

  • p (int, optional) – Power used in the distance evaluation (p-norm). Defaults to 2.

Returns:

Pair-wise distance matrix.

Return type:

Tensor