cdist¶
- paddle. cdist ( x: Tensor, y: Tensor, p: float = 2.0, compute_mode: Literal[use_mm_for_euclid_dist_if_necessary, use_mm_for_euclid_dist, donot_use_mm_for_euclid_dist] = 'use_mm_for_euclid_dist_if_necessary', name: str | None = None ) Tensor [source]
-
Compute the p-norm distance between each pair of the two collections of inputs.
This function is equivalent to scipy.spatial.distance.cdist(input,’minkowski’, p=p) if p∈(0,∞). When p=0 it is equivalent to scipy.spatial.distance.cdist(input, ‘hamming’) * M. When p=∞, the closest scipy function is scipy.spatial.distance.cdist(xn, lambda x, y: np.abs(x - y).max()).
- Parameters
-
x (Tensor) – A tensor with shape B×P×M.
y (Tensor) – A tensor with shape B×R×M.
p (float, optional) – The value for the p-norm distance to calculate between each vector pair. Default: 2.0.
compute_mode (str, optional) –
The mode for compute distance.
use_mm_for_euclid_dist_if_necessary
, for p = 2.0 and (P > 25 or R > 25), it will use matrix multiplication to calculate euclid distance if possible.use_mm_for_euclid_dist
, for p = 2.0, it will use matrix multiplication to calculate euclid distance.donot_use_mm_for_euclid_dist
, it will not use matrix multiplication to calculate euclid distance.
Default:
use_mm_for_euclid_dist_if_necessary
.name (str|None, optional) – For details, please refer to Name. Generally, no setting is required. Default: None.
- Returns
-
Tensor, the dtype is same as input tensor.
If x has shape B×P×M and y has shape B×R×M then the output will have shape B×P×R.
Examples
>>> import paddle >>> x = paddle.to_tensor([[0.9041, 0.0196], [-0.3108, -2.4423], [-0.4821, 1.059]], dtype=paddle.float32) >>> y = paddle.to_tensor([[-2.1763, -0.4713], [-0.6986, 1.3702]], dtype=paddle.float32) >>> distance = paddle.cdist(x, y) >>> print(distance) Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True, [[3.11927032, 2.09589314], [2.71384072, 3.83217239], [2.28300953, 0.37910119]])