mindspore.ops.norm

View Source On Gitee
mindspore.ops.norm(A, ord=None, dim=None, keepdim=False, *, dtype=None)[source]

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

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

ord

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)

1

max(sum(abs(x),dim=0))

as below

-1

min(sum(abs(x),dim=0))

as below

2

largest singular value

as below

-2

smallest singular value

as below

other int or float

– not supported –

sum(abs(x)ord)(1/ord)

Parameters
  • A (Tensor) – The input tensor.

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

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

    Specify the dimension for computation. Default None .

    • If dim is int, calculate the vector norm.

    • if dim is a 2-tuple, calculate the matrix norm.

    • If dim is None and ord is None , flattened A to 1D and calculate 2-norm of the vector.

    • If dim is None and ord is not None, A must be 1D or 2D.

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

Keyword Arguments

dtype (mindspore.dtype, optional) – The data type returned. The data type returned. When set, A will be converted to the specified data type, before calculaing. Default None .

Returns

Tensor

Supported Platforms:

Ascend GPU CPU

Note

Currently, complex numbers are not supported.

Examples

>>> import mindspore
>>> # Vector norms:
>>> A = mindspore.tensor([3., 4., 12.])
>>> mindspore.ops.norm(A)
Tensor(shape=[], dtype=Float32, value= 13)
>>> mindspore.ops.norm(A, ord=1)
Tensor(shape=[], dtype=Float32, value= 19)
>>> mindspore.ops.norm(A, ord=0)
Tensor(shape=[], dtype=Float32, value= 3)
>>>
>>> # Matrix norms:
>>> A = mindspore.tensor([[1., 2., 3.],
...                       [4., 5., 7.]])
>>> mindspore.ops.norm(A)  # Frobenius norm
Tensor(shape=[], dtype=Float32, value= 10.198)
>>> mindspore.ops.norm(A, ord='nuc')  # nuclear norm
Tensor(shape=[], dtype=Float32, value= 10.7625)
>>> mindspore.ops.norm(A, ord=1)  # 1-norm
Tensor(shape=[], dtype=Float32, value= 10)
>>>
>>> # Batched vector norm:
>>> mindspore.ops.norm(A, dim=1)
Tensor(shape=[2], dtype=Float32, value= [ 3.74165726e+00,  9.48683262e+00])