Skip to content

euclidean_distance

source

.euclidean_distance(
   tensor1, tensor2
)

Computes the euclidean distance between two tensors.

The euclidean distance or L^2 distance between points p and q is the length of the line segment connecting them.

distance(q,p) =\sqrt{\sum_{i=1}^{n}\left(q_{i}-p_{i}\right)^{2}}

Args

  • tensor1 : a Tensor
  • tensor2 : a Tensor
  • dim : dimension along which the euclidean distance is computed

Returns

  • a Tensor with the euclidean distances between the two tensors

cosine_distance

source

.cosine_distance(
   tensor1, tensor2, dtype = tf.float32
)

cosine_distance

Computes the pairwise cosine distance between two non-zero tensors on their last dimension. The cosine distance is defined as 1 - cosine similarity. With the cosine similarity defined as:

similarity =\cos (\theta)=\frac{\mathbf{A} \cdot \mathbf{B}}{\|\mathbf{A}\|\|\mathbf{B}\|}=\frac{ \sum_{i=1}^{n} A_{i} B_{i}}{\sqrt{\sum_{i=1}^{n} A_{i}^{2}} \sqrt{\sum_{i=1}^{n} B_{i}^{2}}}

Args

  • tensor1 (Tensor) : first tensor
  • tensor2 (Tensor) : second tensor
  • dtype (DType) : assumed type of both tensors

Returns

  • distance (Tensor) : the pairwise cosine distance between two tensors

torus_l1_distance

source

.torus_l1_distance(
   point, shape
)

Computes the l1 distance between a given point or batch of points and all other points in a torus

Args

  • point (Tensor) : a rank 0 or rank 1 tensor with the coordinates for a point or a rank 2 tensor with a batch of points.
  • shape (List) : a list with the shape for the torus - either 1D or 2D

Returns

  • distances (Tensor) : a rank 1 or 2 tensor with the distances between each point in the 1D torus and each unique coordinate in the shape

Examples

  • distance for a single point torus_l1_distance(1,[4]) or torus_1d_l1_distance([1],[4])
[ 1.,  0.,  1.,  2.]
  • distance for multiple points torus_l1_distance([[2],[3]],[4])
[[ 2.,  1.,  0.,  1.],
 [ 1.,  2.,  1.,  0.]]
  • distance between a point and other coordinates in a 2D torus
r = torus_l1_distance([[1,1],[1,2]],[3,3])
np.reshape(r,[-1,3,3])

[[[ 2.,  1.,  2.],
  [ 1.,  0.,  1.],
  [ 2.,  1.,  2.]],

 [[ 2.,  2.,  1.],
  [ 1.,  1.,  0.],
  [ 2.,  2.,  1.]]]