On this page
torch.linalg.norm
torch.linalg.norm(A, ord=None, dim=None, keepdim=False, *, out=None, dtype=None) → Tensor-
Computes a vector or matrix norm.
Supports input of float, double, cfloat and cdouble dtypes.
Whether this function computes a vector or matrix norm is determined as follows:
- If
dimis anint, the vector norm will be computed. - If
dimis a2-tuple, the matrix norm will be computed. - If
dim= Noneandord= None,Awill be flattened to 1D and the2-norm of the resulting vector will be computed. - If
dim= Noneandord!= None,Amust be 1D or 2D.
orddefines the norm that is computed. The following norms are supported:ordnorm for matrices
norm for vectors
None(default)Frobenius norm
2-norm (see below)‘fro’Frobenius norm
– not supported –
‘nuc’nuclear norm
– not supported –
infmax(sum(abs(x), dim=1))max(abs(x))-infmin(sum(abs(x), dim=1))min(abs(x))0– not supported –
sum(x != 0)1max(sum(abs(x), dim=0))as below
-1min(sum(abs(x), dim=0))as below
2largest singular value
as below
-2smallest singular value
as below
other
intorfloat– not supported –
sum(abs(x)^{ord})^{(1 / ord)}where
infrefers tofloat(‘inf’), NumPy’sinfobject, or any equivalent object.See also
torch.linalg.vector_norm()computes a vector norm.torch.linalg.matrix_norm()computes a matrix norm.The above functions are often clearer and more flexible than using
torch.linalg.norm(). For example,torch.linalg.norm(A, ord=1, dim=(0, 1))always computes a matrix norm, but withtorch.linalg.vector_norm(A, ord=1, dim=(0, 1))it is possible to compute a vector norm over the two dimensions.- Parameters
-
- A (Tensor) – tensor of shape
(*, n)or(*, m, n)where*is zero or more batch dimensions - ord (int, float, inf, -inf, 'fro', 'nuc', optional) – order of norm. Default:
None - dim (int, Tuple[int], optional) – dimensions over which to compute the vector or matrix norm. See above for the behavior when
dim= None. Default:None - 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 of shape
- 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) - 4 >>> a tensor([-4., -3., -2., -1., 0., 1., 2., 3., 4.]) >>> B = a.reshape((3, 3)) >>> B tensor([[-4., -3., -2.], [-1., 0., 1.], [ 2., 3., 4.]]) >>> LA.norm(a) tensor(7.7460) >>> LA.norm(B) tensor(7.7460) >>> LA.norm(B, 'fro') tensor(7.7460) >>> LA.norm(a, float('inf')) tensor(4.) >>> LA.norm(B, float('inf')) tensor(9.) >>> LA.norm(a, -float('inf')) tensor(0.) >>> LA.norm(B, -float('inf')) tensor(2.) >>> LA.norm(a, 1) tensor(20.) >>> LA.norm(B, 1) tensor(7.) >>> LA.norm(a, -1) tensor(0.) >>> LA.norm(B, -1) tensor(6.) >>> LA.norm(a, 2) tensor(7.7460) >>> LA.norm(B, 2) tensor(7.3485) >>> LA.norm(a, -2) tensor(0.) >>> LA.norm(B.double(), -2) tensor(1.8570e-16, dtype=torch.float64) >>> LA.norm(a, 3) tensor(5.8480) >>> LA.norm(a, -3) tensor(0.)Using the
dimargument to compute vector norms:>>> c = torch.tensor([[1., 2., 3.], ... [-1, 1, 4]]) >>> LA.norm(c, dim=0) tensor([1.4142, 2.2361, 5.0000]) >>> LA.norm(c, dim=1) tensor([3.7417, 4.2426]) >>> LA.norm(c, ord=1, dim=1) tensor([6., 6.])Using the
dimargument to compute matrix norms:>>> A = torch.arange(8, dtype=torch.float).reshape(2, 2, 2) >>> LA.norm(A, dim=(1,2)) tensor([ 3.7417, 11.2250]) >>> LA.norm(A[0, :, :]), LA.norm(A[1, :, :]) (tensor(3.7417), tensor(11.2250)) - If
© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.linalg.norm.html