Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[内部 review] add paddle.nn.functional.pairwise_distance #273

Merged
merged 14 commits into from
Jul 7, 2022
23 changes: 23 additions & 0 deletions python/paddle/fluid/tests/unittests/test_distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
Ainavo marked this conversation as resolved.
Show resolved Hide resolved
#
# 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.

from __future__ import print_function

import unittest
import paddle
import paddle.nn.functional as F
import paddle.fluid as fluid
import paddle.fluid.core as core
import numpy as np
from paddle.fluid.framework import _test_eager_guard
Ainavo marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions python/paddle/nn/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
from .conv import conv2d_transpose # noqa: F401
from .conv import conv3d # noqa: F401
from .conv import conv3d_transpose # noqa: F401
from .distance import pairwise_distance # noqa: F401
from .extension import diag_embed # noqa: F401
from .extension import sequence_mask
from .loss import binary_cross_entropy # noqa: F401
Expand Down
104 changes: 104 additions & 0 deletions python/paddle/nn/functional/distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
SigureMo marked this conversation as resolved.
Show resolved Hide resolved
#
# 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.

import numpy as np
Ainavo marked this conversation as resolved.
Show resolved Hide resolved

import paddle
from .. import Layer
from ...fluid.data_feeder import check_variable_and_dtype, check_type
from ...fluid.layer_helper import LayerHelper
from paddle import _C_ops
from paddle import in_dynamic_mode
from paddle.fluid.framework import in_dygraph_mode, _in_legacy_dygraph

__all__ = []

def pairwise_distance(x, y, p=2., epsilon=1e-6, keepdim=False, name=None):
r"""
This operator computes the pairwise distance between two vectors. The
distance is calculated by p-oreder norm:

.. math::

\Vert x \Vert _p = \left( \sum_{i=1}^n \vert x_i \vert ^ p \right) ^ {1/p}.

Parameters:
SigureMo marked this conversation as resolved.
Show resolved Hide resolved
p (float): The order of norm. The default value is 2.
epsilon (float, optional): Add small value to avoid division by zero,
default value is 1e-6.
keepdim (bool, optional): Whether to reserve the reduced dimension
in the output Tensor. The result tensor is one dimension less than
the result of ``'x-y'`` unless :attr:`keepdim` is True, default
value is False.
name (str, optional): Name for the operation (optional, default is None).
For more information, please refer to :ref:`api_guide_Name`.

Shape:
x: :math:`[N, D]` where `D` is the dimension of vector, available dtype
is float32, float64.
y: :math:`[N, D]`, y have the same shape and dtype as x.
out: :math:`[N]`. If :attr:`keepdim` is ``True``, the out shape is :math:`[N, 1]`.
The same dtype as input tensor.

Examples:
.. code-block:: python

import paddle
import numpy as np
paddle.disable_static()
x_np = np.array([[1., 3.], [3., 5.]]).astype(np.float64)
Ainavo marked this conversation as resolved.
Show resolved Hide resolved
y_np = np.array([[5., 6.], [7., 8.]]).astype(np.float64)
x = paddle.to_tensor(x_np)
y = paddle.to_tensor(y_np)
dist = paddle.nn.PairwiseDistance()
distance = dist(x, y)
print(distance.numpy()) # [5. 5.]

"""
check_type(p, 'porder', (float, int), 'PairwiseDistance')
check_type(epsilon, 'epsilon', (float), 'PairwiseDistance')
check_type(keepdim, 'keepdim', (bool), 'PairwiseDistance')
if in_dygraph_mode():
sub = _C_ops.elementwise_sub(x, y)
return _C_ops.final_state_p_norm(sub, p, -1, epsilon,
keepdim, False)

if _in_legacy_dygraph():
sub = _C_ops.elementwise_sub(x, y)
return _C_ops.p_norm(sub, 'axis', -1, 'porder', p, 'keepdim',
keepdim, 'epsilon', epsilon)

check_variable_and_dtype(x, 'x', ['float32', 'float64'],
'PairwiseDistance')
check_variable_and_dtype(y, 'y', ['float32', 'float64'],
'PairwiseDistance')
sub = paddle.subtract(x, y)

helper = LayerHelper("PairwiseDistance", name=name)
attrs = {
'axis': -1,
'porder': p,
'keepdim': keepdim,
'epsilon': epsilon,
}
out = helper.create_variable_for_type_inference(dtype=x.dtype)
helper.append_op(type='p_norm',
inputs={'X': sub},
outputs={'Out': out},
attrs=attrs)

return out



1 change: 1 addition & 0 deletions python/paddle/nn/layer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
from .conv import Conv1DTranspose # noqa: F401
from .conv import Conv2DTranspose # noqa: F401
from .conv import Conv3DTranspose # noqa: F401
from .distance import PairwiseDistance # noqa: F401
Ainavo marked this conversation as resolved.
Show resolved Hide resolved
from .loss import BCEWithLogitsLoss # noqa: F401
from .loss import CrossEntropyLoss # noqa: F401
from .loss import MSELoss # noqa: F401
Expand Down
44 changes: 3 additions & 41 deletions python/paddle/nn/layer/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,9 @@

import paddle
from .. import Layer
from ...fluid.data_feeder import check_variable_and_dtype, check_type
from ...fluid.layer_helper import LayerHelper
from paddle import _C_ops
from paddle import in_dynamic_mode
from paddle.fluid.framework import in_dygraph_mode, _in_legacy_dygraph

from .. import functional as F
__all__ = []


class PairwiseDistance(Layer):
r"""
This operator computes the pairwise distance between two vectors. The
Expand Down Expand Up @@ -74,41 +68,9 @@ def __init__(self, p=2., epsilon=1e-6, keepdim=False, name=None):
self.epsilon = epsilon
self.keepdim = keepdim
self.name = name
check_type(self.p, 'porder', (float, int), 'PairwiseDistance')
check_type(self.epsilon, 'epsilon', (float), 'PairwiseDistance')
check_type(self.keepdim, 'keepdim', (bool), 'PairwiseDistance')

def forward(self, x, y):
if in_dygraph_mode():
sub = _C_ops.elementwise_sub(x, y)
return _C_ops.final_state_p_norm(sub, self.p, 1, self.epsilon,
self.keepdim, False)

if _in_legacy_dygraph():
sub = _C_ops.elementwise_sub(x, y)
return _C_ops.p_norm(sub, 'axis', 1, 'porder', self.p, 'keepdim',
self.keepdim, 'epsilon', self.epsilon)

check_variable_and_dtype(x, 'x', ['float32', 'float64'],
'PairwiseDistance')
check_variable_and_dtype(y, 'y', ['float32', 'float64'],
'PairwiseDistance')
sub = paddle.subtract(x, y)

helper = LayerHelper("PairwiseDistance", name=self.name)
attrs = {
'axis': 1,
'porder': self.p,
'keepdim': self.keepdim,
'epsilon': self.epsilon,
}
out = helper.create_variable_for_type_inference(dtype=x.dtype)
helper.append_op(type='p_norm',
inputs={'X': sub},
outputs={'Out': out},
attrs=attrs)

return out
return F.pairwise_distance(x, y, self.p, self.epsilon, self.keepdim, self.name)
SigureMo marked this conversation as resolved.
Show resolved Hide resolved

def extra_repr(self):
main_str = 'p={p}'
Expand All @@ -118,4 +80,4 @@ def extra_repr(self):
main_str += ', keepdim={keepdim}'
if self.name != None:
main_str += ', name={name}'
return main_str.format(**self.__dict__)
return main_str.format(**self.__dict__)