Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
59be92e0
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看板
提交
59be92e0
编写于
4月 25, 2017
作者:
T
Tao Luo
提交者:
GitHub
4月 25, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1870 from qingqing01/smoothl1_loss
Smoothl1 loss and Python API.
上级
80277b50
8b997df3
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
136 addition
and
22 deletion
+136
-22
doc/api/v1/trainer_config_helpers/layers.rst
doc/api/v1/trainer_config_helpers/layers.rst
+6
-0
doc/api/v2/config/layer.rst
doc/api/v2/config/layer.rst
+5
-0
paddle/gserver/layers/CostLayer.cpp
paddle/gserver/layers/CostLayer.cpp
+1
-1
paddle/gserver/layers/CostLayer.h
paddle/gserver/layers/CostLayer.h
+4
-2
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+3
-3
paddle/math/Matrix.cpp
paddle/math/Matrix.cpp
+17
-13
python/paddle/trainer/config_parser.py
python/paddle/trainer/config_parser.py
+1
-0
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+51
-2
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_smooth_l1.protostr
...ig_helpers/tests/configs/protostr/test_smooth_l1.protostr
+40
-0
python/paddle/trainer_config_helpers/tests/configs/test_smooth_l1.py
...le/trainer_config_helpers/tests/configs/test_smooth_l1.py
+7
-0
未找到文件。
doc/api/v1/trainer_config_helpers/layers.rst
浏览文件 @
59be92e0
...
...
@@ -498,6 +498,12 @@ hsigmoid
:members: hsigmoid
:noindex:
smooth_l1_cost
--------------
.. automodule:: paddle.trainer_config_helpers.layers
:members: smooth_l1_cost
:noindex:
Check Layer
============
...
...
doc/api/v2/config/layer.rst
浏览文件 @
59be92e0
...
...
@@ -419,6 +419,11 @@ hsigmoid
.. autoclass:: paddle.v2.layer.hsigmoid
:noindex:
smooth_l1_cost
--------------
.. autoclass:: paddle.v2.layer.smooth_l1_cost
:noindex:
Check Layer
============
...
...
paddle/gserver/layers/CostLayer.cpp
浏览文件 @
59be92e0
...
...
@@ -217,7 +217,7 @@ void SmoothL1CostLayer::forwardImp(Matrix& output,
targetCpu
->
copyFrom
(
target
);
outputCpu
->
copyFrom
(
output
);
labelCpu
->
copyFrom
(
*
label
.
value
);
targetCpu
->
smoothL1
(
*
outputCpu
,
*
(
labelCpu
)
);
targetCpu
->
smoothL1
(
*
outputCpu
,
*
labelCpu
);
target
.
copyFrom
(
*
targetCpu
);
}
else
{
target
.
smoothL1
(
output
,
*
label
.
value
);
...
...
paddle/gserver/layers/CostLayer.h
浏览文件 @
59be92e0
...
...
@@ -164,9 +164,11 @@ public:
* tasks.
* \f[
* L =
*
(output - label)^2 * 0.5 / -1 < (output - label)
< 1 /
*
(output - label) - 0.5 / otherwise
/
*
0.5 * x^2 if / -1 < |x|
< 1 /
*
|x| - 0.5 / otherwise
/
* \f]
*
* x = output - label
*/
class
SmoothL1CostLayer
:
public
CostLayer
{
public:
...
...
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
59be92e0
...
...
@@ -1679,13 +1679,13 @@ TEST(Layer, smooth_l1) {
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"smooth_l1"
);
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1
,
0
});
config
.
inputDefs
.
push_back
({
INPUT_DATA_TARGET
,
"layer_1"
,
1
,
0
});
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
200
,
0
});
config
.
inputDefs
.
push_back
({
INPUT_DATA_TARGET
,
"layer_1"
,
200
,
0
});
config
.
layerConfig
.
add_inputs
();
config
.
layerConfig
.
add_inputs
();
for
(
auto
useGpu
:
{
false
,
true
})
{
testLayerGrad
(
config
,
"smooth_l1"
,
100
,
false
,
useGpu
,
false
,
2.0
);
testLayerGrad
(
config
,
"smooth_l1"
,
100
,
false
,
useGpu
,
false
);
}
}
...
...
paddle/math/Matrix.cpp
浏览文件 @
59be92e0
...
...
@@ -3616,17 +3616,18 @@ void CpuMatrix::smoothL1(Matrix& output, Matrix& label) {
CHECK_EQ
(
output
.
getHeight
(),
numSamples
);
CHECK_EQ
(
label
.
getWidth
(),
dim
);
CHECK_EQ
(
getWidth
(),
(
size_t
)
1
);
real
*
out
=
output
.
getData
();
real
*
cost
=
getData
();
real
*
out
=
output
.
getData
();
real
*
lbl
=
label
.
getData
();
for
(
size_t
i
=
0
;
i
<
numSamples
;
++
i
,
out
+=
dim
,
cost
+=
dim
,
lbl
+=
dim
)
{
for
(
size_t
i
=
0
;
i
<
numSamples
;
++
i
,
out
+=
dim
,
lbl
+=
dim
)
{
for
(
size_t
j
=
0
;
j
<
dim
;
++
j
)
{
cost
[
j
]
=
std
::
fabs
(
out
[
j
]
-
lbl
[
j
]);
if
(
cost
[
j
]
<
1.0
)
cost
[
j
]
=
0.5
*
cost
[
j
]
*
cost
[
j
]
;
real
absVal
=
std
::
fabs
(
out
[
j
]
-
lbl
[
j
]);
if
(
absVal
<
1.0
)
cost
[
i
]
+=
0.5
*
absVal
*
absVal
;
else
cost
[
j
]
=
cost
[
j
]
-
0.5
;
cost
[
i
]
+=
absVal
-
0.5
;
}
}
}
...
...
@@ -3640,17 +3641,20 @@ void CpuMatrix::smoothL1Bp(Matrix& output, Matrix& label) {
CHECK_EQ
(
label
.
getHeight
(),
numSamples
);
CHECK_EQ
(
output
.
getHeight
(),
numSamples
);
CHECK_EQ
(
label
.
getWidth
(),
dim
);
CHECK_EQ
(
getWidth
(),
(
size_t
)
1
);
CHECK_EQ
(
getWidth
(),
dim
);
real
*
out
=
output
.
getData
();
real
*
cost
=
getData
();
real
*
lbl
=
label
.
getData
();
real
*
grad
=
getData
();
// f'(x) = x if |x| < 1
// = sign(x) otherwise
for
(
size_t
i
=
0
;
i
<
numSamples
;
++
i
,
out
+=
dim
,
cost
+=
dim
,
lbl
+=
dim
)
{
for
(
size_t
i
=
0
;
i
<
numSamples
;
++
i
,
out
+=
dim
,
grad
+=
dim
,
lbl
+=
dim
)
{
for
(
size_t
j
=
0
;
j
<
dim
;
++
j
)
{
cost
[
j
]
=
out
[
j
]
-
lbl
[
j
];
if
(
std
::
fabs
(
cost
[
j
])
>=
1
)
cost
[
j
]
=
(
0
<
cost
[
j
])
-
(
cost
[
j
]
<
0
);
real
val
=
out
[
j
]
-
lbl
[
j
];
if
(
std
::
fabs
(
val
)
<
1
)
{
grad
[
j
]
+=
val
;
}
else
{
grad
[
j
]
+=
(
real
(
0
)
<
val
)
-
(
val
<
real
(
0
));
}
}
}
}
...
...
python/paddle/trainer/config_parser.py
浏览文件 @
59be92e0
...
...
@@ -2119,6 +2119,7 @@ define_cost('MultiBinaryLabelCrossEntropy', 'multi_binary_label_cross_entropy')
define_cost
(
'SoftBinaryClassCrossEntropy'
,
'soft_binary_class_cross_entropy'
)
define_cost
(
'HuberTwoClass'
,
'huber'
)
define_cost
(
'SumCost'
,
'sum_cost'
)
define_cost
(
'SmoothL1Cost'
,
'smooth_l1'
)
@
config_layer
(
'hsigmoid'
)
...
...
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
59be92e0
...
...
@@ -116,6 +116,7 @@ __all__ = [
'spp_layer'
,
'pad_layer'
,
'eos_layer'
,
'smooth_l1_cost'
,
'layer_support'
,
]
...
...
@@ -201,6 +202,7 @@ class LayerType(object):
SOFT_BIN_CLASS_CROSS_ENTROPY
=
"soft_binary_class_cross_entropy"
MULTI_BIN_LABEL_CROSS_ENTROPY
=
"multi_binary_label_cross_entropy"
SUM_COST
=
"sum_cost"
SMOOTH_L1
=
"smooth_l1"
@
staticmethod
def
is_layer_type
(
type_name
):
...
...
@@ -5249,8 +5251,6 @@ def multi_binary_label_cross_entropy(input,
:type input: LayerOutput
:param label: The input label.
:type input: LayerOutput
:param type: The type of cost.
:type type: basestring
:param name: The name of this layers. It is not necessary.
:type name: None|basestring
:param coeff: The coefficient affects the gradient in the backward.
...
...
@@ -5279,3 +5279,52 @@ def multi_binary_label_cross_entropy(input,
LayerType
.
MULTI_BIN_LABEL_CROSS_ENTROPY
,
parents
=
[
input
,
label
],
size
=
1
)
@
wrap_name_default
()
@
layer_support
()
def
smooth_l1_cost
(
input
,
label
,
name
=
None
,
layer_attr
=
None
):
"""
This is a L1 loss but more smooth. It requires that the
size of input and label are equal. The formula is as follows,
.. math::
L = \sum_{i} smooth_{L1}(input_i - label_i)
in which
.. math::
smooth_{L1}(x) =
\\
begin{cases} 0.5x^2&
\\
text{if}
\\
|x| < 1
\\\\
|x|-0.5&
\\
text{otherwise} \end{cases}
More details can be found by referring to `Fast R-CNN
<https://arxiv.org/pdf/1504.08083v2.pdf>`_
.. code-block:: python
cost = smooth_l1_cost(input=input_layer,
label=label_layer)
:param input: The input layer.
:type input: LayerOutput
:param label: The input label.
:type input: LayerOutput
:param name: The name of this layers. It is not necessary.
:type name: None|basestring
:param layer_attr: Extra Layer Attribute.
:type layer_attr: ExtraLayerAttribute
:return: LayerOutput object.
:rtype: LayerOutput
"""
assert
isinstance
(
input
,
LayerOutput
)
assert
isinstance
(
label
,
LayerOutput
)
assert
input
.
size
==
label
.
size
Layer
(
name
=
name
,
type
=
LayerType
.
SMOOTH_L1
,
inputs
=
[
input
.
name
,
label
.
name
],
**
ExtraLayerAttribute
.
to_kwargs
(
layer_attr
))
return
LayerOutput
(
name
,
LayerType
.
SMOOTH_L1
,
parents
=
[
input
,
label
],
size
=
1
)
python/paddle/trainer_config_helpers/tests/configs/file_list.sh
浏览文件 @
59be92e0
...
...
@@ -5,6 +5,6 @@ last_first_seq test_expand_layer test_ntm_layers test_hsigmoid
img_layers img_trans_layers util_layers simple_rnn_layers unused_layers test_cost_layers
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_seq_concat_reshape test_pad
test_smooth_l1
)
export
whole_configs
=(
test_split_datasource
)
python/paddle/trainer_config_helpers/tests/configs/protostr/test_smooth_l1.protostr
0 → 100644
浏览文件 @
59be92e0
type: "nn"
layers {
name: "input"
type: "data"
size: 300
active_type: ""
}
layers {
name: "label"
type: "data"
size: 300
active_type: ""
}
layers {
name: "__smooth_l1_cost_0__"
type: "smooth_l1"
size: 1
active_type: ""
inputs {
input_layer_name: "input"
}
inputs {
input_layer_name: "label"
}
coeff: 1.0
}
input_layer_names: "input"
input_layer_names: "label"
output_layer_names: "__smooth_l1_cost_0__"
sub_models {
name: "root"
layer_names: "input"
layer_names: "label"
layer_names: "__smooth_l1_cost_0__"
input_layer_names: "input"
input_layer_names: "label"
output_layer_names: "__smooth_l1_cost_0__"
is_recurrent_layer_group: false
}
python/paddle/trainer_config_helpers/tests/configs/test_smooth_l1.py
0 → 100644
浏览文件 @
59be92e0
from
paddle.trainer_config_helpers
import
*
data
=
data_layer
(
name
=
'input'
,
size
=
300
)
lbl
=
data_layer
(
name
=
'label'
,
size
=
300
)
smooth_l1
=
smooth_l1_cost
(
input
=
data
,
label
=
lbl
)
outputs
(
smooth_l1
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录