Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wzpei9
Paddle
提交
cda3a774
P
Paddle
项目概览
wzpei9
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
cda3a774
编写于
11月 27, 2017
作者:
P
peterzhang2029
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
bug fix when using hsigmoid with gpu
上级
54b39949
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
134 addition
and
16 deletion
+134
-16
paddle/gserver/layers/HierarchicalSigmoidLayer.cpp
paddle/gserver/layers/HierarchicalSigmoidLayer.cpp
+124
-16
paddle/gserver/layers/HierarchicalSigmoidLayer.h
paddle/gserver/layers/HierarchicalSigmoidLayer.h
+10
-0
未找到文件。
paddle/gserver/layers/HierarchicalSigmoidLayer.cpp
浏览文件 @
cda3a774
...
...
@@ -64,49 +64,113 @@ void HierarchicalSigmoidLayer::forward(PassType passType) {
batchSize
,
codeLength_
,
/* trans */
false
,
useGpu
(
deviceId_
)
);
false
);
Matrix
::
resizeOrCreate
(
preOutput_
.
grad
,
batchSize
,
codeLength_
,
/* trans */
false
,
useGpu
(
deviceId_
));
false
);
IVectorPtr
label
=
getInput
(
*
getLabelLayer
()).
ids
;
preOutput_
.
value
->
zeroMem
();
if
(
useGpu_
)
{
Matrix
::
resizeOrCreate
(
cpuOutput_
,
output_
.
value
->
getHeight
(),
output_
.
value
->
getWidth
(),
/* trans */
false
,
false
);
IVector
::
resizeOrCreate
(
cpuLabel_
,
label
->
getSize
(),
false
);
cpuLabel_
->
copyFrom
(
*
label
);
cpuOutput_
->
copyFrom
(
*
output_
.
value
);
}
else
{
cpuOutput_
=
output_
.
value
;
cpuLabel_
=
label
;
}
/* add the bias-vector */
if
(
biases_
.
get
()
!=
NULL
)
{
preOutput_
.
value
->
addByBitCode
(
numClasses_
,
*
label
,
*
biases_
->
getW
());
if
(
useGpu_
)
{
Matrix
::
resizeOrCreate
(
cpuBias_
,
1
,
numClasses_
-
1
,
/* trans */
false
,
false
);
cpuBias_
->
copyFrom
(
*
biases_
->
getW
());
}
else
{
cpuBias_
=
biases_
->
getW
();
}
preOutput_
.
value
->
addByBitCode
(
numClasses_
,
*
cpuLabel_
,
*
cpuBias_
);
}
for
(
size_t
i
=
0
;
i
<
inputLayers_
.
size
()
-
1
;
++
i
)
{
MatrixPtr
input
=
getInputValue
(
i
);
if
(
useGpu_
)
{
Matrix
::
resizeOrCreate
(
cpuInput_
,
input
->
getHeight
(),
input
->
getWidth
(),
/* trans */
false
,
false
);
Matrix
::
resizeOrCreate
(
cpuWeight_
,
weights_
[
i
]
->
getW
()
->
getHeight
(),
weights_
[
i
]
->
getW
()
->
getWidth
(),
/* trans */
false
,
false
);
cpuInput_
->
copyFrom
(
*
input
);
cpuWeight_
->
copyFrom
(
*
weights_
[
i
]
->
getW
());
}
else
{
cpuInput_
=
input
;
cpuWeight_
=
weights_
[
i
]
->
getW
();
}
preOutput_
.
value
->
mulByBitCode
(
numClasses_
,
*
label
,
*
weights_
[
i
]
->
getW
(),
*
input
);
numClasses_
,
*
cpuLabel_
,
*
cpuWeight_
,
*
cpuInput_
);
}
// keep consistent with the clipping in the following softrelu
preOutput_
.
value
->
clip
(
-
40.0
,
40.0
);
preOutput_
.
value
->
sumByBitCode
(
numClasses_
,
*
label
,
*
output_
.
value
,
*
cpuLabel_
,
*
cpuOutput_
,
-
1
);
// scaleSum
preOutput_
.
value
->
softrelu
(
*
preOutput_
.
value
);
MatrixPtr
sum
=
Matrix
::
create
(
batchSize
,
1
,
/* trans= */
false
,
useGpu
(
deviceId_
)
);
Matrix
::
create
(
batchSize
,
1
,
/* trans= */
false
,
false
);
preOutput_
.
value
->
rowSum
(
*
sum
);
output_
.
value
->
add
(
*
sum
);
cpuOutput_
->
add
(
*
sum
);
if
(
useGpu_
)
{
output_
.
value
->
copyFrom
(
*
cpuOutput_
);
}
else
{
output_
.
value
=
cpuOutput_
;
}
}
void
HierarchicalSigmoidLayer
::
backward
(
const
UpdateCallback
&
callback
)
{
IVectorPtr
label
=
getInput
(
*
getLabelLayer
()).
ids
;
if
(
useGpu_
)
{
IVector
::
resizeOrCreate
(
cpuLabel_
,
label
->
getSize
(),
false
);
cpuLabel_
->
copyFrom
(
*
label
);
}
else
{
cpuLabel_
=
label
;
}
preOutput_
.
grad
->
one
();
preOutput_
.
grad
->
softreluDerivative
(
*
preOutput_
.
value
);
preOutput_
.
grad
->
subByBitCode
(
numClasses_
,
*
label
);
preOutput_
.
grad
->
subByBitCode
(
numClasses_
,
*
cpuLabel_
);
if
(
biases_
&&
biases_
->
getWGrad
())
{
MatrixPtr
biases_grad
=
biases_
->
getWGrad
();
if
(
useGpu_
)
{
Matrix
::
resizeOrCreate
(
cpuBias_
,
1
,
numClasses_
-
1
,
/* trans */
false
,
false
);
cpuBias_
->
copyFrom
(
*
biases_grad
);
}
else
{
cpuBias_
=
biases_grad
;
}
preOutput_
.
grad
->
addByBitCodeBackward
(
numClasses_
,
*
label
,
*
biases_
->
getWGrad
());
numClasses_
,
*
cpuLabel_
,
*
cpuBias_
);
if
(
useGpu
)
{
biases_grad
->
copyFrom
(
*
cpuBias_
);
}
else
{
biases_grad
=
cpuBias_
;
}
/* Increasing the number of gradient */
biases_
->
getParameterPtr
()
->
incUpdate
(
callback
);
}
...
...
@@ -115,9 +179,31 @@ void HierarchicalSigmoidLayer::backward(const UpdateCallback& callback) {
/* Calculate the W-gradient for the current layer */
MatrixPtr
input
=
getInputValue
(
i
);
if
(
weights_
[
i
]
->
getWGrad
())
{
MatrixPtr
weights_grad
=
weights_
[
i
]
->
getWGrad
();
if
(
useGpu_
)
{
Matrix
::
resizeOrCreate
(
cpuInput_
,
input
->
getHeight
(),
input
->
getWidth
(),
/* trans */
false
,
false
);
Matrix
::
resizeOrCreate
(
cpuWeightGrad_
,
weights_grad
->
getHeight
(),
weights_grad
->
getWidth
(),
/* trans */
false
,
false
);
cpuInput_
->
copyFrom
(
*
input
);
cpuWeightGrad_
->
copyFrom
(
*
weights_grad
);
}
else
{
cpuInput_
=
input
;
cpuWeightGrad_
=
weights_grad
;
}
preOutput_
.
grad
->
mulByBitCodeBackwardWeight
(
numClasses_
,
*
label
,
*
weights_
[
i
]
->
getWGrad
(),
*
input
);
numClasses_
,
*
cpuLabel_
,
*
cpuWeightGrad_
,
*
cpuInput_
);
if
(
useGpu_
)
{
weights_grad
->
copyFrom
(
*
cpuWeightGrad_
);
}
else
{
weights_grad
=
cpuWeightGrad_
;
}
/* Increasing the number of gradient */
weights_
[
i
]
->
getParameterPtr
()
->
incUpdate
(
callback
);
}
...
...
@@ -125,8 +211,30 @@ void HierarchicalSigmoidLayer::backward(const UpdateCallback& callback) {
/* Calculate the input layers error */
MatrixPtr
inputGrad
=
getInputGrad
(
i
);
if
(
inputGrad
)
{
if
(
useGpu_
)
{
Matrix
::
resizeOrCreate
(
cpuInputGrad_
,
inputGrad
->
getHeight
(),
inputGrad
->
getWidth
(),
/* trans */
false
,
false
);
Matrix
::
resizeOrCreate
(
cpuWeight_
,
weights_
[
i
]
->
getW
()
->
getHeight
(),
weights_
[
i
]
->
getW
()
->
getWidth
(),
/* trans */
false
,
false
);
cpuInputGrad_
->
copyFrom
(
*
inputGrad
);
cpuWeight_
->
copyFrom
(
*
weights_
[
i
]
->
getW
());
}
else
{
cpuInputGrad_
=
inputGrad
;
cpuWeight_
=
weights_
[
i
]
->
getW
();
}
preOutput_
.
grad
->
mulByBitCodeBackwardError
(
numClasses_
,
*
label
,
*
weights_
[
i
]
->
getW
(),
*
inputGrad
);
numClasses_
,
*
cpuLabel_
,
*
cpuWeight_
,
*
cpuInputGrad_
);
if
(
useGpu_
)
{
inputGrad
->
copyFrom
(
*
cpuInputGrad_
);
}
else
{
inputGrad
=
cpuInputGrad_
;
}
}
}
}
...
...
paddle/gserver/layers/HierarchicalSigmoidLayer.h
浏览文件 @
cda3a774
...
...
@@ -80,6 +80,16 @@ protected:
int
codeLength_
;
/// temporary result of output_
Argument
preOutput_
;
/// The temporary variables in CPU memory.
MatrixPtr
cpuWeight_
;
MatrixPtr
cpuWeightGrad_
;
MatrixPtr
cpuInput_
;
MatrixPtr
cpuInputGrad_
;
MatrixPtr
cpuBias_
;
MatrixPtr
cpuOutput_
;
IVectorPtr
cpuLabel_
;
};
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录