2022-07-29 23:35:12 +08:00
|
|
|
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
|
2020-08-12 17:08:55 +08:00
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
2024-06-14 01:46:08 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2024-06-29 11:36:39 +08:00
|
|
|
from typing import TYPE_CHECKING
|
2024-06-14 01:46:08 +08:00
|
|
|
|
2025-11-13 10:56:17 +08:00
|
|
|
from paddle.utils.decorator_utils import param_one_alias, param_two_alias
|
|
|
|
|
|
2022-07-29 23:35:12 +08:00
|
|
|
from .. import functional as F
|
2023-03-20 15:33:37 +08:00
|
|
|
from .layers import Layer
|
2020-08-12 17:08:55 +08:00
|
|
|
|
2024-06-29 11:36:39 +08:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
import paddle
|
|
|
|
|
|
2021-04-29 19:31:40 +08:00
|
|
|
__all__ = []
|
|
|
|
|
|
2020-08-12 17:08:55 +08:00
|
|
|
|
2021-07-26 14:41:27 +08:00
|
|
|
class PairwiseDistance(Layer):
|
2020-11-24 14:53:51 +08:00
|
|
|
r"""
|
[Docs]fix math api en docs issue (#47448)
* fix_docx_stanh
* fix einsum api en docs issue
* fix model api en docs issue
* for codestyle
* fix_einsum.py_einsum, test=document_fix
* fix_model.py_Model, test=ducument_fix
* fix_creation.py_meshgrid, test=document_fix
* fix_linalg.py_slogdet, test=document_fix
* fix_loss.py_SoftMarginLoss_CrossEntropyLoss_NLLLoss_BCELoss, test=document_fix
* norm.py_SyncBatchNorm, test=document-fix
* norm.py_SyncBatchNorm, test=document_fix
* norm.py_SyncBatchNorm, test=document_fix
* list18-30, test=document_fix
* refix_list1-15, test=document_fix
* deletefiles, test=document_fix
* fixedapi_pre-commit, test=document_fix
* fix_list31-45, test=document_fix
* list111, test=document_fix
* some_fix, test=document_fix
* some_fix, test=document_fix
* somefix, test=document_fix
* somefix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* rerfix, test=document_fix
Co-authored-by: Ligoml <limengliu@tiaozhan.com>
2022-11-22 17:31:35 +08:00
|
|
|
|
2022-07-29 23:35:12 +08:00
|
|
|
It computes the pairwise distance between two vectors. The
|
[CodeStyle][Typos][O-[15-20],O-[24-26]] Fix typos(`optin`, `Optin`, `rder`, `oreder`, `orignal`, `orginal`, `onces`, `outter`, `outpus`, `overriden`, `Overide`, `overide`, `overrided`) (#70759)
2025-01-17 19:59:27 +08:00
|
|
|
distance is calculated by p-order norm:
|
2020-08-12 17:08:55 +08:00
|
|
|
|
|
|
|
|
.. math::
|
|
|
|
|
|
|
|
|
|
\Vert x \Vert _p = \left( \sum_{i=1}^n \vert x_i \vert ^ p \right) ^ {1/p}.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
2022-07-29 23:35:12 +08:00
|
|
|
p (float, optional): The order of norm. Default: :math:`2.0`.
|
|
|
|
|
epsilon (float, optional): Add small value to avoid division by zero.
|
|
|
|
|
Default: :math:`1e-6`.
|
2020-08-12 17:08:55 +08:00
|
|
|
keepdim (bool, optional): Whether to reserve the reduced dimension
|
|
|
|
|
in the output Tensor. The result tensor is one dimension less than
|
2022-07-29 23:35:12 +08:00
|
|
|
the result of ``|x-y|`` unless :attr:`keepdim` is True. Default: False.
|
|
|
|
|
name (str, optional): For details, please refer to :ref:`api_guide_Name`.
|
|
|
|
|
Generally, no setting is required. Default: None.
|
2020-08-12 17:08:55 +08:00
|
|
|
|
|
|
|
|
Shape:
|
[Docs]fix math api en docs issue (#47448)
* fix_docx_stanh
* fix einsum api en docs issue
* fix model api en docs issue
* for codestyle
* fix_einsum.py_einsum, test=document_fix
* fix_model.py_Model, test=ducument_fix
* fix_creation.py_meshgrid, test=document_fix
* fix_linalg.py_slogdet, test=document_fix
* fix_loss.py_SoftMarginLoss_CrossEntropyLoss_NLLLoss_BCELoss, test=document_fix
* norm.py_SyncBatchNorm, test=document-fix
* norm.py_SyncBatchNorm, test=document_fix
* norm.py_SyncBatchNorm, test=document_fix
* list18-30, test=document_fix
* refix_list1-15, test=document_fix
* deletefiles, test=document_fix
* fixedapi_pre-commit, test=document_fix
* fix_list31-45, test=document_fix
* list111, test=document_fix
* some_fix, test=document_fix
* some_fix, test=document_fix
* somefix, test=document_fix
* somefix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* rerfix, test=document_fix
Co-authored-by: Ligoml <limengliu@tiaozhan.com>
2022-11-22 17:31:35 +08:00
|
|
|
- x: :math:`[N, D]` or :math:`[D]`, where :math:`N` is batch size, :math:`D`
|
2023-02-27 14:56:43 +08:00
|
|
|
is the dimension of the data. Available data type is float16, float32, float64.
|
[Docs]fix math api en docs issue (#47448)
* fix_docx_stanh
* fix einsum api en docs issue
* fix model api en docs issue
* for codestyle
* fix_einsum.py_einsum, test=document_fix
* fix_model.py_Model, test=ducument_fix
* fix_creation.py_meshgrid, test=document_fix
* fix_linalg.py_slogdet, test=document_fix
* fix_loss.py_SoftMarginLoss_CrossEntropyLoss_NLLLoss_BCELoss, test=document_fix
* norm.py_SyncBatchNorm, test=document-fix
* norm.py_SyncBatchNorm, test=document_fix
* norm.py_SyncBatchNorm, test=document_fix
* list18-30, test=document_fix
* refix_list1-15, test=document_fix
* deletefiles, test=document_fix
* fixedapi_pre-commit, test=document_fix
* fix_list31-45, test=document_fix
* list111, test=document_fix
* some_fix, test=document_fix
* some_fix, test=document_fix
* somefix, test=document_fix
* somefix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* rerfix, test=document_fix
Co-authored-by: Ligoml <limengliu@tiaozhan.com>
2022-11-22 17:31:35 +08:00
|
|
|
- y: :math:`[N, D]` or :math:`[D]`, y have the same dtype as x.
|
|
|
|
|
- output: The same dtype as input tensor.
|
2022-07-29 23:35:12 +08:00
|
|
|
- If :attr:`keepdim` is True, the output shape is :math:`[N, 1]` or :math:`[1]`,
|
[Docs]fix math api en docs issue (#47448)
* fix_docx_stanh
* fix einsum api en docs issue
* fix model api en docs issue
* for codestyle
* fix_einsum.py_einsum, test=document_fix
* fix_model.py_Model, test=ducument_fix
* fix_creation.py_meshgrid, test=document_fix
* fix_linalg.py_slogdet, test=document_fix
* fix_loss.py_SoftMarginLoss_CrossEntropyLoss_NLLLoss_BCELoss, test=document_fix
* norm.py_SyncBatchNorm, test=document-fix
* norm.py_SyncBatchNorm, test=document_fix
* norm.py_SyncBatchNorm, test=document_fix
* list18-30, test=document_fix
* refix_list1-15, test=document_fix
* deletefiles, test=document_fix
* fixedapi_pre-commit, test=document_fix
* fix_list31-45, test=document_fix
* list111, test=document_fix
* some_fix, test=document_fix
* some_fix, test=document_fix
* somefix, test=document_fix
* somefix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* rerfix, test=document_fix
Co-authored-by: Ligoml <limengliu@tiaozhan.com>
2022-11-22 17:31:35 +08:00
|
|
|
depending on whether the input has data shaped as :math:`[N, D]`.
|
2022-07-29 23:35:12 +08:00
|
|
|
- If :attr:`keepdim` is False, the output shape is :math:`[N]` or :math:`[]`,
|
[Docs]fix math api en docs issue (#47448)
* fix_docx_stanh
* fix einsum api en docs issue
* fix model api en docs issue
* for codestyle
* fix_einsum.py_einsum, test=document_fix
* fix_model.py_Model, test=ducument_fix
* fix_creation.py_meshgrid, test=document_fix
* fix_linalg.py_slogdet, test=document_fix
* fix_loss.py_SoftMarginLoss_CrossEntropyLoss_NLLLoss_BCELoss, test=document_fix
* norm.py_SyncBatchNorm, test=document-fix
* norm.py_SyncBatchNorm, test=document_fix
* norm.py_SyncBatchNorm, test=document_fix
* list18-30, test=document_fix
* refix_list1-15, test=document_fix
* deletefiles, test=document_fix
* fixedapi_pre-commit, test=document_fix
* fix_list31-45, test=document_fix
* list111, test=document_fix
* some_fix, test=document_fix
* some_fix, test=document_fix
* somefix, test=document_fix
* somefix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* refix, test=document_fix
* rerfix, test=document_fix
Co-authored-by: Ligoml <limengliu@tiaozhan.com>
2022-11-22 17:31:35 +08:00
|
|
|
depending on whether the input has data shaped as :math:`[N, D]`.
|
2020-08-12 17:08:55 +08:00
|
|
|
|
|
|
|
|
Examples:
|
2026-02-08 20:48:52 +08:00
|
|
|
.. code-block:: pycon
|
2020-08-12 17:08:55 +08:00
|
|
|
|
2023-07-18 11:28:40 +08:00
|
|
|
>>> import paddle
|
2026-02-08 20:48:52 +08:00
|
|
|
>>> x = paddle.to_tensor([[1.0, 3.0], [3.0, 5.0]], dtype=paddle.float64)
|
|
|
|
|
>>> y = paddle.to_tensor([[5.0, 6.0], [7.0, 8.0]], dtype=paddle.float64)
|
2023-07-18 11:28:40 +08:00
|
|
|
>>> dist = paddle.nn.PairwiseDistance()
|
|
|
|
|
>>> distance = dist(x, y)
|
|
|
|
|
>>> print(distance)
|
|
|
|
|
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True,
|
|
|
|
|
[4.99999860, 4.99999860])
|
2020-08-12 17:08:55 +08:00
|
|
|
"""
|
|
|
|
|
|
2025-11-13 10:56:17 +08:00
|
|
|
@param_one_alias(["epsilon", "eps"])
|
2024-06-14 01:46:08 +08:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
p: float = 2.0,
|
|
|
|
|
epsilon: float = 1e-6,
|
|
|
|
|
keepdim: bool = False,
|
|
|
|
|
name: str | None = None,
|
|
|
|
|
):
|
2022-11-03 14:33:00 +08:00
|
|
|
super().__init__()
|
2020-08-12 17:08:55 +08:00
|
|
|
self.p = p
|
2020-08-13 09:43:16 +08:00
|
|
|
self.epsilon = epsilon
|
2020-08-12 17:08:55 +08:00
|
|
|
self.keepdim = keepdim
|
|
|
|
|
self.name = name
|
|
|
|
|
|
2025-11-13 10:56:17 +08:00
|
|
|
@param_two_alias(["x", "x1"], ["y", "x2"])
|
2024-06-14 01:46:08 +08:00
|
|
|
def forward(self, x: paddle.Tensor, y: paddle.Tensor) -> paddle.Tensor:
|
2022-10-23 20:01:27 +08:00
|
|
|
return F.pairwise_distance(
|
|
|
|
|
x, y, self.p, self.epsilon, self.keepdim, self.name
|
|
|
|
|
)
|
2021-01-06 20:55:33 +08:00
|
|
|
|
2024-06-14 01:46:08 +08:00
|
|
|
def extra_repr(self) -> str:
|
2021-01-06 20:55:33 +08:00
|
|
|
main_str = 'p={p}'
|
|
|
|
|
if self.epsilon != 1e-6:
|
|
|
|
|
main_str += ', epsilon={epsilon}'
|
2022-11-01 17:12:28 +08:00
|
|
|
if self.keepdim is not False:
|
2021-01-06 20:55:33 +08:00
|
|
|
main_str += ', keepdim={keepdim}'
|
2022-11-01 22:14:52 +08:00
|
|
|
if self.name is not None:
|
2021-01-06 20:55:33 +08:00
|
|
|
main_str += ', name={name}'
|
|
|
|
|
return main_str.format(**self.__dict__)
|
2025-11-13 10:56:17 +08:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def eps(self) -> float:
|
|
|
|
|
return self.epsilon
|
|
|
|
|
|
|
|
|
|
@eps.setter
|
|
|
|
|
def eps(self, value: float) -> None:
|
|
|
|
|
self.epsilon = value
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def norm(self) -> float:
|
|
|
|
|
return self.p
|
|
|
|
|
|
|
|
|
|
@norm.setter
|
|
|
|
|
def norm(self, value: float) -> None:
|
|
|
|
|
self.p = value
|