Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
ebd992ec
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看板
提交
ebd992ec
编写于
10月 31, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
backpropagate gradients the CRF operator receives.
上级
2ac9a3d8
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
17 addition
and
8 deletion
+17
-8
paddle/operators/linear_chain_crf_op.h
paddle/operators/linear_chain_crf_op.h
+17
-8
未找到文件。
paddle/operators/linear_chain_crf_op.h
浏览文件 @
ebd992ec
...
...
@@ -35,6 +35,14 @@ static inline T NormalizeL1(T* x, size_t len) {
return
sum
;
}
template
<
typename
T
>
struct
ScalarMul
{
explicit
ScalarMul
(
const
T
&
scalar
)
:
scalar
(
scalar
)
{}
T
operator
()(
const
T
&
val
)
const
{
return
val
*
scalar
;
}
T
scalar
;
};
using
framework
::
LoDTensor
;
using
framework
::
LoD
;
using
framework
::
Tensor
;
...
...
@@ -349,8 +357,6 @@ class LinearChainCRFGradOpKernel : public framework::OpKernel<T> {
// data reader operator, it can have no gradients.
PADDLE_ENFORCE
(
emission_grad
,
"Output(Emission@Grad) should not be null."
);
emission_grad
->
mutable_data
<
T
>
(
platform
::
CPUPlace
());
math
::
SetConstant
<
platform
::
CPUPlace
,
T
>
()(
ctx
.
device_context
(),
emission_grad
,
0.
);
if
(
transition_grad
)
{
transition_grad
->
mutable_data
<
T
>
(
platform
::
CPUPlace
());
math
::
SetConstant
<
platform
::
CPUPlace
,
T
>
()(
ctx
.
device_context
(),
...
...
@@ -480,15 +486,18 @@ class LinearChainCRFGradOpKernel : public framework::OpKernel<T> {
auto
row_sum
=
prob
.
sum
(
Eigen
::
DSizes
<
int
,
1
>
(
1
))
.
reshape
(
Eigen
::
DSizes
<
int
,
2
>
(
seq_length
,
1
))
.
broadcast
(
Eigen
::
DSizes
<
int
,
2
>
(
1
,
tag_num
));
x_grad_mat
.
device
(
*
place
)
=
prob
/
row_sum
;
x_grad_mat
.
device
(
*
place
)
=
(
prob
/
row_sum
).
unaryExpr
(
ScalarMul
<
T
>
(
ll_grad
));
for
(
size_t
k
=
0
;
k
<
seq_length
;
++
k
)
{
x_grad_mat
(
k
,
label_value
[
k
])
-=
static_cast
<
T
>
(
1.
);
x_grad_mat
(
k
,
label_value
[
k
])
-=
static_cast
<
T
>
(
ll_grad
);
}
if
(
transition_grad
)
{
T
*
trans_grad
=
transition_grad
->
data
<
T
>
();
for
(
size_t
k
=
0
;
k
<
tag_num
;
++
k
)
{
// Do not multiply by the output gradient here, because x_grad_mat has
// alrealy done this.
trans_grad
[
k
]
+=
x_grad_mat
(
/*from start state*/
0
,
k
);
trans_grad
[
tag_num
+
k
]
+=
x_grad_mat
(
/*to end state*/
seq_length
-
1
,
k
);
...
...
@@ -496,8 +505,8 @@ class LinearChainCRFGradOpKernel : public framework::OpKernel<T> {
auto
x_exps_mat
=
EigenMatrix
<
T
>::
From
(
emission_exps
);
// TODO(caoying): Fix this to avoid using this local variable if w
hen
can
// profil
ing
the training process.
// TODO(caoying): Fix this to avoid using this local variable if w
e
can
// profil
e
the training process.
Tensor
tmp
;
tmp
.
mutable_data
<
T
>
(
beta
->
dims
(),
platform
::
CPUPlace
());
auto
tmp_mat
=
EigenMatrix
<
T
>::
From
(
tmp
);
...
...
@@ -520,11 +529,11 @@ class LinearChainCRFGradOpKernel : public framework::OpKernel<T> {
for
(
size_t
j
=
0
;
j
<
tag_num
;
++
j
)
{
trans_grad
[(
i
+
state_trans_base_idx
)
*
tag_num
+
j
]
+=
sum
*
w_exps
[(
i
+
state_trans_base_idx
)
*
tag_num
+
j
]
*
alpha_mat
(
k
-
1
,
i
)
*
tmp_mat
(
k
,
j
);
alpha_mat
(
k
-
1
,
i
)
*
tmp_mat
(
k
,
j
)
*
ll_grad
;
}
}
trans_grad
[(
label_value
[
k
-
1
]
+
state_trans_base_idx
)
*
tag_num
+
label_value
[
k
]]
-=
static_cast
<
T
>
(
1.
);
label_value
[
k
]]
-=
static_cast
<
T
>
(
ll_grad
);
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录