Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
d19355a5
P
Paddle
项目概览
机器未来
/
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看板
提交
d19355a5
编写于
8月 02, 2017
作者:
G
guosheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refine ClipLayer and add unit test for it
上级
f35e0847
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
45 addition
and
36 deletion
+45
-36
doc/api/v2/config/layer.rst
doc/api/v2/config/layer.rst
+5
-0
paddle/gserver/layers/ClipLayer.cpp
paddle/gserver/layers/ClipLayer.cpp
+15
-14
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+2
-2
proto/ModelConfig.proto
proto/ModelConfig.proto
+2
-2
python/paddle/trainer/config_parser.py
python/paddle/trainer/config_parser.py
+6
-9
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+8
-8
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/test_clip_layer.py
...e/trainer_config_helpers/tests/configs/test_clip_layer.py
+6
-0
未找到文件。
doc/api/v2/config/layer.rst
浏览文件 @
d19355a5
...
...
@@ -316,6 +316,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
浏览文件 @
d19355a5
...
...
@@ -13,7 +13,6 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "Layer.h"
#include "paddle/math/Matrix.h"
namespace
paddle
{
...
...
@@ -26,8 +25,8 @@ namespace paddle {
class
ClipLayer
:
public
Layer
{
protected:
real
clipThresholdLow
_
;
real
clipThresholdHigh
_
;
double
min
_
;
double
max
_
;
public:
explicit
ClipLayer
(
const
LayerConfig
&
config
)
:
Layer
(
config
)
{}
...
...
@@ -47,9 +46,9 @@ bool ClipLayer::init(const LayerMap& layerMap,
CHECK_EQ
(
inputLayers_
.
size
(),
1U
);
auto
layerConf
=
config_
.
inputs
(
0
).
clip_conf
();
clipThresholdLow_
=
layerConf
.
clip_threshold_low
();
clipThresholdHigh_
=
layerConf
.
clip_threshold_high
();
CHECK_LT
(
clipThresholdLow_
,
clipThresholdHigh
_
);
min_
=
layerConf
.
min
();
max_
=
layerConf
.
max
();
CHECK_LT
(
min_
,
max
_
);
return
true
;
}
...
...
@@ -60,19 +59,21 @@ void ClipLayer::forward(PassType passType) {
resetOutput
(
inV
->
getHeight
(),
inV
->
getWidth
());
MatrixPtr
outV
=
getOutputValue
();
outV
->
copyFrom
(
*
inV
);
outV
->
clip
(
clipThresholdLow_
,
clipThresholdHigh
_
);
outV
->
clip
(
min_
,
max
_
);
}
void
ClipLayer
::
backward
(
const
UpdateCallback
&
callback
)
{
MatrixPtr
inV
=
getInputValue
(
0
);
MatrixPtr
inG
=
getInputGrad
(
0
);
MatrixPtr
outV
=
getOutputValue
();
MatrixPtr
outG
=
getOutputGrad
();
MatrixPtr
tmpMtx
;
Matrix
::
resizeOrCreate
(
tmpMtx
,
outG
->
getHeight
(),
outG
->
getWidth
(),
false
,
useGpu_
);
tmpMtx
->
clipDerivative
(
*
inV
,
clipThresholdLow_
,
clipThresholdHigh_
);
inG
->
addDotMul
(
*
outG
,
*
tmpMtx
,
1
,
1
);
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
浏览文件 @
d19355a5
...
...
@@ -1887,8 +1887,8 @@ TEST(Layer, ClipLayer) {
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"input"
,
size
,
0
});
LayerInputConfig
*
input
=
config
.
layerConfig
.
add_inputs
();
ClipConfig
*
layerConf
=
input
->
mutable_clip_conf
();
layerConf
->
set_
clip_threshold_low
(
std
::
rand
()
/
(
real
)
RAND_MAX
);
layerConf
->
set_
clip_threshold_high
(
std
::
rand
()
/
(
real
)
RAND_MAX
);
layerConf
->
set_
min
(
std
::
rand
()
/
(
double
)
RAND_MAX
);
layerConf
->
set_
max
(
std
::
rand
()
/
(
double
)
RAND_MAX
);
for
(
auto
useGpu
:
{
false
,
true
})
{
testLayerGrad
(
config
,
"clip"
,
batchSize
,
false
,
useGpu
,
false
);
}
...
...
proto/ModelConfig.proto
浏览文件 @
d19355a5
...
...
@@ -290,8 +290,8 @@ message DetectionOutputConfig {
}
message
ClipConfig
{
required
float
clip_threshold_low
=
1
;
required
float
clip_threshold_high
=
2
;
required
double
min
=
1
;
required
double
max
=
2
;
}
message
LayerInputConfig
{
...
...
python/paddle/trainer/config_parser.py
浏览文件 @
d19355a5
...
...
@@ -2171,19 +2171,16 @@ class RowConvLayer(LayerBase):
@
config_layer
(
'clip'
)
class
ClipLayer
(
LayerBase
):
def
__init__
(
self
,
name
,
inputs
,
clip_threshold_low
,
clip_threshold_high
):
super
(
ClipLayer
,
self
).
__init__
(
name
,
'clip'
,
0
,
inputs
=
inputs
)
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 layer must have one and only one input.'
)
config_assert
(
clip_threshold_low
<
clip_threshold_high
,
'clip_threshold_low must be less than clip_threshold_high.'
)
'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
.
clip_threshold_low
=
clip_threshold_low
self
.
config
.
inputs
[
0
].
clip_conf
.
clip_threshold_high
=
clip_threshold_high
self
.
config
.
inputs
[
0
].
clip_conf
.
min
=
min
self
.
config
.
inputs
[
0
].
clip_conf
.
max
=
max
# key: cost type
...
...
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
d19355a5
...
...
@@ -6011,7 +6011,7 @@ def crop_layer(input, offset, axis=2, shape=None, name=None, layer_attr=None):
@
wrap_name_default
(
"clip"
)
def
clip_layer
(
input
,
clip_threshold_low
,
clip_threshold_high
,
name
=
None
):
def
clip_layer
(
input
,
min
,
max
,
name
=
None
):
"""
A layer for clipping the input value by the threshold.
...
...
@@ -6021,23 +6021,23 @@ def clip_layer(input, clip_threshold_low, clip_threshold_high, name=None):
.. code-block:: python
clip = clip_layer(input=input_layer,
clip_threshold_low=-10, clip_threshold_high
=10)
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
clip_threshold_low
: The lower threshold for clipping.
:type
clip_threshold_low: float
:param
clip_threshold_high
: The upper threshold for clipping.
:type
clip_threshold_high: float
: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
],
clip_threshold_low
=
clip_threshold_low
,
clip_threshold_high
=
clip_threshold_high
)
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
浏览文件 @
d19355a5
...
...
@@ -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_recursive_topology test_gated_unit_layer
test_clip_layer
)
export
whole_configs
=(
test_split_datasource
)
python/paddle/trainer_config_helpers/tests/configs/test_clip_layer.py
0 → 100644
浏览文件 @
d19355a5
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录