On this page
torch.linalg.vector_norm
torch.linalg.vector_norm(x, ord=2, dim=None, keepdim=False, *, dtype=None, out=None) → Tensor-
Computes a vector norm.
If
xis complex valued, it computes the norm ofx.abs()Supports input of float, double, cfloat and cdouble dtypes.
This function does not necessarily treat multidimensional
xas a batch of vectors, instead:- If
dim= None,xwill be flattened before the norm is computed. - If
dimis anintor atuple, the norm will be computed over these dimensions and the other dimensions will be treated as batch dimensions.
This behavior is for consistency with
torch.linalg.norm().orddefines the vector norm that is computed. The following norms are supported:ordvector norm
2(default)2-norm (see below)infmax(abs(x))-infmin(abs(x))0sum(x != 0)other
intorfloatsum(abs(x)^{ord})^{(1 / ord)}where
infrefers tofloat(‘inf’), NumPy’sinfobject, or any equivalent object.dtypemay be used to perform the computation in a more precise dtype. It is semantically equivalent to callinglinalg.vector_norm(x.to(dtype))but it is faster in some cases.See also
torch.linalg.matrix_norm()computes a matrix norm.- Parameters
-
- x (Tensor) – tensor, flattened by default, but this behavior can be controlled using
dim. - ord (int, float, inf, -inf, 'fro', 'nuc', optional) – order of norm. Default:
2 - dim (int, Tuple[int], optional) – dimensions over which to compute the 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
- x (Tensor) – tensor, flattened by default, but this behavior can be controlled using
- Keyword Arguments
-
- out (Tensor, optional) – output tensor. Ignored if
None. Default:None. - dtype (
torch.dtype, optional) – type used to perform the accumulation and the return. If specified,xis cast todtypebefore performing the operation, and the returned tensor’s type will bedtypeif real and of its real counterpart if complex.dtypemay be complex ifxis complex, otherwise it must be real.xshould be convertible without narrowing todtype. Default: None
- out (Tensor, optional) – output tensor. Ignored if
- Returns
-
A real-valued tensor, even when
xis 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.vector_norm(a, ord=3.5) tensor(5.4345) >>> LA.vector_norm(B, ord=3.5) tensor(5.4345) - 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.vector_norm.html