Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
361363c3
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
361363c3
编写于
8月 12, 2020
作者:
Z
Zhong Hui
提交者:
GitHub
8月 12, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add pairewise distance for the paddlepaddle api 2.0
add pairewise distance for the paddlepaddle api 2.0
上级
1d870c44
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
215 addition
and
0 deletion
+215
-0
python/paddle/fluid/tests/unittests/test_pairwise_distance.py
...on/paddle/fluid/tests/unittests/test_pairwise_distance.py
+109
-0
python/paddle/nn/__init__.py
python/paddle/nn/__init__.py
+1
-0
python/paddle/nn/layer/__init__.py
python/paddle/nn/layer/__init__.py
+2
-0
python/paddle/nn/layer/distance.py
python/paddle/nn/layer/distance.py
+103
-0
未找到文件。
python/paddle/fluid/tests/unittests/test_pairwise_distance.py
0 → 100644
浏览文件 @
361363c3
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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
paddle
import
paddle.fluid
as
fluid
import
numpy
as
np
import
unittest
def
pairwise_distance
(
x
,
y
,
p
=
2.0
,
eps
=
1e-6
,
keepdim
=
False
):
return
np
.
linalg
.
norm
(
x
-
y
,
ord
=
p
,
axis
=
1
,
keepdims
=
keepdim
)
def
test_static
(
x_np
,
y_np
,
p
=
2.0
,
eps
=
1e-6
,
keepdim
=
False
):
prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
place
=
fluid
.
CUDAPlace
(
0
)
if
paddle
.
fluid
.
core
.
is_compiled_with_cuda
(
)
else
fluid
.
CPUPlace
()
with
paddle
.
static
.
program_guard
(
prog
,
startup_prog
):
x
=
paddle
.
data
(
name
=
'x'
,
shape
=
x_np
.
shape
,
dtype
=
x_np
.
dtype
)
y
=
paddle
.
data
(
name
=
'y'
,
shape
=
y_np
.
shape
,
dtype
=
x_np
.
dtype
)
dist
=
paddle
.
nn
.
layer
.
distance
.
PairwiseDistance
(
p
=
p
,
eps
=
eps
,
keepdim
=
keepdim
)
distance
=
dist
(
x
,
y
)
exe
=
paddle
.
static
.
Executor
(
place
)
static_ret
=
exe
.
run
(
prog
,
feed
=
{
'x'
:
x_np
,
'y'
:
y_np
},
fetch_list
=
[
distance
])
static_ret
=
static_ret
[
0
]
return
static_ret
def
test_dygraph
(
x_np
,
y_np
,
p
=
2.0
,
eps
=
1e-6
,
keepdim
=
False
):
paddle
.
disable_static
()
x
=
paddle
.
to_variable
(
x_np
)
y
=
paddle
.
to_variable
(
y_np
)
dist
=
paddle
.
nn
.
layer
.
distance
.
PairwiseDistance
(
p
=
p
,
eps
=
eps
,
keepdim
=
keepdim
)
distance
=
dist
(
x
,
y
)
dygraph_ret
=
distance
.
numpy
()
paddle
.
enable_static
()
return
dygraph_ret
class
TestPairwiseDistance
(
unittest
.
TestCase
):
def
test_pairwise_distance
(
self
):
all_shape
=
[[
100
,
100
],
[
4
,
5
,
6
,
7
]]
dtypes
=
[
'float32'
,
'float64'
]
keeps
=
[
False
,
True
]
for
shape
in
all_shape
:
for
dtype
in
dtypes
:
for
keepdim
in
keeps
:
x_np
=
np
.
random
.
random
(
shape
).
astype
(
dtype
)
y_np
=
np
.
random
.
random
(
shape
).
astype
(
dtype
)
static_ret
=
test_static
(
x_np
,
y_np
,
keepdim
=
keepdim
)
dygraph_ret
=
test_dygraph
(
x_np
,
y_np
,
keepdim
=
keepdim
)
excepted_value
=
pairwise_distance
(
x_np
,
y_np
,
keepdim
=
keepdim
)
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
dygraph_ret
))
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
excepted_value
))
self
.
assertTrue
(
np
.
allclose
(
dygraph_ret
,
excepted_value
))
def
test_pairwise_distance_broadcast
(
self
):
shape_x
=
[
100
,
100
]
shape_y
=
[
100
,
1
]
keepdim
=
False
x_np
=
np
.
random
.
random
(
shape_x
).
astype
(
'float32'
)
y_np
=
np
.
random
.
random
(
shape_y
).
astype
(
'float32'
)
static_ret
=
test_static
(
x_np
,
y_np
,
keepdim
=
keepdim
)
dygraph_ret
=
test_dygraph
(
x_np
,
y_np
,
keepdim
=
keepdim
)
excepted_value
=
pairwise_distance
(
x_np
,
y_np
,
keepdim
=
keepdim
)
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
dygraph_ret
))
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
excepted_value
))
self
.
assertTrue
(
np
.
allclose
(
dygraph_ret
,
excepted_value
))
def
test_pairwise_distance_different_p
(
self
):
shape
=
[
100
,
100
]
keepdim
=
False
p
=
3.0
x_np
=
np
.
random
.
random
(
shape
).
astype
(
'float32'
)
y_np
=
np
.
random
.
random
(
shape
).
astype
(
'float32'
)
static_ret
=
test_static
(
x_np
,
y_np
,
p
=
p
,
keepdim
=
keepdim
)
dygraph_ret
=
test_dygraph
(
x_np
,
y_np
,
p
=
p
,
keepdim
=
keepdim
)
excepted_value
=
pairwise_distance
(
x_np
,
y_np
,
p
=
p
,
keepdim
=
keepdim
)
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
dygraph_ret
))
self
.
assertTrue
(
np
.
allclose
(
static_ret
,
excepted_value
))
self
.
assertTrue
(
np
.
allclose
(
dygraph_ret
,
excepted_value
))
if
__name__
==
"__main__"
:
unittest
.
main
()
python/paddle/nn/__init__.py
浏览文件 @
361363c3
...
...
@@ -93,6 +93,7 @@ from .layer.norm import InstanceNorm #DEFINE_ALIAS
# from .layer.rnn import RNNCell #DEFINE_ALIAS
# from .layer.rnn import GRUCell #DEFINE_ALIAS
# from .layer.rnn import LSTMCell #DEFINE_ALIAS
from
.layer.distance
import
PairwiseDistance
#DEFINE_ALIAS
from
.layer
import
loss
#DEFINE_ALIAS
from
.layer
import
conv
#DEFINE_ALIAS
...
...
python/paddle/nn/layer/__init__.py
浏览文件 @
361363c3
...
...
@@ -20,6 +20,7 @@ from . import conv
from
.
import
extension
from
.
import
activation
from
.
import
norm
from
.
import
distance
from
.activation
import
*
from
.loss
import
*
...
...
@@ -69,3 +70,4 @@ from .norm import InstanceNorm #DEFINE_ALIAS
# from .rnn import RNNCell #DEFINE_ALIAS
# from .rnn import GRUCell #DEFINE_ALIAS
# from .rnn import LSTMCell #DEFINE_ALIAS
from
.distance
import
PairwiseDistance
#DEFINE_ALIAS
python/paddle/nn/layer/distance.py
0 → 100644
浏览文件 @
361363c3
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# 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.
__all__
=
[
'PairwiseDistance'
]
import
numpy
as
np
import
paddle
from
...fluid.dygraph
import
layers
from
...fluid.framework
import
core
,
in_dygraph_mode
from
...fluid.data_feeder
import
check_variable_and_dtype
,
check_type
from
...fluid.layer_helper
import
LayerHelper
class
PairwiseDistance
(
layers
.
Layer
):
"""
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
\v
ert x_i
\v
ert ^ p
\r
ight) ^ {1/p}.
Parameters:
p (float): The order of norm. The default value is 2.
eps (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)
y_np = np.array([[5., 6.], [7., 8.]]).astype(np.float64)
x = paddle.to_variable(x_np)
y = paddle.to_variable(y_np)
dist = paddle.nn.PairwiseDistance()
distance = dist(x, y)
print(distance.numpy()) # [5. 5.]
"""
def
__init__
(
self
,
p
=
2.
,
eps
=
1e-6
,
keepdim
=
False
,
name
=
None
):
super
(
PairwiseDistance
,
self
).
__init__
()
self
.
p
=
p
self
.
eps
=
eps
self
.
keepdim
=
keepdim
self
.
name
=
name
check_type
(
self
.
p
,
'porder'
,
(
float
,
int
),
'PairwiseDistance'
)
check_type
(
self
.
eps
,
'epsilon'
,
(
float
),
'PairwiseDistance'
)
check_type
(
self
.
keepdim
,
'keepdim'
,
(
bool
),
'PairwiseDistance'
)
def
forward
(
self
,
x
,
y
):
if
in_dygraph_mode
():
sub
=
core
.
ops
.
elementwise_sub
(
x
,
y
)
return
core
.
ops
.
p_norm
(
sub
,
'axis'
,
1
,
'porder'
,
self
.
p
,
'keepdim'
,
self
.
keepdim
,
'epsilon'
,
self
.
eps
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
],
'PairwiseDistance'
)
check_variable_and_dtype
(
y
,
'y'
,
[
'float32'
,
'float64'
],
'PairwiseDistance'
)
sub
=
paddle
.
elementwise_sub
(
x
,
y
)
helper
=
LayerHelper
(
"p_norm"
,
name
=
self
.
name
)
attrs
=
{
'axis'
:
1
,
'porder'
:
self
.
p
,
'keepdim'
:
self
.
keepdim
,
'epsilon'
:
self
.
eps
,
}
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
self
.
_helper
.
input_dtype
(
x
))
helper
.
append_op
(
type
=
'p_norm'
,
inputs
=
{
'X'
:
sub
},
outputs
=
{
'Out'
:
out
},
attrs
=
attrs
)
return
out
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录