matrix_norm

paddle.linalg. matrix_norm ( x, p='fro', axis=[- 2, - 1], keepdim=False, name=None ) [source]

Calculate the p-order matrix norm for certain dimension of Tensor input.

Parameters
  • x (Tensor) – Tensor, data type float32, float64.

  • p (int|float|string, optional) – Default ‘fro’.

  • axis (list, optional) – The axis is a list(int)/tuple(int) with two elements. Default last two dimensions.

  • keepdim (bool, optional) – Whether keep the dimensions as the input, Default 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 matrix_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.]]])

>>> out_matrix_norm = paddle.linalg.matrix_norm(x=x,p=2,axis=[0,1],keepdim=False)
>>> print(out_matrix_norm)
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[15.75857544, 14.97978878, 14.69693947, 14.97978973])

>>> out_matrix_norm = paddle.linalg.matrix_norm(x=x,p='fro',axis=[0,1],keepdim=False)
>>> print(out_matrix_norm)
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[17.43559647, 16.91153526, 16.73320007, 16.91153526])

>>> out_matrix_norm = paddle.linalg.matrix_norm(x=x,p=float('inf'),axis=[1,2],keepdim=False)
>>> print(out_matrix_norm)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[42., 38.])

>>> out_matrix_norm = paddle.linalg.matrix_norm(x=x,p=-1,axis=[0,1],keepdim=False)
>>> print(out_matrix_norm)
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[12., 12., 12., 12.])

>>> out_matrix_norm = paddle.linalg.matrix_norm(x=x,p='nuc',axis=[0,1],keepdim=False)
>>> print(out_matrix_norm)
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[23.21962357, 22.82873154, 22.69693947, 22.82873154])