Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0d1a62e9
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2305
Star
20932
Fork
5423
代码
文件
提交
分支
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看板
未验证
提交
0d1a62e9
编写于
8月 21, 2020
作者:
L
littletomatodonkey
提交者:
GitHub
8月 21, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test=develop rename CosSim interface (#26522)
上级
dbf232a9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
26 addition
and
7 deletion
+26
-7
python/paddle/fluid/tests/unittests/test_cosine_similarity_api.py
...addle/fluid/tests/unittests/test_cosine_similarity_api.py
+19
-0
python/paddle/nn/layer/common.py
python/paddle/nn/layer/common.py
+7
-7
未找到文件。
python/paddle/fluid/tests/unittests/test_cosine_similarity_api.py
浏览文件 @
0d1a62e9
...
...
@@ -116,6 +116,25 @@ class TestCosineSimilarityAPI(unittest.TestCase):
self
.
assertTrue
(
np
.
allclose
(
y
.
numpy
(),
np_out
))
def
test_dygraph_4
(
self
):
paddle
.
disable_static
()
shape1
=
[
23
,
12
,
1
]
shape2
=
[
23
,
1
,
10
]
axis
=
2
eps
=
1e-6
np
.
random
.
seed
(
1
)
np_x1
=
np
.
random
.
rand
(
*
shape1
).
astype
(
np
.
float32
)
np_x2
=
np
.
random
.
rand
(
*
shape2
).
astype
(
np
.
float32
)
np_out
=
self
.
_get_numpy_out
(
np_x1
,
np_x2
,
axis
=
axis
,
eps
=
eps
)
cos_sim_func
=
nn
.
CosineSimilarity
(
axis
=
axis
,
eps
=
eps
)
tesnor_x1
=
paddle
.
to_variable
(
np_x1
)
tesnor_x2
=
paddle
.
to_variable
(
np_x2
)
y
=
cos_sim_func
(
tesnor_x1
,
tesnor_x2
)
self
.
assertTrue
(
np
.
allclose
(
y
.
numpy
(),
np_out
))
if
__name__
==
'__main__'
:
unittest
.
main
()
python/paddle/nn/layer/common.py
浏览文件 @
0d1a62e9
...
...
@@ -913,10 +913,10 @@ class ReplicationPad3d(layers.Layer):
class
CosineSimilarity
(
layers
.
Layer
):
"""
This interface is used to compute cosine similarity between x1 and x2 along
dim
.
This interface is used to compute cosine similarity between x1 and x2 along
axis
.
Parameters:
dim
(int): Dimension of vectors to compute cosine similarity. Default is 1.
axis
(int): Dimension of vectors to compute cosine similarity. Default is 1.
eps(float): Small value to avoid division by zero. Default is 1e-8.
Returns:
None
...
...
@@ -933,7 +933,7 @@ class CosineSimilarity(layers.Layer):
[0.9098952 0.15715368 0.8671125 0.3156102 ]
[0.4427798 0.54136837 0.5276275 0.32394758]
[0.3769419 0.8535014 0.48041078 0.9256797 ]]
dim
= 1
axis
= 1
eps = 1e-8
Out: [0.5275037 0.8368967 0.75037485 0.9245899]
...
...
@@ -951,16 +951,16 @@ class CosineSimilarity(layers.Layer):
x1 = paddle.to_tensor(x1)
x2 = paddle.to_tensor(x2)
cos_sim_func = nn.CosineSimilarity(
dim
=0)
cos_sim_func = nn.CosineSimilarity(
axis
=0)
result = cos_sim_func(x1, x2)
print(result.numpy())
# [0.99806249 0.9817672 0.94987036]
"""
def
__init__
(
self
,
dim
=
1
,
eps
=
1e-8
):
def
__init__
(
self
,
axis
=
1
,
eps
=
1e-8
):
super
(
CosineSimilarity
,
self
).
__init__
()
self
.
_
dim
=
dim
self
.
_
axis
=
axis
self
.
_eps
=
eps
def
forward
(
self
,
x1
,
x2
):
return
F
.
cosine_similarity
(
x1
,
x2
,
dim
=
self
.
_dim
,
eps
=
self
.
_eps
)
return
F
.
cosine_similarity
(
x1
,
x2
,
axis
=
self
.
_axis
,
eps
=
self
.
_eps
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录