pytorch / 2 / generated / torch.nn.functional.normalize.html

torch.nn.functional.normalize

torch.nn.functional.normalize(input, p=2.0, dim=1, eps=1e-12, out=None) [source]

Performs L p L_p normalization of inputs over specified dimension.

For a tensor input of sizes ( n 0 , . . . , n d i m , . . . , n k ) (n_0, ..., n_{dim}, ..., n_k) , each n d i m n_{dim} -element vector v v along dimension dim is transformed as

v = v max ( v p , ϵ ) . v = \frac{v}{\max(\lVert v \rVert_p, \epsilon)}.

With the default arguments it uses the Euclidean norm over vectors along dimension 1 1 for normalization.

Parameters
  • input (Tensor) – input tensor of any shape
  • p (float) – the exponent value in the norm formulation. Default: 2
  • dim (int or tuple of ints) – the dimension to reduce. Default: 1
  • eps (float) – small value to avoid division by zero. Default: 1e-12
  • out (Tensor, optional) – the output tensor. If out is used, this operation won’t be differentiable.
Return type

Tensor

© 2024, PyTorch Contributors
PyTorch has a BSD-style license, as found in the LICENSE file.
https://pytorch.org/docs/2.1/generated/torch.nn.functional.normalize.html