Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
3065cb26
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看板
提交
3065cb26
编写于
8月 18, 2017
作者:
L
Luo Tao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add huber_regression_cost
上级
27a99bfb
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
192 addition
and
3 deletion
+192
-3
doc/api/v2/config/layer.rst
doc/api/v2/config/layer.rst
+5
-0
paddle/gserver/layers/CostLayer.cpp
paddle/gserver/layers/CostLayer.cpp
+55
-0
paddle/gserver/layers/CostLayer.h
paddle/gserver/layers/CostLayer.h
+24
-0
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+19
-1
proto/ModelConfig.proto
proto/ModelConfig.proto
+3
-0
python/paddle/trainer/config_parser.py
python/paddle/trainer/config_parser.py
+11
-0
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+53
-0
python/paddle/trainer_config_helpers/tests/configs/protostr/test_cost_layers.protostr
..._helpers/tests/configs/protostr/test_cost_layers.protostr
+17
-0
python/paddle/trainer_config_helpers/tests/configs/test_cost_layers.py
.../trainer_config_helpers/tests/configs/test_cost_layers.py
+2
-0
python/paddle/v2/tests/test_layer.py
python/paddle/v2/tests/test_layer.py
+3
-2
未找到文件。
doc/api/v2/config/layer.rst
浏览文件 @
3065cb26
...
...
@@ -409,6 +409,11 @@ multi_binary_label_cross_entropy_cost
.. autoclass:: paddle.v2.layer.multi_binary_label_cross_entropy_cost
:noindex:
huber_regression_cost
-------------------------
.. autoclass:: paddle.v2.layer.huber_regression_cost
:noindex:
huber_classification_cost
-------------------------
.. autoclass:: paddle.v2.layer.huber_classification_cost
...
...
paddle/gserver/layers/CostLayer.cpp
浏览文件 @
3065cb26
...
...
@@ -594,6 +594,61 @@ void HuberCost::forwardImp(Matrix& output, Argument& label, Matrix& cost) {
}
}
//
// Huber loss for robust regression.
//
REGISTER_LAYER
(
huber_regression
,
HuberRegressionLoss
);
bool
HuberRegressionLoss
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
HuberCost
::
init
(
layerMap
,
parameterMap
);
delta_
=
config_
.
delta
();
return
true
;
}
void
HuberRegressionLoss
::
forwardImp
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
target
)
{
HuberCost
::
forwardImp
(
output
,
label
,
target
);
size_t
numSamples
=
target
.
getHeight
();
CHECK
(
label
.
value
);
CHECK_EQ
((
*
label
.
value
).
getHeight
(),
numSamples
);
CHECK_EQ
(
output
.
getHeight
(),
numSamples
);
CHECK_EQ
(
output
.
getWidth
(),
(
*
label
.
value
).
getWidth
());
CHECK_EQ
(
target
.
getWidth
(),
(
size_t
)
1
);
real
*
out
=
useGpu_
?
tmpCpuInput_
[
0
].
value
->
getData
()
:
output
.
getData
();
real
*
lbl
=
useGpu_
?
tmpCpuInput_
[
1
].
value
->
getData
()
:
(
*
label
.
value
).
getData
();
std
::
vector
<
real
>
cost
(
numSamples
);
for
(
size_t
i
=
0
;
i
<
numSamples
;
++
i
)
{
real
a
=
std
::
abs
(
lbl
[
i
]
-
out
[
i
]);
if
(
a
<=
delta_
)
cost
[
i
]
=
a
*
a
/
2
;
else
cost
[
i
]
=
delta_
*
(
a
-
delta_
/
2
);
}
target
.
copyFrom
(
cost
.
data
(),
numSamples
);
}
void
HuberRegressionLoss
::
backwardImp
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
outputG
)
{
size_t
numSamples
=
output
.
getHeight
();
real
*
out
=
useGpu_
?
tmpCpuInput_
[
0
].
value
->
getData
()
:
output
.
getData
();
real
*
lbl
=
useGpu_
?
tmpCpuInput_
[
1
].
value
->
getData
()
:
(
*
label
.
value
).
getData
();
real
*
grad
=
useGpu_
?
tmpCpuInput_
[
0
].
grad
->
getData
()
:
outputG
.
getData
();
for
(
size_t
i
=
0
;
i
<
numSamples
;
++
i
)
{
real
a
=
lbl
[
i
]
-
out
[
i
];
if
(
std
::
abs
(
a
)
<=
delta_
)
grad
[
i
]
+=
-
a
;
else
grad
[
i
]
+=
a
>
0
?
delta_
:
-
delta_
;
}
if
(
useGpu_
)
outputG
.
copyFrom
(
grad
,
numSamples
);
}
//
// Huber loss for robust 2-classes classification
//
...
...
paddle/gserver/layers/CostLayer.h
浏览文件 @
3065cb26
...
...
@@ -321,6 +321,30 @@ public:
void
backwardImp
(
Matrix
&
outputValue
,
Argument
&
label
,
Matrix
&
outputGrad
)
{}
};
/**
* Huber loss for robust regression.
*
* Given output f(x), label y and delta, the loss is:
* Loss = 0.5 * (1 - y * f)^2, if abs(y - f) <= delta \\
* Loss = delta * abs(y - f) - 0.5 * delta^2, otherwise
*/
class
HuberRegressionLoss
:
public
HuberCost
{
public:
explicit
HuberRegressionLoss
(
const
LayerConfig
&
config
)
:
HuberCost
(
config
)
{}
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
override
;
void
forwardImp
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
cost
)
override
;
void
backwardImp
(
Matrix
&
outputValue
,
Argument
&
label
,
Matrix
&
outputGrad
)
override
;
protected:
real
delta_
;
};
/**
* Huber loss for robust 2-classes classification.
*
...
...
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
3065cb26
...
...
@@ -828,6 +828,24 @@ TEST(Layer, square_error_weighted) {
}
}
TEST
(
Layer
,
huber_regression_loss
)
{
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"huber_regression"
);
config
.
biasSize
=
0
;
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
10
,
0
});
config
.
inputDefs
.
push_back
({
INPUT_DATA_TARGET
,
"layer_1"
,
10
,
0
});
config
.
layerConfig
.
add_inputs
();
config
.
layerConfig
.
add_inputs
();
for
(
auto
useGpu
:
{
false
,
true
})
{
for
(
auto
delta
:
{
1
,
3
,
5
})
{
config
.
layerConfig
.
set_delta
(
delta
);
testLayerGrad
(
config
,
"huber_regression"
,
100
,
/* trans */
false
,
useGpu
);
}
}
}
TEST
(
Layer
,
huber_two_class
)
{
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"huber_classification"
);
...
...
@@ -839,7 +857,7 @@ TEST(Layer, huber_two_class) {
config
.
layerConfig
.
add_inputs
();
for
(
auto
useGpu
:
{
false
,
true
})
{
testLayerGrad
(
config
,
"huber"
,
100
,
/* trans */
false
,
useGpu
);
testLayerGrad
(
config
,
"huber
_two_class
"
,
100
,
/* trans */
false
,
useGpu
);
}
}
...
...
proto/ModelConfig.proto
浏览文件 @
3065cb26
...
...
@@ -496,6 +496,9 @@ message LayerConfig {
optional
int32
axis
=
54
[
default
=
2
];
repeated
uint32
offset
=
55
;
repeated
uint32
shape
=
56
;
// for HuberRegressionLoss
optional
double
delta
=
57
[
default
=
1.0
];
}
message
EvaluatorConfig
{
...
...
python/paddle/trainer/config_parser.py
浏览文件 @
3065cb26
...
...
@@ -2317,6 +2317,17 @@ class LambdaCost(LayerBase):
self
.
config
.
max_sort_size
=
max_sort_size
@
config_layer
(
'huber_regression'
)
class
HuberRegressionLoss
(
LayerBase
):
def
__init__
(
self
,
name
,
inputs
,
delta
=
1.
,
coeff
=
1.
,
device
=
None
):
super
(
HuberRegressionLoss
,
self
).
__init__
(
name
,
'huber_regression'
,
1
,
inputs
=
inputs
,
device
=
device
)
config_assert
(
len
(
self
.
inputs
)
==
2
,
'HuberRegression must have 2 inputs'
)
self
.
config
.
delta
=
delta
self
.
config
.
coeff
=
coeff
@
config_layer
(
'nce'
)
class
NCELayer
(
LayerBase
):
def
__init__
(
self
,
...
...
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
3065cb26
...
...
@@ -108,6 +108,7 @@ __all__ = [
'sum_cost'
,
'rank_cost'
,
'lambda_cost'
,
'huber_regression_cost'
,
'huber_classification_cost'
,
'block_expand_layer'
,
'maxout_layer'
,
...
...
@@ -216,6 +217,7 @@ class LayerType(object):
RANK_COST
=
'rank-cost'
LAMBDA_COST
=
'lambda_cost'
HUBER_REGRESSION
=
'huber_regression'
HUBER_CLASSIFICATION
=
'huber_classification'
CROSS_ENTROPY
=
'multi-class-cross-entropy'
CROSS_ENTROPY_WITH_SELFNORM
=
'multi_class_cross_entropy_with_selfnorm'
...
...
@@ -5603,6 +5605,57 @@ def sum_cost(input, name=None, layer_attr=None):
return
LayerOutput
(
name
,
LayerType
.
SUM_COST
,
parents
=
[
input
],
size
=
1
)
@
wrap_name_default
()
@
layer_support
()
def
huber_regression_cost
(
input
,
label
,
name
=
None
,
delta
=
1.0
,
coeff
=
1.0
,
layer_attr
=
None
):
"""
In statistics, the Huber loss is a loss function used in robust regression,
that is less sensitive to outliers in data than the squared error loss.
Given a prediction f(x), a label y and :math:`\delta`, the loss function
is defined as:
.. math:
loss = 0.5*\left ( y-f(x)
\r
ight )^2, \left | y-f(x)
\r
ight |\leq \delta
loss = \delta \left | y-f(x)
\r
ight |-0.5\delta ^2, otherwise
The example usage is:
.. code-block:: python
cost = huber_regression_cost(input=input_layer, label=label_layer)
:param input: The first 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 delta: The difference between the observed and predicted values.
:type delta: float.
:param coeff: The coefficient affects the gradient in the backward.
:type coeff: float.
:param layer_attr: Extra Layer Attribute.
:type layer_attr: ExtraLayerAttribute
:return: LayerOutput object.
:rtype: LayerOutput.
"""
assert
isinstance
(
input
,
LayerOutput
)
Layer
(
name
=
name
,
type
=
LayerType
.
HUBER_REGRESSION
,
inputs
=
[
input
.
name
,
label
.
name
],
delta
=
delta
,
coeff
=
coeff
,
**
ExtraLayerAttribute
.
to_kwargs
(
layer_attr
))
return
LayerOutput
(
name
,
LayerType
.
HUBER_REGRESSION
,
parents
=
[
input
,
label
],
size
=
1
)
@
wrap_name_default
()
@
layer_support
()
def
huber_classification_cost
(
input
,
...
...
python/paddle/trainer_config_helpers/tests/configs/protostr/test_cost_layers.protostr
浏览文件 @
3065cb26
...
...
@@ -167,6 +167,20 @@ layers {
softmax_selfnorm_alpha: 0.1
coeff: 1.0
}
layers {
name: "__huber_regression_cost_0__"
type: "huber_regression"
size: 1
active_type: ""
inputs {
input_layer_name: "input"
}
inputs {
input_layer_name: "labels"
}
coeff: 1.0
delta: 1.0
}
layers {
name: "huber_probs"
type: "data"
...
...
@@ -300,6 +314,7 @@ output_layer_names: "__rank_cost_0__"
output_layer_names: "__lambda_cost_0__"
output_layer_names: "__cross_entropy_0__"
output_layer_names: "__cross_entropy_with_selfnorm_0__"
output_layer_names: "__huber_regression_cost_0__"
output_layer_names: "__huber_classification_cost_0__"
output_layer_names: "__multi_binary_label_cross_entropy_0__"
output_layer_names: "__sum_cost_0__"
...
...
@@ -324,6 +339,7 @@ sub_models {
layer_names: "__lambda_cost_0__"
layer_names: "__cross_entropy_0__"
layer_names: "__cross_entropy_with_selfnorm_0__"
layer_names: "__huber_regression_cost_0__"
layer_names: "huber_probs"
layer_names: "huber_label"
layer_names: "__huber_classification_cost_0__"
...
...
@@ -349,6 +365,7 @@ sub_models {
output_layer_names: "__lambda_cost_0__"
output_layer_names: "__cross_entropy_0__"
output_layer_names: "__cross_entropy_with_selfnorm_0__"
output_layer_names: "__huber_regression_cost_0__"
output_layer_names: "__huber_classification_cost_0__"
output_layer_names: "__multi_binary_label_cross_entropy_0__"
output_layer_names: "__sum_cost_0__"
...
...
python/paddle/trainer_config_helpers/tests/configs/test_cost_layers.py
浏览文件 @
3065cb26
...
...
@@ -33,6 +33,8 @@ outputs(
input
=
probs
,
label
=
xe_label
),
cross_entropy_with_selfnorm
(
input
=
probs
,
label
=
xe_label
),
huber_regression_cost
(
input
=
seq_in
,
label
=
labels
),
huber_classification_cost
(
input
=
data_layer
(
name
=
'huber_probs'
,
size
=
1
),
...
...
python/paddle/v2/tests/test_layer.py
浏览文件 @
3065cb26
...
...
@@ -141,12 +141,13 @@ class CostLayerTest(unittest.TestCase):
cost8
=
layer
.
rank_cost
(
left
=
score
,
right
=
score
,
label
=
score
)
cost9
=
layer
.
lambda_cost
(
input
=
inference
,
score
=
score
)
cost10
=
layer
.
sum_cost
(
input
=
inference
)
cost11
=
layer
.
huber_classification_cost
(
input
=
score
,
label
=
label
)
cost11
=
layer
.
huber_regression_cost
(
input
=
score
,
label
=
label
)
cost12
=
layer
.
huber_classification_cost
(
input
=
score
,
label
=
label
)
print
layer
.
parse_network
([
cost1
,
cost2
])
print
layer
.
parse_network
([
cost3
,
cost4
])
print
layer
.
parse_network
([
cost5
,
cost6
])
print
layer
.
parse_network
([
cost7
,
cost8
,
cost9
,
cost10
,
cost11
])
print
layer
.
parse_network
([
cost7
,
cost8
,
cost9
,
cost10
,
cost11
,
cost12
])
crf
=
layer
.
crf
(
input
=
inference
,
label
=
label
)
crf_decoding
=
layer
.
crf_decoding
(
input
=
inference
,
size
=
3
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录