mindspore.ops.norm
- mindspore.ops.norm(A, ord=None, dim=None, keepdim=False, *, dtype=None)[source]
Returns the matrix norm or vector norm of a given tensor.
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) – Tensor of shape \((*, n)\) or \((*, m, n)\) where * is zero or more batch dimensions.
ord (Union[int, float, inf, -inf, 'fro', 'nuc'], optional) – norm’s mode. refer to the table above for behavior. Default:
None
.dim (Union[int, Tuple(int)], optional) –
calculate the dimension of vector norm or matrix norm. Default:
None
.When dim is int, it will be calculated by vector norm.
When dim is a 2-tuple, it will be calculated by matrix norm.
If dim is None and ord is None, A will be flattened to 1D and the 2-norm of the vector will be calculated.
If dim is None and ord is not None, A must be 1D or 2D.
keepdim (bool) – whether the output Tensor retains the original dimension. Default:
False
.
- Keyword Arguments
dtype (
mindspore.dtype
, optional) – When set, A 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 A.
- Raises
ValueError – If dim is out of range.
TypeError – If dim is neither an int nor a tuple of int.
TypeError – If A is a vector and ord is a str.
ValueError – If A is a matrices and ord is not in valid mode.
ValueError – If A is a matrices and ord is an integer but not in [1, -1, 2, -2].
ValueError – If two elements of dim is same after normalize.
ValueError – If any elements of dim is out of range.
- Supported Platforms:
Ascend
GPU
CPU
Note
Currently, complex numbers are not supported.
Running on
Ascend
platform is not supported when ord is 2 , -2 or nuc .
Examples
>>> import mindspore as ms >>> import mindspore.ops as ops >>> x = ops.arange(-12, 13, dtype=ms.float32) >>> y = x.reshape(5, 5) >>> print(ops.norm(x)) 36.05551 >>> print(ops.norm(x, float('inf'))) 12.0 >>> print(ops.norm(x, float('-inf'))) 0.0 >>> print(ops.norm(x, 0)) 24.0 >>> print(ops.norm(x, 1)) 156.0 >>> print(ops.norm(x, -1)) 0.0 >>> print(ops.norm(x, 2)) 36.05551 >>> print(ops.norm(x, -2)) 0.0 >>> print(ops.norm(x, 3)) 23.000631 >>> print(ops.norm(x, -3)) 0.0 >>> print(ops.norm(y)) 36.05551 >>> print(ops.norm(y, 'fro')) 36.05551 >>> print(ops.norm(y, 'nuc')) 42.42641 >>> print(ops.norm(y, float('inf'))) 50.0 >>> print(ops.norm(y, float('-inf'))) 6.0 >>> print(ops.norm(y, 1)) 32.0 >>> print(ops.norm(y, -1)) 30.0 >>> print(ops.norm(y, 2)) 35.355343 >>> m = ms.Tensor([[1., -1., 2.], [-2., 3., -4.]]) >>> print(ops.norm(m, dim=0)) [2.236068 3.1622777 4.472136 ] >>> print(ops.norm(m, dim=1)) [2.4494898 5.3851647] >>> print(ops.norm(m, ord=1, dim=1)) [4. 9.] >>> n = ops.arange(27, dtype=ms.float32).reshape(3, 3, 3) >>> print(ops.norm(n, dim=(1, 2))) [14.282857 39.76179 66.45299 ] >>> print(ops.norm(n[0, :, :]), ops.norm(n[1, :, :]), ops.norm(n[2, :, :])) 14.282857 39.76179 66.45299