Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
fbfd24e6
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
fbfd24e6
编写于
9月 07, 2016
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
revert CRFLayer, remove wrong gpu support
Change-Id: I636cf13af5becb1168bc9749266b55580c46f6c9
上级
6a873f50
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
17 addition
and
76 deletion
+17
-76
paddle/gserver/layers/CRFLayer.cpp
paddle/gserver/layers/CRFLayer.cpp
+14
-67
paddle/gserver/layers/CRFLayer.h
paddle/gserver/layers/CRFLayer.h
+0
-5
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+3
-4
未找到文件。
paddle/gserver/layers/CRFLayer.cpp
浏览文件 @
fbfd24e6
...
...
@@ -47,81 +47,40 @@ bool CRFLayer::init(const LayerMap& layerMap,
// We don't need sequenceStartPositions because each sample of output_ is
// for the cost of one sequence.
setNeedSequenceInfo
(
false
);
if
(
useGpu_
)
{
tmpCpuInput_
.
reserve
(
inputLayers_
.
size
());
for
(
size_t
i
=
0
;
i
<
inputLayers_
.
size
();
i
++
)
{
tmpCpuInput_
.
push_back
(
Argument
());
}
}
return
true
;
}
void
CRFLayer
::
forward
(
PassType
passType
)
{
Layer
::
forward
(
passType
);
if
(
useGpu_
)
{
for
(
size_t
i
=
0
;
i
<
inputLayers_
.
size
();
i
++
)
{
tmpCpuInput_
[
i
].
resizeAndCopyFrom
(
getInput
(
i
),
false
,
HPPL_STREAM_1
);
}
VectorPtr
cpuParameterValue
;
VectorPtr
cpuParameterGradient
;
cpuParameterValue
=
Vector
::
create
(
parameter_
->
getBuf
(
PARAMETER_VALUE
)
->
getSize
(),
false
);
cpuParameterValue
->
copyFrom
(
*
parameter_
->
getBuf
(
PARAMETER_VALUE
),
HPPL_STREAM_1
);
if
(
parameter_
->
getBuf
(
PARAMETER_GRADIENT
))
{
cpuParameterGradient
=
Vector
::
create
(
parameter_
->
getBuf
(
PARAMETER_GRADIENT
)
->
getSize
(),
false
);
cpuParameterGradient
->
copyFrom
(
*
parameter_
->
getBuf
(
PARAMETER_GRADIENT
),
HPPL_STREAM_1
);
}
else
{
cpuParameterGradient
=
nullptr
;
}
forwardImp
(
tmpCpuInput_
[
0
],
tmpCpuInput_
[
1
],
cpuParameterValue
,
cpuParameterGradient
);
parameter_
->
getBuf
(
PARAMETER_VALUE
)
->
copyFrom
(
*
cpuParameterValue
,
HPPL_STREAM_1
);
if
(
parameter_
->
getBuf
(
PARAMETER_GRADIENT
))
{
parameter_
->
getBuf
(
PARAMETER_GRADIENT
)
->
copyFrom
(
*
cpuParameterGradient
,
HPPL_STREAM_1
);
}
}
else
{
forwardImp
(
getInput
(
0
),
getInput
(
1
),
parameter_
->
getBuf
(
PARAMETER_VALUE
),
parameter_
->
getBuf
(
PARAMETER_GRADIENT
));
}
}
void
CRFLayer
::
forwardImp
(
const
Argument
&
output
,
const
Argument
&
label
,
VectorPtr
parameterValue
,
VectorPtr
parameterGradient
)
{
CHECK
(
!
useGpu_
)
<<
"GPU is not supported"
;
const
Argument
&
output
=
getInput
(
0
);
const
Argument
&
label
=
getInput
(
1
);
CHECK
(
label
.
sequenceStartPositions
);
CHECK
(
label
.
ids
);
int
batchSize
=
output
.
getBatchSize
();
size_t
numSequences
=
label
.
sequenceStartPositions
->
getSize
()
-
1
;
resizeOutput
(
numSequences
,
1
);
std
::
vector
<
real
>
out
(
numSequences
);
const
int
*
starts
=
label
.
sequenceStartPositions
->
getData
(
false
);
CHECK_EQ
(
starts
[
numSequences
],
batchSize
);
VectorPtr
cpuParameterValue
;
VectorPtr
cpuParameterGradient
;
for
(
size_t
i
=
0
;
i
<
numSequences
;
++
i
)
{
if
(
i
>=
crfs_
.
size
())
{
crfs_
.
emplace_back
(
numClasses_
,
parameter
Value
->
getData
(),
parameter
Gradient
?
parameter
Gradient
->
getData
()
parameter
_
->
getBuf
(
PARAMETER_VALUE
)
->
getData
(),
parameter
_
->
getBuf
(
PARAMETER_GRADIENT
)
?
parameter
_
->
getBuf
(
PARAMETER_GRADIENT
)
->
getData
()
:
nullptr
);
}
out
[
i
]
=
crfs_
[
i
].
forward
(
out
put_
.
value
->
getData
()
[
i
]
=
crfs_
[
i
].
forward
(
output
.
value
->
getData
()
+
numClasses_
*
starts
[
i
],
label
.
ids
->
getData
()
+
starts
[
i
],
starts
[
i
+
1
]
-
starts
[
i
]);
}
output_
.
value
->
copyFrom
(
out
.
data
(),
numSequences
);
if
(
weightLayer_
)
{
const
MatrixPtr
&
weight
=
getInputValue
(
*
weightLayer_
);
getOutputValue
()
->
dotMul
(
*
getOutputValue
(),
*
weight
);
...
...
@@ -129,22 +88,8 @@ void CRFLayer::forwardImp(const Argument&output,
}
void
CRFLayer
::
backward
(
const
UpdateCallback
&
callback
)
{
(
void
)
callback
;
if
(
useGpu_
)
{
backwardImp
(
callback
,
tmpCpuInput_
[
0
],
tmpCpuInput_
[
1
]);
const_cast
<
Argument
&>
(
getInput
(
0
)).
resizeAndCopyFrom
(
tmpCpuInput_
[
0
],
true
,
HPPL_STREAM_1
);
const_cast
<
Argument
&>
(
getInput
(
1
)).
resizeAndCopyFrom
(
tmpCpuInput_
[
1
],
true
,
HPPL_STREAM_1
);
}
else
{
backwardImp
(
callback
,
getInput
(
0
),
getInput
(
1
));
}
}
void
CRFLayer
::
backwardImp
(
const
UpdateCallback
&
callback
,
const
Argument
&
output
,
const
Argument
&
label
)
{
const
Argument
&
output
=
getInput
(
0
);
const
Argument
&
label
=
getInput
(
1
);
const
int
*
starts
=
label
.
sequenceStartPositions
->
getData
(
false
);
int
numSequences
=
label
.
sequenceStartPositions
->
getSize
()
-
1
;
...
...
@@ -159,9 +104,11 @@ void CRFLayer::backwardImp(const UpdateCallback& callback,
grad
->
mulScalar
(
weight
);
}
}
if
(
coeff_
!=
real
(
1.0
f
))
{
output
.
grad
->
mulScalar
(
coeff_
);
}
parameter_
->
incUpdate
(
callback
);
}
...
...
paddle/gserver/layers/CRFLayer.h
浏览文件 @
fbfd24e6
...
...
@@ -32,11 +32,7 @@ public:
explicit
CRFLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
virtual
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
);
virtual
void
forward
(
PassType
passType
);
void
forwardImp
(
const
Argument
&
output
,
const
Argument
&
label
,
VectorPtr
parameterValue
,
VectorPtr
parameterGradient
);
virtual
void
backward
(
const
UpdateCallback
&
callback
);
void
backwardImp
(
const
UpdateCallback
&
callback
,
const
Argument
&
output
,
const
Argument
&
label
);
protected:
size_t
numClasses_
;
...
...
@@ -44,7 +40,6 @@ protected:
std
::
vector
<
LinearChainCRF
>
crfs_
;
LayerPtr
weightLayer_
;
// weight for each sequence
real
coeff_
;
// weight for the layer
std
::
vector
<
Argument
>
tmpCpuInput_
;
};
}
// namespace paddle
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
fbfd24e6
...
...
@@ -179,10 +179,9 @@ TEST(Layer, CRFLayer) {
config
.
layerConfig
.
add_inputs
();
config
.
layerConfig
.
add_inputs
();
for
(
auto
useGpu
:
{
false
,
true
})
{
testLayerGrad
(
config
,
"crf"
,
100
,
/* trans */
false
,
/* useGpu */
useGpu
,
false
/*useWeight*/
,
0.03
/*epsilon*/
);
}
// Not support GPU now
testLayerGrad
(
config
,
"crf"
,
100
,
/* trans */
false
,
/* useGpu */
false
,
false
/*useWeight*/
,
0.03
/*epsilon*/
);
}
TEST
(
Layer
,
CTCLayer
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录