mindspore.mint.norm

View Source On Gitee
mindspore.mint.norm(input, p='fro', dim=None, keepdim=False, *, dtype=None)[source]

Returns the matrix norm or vector norm of a given tensor.

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

p

norm for matrices

norm for vectors

None (default)

Frobenius norm

2-norm (see below)

'fro'

Frobenius norm

– not supported –

'nuc'

nuclear norm

– not supported –

inf

\(max(sum(abs(x), dim=1))\)

\(max(abs(x))\)

-inf

\(min(sum(abs(x), dim=1))\)

\(min(abs(x))\)

0

– not supported –

\(sum(x != 0)\)

other int or float

– not supported –

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

Warning

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

Parameters
  • input (Tensor) – The input of norm with data type of bfloat16, float16 or float32. The shape is \((*)\) where \(*\) means, any number of additional dimensions.

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

  • dim (Union[int, Tuple(int)], optional) – calculate the dimension of vector norm or matrix norm. Default: None .

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

Keyword Arguments

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

Returns

Tensor, the result of norm calculation on the specified dimension, dim, has the same dtype as input.

Raises
  • ValueError – If dim is out of range.

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

  • ValueError – If two elements of dim is same after normalize.

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

Supported Platforms:

Ascend

Note

Currently, it only support ops.function.math_func.norm_ext(input, p=number).

Examples

>>> import mindspore as ms
>>> from mindspore import mint
>>> data_range = ops.arange(-13, 13, dtype=ms.float32)
>>> x = data_range[data_range != 0]
>>> y = x.reshape(5, 5)
>>> print(mint.norm(x, 2.0))
38.327538