Instructions for use
The introduction to the use of torch.cdist is shown on the official website .
It calculates the distance between two sets of vectors in batches.
Among them, x1 and x2 are the two input vector sets.
p defaults to 2, which is the Euclidean distance.
It is functionally equivalent to scipy.spatial.distance.cdist (input,'minkowski', p=p)
If the shape of x1 is [B,P,M], and the shape of x2 is [B,R,M], then the result shape of cdist is [B,P,R]
further explanation
x1 is generally an input vector, and x2 is generally a codebook.
Find the Euclidean distance between all elements in x2 and each element in x1 (when p defaults to 2)
As the example below
import torch
x1 = torch.FloatTensor([0.1, 0.2, 0, 0.5]).view(4, 1)
x2 = torch.FloatTensor([0.2, 0.3]).view(2, 1)
print(torch.cdist(x1,x2))
Calculate the Euclidean distance between all elements in x2 and each element in x1 respectively, that is, the following steps
x 11 = ( 0.1 − 0.2 ) 2 = 0.1 x 12 = ( 0.1 − 0.3 ) 2 = 0.2 x 21 = ( 0.2 − 0.2 ) 2 = 0 x 22 = ( 0.2 − 0.3 ) 2 = 0.1 x 31 = ( 0 − 0.2 ) 2 = 0.2 x 32 = ( 0 − 0.3 ) 2 = 0.3 x 41 = ( 0.5 − 0.2 ) 2 = 0.3 x 42 = ( 0.5 − 0.3 ) 2 = 0.2 x_{11} = \sqrt{ (0.1-0.2)^2} = 0.1 \newline x_{12} = \sqrt { (0.1-0.3)^2} = 0.2 \newline x_{21} = \sqrt { (0.2-0.2)^2} = 0 \newline x_{22} = \sqrt { (0.2-0.3)^2} = 0.1 \newline x_{31} = \sqrt { (0-0.2)^2} = 0.2 \newline x_{32} = \sqrt { (0-0.3)^2} = 0.3 \newline x_{41} = \sqrt { (0.5-0.2)^2 } =0.3\newline x_{42} = \sqrt { (0.5-0.3)^2 } = 0.2\newline x11=(0.1−0.2)2=0.1x12=(0.1−0.3)2=0.2x21=(0.2−0.2)2=0x22=(0.2−0.3)2=0.1x31=(0−0.2)2=0.2x32=(0−0.3)2=0.3x41=(0.5−0.2)2=0.3x42=(0.5−0.3)2=0.2
So the result of running is
Expanded to 2-dimensional case
As the example below
import torch
x1 = torch.FloatTensor([0.1, 0.2, 0.1, 0.5, 0.2, -0.9, 0.8, 0.4]).view(4, 2)
x2 = torch.FloatTensor([0.2, 0.3, 0, 0.1]).view(2, 2)
print(torch.cdist(x1,x2))
The x1 and x2 data are two-dimensional,
Calculate the Euclidean distance between all elements in x2 and each element in x1 respectively, that is, the following steps
x 11 = ( 0.1 − 0.2 ) 2 + ( 0.2 − 0.3 ) 2 = 0.02 = 0.1414 x 12 = ( 0.1 − 0.0 ) 2 + ( 0.2 − 0.1 ) 2 = 0.02 = 0.1414 x 21 = ( 0.1 − 0.2 ) 2 + ( 0.5 − 0.3 ) 2 = 0.05 = 0.2236 x 22 = ( 0.1 − 0.0 ) 2 + ( 0.5 − 0.1 ) 2 = 0.17 = 0.4123 x 31 = ( 0.2 − 0.2 ) 2 + ( − 0.9 − 0.3 ) 2 = 1.2 x 32 = ( 0.2 − 0.0 ) 2 + ( − 0.9 − 0.1 ) 2 = ( 1.04 ) = 1.0198 x 41 = ( 0.8 − 0.2 ) 2 + ( 0.4 − 0.3 ) 2 = ( 0.37 ) = 0.6083 x 42 = ( 0.8 − 0.0 ) 2 + ( 0.4 − 0.1 ) 2 = ( 0.73 ) = 0.8544 x_{11} = \sqrt{ (0.1-0.2)^2 + (0.2-0.3)^2 } = \sqrt{0.02} = 0.1414 \newline x_{12} = \sqrt { (0.1-0.0)^2 + (0.2-0.1)^2 } = \sqrt{0.02} = 0.1414 \newline x_{21} = \sqrt { (0.1-0.2)^2 + (0.5-0.3)^2 } = \sqrt{0.05} = 0.2236 \newline x_{22} = \sqrt { (0.1-0.0)^2 + (0.5-0.1)^2 } = \sqrt{0.17} = 0.4123 \newline x_{31} = \sqrt { (0.2-0.2)^2 + (-0.9-0.3)^2} = 1.2 \newline x_{32} = \sqrt { (0.2-0.0)^2 + (-0.9-0.1)^2} = \sqrt(1.04) = 1.0198 \newline x_{41} = \sqrt { (0.8-0.2)^2 + (0.4-0.3)^2 } = \sqrt(0.37) = 0.6083 \newline x_{42} = \sqrt { (0.8-0.0)^2 + (0.4-0.1)^2 } = \sqrt(0.73) = 0.8544 \newline x11=(0.1−0.2)2+(0.2−0.3)2=0.02=0.1414x12=(0.1−0.0)2+(0.2−0.1)2=0.02=0.1414x21=(0.1−0.2)2+(0.5−0.3)2=0.05=0.2236x22=(0.1−0.0)2+(0.5−0.1)2=0.17=0.4123x31=(0.2−0.2)2+(−0.9−0.3)2=1.2x32=(0.2−0.0)2+(−0.9−0.1)2=(1.04)=1.0198x41=(0.8−0.2)2+(0.4−0.3)2=(0.37)=0.6083x42=(0.8−0.0)2+(0.4−0.1)2=(0.73)=0.8544
So the result is as follows
The Euclidean distance of p=2 is also the L2 paradigm, if p=1 is the L1 paradigm.
In the above example, modify the p parameter
import torch
x1 = torch.FloatTensor([0.1, 0.2, 0.1, 0.5, 0.2, -0.9, 0.8, 0.4]).view(4, 2)
x2 = torch.FloatTensor([0.2, 0.3, 0, 0.1]).view(2, 2)
print(torch.cdist(x1,x2,p=1))
The results are as follows, and we will not calculate them one by one here.