mindspore.mint.linalg.matrix_norm
- mindspore.mint.linalg.matrix_norm(A, ord='fro', dim=(- 2, - 1), keepdim=False, *, dtype=None)[source]
Returns the matrix norm of a given tensor on the specified dimensions.
ord is the calculation mode of norm. The following norm modes are supported.
ord
norm for matrix
'fro'
(Default)Frobenius norm
'nuc'
nuclear norm
inf
\(max(sum(abs(x), dim=1))\)
-inf
\(min(sum(abs(x), dim=1))\)
1
\(max(sum(abs(x), dim=0))\)
-1
\(min(sum(abs(x), dim=0))\)
2
largest singular value
-2
smallest singular value
Warning
This is an experimental API that is subject to change or deletion.
- Parameters
A (Tensor) – Tensor of shape \((*, m, n)\) where * is zero or more batch dimensions.
ord (Union[int, inf, -inf, 'fro', 'nuc'], optional) – norm's mode. refer to the table above for behavior. Default:
'fro'
.dim (Tuple(int, int), optional) – calculate the dimension of the matrix norm. Default:
(-2, -1)
.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. When dtype isNone
, the dtype of A is preserved. Default:None
.- Returns
Tensor, the result of norm calculation on the specified dimension, dim.
- Raises
TypeError – If dim is not a tuple of int.
ValueError – If the length of dim is not equal to 2.
ValueError – If ord is not in [2, -2, 1, -1, float('inf'), float('-inf'), 'fro', 'nuc'].
ValueError – If two elements of dim is same after normalize.
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 >>> A = ms.ops.arange(0, 12, dtype=ms.float32).reshape(3, 4) >>> print(ms.mint.linalg.matrix_norm(A, ord='fro')) 22.494444 >>> print(ms.mint.linalg.matrix_norm(A, ord='nuc')) 24.364643 >>> print(ms.mint.linalg.matrix_norm(A, ord=float('inf'))) 38.0 >>> print(ms.mint.linalg.matrix_norm(A, ord=float('-inf'))) 6.0