Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
7165f484
P
Paddle
项目概览
PaddlePaddle
/
Paddle
12 个月 前同步成功
通知
2292
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
7165f484
编写于
8月 13, 2020
作者:
Z
Zhong Hui
提交者:
GitHub
8月 13, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change api name eps to epsilon for the pair_distance
change api name eps to epsilon for the pair_distance
上级
dea41da7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
13 addition
and
14 deletion
+13
-14
python/paddle/fluid/tests/unittests/test_pairwise_distance.py
...on/paddle/fluid/tests/unittests/test_pairwise_distance.py
+5
-5
python/paddle/nn/layer/distance.py
python/paddle/nn/layer/distance.py
+8
-9
未找到文件。
python/paddle/fluid/tests/unittests/test_pairwise_distance.py
浏览文件 @
7165f484
...
...
@@ -20,11 +20,11 @@ import numpy as np
import
unittest
def
pairwise_distance
(
x
,
y
,
p
=
2.0
,
eps
=
1e-6
,
keepdim
=
False
):
def
pairwise_distance
(
x
,
y
,
p
=
2.0
,
eps
ilon
=
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
):
def
test_static
(
x_np
,
y_np
,
p
=
2.0
,
eps
ilon
=
1e-6
,
keepdim
=
False
):
prog
=
paddle
.
static
.
Program
()
startup_prog
=
paddle
.
static
.
Program
()
...
...
@@ -35,7 +35,7 @@ def test_static(x_np, y_np, p=2.0, eps=1e-6, keepdim=False):
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
)
p
=
p
,
eps
ilon
=
epsilon
,
keepdim
=
keepdim
)
distance
=
dist
(
x
,
y
)
exe
=
paddle
.
static
.
Executor
(
place
)
static_ret
=
exe
.
run
(
prog
,
...
...
@@ -46,12 +46,12 @@ def test_static(x_np, y_np, p=2.0, eps=1e-6, keepdim=False):
return
static_ret
def
test_dygraph
(
x_np
,
y_np
,
p
=
2.0
,
eps
=
1e-6
,
keepdim
=
False
):
def
test_dygraph
(
x_np
,
y_np
,
p
=
2.0
,
eps
ilon
=
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
)
p
=
p
,
eps
ilon
=
epsilon
,
keepdim
=
keepdim
)
distance
=
dist
(
x
,
y
)
dygraph_ret
=
distance
.
numpy
()
paddle
.
enable_static
()
...
...
python/paddle/nn/layer/distance.py
浏览文件 @
7165f484
...
...
@@ -34,7 +34,7 @@ class PairwiseDistance(layers.Layer):
Parameters:
p (float): The order of norm. The default value is 2.
eps (float, optional): Add small value to avoid division by zero,
eps
ilon
(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
...
...
@@ -66,21 +66,21 @@ class PairwiseDistance(layers.Layer):
"""
def
__init__
(
self
,
p
=
2.
,
eps
=
1e-6
,
keepdim
=
False
,
name
=
None
):
def
__init__
(
self
,
p
=
2.
,
eps
ilon
=
1e-6
,
keepdim
=
False
,
name
=
None
):
super
(
PairwiseDistance
,
self
).
__init__
()
self
.
p
=
p
self
.
eps
=
eps
self
.
eps
ilon
=
epsilon
self
.
keepdim
=
keepdim
self
.
name
=
name
check_type
(
self
.
p
,
'porder'
,
(
float
,
int
),
'PairwiseDistance'
)
check_type
(
self
.
eps
,
'epsilon'
,
(
float
),
'PairwiseDistance'
)
check_type
(
self
.
eps
ilon
,
'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
)
self
.
keepdim
,
'epsilon'
,
self
.
eps
ilon
)
check_variable_and_dtype
(
x
,
'x'
,
[
'float32'
,
'float64'
],
'PairwiseDistance'
)
...
...
@@ -88,15 +88,14 @@ class PairwiseDistance(layers.Layer):
'PairwiseDistance'
)
sub
=
paddle
.
elementwise_sub
(
x
,
y
)
helper
=
LayerHelper
(
"
p_norm
"
,
name
=
self
.
name
)
helper
=
LayerHelper
(
"
PairwiseDistance
"
,
name
=
self
.
name
)
attrs
=
{
'axis'
:
1
,
'porder'
:
self
.
p
,
'keepdim'
:
self
.
keepdim
,
'epsilon'
:
self
.
eps
,
'epsilon'
:
self
.
eps
ilon
,
}
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
self
.
_helper
.
input_dtype
(
x
))
out
=
helper
.
create_variable_for_type_inference
(
dtype
=
x
.
dtype
)
helper
.
append_op
(
type
=
'p_norm'
,
inputs
=
{
'X'
:
sub
},
outputs
=
{
'Out'
:
out
},
attrs
=
attrs
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录