mindspore.ops.norm
- 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.
Warning
Non-backward-compatible change after version 2.9.0: ord will be renamed to p, and the default value will change from
Noneto2.ord is the calculation mode of norm. The following norm modes are supported.
- 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, flatten 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. When set, A will be converted to the specified data type, before calculating. DefaultNone.- Returns:
Tensor
- Supported Platforms:
AscendGPUCPU
Note
Currently, complex numbers are not supported.
Depending on the input range of values, the Ascend backend calculation results may have precision errors.
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])