Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
3bce32ba
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
提交
3bce32ba
编写于
3月 23, 2017
作者:
G
gaoyuan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add create matrix pointer funtion
上级
17c697c7
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
42 addition
and
39 deletion
+42
-39
paddle/gserver/layers/CrossChannelNormLayer.cpp
paddle/gserver/layers/CrossChannelNormLayer.cpp
+40
-38
paddle/gserver/layers/NormLayer.h
paddle/gserver/layers/NormLayer.h
+2
-1
未找到文件。
paddle/gserver/layers/CrossChannelNormLayer.cpp
浏览文件 @
3bce32ba
...
@@ -19,6 +19,23 @@ limitations under the License. */
...
@@ -19,6 +19,23 @@ limitations under the License. */
namespace
paddle
{
namespace
paddle
{
MatrixPtr
CrossChannelNormLayer
::
createSampleMatrix
(
MatrixPtr
data
,
size_t
iter
,
size_t
spatialDim
)
{
return
Matrix
::
create
(
data
->
getData
()
+
iter
*
channels_
*
spatialDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
}
MatrixPtr
CrossChannelNormLayer
::
createSpatialMatrix
(
MatrixPtr
data
,
size_t
iter
,
size_t
spatialDim
)
{
return
Matrix
::
create
(
data
->
getData
()
+
iter
*
spatialDim
,
1
,
spatialDim
,
false
,
useGpu_
);
}
void
CrossChannelNormLayer
::
forward
(
PassType
passType
)
{
void
CrossChannelNormLayer
::
forward
(
PassType
passType
)
{
Layer
::
forward
(
passType
);
Layer
::
forward
(
passType
);
MatrixPtr
inV
=
getInputValue
(
0
);
MatrixPtr
inV
=
getInputValue
(
0
);
...
@@ -40,25 +57,19 @@ void CrossChannelNormLayer::forward(PassType passType) {
...
@@ -40,25 +57,19 @@ void CrossChannelNormLayer::forward(PassType passType) {
normBuffer_
->
addScalar
(
*
normBuffer_
,
1e-6
);
normBuffer_
->
addScalar
(
*
normBuffer_
,
1e-6
);
inV
->
square2
(
*
dataBuffer_
);
inV
->
square2
(
*
dataBuffer_
);
for
(
size_t
i
=
0
;
i
<
batchSize
;
i
++
)
{
for
(
size_t
i
=
0
;
i
<
batchSize
;
i
++
)
{
MatrixPtr
inTmp
=
Matrix
::
create
(
const
MatrixPtr
inVTmp
=
createSampleMatrix
(
inV
,
i
,
spatialDim
);
inV
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
const
MatrixPtr
dataTmp
=
createSampleMatrix
(
dataBuffer_
,
i
,
spatialDim
);
MatrixPtr
dataTmp
=
Matrix
::
create
(
dataBuffer_
->
getData
()
+
i
*
dataDim
,
MatrixPtr
outVTmp
=
createSampleMatrix
(
outV
,
i
,
spatialDim
);
channels_
,
MatrixPtr
normTmp
=
createSpatialMatrix
(
normBuffer_
,
i
,
spatialDim
);
spatialDim
,
false
,
useGpu_
);
MatrixPtr
outTmp
=
Matrix
::
create
(
outV
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
MatrixPtr
normTmp
=
Matrix
::
create
(
normBuffer_
->
getData
()
+
i
*
spatialDim
,
1
,
spatialDim
,
false
,
useGpu_
);
// compute norm.
// compute norm.
spatialBuffer_
->
sumCols
(
*
dataTmp
,
1
,
1
);
spatialBuffer_
->
sumCols
(
*
dataTmp
,
1
,
0
);
spatialBuffer_
->
sqrt2
(
*
spatialBuffer_
);
spatialBuffer_
->
sqrt2
(
*
spatialBuffer_
);
normTmp
->
copyFrom
(
*
spatialBuffer_
);
normTmp
->
copyFrom
(
*
spatialBuffer_
);
out
Tmp
->
copyFrom
(
*
in
Tmp
);
out
VTmp
->
copyFrom
(
*
inV
Tmp
);
outTmp
->
divRowVector
(
*
spatialBuffer_
);
out
V
Tmp
->
divRowVector
(
*
spatialBuffer_
);
// scale the layer.
// scale the layer.
outTmp
->
mulColVector
(
*
scale_
->
getW
());
out
V
Tmp
->
mulColVector
(
*
scale_
->
getW
());
}
}
}
}
...
@@ -78,40 +89,31 @@ void CrossChannelNormLayer::backward(const UpdateCallback& callback) {
...
@@ -78,40 +89,31 @@ void CrossChannelNormLayer::backward(const UpdateCallback& callback) {
Matrix
::
resizeOrCreate
(
sampleBuffer_
,
channels_
,
spatialDim
,
false
,
useGpu_
);
Matrix
::
resizeOrCreate
(
sampleBuffer_
,
channels_
,
spatialDim
,
false
,
useGpu_
);
scaleDiff_
->
zeroMem
();
scaleDiff_
->
zeroMem
();
for
(
size_t
i
=
0
;
i
<
batchSize
;
i
++
)
{
for
(
size_t
i
=
0
;
i
<
batchSize
;
i
++
)
{
// propagate to param.
MatrixPtr
outGTmp
=
createSampleMatrix
(
outG
,
i
,
spatialDim
);
MatrixPtr
dataBufferTmp
=
const
MatrixPtr
dataTmp
=
createSampleMatrix
(
dataBuffer_
,
i
,
spatialDim
);
Matrix
::
create
(
dataBuffer_
->
getData
()
+
i
*
dataDim
,
const
MatrixPtr
inVTmp
=
createSampleMatrix
(
inV
,
i
,
spatialDim
);
channels_
,
const
MatrixPtr
inGTmp
=
createSampleMatrix
(
inG
,
i
,
spatialDim
);
spatialDim
,
const
MatrixPtr
normTmp
=
createSpatialMatrix
(
normBuffer_
,
i
,
spatialDim
);
false
,
useGpu_
);
channelBuffer_
->
sumRows
(
*
dataTmp
,
1
,
0
);
const
MatrixPtr
inValueTmp
=
Matrix
::
create
(
inV
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
const
MatrixPtr
outGradTmp
=
Matrix
::
create
(
outG
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
MatrixPtr
inGradTmp
=
Matrix
::
create
(
inG
->
getData
()
+
i
*
dataDim
,
channels_
,
spatialDim
,
false
,
useGpu_
);
const
MatrixPtr
normTmp
=
Matrix
::
create
(
normBuffer_
->
getData
()
+
i
*
spatialDim
,
1
,
spatialDim
,
false
,
useGpu_
);
channelBuffer_
->
sumRows
(
*
dataBufferTmp
,
1
,
1
);
channelBuffer_
->
dotDiv
(
*
channelBuffer_
,
*
(
scale_
->
getW
()));
channelBuffer_
->
dotDiv
(
*
channelBuffer_
,
*
(
scale_
->
getW
()));
// store a / scale[i] in scaleDiff_ temporary
// store a / scale[i] in scaleDiff_ temporary
scaleDiff_
->
add
(
*
channelBuffer_
,
1.
);
scaleDiff_
->
add
(
*
channelBuffer_
,
1.
);
sampleBuffer_
->
dotMul
(
*
inV
alueTmp
,
*
outGrad
Tmp
);
sampleBuffer_
->
dotMul
(
*
inV
Tmp
,
*
outG
Tmp
);
spatialBuffer_
->
sumCols
(
*
sampleBuffer_
,
1.
,
1.
);
spatialBuffer_
->
sumCols
(
*
sampleBuffer_
,
1.
,
1.
);
// scale the grad
// scale the grad
inG
radTmp
->
copyFrom
(
*
inValue
Tmp
);
inG
Tmp
->
copyFrom
(
*
inV
Tmp
);
inG
rad
Tmp
->
mulRowVector
(
*
spatialBuffer_
);
inGTmp
->
mulRowVector
(
*
spatialBuffer_
);
// divide by square of norm
// divide by square of norm
spatialBuffer_
->
dotMul
(
*
normTmp
,
*
normTmp
);
spatialBuffer_
->
dotMul
(
*
normTmp
,
*
normTmp
);
inG
rad
Tmp
->
divRowVector
(
*
spatialBuffer_
);
inGTmp
->
divRowVector
(
*
spatialBuffer_
);
// subtract
// subtract
inG
radTmp
->
add
(
*
outGrad
Tmp
,
-
1
,
1
);
inG
Tmp
->
add
(
*
outG
Tmp
,
-
1
,
1
);
// divide by norm
// divide by norm
inG
rad
Tmp
->
divRowVector
(
*
normTmp
);
inGTmp
->
divRowVector
(
*
normTmp
);
// scale the diff
// scale the diff
inG
rad
Tmp
->
mulColVector
(
*
scale_
->
getW
());
inGTmp
->
mulColVector
(
*
scale_
->
getW
());
}
}
// updata scale
// updata scale
if
(
scale_
->
getWGrad
())
scale_
->
getWGrad
()
->
copyFrom
(
*
scaleDiff_
);
if
(
scale_
->
getWGrad
())
scale_
->
getWGrad
()
->
copyFrom
(
*
scaleDiff_
);
...
...
paddle/gserver/layers/NormLayer.h
浏览文件 @
3bce32ba
...
@@ -80,9 +80,10 @@ public:
...
@@ -80,9 +80,10 @@ public:
explicit
CrossChannelNormLayer
(
const
LayerConfig
&
config
)
explicit
CrossChannelNormLayer
(
const
LayerConfig
&
config
)
:
NormLayer
(
config
)
{}
:
NormLayer
(
config
)
{}
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
);
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
);
void
forward
(
PassType
passType
);
void
forward
(
PassType
passType
);
void
backward
(
const
UpdateCallback
&
callback
);
void
backward
(
const
UpdateCallback
&
callback
);
MatrixPtr
createSampleMatrix
(
MatrixPtr
data
,
size_t
iter
,
size_t
spatialDim
);
MatrixPtr
createSpatialMatrix
(
MatrixPtr
data
,
size_t
iter
,
size_t
spatialDim
);
protected:
protected:
size_t
channels_
;
size_t
channels_
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录