Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
093c526d
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
未验证
提交
093c526d
编写于
11月 17, 2017
作者:
R
ranqiu92
提交者:
GitHub
11月 17, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5724 from ranqiu92/dot_product
add dot_prod_layer
上级
044d671e
2e1cd331
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
222 addition
and
1 deletion
+222
-1
doc/api/v2/config/layer.rst
doc/api/v2/config/layer.rst
+10
-0
paddle/gserver/layers/DotProdLayer.cpp
paddle/gserver/layers/DotProdLayer.cpp
+97
-0
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+15
-0
python/paddle/trainer/config_parser.py
python/paddle/trainer/config_parser.py
+12
-0
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+41
-0
python/paddle/trainer_config_helpers/tests/configs/file_list.sh
.../paddle/trainer_config_helpers/tests/configs/file_list.sh
+2
-1
python/paddle/trainer_config_helpers/tests/configs/protostr/test_dot_prod_layer.protostr
...lpers/tests/configs/protostr/test_dot_prod_layer.protostr
+38
-0
python/paddle/trainer_config_helpers/tests/configs/test_dot_prod_layer.py
...ainer_config_helpers/tests/configs/test_dot_prod_layer.py
+7
-0
未找到文件。
doc/api/v2/config/layer.rst
浏览文件 @
093c526d
...
...
@@ -335,6 +335,16 @@ bilinear_interp
.. autoclass:: paddle.v2.layer.bilinear_interp
:noindex:
dot_prod
---------
.. autoclass:: paddle.v2.layer.dot_prod
:noindex:
out_prod
--------
.. autoclass:: paddle.v2.layer.out_prod
:noindex:
power
-----
.. autoclass:: paddle.v2.layer.power
...
...
paddle/gserver/layers/DotProdLayer.cpp
0 → 100644
浏览文件 @
093c526d
/* 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"
#include "paddle/utils/Logging.h"
#include "paddle/utils/Stat.h"
namespace
paddle
{
/**
* @brief A layer for computing the dot product of two vectors.
* Input1: vector (batchSize * dim)
* Input2: vector (batchSize * dim)
* Output: a matrix: (batchSize * 1)
*/
class
DotProdLayer
:
public
Layer
{
public:
explicit
DotProdLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
~
DotProdLayer
()
{}
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
override
;
void
forward
(
PassType
passType
)
override
;
void
backward
(
const
UpdateCallback
&
callback
=
nullptr
)
override
;
};
REGISTER_LAYER
(
dot_prod
,
DotProdLayer
);
bool
DotProdLayer
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
Layer
::
init
(
layerMap
,
parameterMap
);
CHECK_EQ
(
inputLayers_
.
size
(),
2U
);
CHECK_EQ
(
1UL
,
getSize
())
<<
"The output dimensionality of this layer should be fixed to 1."
;
return
true
;
}
void
DotProdLayer
::
forward
(
PassType
passType
)
{
Layer
::
forward
(
passType
);
MatrixPtr
inV0
=
getInputValue
(
0
);
MatrixPtr
inV1
=
getInputValue
(
1
);
size_t
batchSize
=
inV0
->
getHeight
();
CHECK_EQ
(
inV1
->
getHeight
(),
batchSize
);
CHECK_EQ
(
inV0
->
getWidth
(),
inV1
->
getWidth
());
{
REGISTER_TIMER_INFO
(
"FwResetTimer"
,
getName
().
c_str
());
reserveOutput
(
batchSize
,
1
);
}
MatrixPtr
outV
=
getOutputValue
();
{
REGISTER_TIMER_INFO
(
"FwDotProdTimer"
,
getName
().
c_str
());
outV
->
sumOfProducts
(
*
inV0
,
*
inV1
,
1
,
0
);
}
}
void
DotProdLayer
::
backward
(
const
UpdateCallback
&
callback
)
{
MatrixPtr
inV0
=
getInputValue
(
0
);
MatrixPtr
inV1
=
getInputValue
(
1
);
MatrixPtr
outG
=
getOutputGrad
();
MatrixPtr
inG0
=
getInputGrad
(
0
);
MatrixPtr
inG1
=
getInputGrad
(
1
);
{
REGISTER_TIMER_INFO
(
"BwDotProdTimer"
,
getName
().
c_str
());
if
(
inG0
)
{
inG0
->
addRowScale
(
0
,
*
inV1
,
*
outG
);
}
if
(
inG1
)
{
inG1
->
addRowScale
(
0
,
*
inV0
,
*
outG
);
}
}
}
}
// namespace paddle
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
093c526d
...
...
@@ -1081,6 +1081,21 @@ TEST(Layer, InterpolationLayer) {
}
}
TEST
(
Layer
,
DotProdLayer
)
{
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"dot_prod"
);
config
.
layerConfig
.
set_size
(
1
);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
10
,
0
});
config
.
layerConfig
.
add_inputs
();
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_1"
,
10
,
0
});
config
.
layerConfig
.
add_inputs
();
for
(
auto
useGpu
:
{
false
,
true
})
{
testLayerGrad
(
config
,
"dot_prod"
,
10
,
false
,
useGpu
);
}
}
TEST
(
Layer
,
OuterProdLayer
)
{
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"out_prod"
);
...
...
python/paddle/trainer/config_parser.py
浏览文件 @
093c526d
...
...
@@ -3209,6 +3209,18 @@ class SubNestedSequenceLayer(LayerBase):
self
.
set_layer_size
(
size
)
@
config_layer
(
'dot_prod'
)
class
DotProdLayer
(
LayerBase
):
def
__init__
(
self
,
name
,
inputs
,
device
=
None
):
super
(
DotProdLayer
,
self
).
__init__
(
name
,
'dot_prod'
,
0
,
inputs
,
device
=
device
)
config_assert
(
len
(
inputs
)
==
2
,
'DotProdLayer must have 2 inputs.'
)
config_assert
(
self
.
get_input_layer
(
0
).
size
==
self
.
get_input_layer
(
1
).
size
,
"Two inputs should have the same size."
)
self
.
set_layer_size
(
1
)
@
config_layer
(
'out_prod'
)
class
OuterProdLayer
(
LayerBase
):
def
__init__
(
self
,
name
,
inputs
,
device
=
None
):
...
...
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
093c526d
...
...
@@ -115,6 +115,7 @@ __all__ = [
'huber_classification_cost'
,
'block_expand_layer'
,
'maxout_layer'
,
'dot_prod_layer'
,
'out_prod_layer'
,
'printer_layer'
,
'print_layer'
,
...
...
@@ -197,6 +198,7 @@ class LayerType(object):
SCALING_LAYER
=
'scaling'
TRANS_LAYER
=
'trans'
ROTATE_LAYER
=
'rotate'
DOT_PROD_LAYER
=
'dot_prod'
OUT_PROD_LAYER
=
'out_prod'
FEATURE_MAP_EXPAND_LAYER
=
'featmap_expand'
...
...
@@ -4140,6 +4142,45 @@ def maxid_layer(input, name=None, layer_attr=None):
size
=
l
.
config
.
size
)
@
wrap_name_default
()
def
dot_prod_layer
(
input1
,
input2
,
name
=
None
,
layer_attr
=
None
):
"""
A layer for computing the dot product of two vectors.
The example usage is:
.. code-block:: python
dot_prod = dot_prod_layer(input1=vec1, input2=vec2)
:param name: The name of this layer. It is optional.
:type name: basestring
:param input1: The first input layer.
:type input: LayerOutput
:param input2: The second input layer.
:type input2: LayerOutput
:param layer_attr: The extra layer attribute. See ExtraLayerAttribute for
details.
:type layer_attr: ExtraLayerAttribute.
:return: LayerOutput object.
:rtype: LayerOutput
"""
assert
isinstance
(
input1
,
LayerOutput
)
assert
isinstance
(
input2
,
LayerOutput
)
assert
input1
.
size
==
input2
.
size
,
(
"Two inputs should have the same size."
)
l
=
Layer
(
name
=
name
,
type
=
LayerType
.
DOT_PROD_LAYER
,
inputs
=
[
input1
.
name
,
input2
.
name
],
**
ExtraLayerAttribute
.
to_kwargs
(
layer_attr
))
return
LayerOutput
(
name
=
name
,
layer_type
=
LayerType
.
DOT_PROD_LAYER
,
parents
=
[
input1
,
input2
],
size
=
l
.
config
.
size
)
@
wrap_name_default
()
def
out_prod_layer
(
input1
,
input2
,
name
=
None
,
layer_attr
=
None
):
"""
...
...
python/paddle/trainer_config_helpers/tests/configs/file_list.sh
浏览文件 @
093c526d
...
...
@@ -10,6 +10,7 @@ test_prelu_layer test_row_conv test_detection_output_layer test_multibox_loss_la
test_recursive_topology test_gated_unit_layer test_clip_layer test_row_l2_norm_layer
test_kmax_seq_socre_layer test_sub_nested_seq_select_layer test_scale_shift_layer
test_seq_slice_layer test_cross_entropy_over_beam test_roi_pool_layer test_pooling3D_layer
test_conv3d_layer test_deconv3d_layer test_BatchNorm3D test_resize_layer test_scale_sub_region_layer
)
test_conv3d_layer test_deconv3d_layer test_BatchNorm3D test_resize_layer test_scale_sub_region_layer
test_dot_prod_layer
)
export
whole_configs
=(
test_split_datasource
)
python/paddle/trainer_config_helpers/tests/configs/protostr/test_dot_prod_layer.protostr
0 → 100644
浏览文件 @
093c526d
type: "nn"
layers {
name: "vector1"
type: "data"
size: 10
active_type: ""
}
layers {
name: "vector2"
type: "data"
size: 10
active_type: ""
}
layers {
name: "__dot_prod_layer_0__"
type: "dot_prod"
size: 1
active_type: ""
inputs {
input_layer_name: "vector1"
}
inputs {
input_layer_name: "vector2"
}
}
input_layer_names: "vector1"
input_layer_names: "vector2"
output_layer_names: "__dot_prod_layer_0__"
sub_models {
name: "root"
layer_names: "vector1"
layer_names: "vector2"
layer_names: "__dot_prod_layer_0__"
input_layer_names: "vector1"
input_layer_names: "vector2"
output_layer_names: "__dot_prod_layer_0__"
is_recurrent_layer_group: false
}
python/paddle/trainer_config_helpers/tests/configs/test_dot_prod_layer.py
0 → 100644
浏览文件 @
093c526d
from
paddle.trainer_config_helpers
import
*
vec1
=
data_layer
(
name
=
'vector1'
,
size
=
10
)
vec2
=
data_layer
(
name
=
'vector2'
,
size
=
10
)
dot_product
=
dot_prod_layer
(
input1
=
vec1
,
input2
=
vec2
)
outputs
(
dot_product
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录