mindspore.mint.norm

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

Compute the matrix norm or vector norm of the tensor along a specified dimension.

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)}\)

Parameters
  • input (Tensor) – The input tensor.

  • p (Union[bool, int, float, inf, -inf, 'fro', 'nuc'], optional) – Specify the kind of norm to take. Default fro .

  • dim (Union[int, List(int), Tuple(int)], optional) – Specify the dimension for computation. Default None .

  • keepdim (bool, optional) – Whether the output tensor has dim retained. Default False .

Keyword Arguments

dtype (mindspore.dtype, optional) – When set, input will be converted to the specified type, dtype, before calculating. Default None .

Returns

Tensor

Note

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

  • Depending on the input range of values, the Ascend backend calculation results may have precision errors.

Supported Platforms:

Ascend

Examples

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