Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
afeixing77
Paddle
提交
bce4f7d6
P
Paddle
项目概览
afeixing77
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
bce4f7d6
编写于
10月 26, 2017
作者:
C
caoying03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
follow comments.
上级
4c630869
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
34 addition
and
32 deletion
+34
-32
paddle/framework/tensor_impl.h
paddle/framework/tensor_impl.h
+3
-2
paddle/operators/linear_chain_crf_op.cc
paddle/operators/linear_chain_crf_op.cc
+29
-28
paddle/operators/linear_chain_crf_op.h
paddle/operators/linear_chain_crf_op.h
+2
-2
未找到文件。
paddle/framework/tensor_impl.h
浏览文件 @
bce4f7d6
...
...
@@ -228,8 +228,9 @@ inline Tensor Tensor::Slice(const int& begin_idx, const int& end_idx) const {
PADDLE_ENFORCE_GE
(
begin_idx
,
0
,
"The start row index must be greater than 0."
);
PADDLE_ENFORCE_LE
(
end_idx
,
dims_
[
0
],
"The end row index is out of bound."
);
PADDLE_ENFORCE_LT
(
begin_idx
,
end_idx
,
"The start row index must be less than the end row index."
);
PADDLE_ENFORCE_LT
(
begin_idx
,
end_idx
,
"The start row index must be smaller than the end row index."
);
if
(
dims_
[
0
]
==
1
)
{
return
*
this
;
...
...
paddle/operators/linear_chain_crf_op.cc
浏览文件 @
bce4f7d6
...
...
@@ -26,9 +26,10 @@ T NormalizeL1(T* x, size_t len) {
// Right now, we just bet that sum won't be zero. If this really happens, we
// will figure out what should be done then.
PADDLE_ENFORCE
(
sum
,
"The unnormalized probabilites of all possible unfinished "
"The unnormalized probabilit
i
es of all possible unfinished "
"sequences must be greater than 0."
);
for
(
size_t
i
=
0
;
i
<
len
;
++
i
)
x
[
i
]
/=
sum
;
T
s
=
1.
/
sum
;
for
(
size_t
i
=
0
;
i
<
len
;
++
i
)
x
[
i
]
*=
s
;
return
sum
;
}
}
// namespace
...
...
@@ -36,9 +37,9 @@ T NormalizeL1(T* x, size_t len) {
using
framework
::
LoDTensor
;
using
framework
::
LoD
;
class
LinearChainC
rf
OpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
class
LinearChainC
RF
OpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
LinearChainC
rf
OpMaker
(
framework
::
OpProto
*
proto
,
LinearChainC
RF
OpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
...
...
@@ -51,11 +52,11 @@ class LinearChainCrfOpMaker : public framework::OpProtoAndCheckerMaker {
AddInput
(
"Transition"
,
"(Tensor, default: Tensor<float>). A Tensor with shape [(D + 2) x D]. "
"The learnable parameter for linear_chain_crf operator. "
"The learnable parameter for
the
linear_chain_crf operator. "
"See more details in the operator's comments."
);
AddInput
(
"Label"
,
"(LoDTensor, default: LoDTensor<int>). The ground
truth which is a 2-D "
"(LoDTensor, default: LoDTensor<int>). The groundtruth which is a 2-D "
"LoDTensor with shape [N x 1], where N is the total element number in "
"a mini-batch."
);
AddOutput
(
...
...
@@ -82,14 +83,11 @@ class LinearChainCrfOpMaker : public framework::OpProtoAndCheckerMaker {
.
AsIntermediate
();
AddOutput
(
"LogLikelihood"
,
"(Tensor, default: Tensor<float>). The logarithm of the "
"conditional "
"(Tensor, default: Tensor<float>). The logarithm of the conditional "
"likelihood of each training sample in a mini-batch. This is a 2-D "
"tensor with shape [S x 1], where S is the sequence number in a "
"mini-batch. "
"Note: S is equal to the sequence number in a mini-batch. The "
"output "
"is no longer a LoDTensor."
);
"mini-batch. Note: S is equal to the sequence number in a mini-batch. "
"The output is no longer a LoDTensor."
);
AddComment
(
R"DOC(
Conditional Random Field defines an undirected probabilistic graph with nodes
denoting random variables and edges denoting dependencies between these
...
...
@@ -100,11 +98,11 @@ variables. CRF learns the conditional probability \f$P(Y|X)\f$, where
Linear chain CRF is a special case of CRF that is useful for sequence labeling
task. Sequence labeling tasks do not assume a lot of conditional
independences among inputs. They only concern about the input and the output
being linear sequences. Thus, the graph model of
CRF is a simple chain or
a line, which results in a
linear chain CRF.
being linear sequences. Thus, the graph model of
such a CRF is a simple chain
or a line, which results in the
linear chain CRF.
This operator implements the Forward-Backward algorithm for
linear chain CRF.
Please see http://www.cs.columbia.edu/~mcollins/fb.pdf for reference.
This operator implements the Forward-Backward algorithm for
the linear chain
CRF.
Please see http://www.cs.columbia.edu/~mcollins/fb.pdf for reference.
Equation:
...
...
@@ -144,7 +142,7 @@ nonlinear activation.
}
};
class
LinearChainC
rf
Op
:
public
framework
::
OperatorWithKernel
{
class
LinearChainC
RF
Op
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
...
...
@@ -211,7 +209,7 @@ class LinearChainCrfOp : public framework::OperatorWithKernel {
};
template
<
typename
T
>
class
LinearChainC
rf
OpKernel
<
platform
::
CPUPlace
,
T
>
class
LinearChainC
RF
OpKernel
<
platform
::
CPUPlace
,
T
>
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -262,11 +260,11 @@ class LinearChainCrfOpKernel<platform::CPUPlace, T>
w_exps
.
device
(
place
)
=
w
.
exp
();
auto
*
alpha
=
ctx
.
Output
<
LoDTensor
>
(
"Alpha"
);
alpha
->
mutable_data
<
T
>
(
ctx
.
Get
Place
());
alpha
->
mutable_data
<
T
>
(
platform
::
CPU
Place
());
auto
*
ll
=
ctx
.
Output
<
LoDTensor
>
(
"LogLikelihood"
);
// resize the output tensor to the correct dimension.
ll
->
Resize
({
static_cast
<
int
>
(
seq_num
),
1
});
T
*
log_likelihood
=
ll
->
mutable_data
<
T
>
(
ctx
.
Get
Place
());
T
*
log_likelihood
=
ll
->
mutable_data
<
T
>
(
platform
::
CPU
Place
());
for
(
size_t
i
=
0
;
i
<
seq_num
;
++
i
)
{
int
start_pos
=
static_cast
<
int
>
(
in_lod
[
level
][
i
]);
int
end_pos
=
static_cast
<
int
>
(
in_lod
[
level
][
i
+
1
]);
...
...
@@ -322,6 +320,7 @@ class LinearChainCrfOpKernel<platform::CPUPlace, T>
}
alpha_value
[
k
*
tag_num
+
i
]
=
x_exps
[
k
*
tag_num
+
i
]
*
sum
;
}
// NormalizeL1 is to avoid underflow or overflow at (*).
ll
-=
x_row_max
[
k
]
+
std
::
log
(
NormalizeL1
<
T
>
(
alpha_value
+
k
*
tag_num
,
tag_num
));
}
...
...
@@ -330,6 +329,7 @@ class LinearChainCrfOpKernel<platform::CPUPlace, T>
sum
+=
alpha_value
[(
seq_length
-
1
)
*
tag_num
+
i
]
*
w_exps
[
tag_num
+
i
];
}
ll
-=
std
::
log
(
sum
);
// Now ll is equal to -log(Z).
const
int
*
lbl
=
label
->
data
<
int
>
();
PADDLE_ENFORCE_LT
(
...
...
@@ -347,7 +347,7 @@ class LinearChainCrfOpKernel<platform::CPUPlace, T>
}
};
class
LinearChainC
rf
GradOp
:
public
framework
::
OperatorWithKernel
{
class
LinearChainC
RF
GradOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
...
...
@@ -407,11 +407,11 @@ class LinearChainCrfGradOp : public framework::OperatorWithKernel {
};
template
<
typename
T
>
class
LinearChainC
rf
GradOpKernel
<
platform
::
CPUPlace
,
T
>
class
LinearChainC
RF
GradOpKernel
<
platform
::
CPUPlace
,
T
>
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
PADDLE_ENFORCE
(
platform
::
is_cpu_place
(
ctx
.
Get
Place
()),
PADDLE_ENFORCE
(
platform
::
is_cpu_place
(
platform
::
CPU
Place
()),
"This kernel only runs on CPU."
);
auto
*
label
=
ctx
.
Input
<
LoDTensor
>
(
"Label"
);
auto
*
emission_exps
=
ctx
.
Input
<
LoDTensor
>
(
"EmissionExps"
);
...
...
@@ -493,6 +493,7 @@ class LinearChainCrfGradOpKernel<platform::CPUPlace, T>
}
beta_value
[
k
*
tag_num
+
i
]
=
sum
;
}
// NormalizeL1 is to avoid underflow or overflow at (**).
NormalizeL1
<
T
>
(
beta_value
+
k
*
tag_num
,
tag_num
);
}
...
...
@@ -534,7 +535,7 @@ class LinearChainCrfGradOpKernel<platform::CPUPlace, T>
T
sum
=
0.
;
for
(
size_t
i
=
0
;
i
<
tag_num
;
++
i
)
{
for
(
size_t
j
=
0
;
j
<
tag_num
;
++
j
)
{
sum
+=
w_exps
[(
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
);
}
}
...
...
@@ -557,11 +558,11 @@ class LinearChainCrfGradOpKernel<platform::CPUPlace, T>
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP
(
linear_chain_crf
,
ops
::
LinearChainC
rfOp
,
ops
::
LinearChainCrf
OpMaker
,
linear_chain_crf_grad
,
ops
::
LinearChainC
rf
GradOp
);
REGISTER_OP
(
linear_chain_crf
,
ops
::
LinearChainC
RFOp
,
ops
::
LinearChainCRF
OpMaker
,
linear_chain_crf_grad
,
ops
::
LinearChainC
RF
GradOp
);
REGISTER_OP_CPU_KERNEL
(
linear_chain_crf
,
ops
::
LinearChainC
rf
OpKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
ops
::
LinearChainC
RF
OpKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
REGISTER_OP_CPU_KERNEL
(
linear_chain_crf_grad
,
ops
::
LinearChainC
rf
GradOpKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
ops
::
LinearChainC
RF
GradOpKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
);
paddle/operators/linear_chain_crf_op.h
浏览文件 @
bce4f7d6
...
...
@@ -25,7 +25,7 @@ template <typename T, int MajorType = Eigen::RowMajor,
using
EigenMatrix
=
framework
::
EigenMatrix
<
T
,
MajorType
,
IndexType
>
;
template
<
typename
Place
,
typename
T
>
class
LinearChainC
rf
OpKernel
:
public
framework
::
OpKernel
<
T
>
{
class
LinearChainC
RF
OpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
;
...
...
@@ -37,7 +37,7 @@ class LinearChainCrfOpKernel : public framework::OpKernel<T> {
};
template
<
typename
Place
,
typename
T
>
class
LinearChainC
rf
GradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
class
LinearChainC
RF
GradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录