norm¶
- paddle.linalg. norm ( x, p='fro', axis=None, keepdim=False, name=None ) [source]
-
Returns the matrix norm (Frobenius) or vector norm (the 1-norm, the Euclidean or 2-norm, and in general the p-norm for p > 0) of a given tensor.
Note
This norm API is different from numpy.linalg.norm. This api supports high-order input tensors (rank >= 3), and certain axis need to be pointed out to calculate the norm. But numpy.linalg.norm only supports 1-D vector or 2-D matrix as input tensor. For p-order matrix norm, this api actually treats matrix as a flattened vector to calculate the vector norm, NOT REAL MATRIX NORM.
- Parameters
-
x (Tensor) – The input tensor could be N-D tensor, and the input data type could be float32 or float64.
p (float|string, optional) – Order of the norm. Supported values are fro, 0, 1, 2, inf, -inf and any positive real number yielding the corresponding p-norm. Not supported: ord < 0 and nuclear norm. Default value is fro.
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, [17.43559647, 16.91153526, 16.73320007, 16.91153526]) >>> # 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.]])