Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
e6db484d
P
Paddle
项目概览
Crayon鑫
/
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看板
提交
e6db484d
编写于
8月 14, 2017
作者:
L
Luo Tao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
make clear that current huber_cost is for two-classification
上级
493e1c04
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
50 addition
and
40 deletion
+50
-40
paddle/gserver/layers/CostLayer.cpp
paddle/gserver/layers/CostLayer.cpp
+16
-13
paddle/gserver/layers/CostLayer.h
paddle/gserver/layers/CostLayer.h
+7
-11
paddle/gserver/tests/test_LayerGrad.cpp
paddle/gserver/tests/test_LayerGrad.cpp
+1
-1
python/paddle/trainer/config_parser.py
python/paddle/trainer/config_parser.py
+1
-1
python/paddle/trainer_config_helpers/layers.py
python/paddle/trainer_config_helpers/layers.py
+19
-8
python/paddle/trainer_config_helpers/tests/configs/protostr/test_cost_layers.protostr
..._helpers/tests/configs/protostr/test_cost_layers.protostr
+5
-5
python/paddle/trainer_config_helpers/tests/configs/test_cost_layers.py
.../trainer_config_helpers/tests/configs/test_cost_layers.py
+1
-1
未找到文件。
paddle/gserver/layers/CostLayer.cpp
浏览文件 @
e6db484d
...
...
@@ -575,9 +575,9 @@ void MultiBinaryLabelCrossEntropy::backwardImp(Matrix& output,
//
// Huber loss for robust 2-classes classification
//
REGISTER_LAYER
(
huber
,
HuberTwoClass
);
REGISTER_LAYER
(
huber
,
HuberTwoClass
ification
);
bool
HuberTwoClass
::
init
(
const
LayerMap
&
layerMap
,
bool
HuberTwoClass
ification
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
CostLayer
::
init
(
layerMap
,
parameterMap
);
if
(
useGpu_
)
{
...
...
@@ -589,7 +589,9 @@ bool HuberTwoClass::init(const LayerMap& layerMap,
return
true
;
}
void
HuberTwoClass
::
forwardImp
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
cost
)
{
void
HuberTwoClassification
::
forwardImp
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
cost
)
{
if
(
useGpu_
)
{
for
(
size_t
i
=
0
;
i
<
inputLayers_
.
size
();
i
++
)
{
tmpCpuInput_
[
i
].
resizeAndCopyFrom
(
...
...
@@ -600,10 +602,11 @@ void HuberTwoClass::forwardImp(Matrix& output, Argument& label, Matrix& cost) {
forwardImpIn
(
output
,
label
,
cost
);
}
void
HuberTwoClass
::
forwardImpIn
(
Matrix
&
output
,
void
HuberTwoClass
ification
::
forwardImpIn
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
target
)
{
size_t
numSamples
=
target
.
getHeight
();
CHECK
(
label
.
ids
);
CHECK_EQ
((
*
label
.
ids
).
getSize
(),
numSamples
);
CHECK_EQ
(
output
.
getHeight
(),
numSamples
);
CHECK_EQ
(
output
.
getWidth
(),
(
size_t
)
1
);
...
...
@@ -624,7 +627,7 @@ void HuberTwoClass::forwardImpIn(Matrix& output,
target
.
copyFrom
(
cost
.
data
(),
numSamples
);
}
void
HuberTwoClass
::
backwardImp
(
Matrix
&
outputValue
,
void
HuberTwoClass
ification
::
backwardImp
(
Matrix
&
outputValue
,
Argument
&
label
,
Matrix
&
outputGrad
)
{
if
(
useGpu_
)
{
...
...
@@ -636,7 +639,7 @@ void HuberTwoClass::backwardImp(Matrix& outputValue,
}
}
void
HuberTwoClass
::
backwardImpIn
(
Matrix
&
output
,
void
HuberTwoClass
ification
::
backwardImpIn
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
outputG
)
{
size_t
numSamples
=
output
.
getHeight
();
...
...
paddle/gserver/layers/CostLayer.h
浏览文件 @
e6db484d
...
...
@@ -307,21 +307,17 @@ public:
/**
* Huber loss for robust 2-classes classification.
*
* For label={0, 1}, let y=2*label-1. Given output f, the loss is:
* \f[
* Loss =
* \left\{\begin{matrix}
* 4 * y * f & \textit{if} \ \ y* f < -1 \\
* (1 - y * f)^2 & \textit{if} \ \ -1 < y * f < 1 \\
* 0 & \textit{otherwise}
* \end{matrix}\right.
* \f]
* For label={0, 1}, let y=2*label-1. Given output f(x), the loss is:
* Loss = 4 * y * f, if y* f < -1 \\
* Loss = (1 - y * f)^2, if -1 < y * f < 1 \\
* Loss = 0, otherwise
*/
class
HuberTwoClass
:
public
CostLayer
{
class
HuberTwoClass
ification
:
public
CostLayer
{
std
::
vector
<
Argument
>
tmpCpuInput_
;
public:
explicit
HuberTwoClass
(
const
LayerConfig
&
config
)
:
CostLayer
(
config
)
{}
explicit
HuberTwoClassification
(
const
LayerConfig
&
config
)
:
CostLayer
(
config
)
{}
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
override
;
...
...
paddle/gserver/tests/test_LayerGrad.cpp
浏览文件 @
e6db484d
...
...
@@ -830,7 +830,7 @@ TEST(Layer, square_error_weighted) {
TEST
(
Layer
,
huber_two_class
)
{
TestConfig
config
;
config
.
layerConfig
.
set_type
(
"huber"
);
config
.
layerConfig
.
set_type
(
"huber
_classification
"
);
config
.
biasSize
=
0
;
config
.
inputDefs
.
push_back
({
INPUT_DATA
,
"layer_0"
,
1
,
0
});
...
...
python/paddle/trainer/config_parser.py
浏览文件 @
e6db484d
...
...
@@ -2255,7 +2255,7 @@ define_cost('PnpairValidation', 'pnpair-validation')
define_cost
(
'SumOfSquaresCostLayer'
,
'square_error'
)
define_cost
(
'MultiBinaryLabelCrossEntropy'
,
'multi_binary_label_cross_entropy'
)
define_cost
(
'SoftBinaryClassCrossEntropy'
,
'soft_binary_class_cross_entropy'
)
define_cost
(
'HuberTwoClass
'
,
'huber
'
)
define_cost
(
'HuberTwoClass
ification'
,
'huber_classification
'
)
define_cost
(
'SumCost'
,
'sum_cost'
)
define_cost
(
'SmoothL1Cost'
,
'smooth_l1'
)
...
...
python/paddle/trainer_config_helpers/layers.py
浏览文件 @
e6db484d
...
...
@@ -108,7 +108,7 @@ __all__ = [
'sum_cost'
,
'rank_cost'
,
'lambda_cost'
,
'huber_cost'
,
'huber_c
lassification_c
ost'
,
'block_expand_layer'
,
'maxout_layer'
,
'out_prod_layer'
,
...
...
@@ -216,7 +216,7 @@ class LayerType(object):
RANK_COST
=
'rank-cost'
LAMBDA_COST
=
'lambda_cost'
HUBER
=
'huber
'
HUBER
_CLASSIFICATION
=
'huber_classification
'
CROSS_ENTROPY
=
'multi-class-cross-entropy'
CROSS_ENTROPY_WITH_SELFNORM
=
'multi_class_cross_entropy_with_selfnorm'
SOFT_BIN_CLASS_CROSS_ENTROPY
=
'soft_binary_class_cross_entropy'
...
...
@@ -5605,16 +5605,26 @@ def sum_cost(input, name=None, layer_attr=None):
@
wrap_name_default
()
@
layer_support
()
def
huber_cost
(
input
,
label
,
name
=
None
,
coeff
=
1.0
,
layer_attr
=
None
):
def
huber_classification_cost
(
input
,
label
,
name
=
None
,
coeff
=
1.0
,
layer_attr
=
None
):
"""
A loss layer for huber loss.
For classification purposes, a variant of the Huber loss called modified Huber
is sometimes used. Given a prediction f(x) (a real-valued classifier score) and
a true binary class label :math:`y\in \left \{-1, 1
\r
ight \}`, the modified Huber
loss is defined as:
.. math:
loss = \max \left ( 0, 1-yf(x)
\r
ight )^2, yf(x)\geq 1
loss = -4yf(x),
\t
ext{otherwise}
The example usage is:
.. code-block:: python
cost = huber_cost(input=input_layer,
label=label_layer)
cost = huber_classification_cost(input=input_layer, label=label_layer)
:param input: The first input layer.
:type input: LayerOutput.
...
...
@@ -5634,11 +5644,12 @@ def huber_cost(input, label, name=None, coeff=1.0, layer_attr=None):
assert
input
.
size
==
1
Layer
(
name
=
name
,
type
=
LayerType
.
HUBER
,
type
=
LayerType
.
HUBER
_CLASSIFICATION
,
inputs
=
[
input
.
name
,
label
.
name
],
coeff
=
coeff
,
**
ExtraLayerAttribute
.
to_kwargs
(
layer_attr
))
return
LayerOutput
(
name
,
LayerType
.
HUBER
,
parents
=
[
input
,
label
],
size
=
1
)
return
LayerOutput
(
name
,
LayerType
.
HUBER_CLASSIFICATION
,
parents
=
[
input
,
label
],
size
=
1
)
@
wrap_name_default
()
...
...
python/paddle/trainer_config_helpers/tests/configs/protostr/test_cost_layers.protostr
浏览文件 @
e6db484d
...
...
@@ -180,8 +180,8 @@ layers {
active_type: ""
}
layers {
name: "__huber_cost_0__"
type: "huber"
name: "__huber_c
lassification_c
ost_0__"
type: "huber
_classification
"
size: 1
active_type: ""
inputs {
...
...
@@ -300,7 +300,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_cost_0__"
output_layer_names: "__huber_c
lassification_c
ost_0__"
output_layer_names: "__multi_binary_label_cross_entropy_0__"
output_layer_names: "__sum_cost_0__"
output_layer_names: "__nce_layer_0__"
...
...
@@ -326,7 +326,7 @@ sub_models {
layer_names: "__cross_entropy_with_selfnorm_0__"
layer_names: "huber_probs"
layer_names: "huber_label"
layer_names: "__huber_cost_0__"
layer_names: "__huber_c
lassification_c
ost_0__"
layer_names: "__multi_binary_label_cross_entropy_0__"
layer_names: "__sum_cost_0__"
layer_names: "__nce_layer_0__"
...
...
@@ -349,7 +349,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_cost_0__"
output_layer_names: "__huber_c
lassification_c
ost_0__"
output_layer_names: "__multi_binary_label_cross_entropy_0__"
output_layer_names: "__sum_cost_0__"
output_layer_names: "__nce_layer_0__"
...
...
python/paddle/trainer_config_helpers/tests/configs/test_cost_layers.py
浏览文件 @
e6db484d
...
...
@@ -33,7 +33,7 @@ outputs(
input
=
probs
,
label
=
xe_label
),
cross_entropy_with_selfnorm
(
input
=
probs
,
label
=
xe_label
),
huber_cost
(
huber_c
lassification_c
ost
(
input
=
data_layer
(
name
=
'huber_probs'
,
size
=
1
),
label
=
data_layer
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录