Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
fbfd24e6
P
Paddle
项目概览
机器未来
/
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看板
提交
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,
...
@@ -47,81 +47,40 @@ bool CRFLayer::init(const LayerMap& layerMap,
// We don't need sequenceStartPositions because each sample of output_ is
// We don't need sequenceStartPositions because each sample of output_ is
// for the cost of one sequence.
// for the cost of one sequence.
setNeedSequenceInfo
(
false
);
setNeedSequenceInfo
(
false
);
if
(
useGpu_
)
{
tmpCpuInput_
.
reserve
(
inputLayers_
.
size
());
for
(
size_t
i
=
0
;
i
<
inputLayers_
.
size
();
i
++
)
{
tmpCpuInput_
.
push_back
(
Argument
());
}
}
return
true
;
return
true
;
}
}
void
CRFLayer
::
forward
(
PassType
passType
)
{
void
CRFLayer
::
forward
(
PassType
passType
)
{
Layer
::
forward
(
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
,
CHECK
(
!
useGpu_
)
<<
"GPU is not supported"
;
const
Argument
&
label
,
VectorPtr
parameterValue
,
const
Argument
&
output
=
getInput
(
0
);
VectorPtr
parameterGradient
)
{
const
Argument
&
label
=
getInput
(
1
);
CHECK
(
label
.
sequenceStartPositions
);
CHECK
(
label
.
sequenceStartPositions
);
CHECK
(
label
.
ids
);
CHECK
(
label
.
ids
);
int
batchSize
=
output
.
getBatchSize
();
int
batchSize
=
output
.
getBatchSize
();
size_t
numSequences
=
label
.
sequenceStartPositions
->
getSize
()
-
1
;
size_t
numSequences
=
label
.
sequenceStartPositions
->
getSize
()
-
1
;
resizeOutput
(
numSequences
,
1
);
resizeOutput
(
numSequences
,
1
);
std
::
vector
<
real
>
out
(
numSequences
);
const
int
*
starts
=
label
.
sequenceStartPositions
->
getData
(
false
);
const
int
*
starts
=
label
.
sequenceStartPositions
->
getData
(
false
);
CHECK_EQ
(
starts
[
numSequences
],
batchSize
);
CHECK_EQ
(
starts
[
numSequences
],
batchSize
);
VectorPtr
cpuParameterValue
;
VectorPtr
cpuParameterGradient
;
for
(
size_t
i
=
0
;
i
<
numSequences
;
++
i
)
{
for
(
size_t
i
=
0
;
i
<
numSequences
;
++
i
)
{
if
(
i
>=
crfs_
.
size
())
{
if
(
i
>=
crfs_
.
size
())
{
crfs_
.
emplace_back
(
numClasses_
,
crfs_
.
emplace_back
(
numClasses_
,
parameter
Value
->
getData
(),
parameter
_
->
getBuf
(
PARAMETER_VALUE
)
->
getData
(),
parameter
Gradient
parameter
_
->
getBuf
(
PARAMETER_GRADIENT
)
?
parameter
Gradient
->
getData
()
?
parameter
_
->
getBuf
(
PARAMETER_GRADIENT
)
->
getData
()
:
nullptr
);
:
nullptr
);
}
}
out
[
i
]
=
crfs_
[
i
].
forward
(
out
put_
.
value
->
getData
()
[
i
]
=
crfs_
[
i
].
forward
(
output
.
value
->
getData
()
+
numClasses_
*
starts
[
i
],
output
.
value
->
getData
()
+
numClasses_
*
starts
[
i
],
label
.
ids
->
getData
()
+
starts
[
i
],
starts
[
i
+
1
]
-
starts
[
i
]);
label
.
ids
->
getData
()
+
starts
[
i
],
starts
[
i
+
1
]
-
starts
[
i
]);
}
}
output_
.
value
->
copyFrom
(
out
.
data
(),
numSequences
);
if
(
weightLayer_
)
{
if
(
weightLayer_
)
{
const
MatrixPtr
&
weight
=
getInputValue
(
*
weightLayer_
);
const
MatrixPtr
&
weight
=
getInputValue
(
*
weightLayer_
);
getOutputValue
()
->
dotMul
(
*
getOutputValue
(),
*
weight
);
getOutputValue
()
->
dotMul
(
*
getOutputValue
(),
*
weight
);
...
@@ -129,22 +88,8 @@ void CRFLayer::forwardImp(const Argument&output,
...
@@ -129,22 +88,8 @@ void CRFLayer::forwardImp(const Argument&output,
}
}
void
CRFLayer
::
backward
(
const
UpdateCallback
&
callback
)
{
void
CRFLayer
::
backward
(
const
UpdateCallback
&
callback
)
{
(
void
)
callback
;
const
Argument
&
output
=
getInput
(
0
);
if
(
useGpu_
)
{
const
Argument
&
label
=
getInput
(
1
);
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
int
*
starts
=
label
.
sequenceStartPositions
->
getData
(
false
);
const
int
*
starts
=
label
.
sequenceStartPositions
->
getData
(
false
);
int
numSequences
=
label
.
sequenceStartPositions
->
getSize
()
-
1
;
int
numSequences
=
label
.
sequenceStartPositions
->
getSize
()
-
1
;
...
@@ -159,9 +104,11 @@ void CRFLayer::backwardImp(const UpdateCallback& callback,
...
@@ -159,9 +104,11 @@ void CRFLayer::backwardImp(const UpdateCallback& callback,
grad
->
mulScalar
(
weight
);
grad
->
mulScalar
(
weight
);
}
}
}
}
if
(
coeff_
!=
real
(
1.0
f
))
{
if
(
coeff_
!=
real
(
1.0
f
))
{
output
.
grad
->
mulScalar
(
coeff_
);
output
.
grad
->
mulScalar
(
coeff_
);
}
}
parameter_
->
incUpdate
(
callback
);
parameter_
->
incUpdate
(
callback
);
}
}
...
...
paddle/gserver/layers/CRFLayer.h
浏览文件 @
fbfd24e6
...
@@ -32,11 +32,7 @@ public:
...
@@ -32,11 +32,7 @@ public:
explicit
CRFLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
explicit
CRFLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
virtual
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
);
virtual
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
);
virtual
void
forward
(
PassType
passType
);
virtual
void
forward
(
PassType
passType
);
void
forwardImp
(
const
Argument
&
output
,
const
Argument
&
label
,
VectorPtr
parameterValue
,
VectorPtr
parameterGradient
);
virtual
void
backward
(
const
UpdateCallback
&
callback
);
virtual
void
backward
(
const
UpdateCallback
&
callback
);
void
backwardImp
(
const
UpdateCallback
&
callback
,
const
Argument
&
output
,
const
Argument
&
label
);
protected:
protected:
size_t
numClasses_
;
size_t
numClasses_
;
...
@@ -44,7 +40,6 @@ protected:
...
@@ -44,7 +40,6 @@ protected:
std
::
vector
<
LinearChainCRF
>
crfs_
;
std
::
vector
<
LinearChainCRF
>
crfs_
;
LayerPtr
weightLayer_
;
// weight for each sequence
LayerPtr
weightLayer_
;
// weight for each sequence
real
coeff_
;
// weight for the layer
real
coeff_
;
// weight for the layer
std
::
vector
<
Argument
>
tmpCpuInput_
;
};
};
}
// namespace paddle
}
// namespace paddle
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
fbfd24e6
...
@@ -179,10 +179,9 @@ TEST(Layer, CRFLayer) {
...
@@ -179,10 +179,9 @@ TEST(Layer, CRFLayer) {
config
.
layerConfig
.
add_inputs
();
config
.
layerConfig
.
add_inputs
();
config
.
layerConfig
.
add_inputs
();
config
.
layerConfig
.
add_inputs
();
for
(
auto
useGpu
:
{
false
,
true
})
{
// Not support GPU now
testLayerGrad
(
config
,
"crf"
,
100
,
/* trans */
false
,
/* useGpu */
useGpu
,
testLayerGrad
(
config
,
"crf"
,
100
,
/* trans */
false
,
/* useGpu */
false
,
false
/*useWeight*/
,
0.03
/*epsilon*/
);
false
/*useWeight*/
,
0.03
/*epsilon*/
);
}
}
}
TEST
(
Layer
,
CTCLayer
)
{
TEST
(
Layer
,
CTCLayer
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录