vector_norm

paddle.linalg. vector_norm ( x, p=2.0, axis=None, keepdim=False, name=None ) [source]

Calculate the p-order vector norm for certain dimension of Tensor input. Returns the vector norm (the 1-norm, the Euclidean or 2-norm, and in general the p-norm) of a given tensor.

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

  • p (int|float, optional) – None for porder=2.0. Default None.

  • axis (int|list, optional) – None for last dimension. Default None.

  • 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 vector_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
>>> import numpy as np
>>> 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_vector_norm = paddle.linalg.vector_norm(x=x,p=2,axis=None,keepdim=False)
>>> print(out_vector_norm)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
34.)
>>> out_vector_norm = paddle.linalg.vector_norm(x=x,p=0,axis=[0,1],keepdim=False)
>>> print(out_vector_norm)
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[5., 6., 6., 6.])
>>> out_vector_norm = paddle.linalg.vector_norm(x=x,p=float("inf"),axis=[1,2],keepdim=False)
>>> print(out_vector_norm)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[12., 11.])
>>> out_vector_norm = paddle.linalg.vector_norm(x=x,p=1,axis=1,keepdim=False)
>>> print(out_vector_norm)
Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[24., 21., 18., 15.],
 [12., 15., 18., 21.]])