Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
c05af910
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c05af910
编写于
3月 19, 2019
作者:
L
luotao1
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine cos_sim infershape
test=develop
上级
13816dd4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
16 addition
and
5 deletion
+16
-5
paddle/fluid/operators/cos_sim_op.cc
paddle/fluid/operators/cos_sim_op.cc
+3
-0
paddle/fluid/operators/cos_sim_op.h
paddle/fluid/operators/cos_sim_op.h
+13
-5
未找到文件。
paddle/fluid/operators/cos_sim_op.cc
浏览文件 @
c05af910
...
...
@@ -74,6 +74,9 @@ class CosSimOpMaker : public framework::OpProtoAndCheckerMaker {
"Norm of the second input, reduced along the 1st "
"dimension."
)
.
AsIntermediate
();
AddAttr
<
bool
>
(
framework
::
kAllKernelsMustComputeRuntimeShape
,
"Skip calling InferShape() function in the runtime."
)
.
SetDefault
(
true
);
AddComment
(
R"DOC(
**Cosine Similarity Operator**
...
...
paddle/fluid/operators/cos_sim_op.h
浏览文件 @
c05af910
...
...
@@ -28,17 +28,21 @@ class CosSimKernel : public framework::OpKernel<T> {
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
// get Tensor
auto
*
in_x
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
*
in_x
=
context
.
Input
<
framework
::
LoD
Tensor
>
(
"X"
);
auto
*
in_y
=
context
.
Input
<
Tensor
>
(
"Y"
);
auto
*
out_z
=
context
.
Output
<
Tensor
>
(
"Out"
);
auto
*
out_z
=
context
.
Output
<
framework
::
LoD
Tensor
>
(
"Out"
);
auto
*
out_x_norm
=
context
.
Output
<
Tensor
>
(
"XNorm"
);
auto
*
out_y_norm
=
context
.
Output
<
Tensor
>
(
"YNorm"
);
out_z
->
mutable_data
<
T
>
(
context
.
GetPlace
());
out_x_norm
->
mutable_data
<
T
>
(
context
.
GetPlace
());
out_y_norm
->
mutable_data
<
T
>
(
context
.
GetPlace
());
int
rows_x
=
in_x
->
dims
()[
0
];
int
rows_y
=
in_y
->
dims
()[
0
];
out_z
->
Resize
({
rows_x
,
1
});
out_x_norm
->
Resize
({
rows_x
,
1
});
out_y_norm
->
Resize
({
rows_y
,
1
});
out_z
->
mutable_data
<
T
>
(
context
.
GetPlace
());
out_x_norm
->
mutable_data
<
T
>
(
context
.
GetPlace
());
out_y_norm
->
mutable_data
<
T
>
(
context
.
GetPlace
());
out_z
->
set_lod
(
in_x
->
lod
());
int
cols
=
framework
::
product
(
in_x
->
dims
())
/
rows_x
;
...
...
@@ -81,6 +85,7 @@ class CosSimGradKernel : public framework::OpKernel<T> {
if
(
rows_x
==
rows_y
)
{
if
(
out_grad_x
)
{
out_grad_x
->
Resize
(
in_x
->
dims
());
math
::
CosSimGradFunctor
<
T
>
functor
(
in_x_norm
->
data
<
T
>
(),
in_y_norm
->
data
<
T
>
(),
in_x
->
data
<
T
>
(),
in_y
->
data
<
T
>
(),
in_z
->
data
<
T
>
(),
in_grad_z
->
data
<
T
>
(),
...
...
@@ -91,6 +96,7 @@ class CosSimGradKernel : public framework::OpKernel<T> {
for_range
(
functor
);
}
if
(
out_grad_y
)
{
out_grad_y
->
Resize
(
in_y
->
dims
());
math
::
CosSimGradFunctor
<
T
>
functor
(
in_y_norm
->
data
<
T
>
(),
in_x_norm
->
data
<
T
>
(),
in_y
->
data
<
T
>
(),
in_x
->
data
<
T
>
(),
in_z
->
data
<
T
>
(),
in_grad_z
->
data
<
T
>
(),
...
...
@@ -102,6 +108,7 @@ class CosSimGradKernel : public framework::OpKernel<T> {
}
}
else
{
if
(
out_grad_x
)
{
out_grad_x
->
Resize
(
in_x
->
dims
());
math
::
CosSimDxFunctor
<
T
>
functor
(
in_x_norm
->
data
<
T
>
(),
in_y_norm
->
data
<
T
>
(),
in_x
->
data
<
T
>
(),
in_y
->
data
<
T
>
(),
in_z
->
data
<
T
>
(),
in_grad_z
->
data
<
T
>
(),
...
...
@@ -112,6 +119,7 @@ class CosSimGradKernel : public framework::OpKernel<T> {
for_range
(
functor
);
}
if
(
out_grad_y
)
{
out_grad_y
->
Resize
(
in_y
->
dims
());
out_grad_y
->
mutable_data
<
T
>
(
context
.
GetPlace
());
math
::
SetConstant
<
DeviceContext
,
T
>
set_zero
;
auto
&
dev_ctx
=
context
.
template
device_context
<
DeviceContext
>();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录