Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
27a99bfb
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看板
提交
27a99bfb
编写于
8月 17, 2017
作者:
L
Luo Tao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add base class for huber_regression_cost and huber_classification_cost
上级
7f9af125
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
46 addition
and
44 deletion
+46
-44
doc/api/v2/config/layer.rst
doc/api/v2/config/layer.rst
+3
-3
paddle/gserver/layers/CostLayer.cpp
paddle/gserver/layers/CostLayer.cpp
+23
-32
paddle/gserver/layers/CostLayer.h
paddle/gserver/layers/CostLayer.h
+19
-8
python/paddle/v2/tests/test_layer.py
python/paddle/v2/tests/test_layer.py
+1
-1
未找到文件。
doc/api/v2/config/layer.rst
浏览文件 @
27a99bfb
...
...
@@ -409,9 +409,9 @@ multi_binary_label_cross_entropy_cost
.. autoclass:: paddle.v2.layer.multi_binary_label_cross_entropy_cost
:noindex:
huber_cost
----------
.. autoclass:: paddle.v2.layer.huber_cost
huber_c
lassification_c
ost
----------
---------------
.. autoclass:: paddle.v2.layer.huber_c
lassification_c
ost
:noindex:
lambda_cost
...
...
paddle/gserver/layers/CostLayer.cpp
浏览文件 @
27a99bfb
...
...
@@ -572,13 +572,8 @@ void MultiBinaryLabelCrossEntropy::backwardImp(Matrix& output,
}
}
//
// Huber loss for robust 2-classes classification
//
REGISTER_LAYER
(
huber
,
HuberTwoClassification
);
bool
HuberTwoClassification
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
bool
HuberCost
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
CostLayer
::
init
(
layerMap
,
parameterMap
);
if
(
useGpu_
)
{
tmpCpuInput_
.
reserve
(
inputLayers_
.
size
());
...
...
@@ -589,9 +584,7 @@ bool HuberTwoClassification::init(const LayerMap& layerMap,
return
true
;
}
void
HuberTwoClassification
::
forwardImp
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
cost
)
{
void
HuberCost
::
forwardImp
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
cost
)
{
if
(
useGpu_
)
{
for
(
size_t
i
=
0
;
i
<
inputLayers_
.
size
();
i
++
)
{
tmpCpuInput_
[
i
].
resizeAndCopyFrom
(
...
...
@@ -599,12 +592,22 @@ void HuberTwoClassification::forwardImp(Matrix& output,
}
hl_stream_synchronize
(
HPPL_STREAM_DEFAULT
);
}
forwardImpIn
(
output
,
label
,
cost
);
}
void
HuberTwoClassification
::
forwardImpIn
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
target
)
{
//
// Huber loss for robust 2-classes classification
//
REGISTER_LAYER
(
huber_classification
,
HuberTwoClassification
);
bool
HuberTwoClassification
::
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
{
return
HuberCost
::
init
(
layerMap
,
parameterMap
);
}
void
HuberTwoClassification
::
forwardImp
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
target
)
{
HuberCost
::
forwardImp
(
output
,
label
,
target
);
size_t
numSamples
=
target
.
getHeight
();
CHECK
(
label
.
ids
);
CHECK_EQ
((
*
label
.
ids
).
getSize
(),
numSamples
);
...
...
@@ -627,25 +630,13 @@ void HuberTwoClassification::forwardImpIn(Matrix& output,
target
.
copyFrom
(
cost
.
data
(),
numSamples
);
}
void
HuberTwoClassification
::
backwardImp
(
Matrix
&
output
Value
,
void
HuberTwoClassification
::
backwardImp
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
outputGrad
)
{
if
(
useGpu_
)
{
backwardImpIn
(
*
tmpCpuInput_
[
0
].
value
,
tmpCpuInput_
[
1
],
*
tmpCpuInput_
[
0
].
grad
);
outputGrad
.
copyFrom
(
*
tmpCpuInput_
[
0
].
grad
);
}
else
{
backwardImpIn
(
outputValue
,
label
,
outputGrad
);
}
}
void
HuberTwoClassification
::
backwardImpIn
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
outputG
)
{
Matrix
&
outputG
)
{
size_t
numSamples
=
output
.
getHeight
();
real
*
out
=
output
.
getData
();
real
*
grad
=
outputG
.
getData
();
int
*
lbl
=
(
*
label
.
ids
)
.
getData
();
real
*
out
=
useGpu_
?
tmpCpuInput_
[
0
].
value
->
getData
()
:
output
.
getData
();
int
*
lbl
=
useGpu_
?
tmpCpuInput_
[
1
].
ids
->
getData
()
:
(
*
label
.
ids
)
.
getData
();
real
*
grad
=
useGpu_
?
tmpCpuInput_
[
0
].
grad
->
getData
()
:
outputG
.
getData
();
for
(
size_t
i
=
0
;
i
<
numSamples
;
++
i
)
{
int
y
=
2
*
lbl
[
i
]
-
1
;
if
(
y
*
out
[
i
]
<
-
1
)
...
...
@@ -653,8 +644,8 @@ void HuberTwoClassification::backwardImpIn(Matrix& output,
else
if
(
y
*
out
[
i
]
<
1
)
grad
[
i
]
+=
-
2
*
(
1
-
y
*
out
[
i
])
*
y
;
}
if
(
useGpu_
)
outputG
.
copyFrom
(
grad
,
numSamples
);
}
/**
* This cost layer compute the sum of its input as loss.
* \f[
...
...
paddle/gserver/layers/CostLayer.h
浏览文件 @
27a99bfb
...
...
@@ -304,6 +304,23 @@ public:
Matrix
&
outputGrad
)
override
;
};
/*
* A base layer for HuberRegressionLoss and HuberTwoClassification.
*/
class
HuberCost
:
public
CostLayer
{
public:
std
::
vector
<
Argument
>
tmpCpuInput_
;
explicit
HuberCost
(
const
LayerConfig
&
config
)
:
CostLayer
(
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
)
{}
};
/**
* Huber loss for robust 2-classes classification.
*
...
...
@@ -312,25 +329,19 @@ public:
* Loss = (1 - y * f)^2, if -1 < y * f < 1 \\
* Loss = 0, otherwise
*/
class
HuberTwoClassification
:
public
CostLayer
{
std
::
vector
<
Argument
>
tmpCpuInput_
;
class
HuberTwoClassification
:
public
HuberCost
{
public:
explicit
HuberTwoClassification
(
const
LayerConfig
&
config
)
:
CostLayer
(
config
)
{}
:
HuberCost
(
config
)
{}
bool
init
(
const
LayerMap
&
layerMap
,
const
ParameterMap
&
parameterMap
)
override
;
void
forwardImp
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
cost
)
override
;
void
forwardImpIn
(
Matrix
&
output
,
Argument
&
label
,
Matrix
&
cost
);
void
backwardImp
(
Matrix
&
outputValue
,
Argument
&
label
,
Matrix
&
outputGrad
)
override
;
void
backwardImpIn
(
Matrix
&
outputValue
,
Argument
&
label
,
Matrix
&
outputGrad
);
};
typedef
std
::
shared_ptr
<
CostLayer
>
CostLayerPtr
;
...
...
python/paddle/v2/tests/test_layer.py
浏览文件 @
27a99bfb
...
...
@@ -141,7 +141,7 @@ 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_cost
(
input
=
score
,
label
=
label
)
cost11
=
layer
.
huber_c
lassification_c
ost
(
input
=
score
,
label
=
label
)
print
layer
.
parse_network
([
cost1
,
cost2
])
print
layer
.
parse_network
([
cost3
,
cost4
])
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录