mindspore.mint.linalg.vector_norm

View Source On Gitee
mindspore.mint.linalg.vector_norm(x, ord=2, dim=None, keepdim=False, *, dtype=None)[source]

Returns the vector norm of the given tensor on the specified dimensions.

ord is the calculation mode of norm. The following norm modes are supported.

ord

norm for vectors

2 (Default)

2-norm (see below)

inf

\(max(abs(x))\)

-inf

\(min(abs(x))\)

0

\(sum(x!=0)\)

other int or float

\(sum(abs(x)^{ord})^{(1 / ord)}\)

Warning

This is an experimental API that is subject to change or deletion.

Parameters
  • x (Tensor) – Tensor of shape \((*)\) where * is zero s more batch dimensions.

  • ord (Union[bool, int, float, inf, -inf], optional) – norm's mode. refer to the table above for behavior. Default: 2 .

  • dim (Union[int, List(int), Tuple(int)], optional) –

    The dimensions along which to perform the vector norm calculation. Default: None .

    • When dim is an integer, a list or a tuple, the norm calculation will be performed across these specified dimensions, while the remaining dimensions will be considered as batch dimensions.

    • When dim is None, the norm will be calculated after flattening the Tensor x .

  • keepdim (bool) – whether the output Tensor retains the original dimension. Default: False .

Keyword Arguments

dtype (mindspore.dtype, optional) – When set, x will be converted to the specified type, dtype before execution, and dtype of returned Tensor will also be dtype. When dtype is None , the dtype of x is preserved. Default: None .

Returns

Tensor, the result of norm calculation on the specified dimension, dim.

Raises
  • TypeError – If x is not a Tensor.

  • TypeError – If dim is neither an int nor a list or tuple.

  • ValueError – If ord is not in [bool, int, float, inf, -inf].

  • ValueError – The elements of dim are duplicate.

  • ValueError – If any elements of dim is out of range.

Supported Platforms:

Ascend

Examples

>>> import mindspore as ms
>>> x = ms.ops.arange(0, 12, dtype=ms.float32) - 6
>>> print(ms.mint.linalg.vector_norm(x, ord=2))
12.083046
>>> print(ms.mint.linalg.vector_norm(x, ord=float('inf')))
6.0
>>> print(ms.mint.linalg.vector_norm(x, ord=float('-inf')))
0.0
>>> print(ms.mint.linalg.vector_norm(x, ord=0))
11.0
>>> print(ms.mint.linalg.vector_norm(x, ord=4.5))
7.2243643