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

'fro'

Frobenius norm

– not supported –

'nuc'

nuclear norm

– not supported –

other int or float

– not supported –

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

Warning

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

Parameters
  • input (Tensor) – The shape is \((*)\) or \((*, m, n)\) where \(*\) means, any number of additional dimensions.

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

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

  • keepdim (bool, optional) – 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.

Raises
  • TypeError – If input is not a Tensor.

  • 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 normalized.

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

Note

Dynamic shape, Dynamic rank and mutable input is not supported in graph mode (mode=mindspore.GRAPH_MODE).

Supported Platforms:

Ascend

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