Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
80a5ee00
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
80a5ee00
编写于
10月 17, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix forward and add backward.
上级
3123e3cf
变更
3
展开全部
隐藏空白更改
内联
并排
Showing
3 changed file
with
302 addition
and
94 deletion
+302
-94
paddle/operators/linear_chain_crf_op.cc
paddle/operators/linear_chain_crf_op.cc
+259
-75
paddle/operators/linear_chain_crf_op.h
paddle/operators/linear_chain_crf_op.h
+12
-8
python/paddle/v2/framework/tests/test_linear_chain_crf_op.py
python/paddle/v2/framework/tests/test_linear_chain_crf_op.py
+31
-11
未找到文件。
paddle/operators/linear_chain_crf_op.cc
浏览文件 @
80a5ee00
此差异已折叠。
点击以展开。
paddle/operators/linear_chain_crf_op.h
浏览文件 @
80a5ee00
...
@@ -30,20 +30,24 @@ class LinearChainCrfOpKernel : public framework::OpKernel<T> {
...
@@ -30,20 +30,24 @@ class LinearChainCrfOpKernel : public framework::OpKernel<T> {
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
;
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
;
protected:
protected:
T
ForwardOneSequence
(
const
platform
::
DeviceContext
&
ctx
,
T
ForwardOneSequence
(
const
Tensor
*
emission
,
const
Tensor
*
emission_row_max
,
const
Tensor
&
emission
,
Tensor
&
emission_row_max
,
const
Tensor
*
emission_exps
,
const
Tensor
*
trans_weights
,
Tensor
&
emission_exps
,
const
Tensor
&
trans_weights
,
const
Tensor
*
trans_weight_exps
,
const
Tensor
*
label
,
Tensor
&
trans_weight_exps
,
const
Tensor
&
label
,
Tensor
*
alpha
)
const
;
Tensor
&
a
)
const
;
private:
T
NormalizeL1
(
T
*
x
,
size_t
len
)
const
;
};
};
template
<
typename
Place
,
typename
T
>
template
<
typename
Place
,
typename
T
>
class
LinearChainCrfGradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
class
LinearChainCrfGradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
;
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
;
protected:
void
BackwardOneSequence
(
const
platform
::
DeviceContext
&
ctx
,
const
Tensor
*
emission_exps
,
const
Tensor
*
transition_exps
,
const
Tensor
*
alpha
,
const
Tensor
*
label
,
Tensor
*
beta
,
Tensor
*
transition_grad
,
Tensor
*
emission_grad
)
const
;
};
};
}
// namespace operators
}
// namespace operators
...
...
python/paddle/v2/framework/tests/test_linear_chain_crf_op.py
浏览文件 @
80a5ee00
...
@@ -4,10 +4,12 @@ import numpy as np
...
@@ -4,10 +4,12 @@ import numpy as np
from
op_test
import
OpTest
from
op_test
import
OpTest
import
pdb
class
LinearChainCrfForward
(
object
):
class
LinearChainCrfForward
(
object
):
def
__init__
(
self
,
seq_start_positions
,
emission_weights
,
def
__init__
(
self
,
seq_start_positions
,
emission_weights
,
emission_row_max
,
transition_weight
s
,
labels
):
emission_exps
,
transition_weights
,
transition_exp
s
,
labels
):
self
.
tag_num
=
emission_weights
.
shape
[
1
]
self
.
tag_num
=
emission_weights
.
shape
[
1
]
self
.
seq_num
=
len
(
seq_start_positions
)
-
1
self
.
seq_num
=
len
(
seq_start_positions
)
-
1
...
@@ -15,25 +17,25 @@ class LinearChainCrfForward(object):
...
@@ -15,25 +17,25 @@ class LinearChainCrfForward(object):
self
.
labels
=
labels
self
.
labels
=
labels
self
.
x
=
emission_weights
self
.
x
=
emission_weights
self
.
x_row_max
=
np
.
amax
(
self
.
x
,
axis
=
1
,
keepdims
=
True
)
self
.
x_row_max
=
emission_row_max
self
.
x_exps
=
np
.
exp
(
self
.
x
-
self
.
x_row_max
)
self
.
x_exps
=
emission_exps
# unnormalized logits of the transition weights for the start mark.
# unnormalized logits of the transition weights for the start mark.
self
.
a
=
transition_weights
[
0
,
:]
self
.
a
=
transition_weights
[
0
,
:]
self
.
a_exps
=
np
.
exp
(
self
.
a
)
self
.
a_exps
=
transition_exps
[
0
,
:]
# unnormalized logits of the transition weights for the end mark.
# unnormalized logits of the transition weights for the end mark.
self
.
b
=
transition_weights
[
1
,
:]
self
.
b
=
transition_weights
[
1
,
:]
self
.
b_exps
=
np
.
exp
(
self
.
b
)
self
.
b_exps
=
transition_exps
[
1
,
:]
# unnormalized logits of the transition weights for all the other tags.
# unnormalized logits of the transition weights for all the other tags.
self
.
w
=
transition_weights
[
2
:,
:]
self
.
w
=
transition_weights
[
2
:,
:]
self
.
w_exps
=
np
.
exp
(
self
.
w
)
self
.
w_exps
=
transition_exps
[
2
:,
:]
# The output of linear chain crf operator.
# The output of linear chain crf operator.
# alpha is a memo table in dynamic programming to caculate
# alpha is a memo table in dynamic programming to caculate
# nomalization factor.
# nomalization factor.
self
.
alpha
=
np
.
zeros
(
self
.
alpha
=
np
.
zeros
(
(
seq_start_positions
[
-
1
],
self
.
tag_num
),
dtype
=
"float32"
)
(
seq_start_positions
[
-
1
],
self
.
tag_num
),
dtype
=
"float32"
)
self
.
log_likelihood
=
np
.
zeros
((
self
.
tag
_num
,
1
))
self
.
log_likelihood
=
np
.
zeros
((
self
.
seq
_num
,
1
))
def
_l1_norm
(
self
,
x
):
def
_l1_norm
(
self
,
x
):
s
=
np
.
sum
(
x
)
s
=
np
.
sum
(
x
)
...
@@ -91,11 +93,15 @@ class TestLinearChainCrfOp(OpTest):
...
@@ -91,11 +93,15 @@ class TestLinearChainCrfOp(OpTest):
lod
=
[[
0
]]
lod
=
[[
0
]]
for
i
in
range
(
SEQ_NUM
):
for
i
in
range
(
SEQ_NUM
):
lod
[
-
1
].
append
(
lod
[
-
1
][
-
1
]
+
random
.
randint
(
1
,
MAX_SEQ_LEN
))
lod
[
-
1
].
append
(
lod
[
-
1
][
-
1
]
+
random
.
randint
(
1
,
MAX_SEQ_LEN
))
emission
=
np
.
random
.
uniform
(
-
1
,
1
,
emission
=
np
.
random
.
uniform
(
-
1
,
1
,
[
lod
[
-
1
][
-
1
],
TAG_NUM
]).
astype
(
"float32"
)
[
lod
[
-
1
][
-
1
],
TAG_NUM
]).
astype
(
"float32"
)
emission_row_max
=
np
.
amax
(
emission
,
axis
=
1
,
keepdims
=
True
)
emission_exps
=
np
.
exp
(
emission
-
emission_row_max
)
transition
=
np
.
random
.
uniform
(
-
0.5
,
0.5
,
transition
=
np
.
random
.
uniform
(
-
0.5
,
0.5
,
[
TAG_NUM
+
2
,
TAG_NUM
]).
astype
(
"float32"
)
[
TAG_NUM
+
2
,
TAG_NUM
]).
astype
(
"float32"
)
transition_exps
=
np
.
exp
(
transition
)
labels
=
np
.
random
.
randint
(
labels
=
np
.
random
.
randint
(
low
=
0
,
high
=
TAG_NUM
,
size
=
(
lod
[
-
1
][
-
1
],
1
),
dtype
=
"int32"
)
low
=
0
,
high
=
TAG_NUM
,
size
=
(
lod
[
-
1
][
-
1
],
1
),
dtype
=
"int32"
)
...
@@ -105,10 +111,17 @@ class TestLinearChainCrfOp(OpTest):
...
@@ -105,10 +111,17 @@ class TestLinearChainCrfOp(OpTest):
"Label"
:
(
labels
,
lod
)
"Label"
:
(
labels
,
lod
)
}
}
crf
=
LinearChainCrfForward
(
lod
[
0
],
emission
,
transition
,
labels
)
crf
=
LinearChainCrfForward
(
lod
[
0
],
emission
,
emission_row_max
,
emission_exps
,
transition
,
transition_exps
,
labels
)
alpha
,
log_likelihood
=
crf
.
crf_forward_compute
()
alpha
,
log_likelihood
=
crf
.
crf_forward_compute
()
self
.
outputs
=
{
"Alpha"
:
alpha
,
"LogLikelihood"
:
log_likelihood
}
self
.
outputs
=
{
"Alpha"
:
alpha
,
"EmissionExps"
:
emission_exps
,
"TransitionExps"
:
transition_exps
,
"LogLikelihood"
:
log_likelihood
}
def
setUp
(
self
):
def
setUp
(
self
):
self
.
op_type
=
"linear_chain_crf"
self
.
op_type
=
"linear_chain_crf"
...
@@ -117,6 +130,13 @@ class TestLinearChainCrfOp(OpTest):
...
@@ -117,6 +130,13 @@ class TestLinearChainCrfOp(OpTest):
def
test_check_output
(
self
):
def
test_check_output
(
self
):
self
.
check_output
()
self
.
check_output
()
def
test_check_grad
(
self
):
self
.
check_grad
([
"Emission"
,
"Transition"
],
"LogLikelihood"
)
def
test_check_grad_ignore_transition
(
self
):
self
.
check_grad
(
[
"Emission"
],
"LogLikelihood"
,
no_grad_set
=
set
(
"Transition"
))
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录