dist¶

paddle.fluid.layers.dist(x, y, p=2)[source]

This OP returns the p-norm of (x - y). It is not a norm in a strict sense, only as a measure of distance. The shapes of x and y must be broadcastable.

Where, z = x - y,

When p = 0, defining $0^0=0$, the zero-norm of z is simply the number of non-zero elements of z.

$||z||_{0}=\lim_{p \rightarrow 0}\sum_{i=1}^{m}|z_i|^{p}$

When p = inf, the inf-norm of z is the maximum element of z.

$||z||_\infty=\max_i |z_i|$

When p = -inf, the negative-inf-norm of z is the minimum element of z.

$||z||_{-\infty}=\min_i |z_i|$

Otherwise, the p-norm of z follows the formula,

$||z||_{p}=(\sum_{i=1}^{m}|z_i|^p)^{\frac{1}{p}}$
Parameters
• x (Variable) – 1-D to 6-D Tensor, its data type is float32 or float64.

• y (Variable) – 1-D to 6-D Tensor, its data type is float32 or float64.

• p (float, optional) – The norm to be computed, its data type is float32 or float64. Default: 2.

Returns

Tensor that is the p-norm of (x - y).

Return type

Variable

Examples

import paddle
import numpy as np

with fluid.dygraph.guard():
x = fluid.dygraph.to_variable(np.array([[3, 3],[3, 3]]).astype(np.float32))
y = fluid.dygraph.to_variable(np.array([[3, 3],[3, 1]]).astype(np.float32))
out = fluid.layers.dist(x, y, 0)
print(out.numpy()) # out = [1.]

out = fluid.layers.dist(x, y, 2)
print(out.numpy()) # out = [2.]

out = fluid.layers.dist(x, y, float("inf"))
print(out.numpy()) # out = [2.]

out = fluid.layers.dist(x, y, float("-inf"))
print(out.numpy()) # out = [0.]