r"""Performs :math:`L_p` normalization of input tensor along given axis. For a tensor of shape :math:`(n_0, ..., n_{dim}, ..., n_k)`, each :math:`n_{dim}` -element vector :math:`v` along dimension :attr:`axis` is transformed as: .. math:: v = \frac{v}{\max(\lVert v \rVert_p, \
(
inp: Tensor, ord: float = None, axis: int = None, eps: float = 1e-12,
)
| 964 | |
| 965 | |
| 966 | def normalize( |
| 967 | inp: Tensor, ord: float = None, axis: int = None, eps: float = 1e-12, |
| 968 | ) -> Tensor: |
| 969 | r"""Performs :math:`L_p` normalization of input tensor along given axis. |
| 970 | |
| 971 | For a tensor of shape :math:`(n_0, ..., n_{dim}, ..., n_k)`, |
| 972 | each :math:`n_{dim}` -element vector :math:`v` along dimension :attr:`axis` is transformed as: |
| 973 | |
| 974 | .. math:: |
| 975 | v = \frac{v}{\max(\lVert v \rVert_p, \epsilon)}. |
| 976 | |
| 977 | Args: |
| 978 | inp: input tensor. |
| 979 | ord: power of value applied to input tensor. |
| 980 | axis: dimension to reduce.If None, input must be a vector. |
| 981 | eps: a small value to avoid division by zero. |
| 982 | |
| 983 | Returns: |
| 984 | normalized output tensor. |
| 985 | |
| 986 | .. seealso:: :func:`numpy.linalg.norm` / :func:`~.functional.norm` |
| 987 | |
| 988 | Examples: |
| 989 | |
| 990 | >>> x = Tensor([[1, 2, 3], [4, 5, 6]]) |
| 991 | >>> F.normalize(x, ord=2, axis=0) |
| 992 | Tensor([[0.2425 0.3714 0.4472] |
| 993 | [0.9701 0.9285 0.8944]], device=xpux:0) |
| 994 | >>> F.normalize(x, ord=2, axis=1) |
| 995 | Tensor([[0.2673 0.5345 0.8018] |
| 996 | [0.4558 0.5698 0.6838]], device=xpux:0) |
| 997 | """ |
| 998 | if axis is None: |
| 999 | return inp / clip(norm(inp, ord, axis), lower=eps) |
| 1000 | else: |
| 1001 | return inp / clip(norm(inp, ord, axis, keepdims=True), lower=eps) |
| 1002 | |
| 1003 | |
| 1004 | def _check_non_finite(inps: Iterable[Tensor], scale=1.0) -> Tensor: |
no test coverage detected