Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
84660653
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看板
提交
84660653
编写于
7月 27, 2017
作者:
G
guosheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add RowL2NormLayer
上级
2200ff5e
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
160 addition
and
0 deletion
+160
-0
paddle/gserver/layers/RowL2NormLayer.cpp
paddle/gserver/layers/RowL2NormLayer.cpp
+99
-0
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+13
-0
python/paddle/trainer/config_parser.py
python/paddle/trainer/config_parser.py
+10
-0
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+38
-0
未找到文件。
paddle/gserver/layers/RowL2NormLayer.cpp
0 → 100644
浏览文件 @
84660653
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "Layer.h"
#include "paddle/math/Matrix.h"
namespace
paddle
{
/**
* A layer for L2 normalization in each row,
* \f[
* out[i] = \frac{in[i]}{\sqrt{\sum_{k=1}^N in[k]^{2}}}
* \f]
* where the size of \f$in\f$ is (batchSize x dataDim),
* and the size of \f$out\f$ is (batchSize x dataDim).
*/
class
RowL2NormLayer
:
public
Layer
{
protected:
MatrixPtr
inSquare_
;
MatrixPtr
reciSqrtRowSquareSum_
;
MatrixPtr
dotSum_
;
public:
explicit
RowL2NormLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
override
;
void
forward
(
PassType
passType
)
override
;
void
backward
(
const
UpdateCallback
&
callback
=
nullptr
)
override
;
};
REGISTER_LAYER
(
row_l2_norm
,
RowL2NormLayer
);
bool
RowL2NormLayer
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
Layer
::
init
(
layerMap
,
parameterMap
);
CHECK_EQ
(
inputLayers_
.
size
(),
1U
);
return
true
;
}
void
RowL2NormLayer
::
forward
(
PassType
passType
)
{
Layer
::
forward
(
passType
);
MatrixPtr
inV
=
getInputValue
(
0
);
/* malloc memory for the output_ if necessary */
size_t
batchSize
=
inV
->
getHeight
();
size_t
dataDim
=
getSize
();
CHECK_EQ
(
dataDim
,
inV
->
getWidth
());
resetOutput
(
batchSize
,
dataDim
);
MatrixPtr
outV
=
getOutputValue
();
Matrix
::
resizeOrCreate
(
inSquare_
,
batchSize
,
dataDim
,
false
,
useGpu_
);
inV
->
square2
(
*
inSquare_
);
Matrix
::
resizeOrCreate
(
reciSqrtRowSquareSum_
,
batchSize
,
1
,
false
,
useGpu_
);
inSquare_
->
rowSum
(
*
reciSqrtRowSquareSum_
);
reciSqrtRowSquareSum_
->
sqrt2
(
*
reciSqrtRowSquareSum_
);
reciSqrtRowSquareSum_
->
scalarDiv
(
*
reciSqrtRowSquareSum_
,
1.0
);
outV
->
rowScale
(
0
,
*
inV
,
*
reciSqrtRowSquareSum_
);
}
void
RowL2NormLayer
::
backward
(
const
UpdateCallback
&
callback
)
{
MatrixPtr
inV
=
getInputValue
(
0
);
MatrixPtr
inG
=
getInputGrad
(
0
);
MatrixPtr
outV
=
getOutputValue
();
MatrixPtr
outG
=
getOutputGrad
();
size_t
batchSize
=
inV
->
getHeight
();
// inG[ij] += outG[ij] / reciSqrtRowSquareSum
// inG[ij] += -inV[ij] * reciSqrtRowSquareSum * reciSqrtRowSquareSum *
// DotMul(outG[i], inV[i])
if
(
inG
)
{
Matrix
::
resizeOrCreate
(
dotSum_
,
batchSize
,
1
,
false
,
useGpu_
);
dotSum_
->
zeroMem
();
dotSum_
->
rowDotMul
(
0
,
*
outG
,
*
outV
);
dotSum_
->
dotMul
(
*
dotSum_
,
*
reciSqrtRowSquareSum_
);
dotSum_
->
dotMul
(
*
dotSum_
,
*
reciSqrtRowSquareSum_
);
inSquare_
->
rowScale
(
0
,
*
inV
,
*
dotSum_
);
inG
->
sub
(
*
inSquare_
);
inG
->
addRowScale
(
0
,
*
outG
,
*
reciSqrtRowSquareSum_
);
}
}
}
// namespace paddle
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
84660653
...
@@ -1879,6 +1879,19 @@ TEST(Layer, CropLayer) {
...
@@ -1879,6 +1879,19 @@ TEST(Layer, CropLayer) {
}
}
}
}
TEST
(
Layer
,
RowL2NormLayer
)
{
const
size_t
batchSize
=
128
;
const
size_t
size
=
512
;
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"row_l2_norm"
);
config
.
layerConfig
.
set_size
(
size
);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"input"
,
size
,
0
});
config
.
layerConfig
.
add_inputs
();
for
(
auto
useGpu
:
{
false
,
true
})
{
testLayerGrad
(
config
,
"row_l2_norm"
,
batchSize
,
false
,
useGpu
,
false
);
}
}
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
testing
::
InitGoogleTest
(
&
argc
,
argv
);
testing
::
InitGoogleTest
(
&
argc
,
argv
);
initMain
(
argc
,
argv
);
initMain
(
argc
,
argv
);
...
...
python/paddle/trainer/config_parser.py
浏览文件 @
84660653
...
@@ -2725,6 +2725,16 @@ class SumToOneNormLayer(LayerBase):
...
@@ -2725,6 +2725,16 @@ class SumToOneNormLayer(LayerBase):
self
.
set_layer_size
(
input_layer0
.
size
)
self
.
set_layer_size
(
input_layer0
.
size
)
@
config_layer
(
'row_l2_norm'
)
class
RowL2NormLayer
(
LayerBase
):
def
__init__
(
self
,
name
,
inputs
,
device
=
None
):
super
(
RowL2NormLayer
,
self
).
__init__
(
name
,
'row_l2_norm'
,
0
,
inputs
=
inputs
,
device
=
device
)
config_assert
(
len
(
self
.
inputs
)
==
1
,
'RowL2NormLayer must have 1 input'
)
input_layer0
=
self
.
get_input_layer
(
0
)
self
.
set_layer_size
(
input_layer0
.
size
)
@
config_layer
(
'cos_vm'
)
@
config_layer
(
'cos_vm'
)
class
CosSimVecMatLayer
(
LayerBase
):
class
CosSimVecMatLayer
(
LayerBase
):
def
__init__
(
self
,
name
,
size
,
inputs
,
cos_scale
=
1.0
,
device
=
None
):
def
__init__
(
self
,
name
,
size
,
inputs
,
cos_scale
=
1.0
,
device
=
None
):
...
...
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
84660653
...
@@ -76,6 +76,7 @@ __all__ = [
...
@@ -76,6 +76,7 @@ __all__ = [
'trans_layer'
,
'trans_layer'
,
'rotate_layer'
,
'rotate_layer'
,
'sum_to_one_norm_layer'
,
'sum_to_one_norm_layer'
,
'row_l2_norm_layer'
,
'get_output_layer'
,
'get_output_layer'
,
'LayerType'
,
'LayerType'
,
'context_projection'
,
'context_projection'
,
...
@@ -159,6 +160,7 @@ class LayerType(object):
...
@@ -159,6 +160,7 @@ class LayerType(object):
BATCH_NORM_LAYER
=
'batch_norm'
BATCH_NORM_LAYER
=
'batch_norm'
NORM_LAYER
=
'norm'
NORM_LAYER
=
'norm'
SUM_TO_ONE_NORM_LAYER
=
'sum_to_one_norm'
SUM_TO_ONE_NORM_LAYER
=
'sum_to_one_norm'
ROW_L2_NORM_LAYER
=
'row_l2_norm'
ADDTO_LAYER
=
'addto'
ADDTO_LAYER
=
'addto'
CONCAT_LAYER
=
'concat'
CONCAT_LAYER
=
'concat'
...
@@ -2849,6 +2851,42 @@ def sum_to_one_norm_layer(input, name=None, layer_attr=None):
...
@@ -2849,6 +2851,42 @@ def sum_to_one_norm_layer(input, name=None, layer_attr=None):
name
,
LayerType
.
SUM_TO_ONE_NORM_LAYER
,
parents
=
[
input
],
size
=
input
.
size
)
name
,
LayerType
.
SUM_TO_ONE_NORM_LAYER
,
parents
=
[
input
],
size
=
input
.
size
)
@
wrap_name_default
()
@
layer_support
()
def
row_l2_norm_layer
(
input
,
name
=
None
,
layer_attr
=
None
):
"""
A layer for L2-normalization in each row.
.. math::
out[i] =
\f
rac{in[i]}{\sqrt{\sum_{k=1}^N in[k]^{2}}}
where the size of :math:`in` is (batchSize x dataDim) ,
and the size of :math:`out` is a (batchSize x dataDim) .
The example usage is:
.. code-block:: python
row_l2_norm_layer = row_l2_norm_layer(input=layer)
:param input: Input layer.
:type input: LayerOutput
:param name: Layer name.
:type name: basestring
:param layer_attr: extra layer attributes.
:type layer_attr: ExtraLayerAttribute.
:return: LayerOutput object.
:rtype: LayerOutput
"""
Layer
(
name
=
name
,
type
=
LayerType
.
ROW_L2_NORM_LAYER
,
inputs
=
[
input
.
name
],
**
ExtraAttr
.
to_kwargs
(
layer_attr
))
return
LayerOutput
(
name
,
LayerType
.
ROW_L2_NORM_LAYER
,
parents
=
[
input
],
size
=
input
.
size
)
@
wrap_name_default
(
"addto"
)
@
wrap_name_default
(
"addto"
)
@
wrap_act_default
(
act
=
LinearActivation
())
@
wrap_act_default
(
act
=
LinearActivation
())
@
wrap_bias_attr_default
(
has_bias
=
False
)
@
wrap_bias_attr_default
(
has_bias
=
False
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录