Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
0c181c63
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
0c181c63
编写于
8月 03, 2017
作者:
G
Guo Sheng
提交者:
GitHub
8月 03, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3112 from guoshengCS/add-ClipLayer
Add ClipLayer
上级
cc6c33b8
b7b956f0
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
207 addition
and
1 deletion
+207
-1
doc/api/v2/config/layer.rst
doc/api/v2/config/layer.rst
+5
-0
paddle/gserver/layers/ClipLayer.cpp
paddle/gserver/layers/ClipLayer.cpp
+79
-0
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+17
-0
paddle/math/BaseMatrix.cu
paddle/math/BaseMatrix.cu
+6
-0
paddle/math/BaseMatrix.h
paddle/math/BaseMatrix.h
+7
-0
proto/ModelConfig.proto
proto/ModelConfig.proto
+6
-0
python/paddle/trainer/config_parser.py
python/paddle/trainer/config_parser.py
+14
-0
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+35
-0
python/paddle/trainer_config_helpers/tests/configs/file_list.sh
.../paddle/trainer_config_helpers/tests/configs/file_list.sh
+1
-1
python/paddle/trainer_config_helpers/tests/configs/protostr/test_clip_layer.protostr
...g_helpers/tests/configs/protostr/test_clip_layer.protostr
+31
-0
python/paddle/trainer_config_helpers/tests/configs/test_clip_layer.py
...e/trainer_config_helpers/tests/configs/test_clip_layer.py
+6
-0
未找到文件。
doc/api/v2/config/layer.rst
浏览文件 @
0c181c63
...
...
@@ -325,6 +325,11 @@ scaling
.. autoclass:: paddle.v2.layer.scaling
:noindex:
clip
----
.. autoclass:: paddle.v2.layer.clip
:noindex:
slope_intercept
---------------
.. autoclass:: paddle.v2.layer.slope_intercept
...
...
paddle/gserver/layers/ClipLayer.cpp
0 → 100644
浏览文件 @
0c181c63
/* 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"
namespace
paddle
{
/**
* A layer for clipping the input value by the threshold.
* \f[
* out[i] = \min\left(\max\left(in[i],p_{1}\right),p_{2}\right)
* \f]
*/
class
ClipLayer
:
public
Layer
{
protected:
double
min_
;
double
max_
;
public:
explicit
ClipLayer
(
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
(
clip
,
ClipLayer
);
bool
ClipLayer
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
Layer
::
init
(
layerMap
,
parameterMap
);
CHECK_EQ
(
inputLayers_
.
size
(),
1U
);
auto
layerConf
=
config_
.
inputs
(
0
).
clip_conf
();
min_
=
layerConf
.
min
();
max_
=
layerConf
.
max
();
CHECK_LT
(
min_
,
max_
);
return
true
;
}
void
ClipLayer
::
forward
(
PassType
passType
)
{
Layer
::
forward
(
passType
);
MatrixPtr
inV
=
getInputValue
(
0
);
resetOutput
(
inV
->
getHeight
(),
inV
->
getWidth
());
MatrixPtr
outV
=
getOutputValue
();
outV
->
copyFrom
(
*
inV
);
outV
->
clip
(
min_
,
max_
);
}
void
ClipLayer
::
backward
(
const
UpdateCallback
&
callback
)
{
MatrixPtr
inV
=
getInputValue
(
0
);
MatrixPtr
inG
=
getInputGrad
(
0
);
if
(
inG
)
{
MatrixPtr
outV
=
getOutputValue
();
MatrixPtr
outG
=
getOutputGrad
();
MatrixPtr
tmpMtx
;
Matrix
::
resizeOrCreate
(
tmpMtx
,
outG
->
getHeight
(),
outG
->
getWidth
(),
false
,
useGpu_
);
tmpMtx
->
clipDerivative
(
*
inV
,
min_
,
max_
);
inG
->
addDotMul
(
*
outG
,
*
tmpMtx
,
1
,
1
);
}
}
}
// namespace paddle
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
0c181c63
...
...
@@ -1899,6 +1899,23 @@ TEST(Layer, CropLayer) {
}
}
TEST
(
Layer
,
ClipLayer
)
{
const
size_t
batchSize
=
128
;
const
size_t
size
=
512
;
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"clip"
);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"input"
,
size
,
0
});
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
ClipConfig
*
layerConf
=
input
->
mutable_clip_conf
();
double
p1
=
std
::
rand
()
/
(
double
)
RAND_MAX
;
double
p2
=
std
::
rand
()
/
(
double
)
RAND_MAX
;
layerConf
->
set_min
(
std
::
min
(
p1
,
p2
));
layerConf
->
set_max
(
std
::
max
(
p1
,
p2
));
for
(
auto
useGpu
:
{
false
,
true
})
{
testLayerGrad
(
config
,
"clip"
,
batchSize
,
false
,
useGpu
,
false
);
}
}
TEST
(
Layer
,
RowL2NormLayer
)
{
const
size_t
batchSize
=
128
;
const
size_t
size
=
512
;
...
...
paddle/math/BaseMatrix.cu
浏览文件 @
0c181c63
...
...
@@ -442,6 +442,12 @@ DEFINE_MATRIX_UNARY_PARAMETER_OP(Clip, TWO_PARAMETER,
template
<
class
T
>
void
BaseMatrixT
<
T
>::
clip
(
T
p1
,
T
p2
)
{
applyUnary
(
unary
::
Clip
<
T
>
(
p1
,
p2
));
}
DEFINE_MATRIX_BINARY_PARAMETER_OP
(
ClipDerivative
,
TWO_PARAMETER
,
a
=
b
<
p1
?
0
:
(
b
>
p2
?
0
:
1
));
template
<
class
T
>
void
BaseMatrixT
<
T
>::
clipDerivative
(
BaseMatrixT
&
b
,
T
p1
,
T
p2
)
{
applyBinary
(
binary
::
ClipDerivative
<
T
>
(
p1
,
p2
),
b
);
}
DEFINE_MATRIX_UNARY_PARAMETER_OP
(
BiggerThanScalar
,
ONE_PARAMETER
,
a
=
a
>
p
?
1.0
f
:
0.0
f
);
template
<
class
T
>
...
...
paddle/math/BaseMatrix.h
浏览文件 @
0c181c63
...
...
@@ -488,6 +488,13 @@ public:
*/
void
clip
(
T
p1
,
T
p2
);
/**
* this = b < low ? 0 : 1
*
* this = b > high ? 0 : 1
*/
void
clipDerivative
(
BaseMatrixT
&
b
,
T
p1
,
T
p2
);
/**
* @code
* a = a > p ? 1.0f : 0.0f
...
...
proto/ModelConfig.proto
浏览文件 @
0c181c63
...
...
@@ -298,6 +298,11 @@ message DetectionOutputConfig {
optional
uint32
width
=
9
[
default
=
1
];
}
message
ClipConfig
{
required
double
min
=
1
;
required
double
max
=
2
;
}
message
LayerInputConfig
{
required
string
input_layer_name
=
1
;
optional
string
input_parameter_name
=
2
;
...
...
@@ -318,6 +323,7 @@ message LayerInputConfig {
optional
RowConvConfig
row_conv_conf
=
15
;
optional
MultiBoxLossConfig
multibox_loss_conf
=
16
;
optional
DetectionOutputConfig
detection_output_conf
=
17
;
optional
ClipConfig
clip_conf
=
18
;
}
message
LayerConfig
{
...
...
python/paddle/trainer/config_parser.py
浏览文件 @
0c181c63
...
...
@@ -2198,6 +2198,20 @@ class RowConvLayer(LayerBase):
self
.
create_input_parameter
(
0
,
psize
,
dims
)
@
config_layer
(
'clip'
)
class
ClipLayer
(
LayerBase
):
def
__init__
(
self
,
name
,
inputs
,
min
,
max
,
**
xargs
):
super
(
ClipLayer
,
self
).
__init__
(
name
,
'clip'
,
0
,
inputs
=
inputs
,
**
xargs
)
config_assert
(
len
(
self
.
inputs
)
==
1
,
'ClipLayer must have one and only one input.'
)
config_assert
(
min
<
max
,
'min must be less than max.'
)
input_layer
=
self
.
get_input_layer
(
0
)
self
.
set_layer_size
(
input_layer
.
size
)
self
.
config
.
inputs
[
0
].
clip_conf
.
min
=
min
self
.
config
.
inputs
[
0
].
clip_conf
.
max
=
max
# key: cost type
# value: cost class
g_cost_map
=
{}
...
...
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
0c181c63
...
...
@@ -129,6 +129,7 @@ __all__ = [
'prelu_layer'
,
'gated_unit_layer'
,
'crop_layer'
,
'clip_layer'
,
'slice_projection'
,
]
...
...
@@ -223,6 +224,7 @@ class LayerType(object):
PRELU
=
'prelu'
CROP_LAYER
=
'crop'
CLIP_LAYER
=
'clip'
@
staticmethod
def
is_layer_type
(
type_name
):
...
...
@@ -6084,3 +6086,36 @@ def crop_layer(input, offset, axis=2, shape=None, name=None, layer_attr=None):
layer_type
=
LayerType
.
CROP_LAYER
,
parents
=
input
,
size
=
l
.
config
.
size
)
@
wrap_name_default
(
"clip"
)
def
clip_layer
(
input
,
min
,
max
,
name
=
None
):
"""
A layer for clipping the input value by the threshold.
.. math::
out[i] = \min\left(\max\left(in[i],p_{1}
\r
ight),p_{2}
\r
ight)
.. code-block:: python
clip = clip_layer(input=input_layer, min=-10, max=10)
:param name: The Layer Name.
:type name: basestring
:param input: The input layer.
:type input: LayerOutput.
:param min: The lower threshold for clipping.
:type min: double
:param max: The upper threshold for clipping.
:type max: double
:return: LayerOutput
"""
Layer
(
name
=
name
,
type
=
LayerType
.
CLIP_LAYER
,
inputs
=
[
input
.
name
],
min
=
min
,
max
=
max
)
return
LayerOutput
(
name
,
LayerType
.
CLIP_LAYER
,
parents
=
[
input
],
size
=
input
.
size
)
python/paddle/trainer_config_helpers/tests/configs/file_list.sh
浏览文件 @
0c181c63
...
...
@@ -7,6 +7,6 @@ test_rnn_group shared_fc shared_lstm shared_gru test_cost_layers_with_weight
test_spp_layer test_bilinear_interp test_maxout test_bi_grumemory math_ops
test_seq_concat_reshape test_pad test_smooth_l1 test_multiplex_layer
test_prelu_layer test_row_conv test_detection_output_layer test_multibox_loss_layer
test_recursive_topology test_gated_unit_layer test_row_l2_norm_layer
)
test_recursive_topology test_gated_unit_layer test_
clip_layer test_
row_l2_norm_layer
)
export
whole_configs
=(
test_split_datasource
)
python/paddle/trainer_config_helpers/tests/configs/protostr/test_clip_layer.protostr
0 → 100644
浏览文件 @
0c181c63
type: "nn"
layers {
name: "input"
type: "data"
size: 300
active_type: ""
}
layers {
name: "__clip_0__"
type: "clip"
size: 300
active_type: ""
inputs {
input_layer_name: "input"
clip_conf {
min: -10
max: 10
}
}
}
input_layer_names: "input"
output_layer_names: "__clip_0__"
sub_models {
name: "root"
layer_names: "input"
layer_names: "__clip_0__"
input_layer_names: "input"
output_layer_names: "__clip_0__"
is_recurrent_layer_group: false
}
python/paddle/trainer_config_helpers/tests/configs/test_clip_layer.py
0 → 100644
浏览文件 @
0c181c63
from
paddle.trainer_config_helpers
import
*
data
=
data_layer
(
name
=
'input'
,
size
=
300
)
clip
=
clip_layer
(
input
=
data
,
min
=-
10
,
max
=
10
)
outputs
(
clip
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录