Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
35279398
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看板
提交
35279398
编写于
12月 21, 2016
作者:
P
Peng Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix merge and style error
上级
b063b0c4
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
23 addition
and
26 deletion
+23
-26
paddle/gserver/layers/CRFLayer.cpp
paddle/gserver/layers/CRFLayer.cpp
+8
-7
paddle/gserver/layers/CRFLayer.h
paddle/gserver/layers/CRFLayer.h
+2
-2
paddle/gserver/tests/test_CRFLayerGrad.cpp
paddle/gserver/tests/test_CRFLayerGrad.cpp
+13
-17
未找到文件。
paddle/gserver/layers/CRFLayer.cpp
浏览文件 @
35279398
...
...
@@ -42,8 +42,7 @@ bool CRFLayer::init(const LayerMap& layerMap,
CHECK_EQ
(
parameters_
[
0
]
->
getSize
(),
numClasses_
*
(
numClasses_
+
2
));
parameter_
=
parameters_
[
0
];
weight_
.
reset
(
new
Weight
(
numClasses_
+
2
,
numClasses_
,
parameter_
));
weight_
.
reset
(
new
Weight
(
numClasses_
+
2
,
numClasses_
,
parameter_
));
// We don't need sequenceStartPositions because each sample of output_ is
// for the cost of one sequence.
...
...
@@ -95,16 +94,18 @@ void CRFLayer::backward(const UpdateCallback& callback) {
for
(
int
i
=
0
;
i
<
numSequences
;
++
i
)
{
crfs_
[
i
].
backward
(
output
.
value
->
getData
()
+
numClasses_
*
starts
[
i
],
label
.
ids
->
getData
()
+
starts
[
i
],
starts
[
i
+
1
]
-
starts
[
i
],
needWGrad
);
real
instanceWeight
=
weightLayer_
?
getInputValue
(
*
weightLayer_
)
->
getElement
(
i
,
0
)
:
real
(
1.0
f
);
starts
[
i
+
1
]
-
starts
[
i
],
needWGrad
);
real
instanceWeight
=
weightLayer_
?
getInputValue
(
*
weightLayer_
)
->
getElement
(
i
,
0
)
:
real
(
1.0
f
);
instanceWeight
*=
coeff_
;
MatrixPtr
grad
=
output
.
grad
->
subRowMatrix
(
starts
[
i
],
starts
[
i
+
1
]);
grad
->
add
(
*
crfs_
[
i
].
getXGrad
(),
real
(
1.0
f
),
instanceWeight
);
if
(
needWGrad
)
{
weight_
->
getWGrad
()
->
add
(
*
crfs_
[
i
].
getWGrad
(),
real
(
1.0
f
),
instanceWeight
);
weight_
->
getWGrad
()
->
add
(
*
crfs_
[
i
].
getWGrad
(),
real
(
1.0
f
),
instanceWeight
);
}
}
...
...
paddle/gserver/layers/CRFLayer.h
浏览文件 @
35279398
...
...
@@ -37,9 +37,9 @@ protected:
size_t
numClasses_
;
ParameterPtr
parameter_
;
std
::
vector
<
LinearChainCRF
>
crfs_
;
LayerPtr
weightLayer_
;
// weight for each sequence
LayerPtr
weightLayer_
;
// weight for each sequence
std
::
unique_ptr
<
Weight
>
weight_
;
// parameters
real
coeff_
;
// weight for the layer
real
coeff_
;
// weight for the layer
};
}
// namespace paddle
paddle/gserver/tests/test_CRFLayerGrad.cpp
浏览文件 @
35279398
...
...
@@ -21,13 +21,10 @@ limitations under the License. */
#include "TestUtil.h"
#include "LayerGradUtil.h"
using
namespace
paddle
;
// NOLINT
using
namespace
paddle
;
// NOLINT
P_DECLARE_bool
(
use_gpu
);
P_DECLARE_int32
(
gpu_id
);
P_DECLARE_double
(
checkgrad_eps
);
P_DECLARE_bool
(
thread_local_rand_use_global_seed
);
P_DECLARE_bool
(
prev_batch_state
);
DECLARE_int32
(
gpu_id
);
DECLARE_bool
(
thread_local_rand_use_global_seed
);
static
inline
bool
getNextSequence
(
std
::
vector
<
int
>&
seq
,
int
numClasses
)
{
for
(
auto
&
v
:
seq
)
{
...
...
@@ -96,8 +93,8 @@ TEST(CRFLayer, cost) {
real
diff
=
fabs
(
trueCost
-
cost
);
diff
/=
fabs
(
cost
)
<
fabs
(
trueCost
)
?
fabs
(
cost
)
:
fabs
(
trueCost
);
VLOG
(
1
)
<<
"cost="
<<
cost
<<
" trueCost="
<<
trueCost
<<
" diff="
<<
diff
<<
std
::
endl
;
VLOG
(
1
)
<<
"cost="
<<
cost
<<
" trueCost="
<<
trueCost
<<
" diff="
<<
diff
<<
std
::
endl
;
if
(
typeid
(
real
)
==
typeid
(
double
))
{
// NOLINT
EXPECT_LE
(
diff
,
1e-10
);
}
else
{
...
...
@@ -107,9 +104,7 @@ TEST(CRFLayer, cost) {
}
}
inline
real
epsilon
()
{
return
typeid
(
real
)
==
typeid
(
double
)
?
1e-10
:
0.06
;
}
inline
real
epsilon
()
{
return
typeid
(
real
)
==
typeid
(
double
)
?
1e-10
:
0.06
;
}
TestConfig
initTestConfig
(
size_t
numClasses
,
bool
withWeight
)
{
TestConfig
config
;
...
...
@@ -117,16 +112,17 @@ TestConfig initTestConfig(size_t numClasses, bool withWeight) {
config
.
layerConfig
.
set_size
(
numClasses
);
config
.
biasSize
=
0
;
config
.
inputDefs
.
push_back
({
INPUT_SEQUENCE_DATA
,
"layer_0"
,
numClasses
,
numClasses
*
(
numClasses
+
2
)});
config
.
inputDefs
.
push_back
({
INPUT_SEQUENCE_DATA
,
"layer_0"
,
numClasses
,
numClasses
*
(
numClasses
+
2
)});
config
.
layerConfig
.
add_inputs
();
config
.
inputDefs
.
push_back
(
{
INPUT_SEQUENCE_LABEL
,
"layer_label"
,
numClasses
,
0
});
config
.
inputDefs
.
push_back
(
{
INPUT_SEQUENCE_LABEL
,
"layer_label"
,
numClasses
,
0
});
config
.
layerConfig
.
add_inputs
();
if
(
withWeight
)
{
config
.
inputDefs
.
push_back
({
INPUT_DENSE_DIM_DATA
,
"layer_weight"
,
1
,
0
});
config
.
inputDefs
.
push_back
({
INPUT_DENSE_DIM_DATA
,
"layer_weight"
,
1
,
0
});
config
.
layerConfig
.
add_inputs
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录