norm¶
- paddle.linalg. norm ( x, p=None, axis=None, keepdim=False, name=None ) [source]
-
Returns the matrix norm (the Frobenius norm, the nuclear norm and p-norm) or vector norm (the 1-norm, the Euclidean or 2-norm, and in general the p-norm) of a given tensor.
Whether the function calculates the vector norm or the matrix norm is determined as follows:
If axis is of type int, calculate the vector norm.
If axis is a two-dimensional array, calculate the matrix norm.
If axis is None, x is compressed into a one-dimensional vector and the vector norm is calculated.
Paddle supports the following norms:
porder
norm for matrices
norm for vectors
None(default)
frobenius norm
2_norm
fro
frobenius norm
not support
nuc
nuclear norm
not support
inf
max(sum(abs(x), dim=1))
max(abs(x))
-inf
min(sum(abs(x), dim=1))
min(abs(x))
0
not support
sum(x != 0)
1
max(sum(abs(x), dim=0))
as below
-1
min(sum(abs(x), dim=0))
as below
2
The maximum singular value of a matrix consisting of axis.
as below
-2
The minimum singular value of a matrix consisting of axis.
as below
- other int
-
or float
not support
sum(abs(x)^{porder})^ {(1 / porder)}
- Parameters
-
x (Tensor) – The input tensor could be N-D tensor, and the input data type could be float32 or float64.
p (int|float|string, optional) – Order of the norm. Supported values are fro, nuc, 0, ±1, ±2, ±inf and any real number yielding the corresponding p-norm. Default value is None.
axis (int|list|tuple, optional) – The axis on which to apply norm operation. If axis is int or list(int)/tuple(int) with only one element, the vector norm is computed over the axis. If axis < 0, the dimension to norm operation is rank(input) + axis. If axis is a list(int)/tuple(int) with two elements, the matrix norm is computed over the axis. Default value is None.
keepdim (bool, optional) – Whether to reserve the reduced dimension in the output Tensor. The result tensor will have fewer dimension than the
input
unlesskeepdim
is true, default value is False.name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.
- Returns
-
results of norm operation on the specified axis of input tensor, it’s data type is the same as input’s Tensor.
- Return type
-
Tensor
Examples
>>> import paddle >>> x = paddle.arange(24, dtype="float32").reshape([2, 3, 4]) - 12 >>> print(x) Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, [[[-12., -11., -10., -9. ], [-8. , -7. , -6. , -5. ], [-4. , -3. , -2. , -1. ]], [[ 0. , 1. , 2. , 3. ], [ 4. , 5. , 6. , 7. ], [ 8. , 9. , 10., 11.]]]) >>> # compute frobenius norm along last two dimensions. >>> out_fro = paddle.linalg.norm(x, p='fro', axis=[0,1]) >>> print(out_fro) Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, [17.43559647, 16.91153526, 16.73320007, 16.91153526]) >>> # compute 2-order vector norm along last dimension. >>> out_pnorm = paddle.linalg.norm(x, p=2, axis=-1) >>> print(out_pnorm) Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True, [[21.11871147, 13.19090557, 5.47722578 ], [3.74165750 , 11.22497177, 19.13112640]]) >>> # compute 2-order norm along [0,1] dimension. >>> out_pnorm = paddle.linalg.norm(x, p=2, axis=[0,1]) >>> print(out_pnorm) Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, [15.75857544, 14.97978878, 14.69693947, 14.97978973]) >>> # compute inf-order norm >>> out_pnorm = paddle.linalg.norm(x, p=float("inf")) >>> print(out_pnorm) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 12.) >>> out_pnorm = paddle.linalg.norm(x, p=float("inf"), axis=0) >>> print(out_pnorm) Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, [[12., 11., 10., 9. ], [8. , 7. , 6. , 7. ], [8. , 9. , 10., 11.]]) >>> # compute -inf-order norm >>> out_pnorm = paddle.linalg.norm(x, p=-float("inf")) >>> print(out_pnorm) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 0.) >>> out_pnorm = paddle.linalg.norm(x, p=-float("inf"), axis=0) >>> print(out_pnorm) Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, [[0., 1., 2., 3.], [4., 5., 6., 5.], [4., 3., 2., 1.]])