On this page
torch.linalg.matrix_norm
torch.linalg.matrix_norm(A, ord='fro', dim=(-2, -1), keepdim=False, *, dtype=None, out=None) → Tensor-
Computes a matrix norm.
If
Ais complex valued, it computes the norm ofA.abs()Support input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices: the norm will be computed over the dimensions specified by the 2-tuple
dimand the other dimensions will be treated as batch dimensions. The output will have the same batch dimensions.orddefines the matrix norm that is computed. The following norms are supported:ordmatrix norm
‘fro’(default)Frobenius norm
‘nuc’nuclear norm
infmax(sum(abs(x), dim=1))-infmin(sum(abs(x), dim=1))1max(sum(abs(x), dim=0))-1min(sum(abs(x), dim=0))2largest singular value
-2smallest singular value
where
infrefers tofloat(‘inf’), NumPy’sinfobject, or any equivalent object.- Parameters
-
- A (Tensor) – tensor with two or more dimensions. By default its shape is interpreted as
(*, m, n)where*is zero or more batch dimensions, but this behavior can be controlled usingdim. - ord (int, inf, -inf, 'fro', 'nuc', optional) – order of norm. Default:
‘fro’ - dim (Tuple[int, int], optional) – dimensions over which to compute the norm. Default:
(-2, -1) - keepdim (bool, optional) – If set to
True, the reduced dimensions are retained in the result as dimensions with size one. Default:False
- A (Tensor) – tensor with two or more dimensions. By default its shape is interpreted as
- Keyword Arguments
-
- out (Tensor, optional) – output tensor. Ignored if
None. Default:None. - dtype (
torch.dtype, optional) – If specified, the input tensor is cast todtypebefore performing the operation, and the returned tensor’s type will bedtype. Default:None
- out (Tensor, optional) – output tensor. Ignored if
- Returns
-
A real-valued tensor, even when
Ais complex.
Examples:
>>> from torch import linalg as LA >>> A = torch.arange(9, dtype=torch.float).reshape(3, 3) >>> A tensor([[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]]) >>> LA.matrix_norm(A) tensor(14.2829) >>> LA.matrix_norm(A, ord=-1) tensor(9.) >>> B = A.expand(2, -1, -1) >>> B tensor([[[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]], [[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]]]) >>> LA.matrix_norm(B) tensor([14.2829, 14.2829]) >>> LA.matrix_norm(B, dim=(0, 2)) tensor([ 3.1623, 10.0000, 17.2627])
© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.linalg.matrix_norm.html