Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
49df2a78
P
Paddle
项目概览
机器未来
/
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看板
提交
49df2a78
编写于
12月 25, 2017
作者:
C
chengduoZH
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine gradient function
上级
bcf0b56f
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
33 addition
and
68 deletion
+33
-68
paddle/operators/cos_sim_op.h
paddle/operators/cos_sim_op.h
+33
-68
未找到文件。
paddle/operators/cos_sim_op.h
浏览文件 @
49df2a78
...
...
@@ -13,7 +13,6 @@
limitations under the License. */
#pragma once
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
#include "paddle/operators/elementwise_op_function.h"
...
...
@@ -21,16 +20,9 @@ namespace paddle {
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenMatrix
=
framework
::
EigenMatrix
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
T
,
int
MajorType
=
Eigen
::
RowMajor
,
typename
IndexType
=
Eigen
::
DenseIndex
>
using
EigenVector
=
framework
::
EigenVector
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
IT1
,
typename
IT2
,
typename
Callback
>
static
void
ForEachZip
(
IT1
begin1
,
IT1
last1
,
IT2
begin2
,
Callback
callback
)
{
// This method could be implemented in CUDA
for
(;
begin1
<
last1
;
++
begin1
,
++
begin2
)
{
callback
(
*
begin1
,
*
begin2
);
}
...
...
@@ -66,15 +58,8 @@ struct CosSimFunctor {
x_norm_
[
x_offset
]
=
xx
;
y_norm_
[
y_offset
]
=
yy
;
z_
[
x_offset
]
=
xy
/
(
xx
*
yy
);
}
else
{
}
else
{
// This can be wrote in a better way.
auto
*
y
=
y_
;
// if (yy == -1) {
// yy = 0;
// for (size_t i = 0; i < cols_; ++i) {
// yy += y[i] * y[i];
// }
// y_norm[0] = sqrt(yy);
// }
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
xx
+=
x
[
i
]
*
x
[
i
];
yy
+=
y
[
i
]
*
y
[
i
];
// only need
...
...
@@ -144,22 +129,25 @@ struct CosSimGradFunctor {
dx_
(
dx
),
cols_
(
static_cast
<
size_t
>
(
cols
))
{}
void
operator
()(
const
T
&
x_norm
,
const
T
&
y_norm
)
const
{
inline
void
operator
()(
const
T
&
x_norm
,
const
T
&
y_norm
)
const
{
size_t
x_offset
=
&
x_norm
-
x_norm_
;
size_t
y_offset
=
&
y_norm
-
y_norm_
;
auto
x_norm_square
=
x_norm_
[
x_offset
]
*
x_norm_
[
x_offset
];
// auto y_norm_square = y_norm_[y_offset] * y_norm_[y_offset];
auto
xy_norm_prod
=
x_norm_
[
x_offset
]
*
y_norm_
[
y_offset
];
auto
dz
=
dz_
[
x_offset
];
auto
z
=
z_
[
x_offset
];
auto
*
dx
=
dx_
+
cols_
*
x_offset
;
auto
*
x
=
x_
+
cols_
*
x_offset
;
auto
*
y
=
y_
+
cols_
*
y_offset
;
auto
z
=
z_
[
x_offset
];
auto
reciprocal_xy_norm_prod
=
1
/
xy_norm_prod
;
auto
reciprocal_x_norm_square
=
1
/
x_norm_square
;
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
dx
[
i
]
=
dz
*
(
y
[
i
]
/
xy_norm_prod
-
z
*
x
[
i
]
/
x_norm_square
);
dx
[
i
]
=
dz
*
(
y
[
i
]
*
reciprocal_xy_norm_prod
-
z
*
x
[
i
]
*
reciprocal_x_norm_square
);
}
}
...
...
@@ -173,10 +161,10 @@ struct CosSimGradFunctor {
const
size_t
cols_
;
};
template
<
typename
T
>
template
<
typename
T
,
bool
Dx
>
struct
CosSimDxFunctor
{
CosSimDxFunctor
(
const
T
*
x_norm
,
const
T
*
y_norm
,
const
T
*
x
,
const
T
*
y
,
const
T
*
z
,
const
T
*
dz
,
T
*
dx
,
int
cols
)
const
T
*
z
,
const
T
*
dz
,
T
*
dx
,
T
*
dy
,
int
cols
)
:
x_norm_
(
x_norm
),
y_norm_
(
y_norm
),
x_
(
x
),
...
...
@@ -184,58 +172,34 @@ struct CosSimDxFunctor {
z_
(
z
),
dz_
(
dz
),
dx_
(
dx
),
cols_
(
static_cast
<
size_t
>
(
cols
))
{}
void
operator
()(
const
T
&
x_norm
,
const
T
&
y_norm
)
const
{
size_t
x_offset
=
&
x_norm
-
x_norm_
;
auto
x_norm_square
=
x_norm_
[
x_offset
]
*
x_norm_
[
x_offset
];
auto
xy_norm_prod
=
x_norm_
[
x_offset
]
*
y_norm_
[
0
];
auto
dz
=
dz_
[
x_offset
];
auto
z
=
z_
[
x_offset
];
auto
*
dx
=
dx_
+
cols_
*
x_offset
;
auto
*
x
=
x_
+
cols_
*
x_offset
;
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
dx
[
i
]
=
dz
*
(
y_
[
i
]
/
xy_norm_prod
-
z
*
x
[
i
]
/
x_norm_square
);
}
}
const
T
*
x_norm_
;
const
T
*
y_norm_
;
const
T
*
x_
;
const
T
*
y_
;
const
T
*
z_
;
const
T
*
dz_
;
T
*
dx_
;
const
size_t
cols_
;
};
template
<
typename
T
>
struct
CosSimDyFunctor
{
CosSimDyFunctor
(
const
T
*
x_norm
,
const
T
*
y_norm
,
const
T
*
x
,
const
T
*
y
,
const
T
*
z
,
const
T
*
dz
,
T
*
dy
,
int
cols
)
:
x_norm_
(
x_norm
),
y_norm_
(
y_norm
),
x_
(
x
),
y_
(
y
),
z_
(
z
),
dz_
(
dz
),
dy_
(
dy
),
cols_
(
static_cast
<
size_t
>
(
cols
))
{}
void
operator
()(
const
T
&
x_norm
,
const
T
&
y_norm
)
const
{
inline
void
operator
()(
const
T
&
x_norm
,
const
T
&
y_norm
)
const
{
size_t
x_offset
=
&
x_norm
-
x_norm_
;
auto
y_norm_square
=
y_norm_
[
0
]
*
y_norm_
[
0
];
auto
xy_norm_prod
=
x_norm_
[
x_offset
]
*
y_norm_
[
0
];
auto
dz
=
dz_
[
x_offset
];
auto
z
=
z_
[
x_offset
];
auto
*
x
=
x_
+
cols_
*
x_offset
;
auto
reciprocal_xy_norm_prod
=
1
/
xy_norm_prod
;
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
dy_
[
i
]
+=
dz
*
(
x
[
i
]
/
xy_norm_prod
-
z
*
y_
[
i
]
/
y_norm_square
);
if
(
Dx
)
{
auto
x_norm_square
=
x_norm_
[
x_offset
]
*
x_norm_
[
x_offset
];
auto
*
dx
=
dx_
+
cols_
*
x_offset
;
auto
*
x
=
x_
+
cols_
*
x_offset
;
auto
reciprocal_x_norm_square
=
1
/
x_norm_square
;
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
dx
[
i
]
=
dz
*
(
y_
[
i
]
*
reciprocal_xy_norm_prod
-
z
*
x
[
i
]
*
reciprocal_x_norm_square
);
}
}
else
{
auto
y_norm_square
=
y_norm_
[
0
]
*
y_norm_
[
0
];
auto
reciprocal_y_norm_square
=
1
/
y_norm_square
;
for
(
size_t
i
=
0
;
i
<
cols_
;
++
i
)
{
dy_
[
i
]
+=
dz
*
(
x
[
i
]
*
reciprocal_xy_norm_prod
-
z
*
y_
[
i
]
*
reciprocal_y_norm_square
);
}
}
}
...
...
@@ -245,6 +209,7 @@ struct CosSimDyFunctor {
const
T
*
y_
;
const
T
*
z_
;
const
T
*
dz_
;
T
*
dx_
;
T
*
dy_
;
const
size_t
cols_
;
};
...
...
@@ -287,17 +252,17 @@ class CosSimGradKernel : public framework::OpKernel<T> {
}
}
else
{
if
(
out_grad_x
)
{
CosSimDxFunctor
<
T
>
functor
(
CosSimDxFunctor
<
T
,
true
>
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
>
(),
out_grad_x
->
mutable_data
<
T
>
(
context
.
GetPlace
()),
cols
);
out_grad_x
->
mutable_data
<
T
>
(
context
.
GetPlace
()),
nullptr
,
cols
);
ForEachZip
(
in_x_norm
->
data
<
T
>
(),
in_x_norm
->
data
<
T
>
()
+
rows_x
,
in_y_norm
->
data
<
T
>
(),
functor
);
}
if
(
out_grad_y
)
{
CosSimD
yFunctor
<
T
>
functor
(
CosSimD
xFunctor
<
T
,
false
>
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
>
(),
in_y
->
data
<
T
>
(),
in_z
->
data
<
T
>
(),
in_grad_z
->
data
<
T
>
(),
nullptr
,
out_grad_y
->
mutable_data
<
T
>
(
context
.
GetPlace
()),
cols
);
ForEachZip
(
in_x_norm
->
data
<
T
>
(),
in_x_norm
->
data
<
T
>
()
+
rows_x
,
in_y_norm
->
data
<
T
>
(),
functor
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录