Calculate the p-order vector norm for certain dimension of Tensor `input`. Returns the vector norm (the 1-norm, the Euclidean or 2-norm, and in general the p-norm) of a given tensor. .. note:: Alias Support: The parameter name ``ord`` can be used as an alias for ``p``, and
(
x: Tensor,
p: float = 2.0,
axis: int | Sequence[int] | None = None,
keepdim: bool = False,
name: str | None = None,
*,
dtype: paddle._typing.DTypeLike | None = None,
out: Tensor | None = None,
)
| 396 | |
| 397 | @param_two_alias(["p", "ord"], ["axis", "dim"]) |
| 398 | def vector_norm( |
| 399 | x: Tensor, |
| 400 | p: float = 2.0, |
| 401 | axis: int | Sequence[int] | None = None, |
| 402 | keepdim: bool = False, |
| 403 | name: str | None = None, |
| 404 | *, |
| 405 | dtype: paddle._typing.DTypeLike | None = None, |
| 406 | out: Tensor | None = None, |
| 407 | ) -> Tensor: |
| 408 | """ |
| 409 | Calculate the p-order vector norm for certain dimension of Tensor `input`. |
| 410 | Returns the vector norm (the 1-norm, the Euclidean or 2-norm, and in general the p-norm) |
| 411 | of a given tensor. |
| 412 | |
| 413 | .. note:: |
| 414 | Alias Support: The parameter name ``ord`` can be used as an alias for ``p``, and ``dim`` can be used as an alias for ``axis``. |
| 415 | |
| 416 | Args: |
| 417 | x (Tensor): Tensor, data type float32, float64. |
| 418 | p (int|float, optional): None for porder=2.0. Default None. |
| 419 | axis (int|list|tuple, optional): None for last dimension. Default None. |
| 420 | keepdim (bool, optional): Whether keep the dimensions as the `input`, Default False. |
| 421 | name (str|None, optional): The default value is None. Normally there is no need for |
| 422 | user to set this property. For more information, please refer to :ref:`api_guide_Name`. |
| 423 | dtype (paddle._typing.DTypeLike, optional): It may be used to perform the computation in a more precise dtype. It is semantically equivalent to calling linalg.vector_norm(x.to(dtype)) but it is faster in some cases. Default None. |
| 424 | out (Tensor| None, optional): output tensor. Ignored if None. Default: None. |
| 425 | |
| 426 | Returns: |
| 427 | Tensor: results of vector_norm operation on the specified axis of input tensor, |
| 428 | it's data type is the same as input's Tensor. |
| 429 | |
| 430 | Examples: |
| 431 | .. code-block:: pycon |
| 432 | |
| 433 | >>> import paddle |
| 434 | >>> import numpy as np |
| 435 | >>> x = paddle.arange(24, dtype="float32").reshape([2, 3, 4]) - 12 |
| 436 | >>> print(x) |
| 437 | Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, |
| 438 | [[[-12., -11., -10., -9. ], |
| 439 | [-8. , -7. , -6. , -5. ], |
| 440 | [-4. , -3. , -2. , -1. ]], |
| 441 | [[ 0. , 1. , 2. , 3. ], |
| 442 | [ 4. , 5. , 6. , 7. ], |
| 443 | [ 8. , 9. , 10., 11.]]]) |
| 444 | >>> out_vector_norm = paddle.linalg.vector_norm(x=x, p=2, axis=None, keepdim=False) |
| 445 | >>> print(out_vector_norm) |
| 446 | Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, |
| 447 | 34.) |
| 448 | >>> out_vector_norm = paddle.linalg.vector_norm(x=x, p=0, axis=[0, 1], keepdim=False) |
| 449 | >>> print(out_vector_norm) |
| 450 | Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True, |
| 451 | [5., 6., 6., 6.]) |
| 452 | >>> out_vector_norm = paddle.linalg.vector_norm(x=x, p=float("inf"), axis=[1, 2], keepdim=False) |
| 453 | >>> print(out_vector_norm) |
| 454 | Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True, |
| 455 | [12., 11.]) |
no test coverage detected