Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
5939a17c
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
5939a17c
编写于
10月 24, 2017
作者:
Y
yangyaming
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Follow comments and adapt to new interface.
上级
05211610
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
47 addition
and
43 deletion
+47
-43
paddle/operators/huber_loss_op.cc
paddle/operators/huber_loss_op.cc
+36
-31
paddle/operators/huber_loss_op.h
paddle/operators/huber_loss_op.h
+8
-9
python/paddle/v2/framework/tests/test_huber_loss_op.py
python/paddle/v2/framework/tests/test_huber_loss_op.py
+3
-3
未找到文件。
paddle/operators/huber_loss_op.cc
浏览文件 @
5939a17c
...
...
@@ -21,24 +21,24 @@ class HuberLossOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"X"
),
"Input(X) must be initialized."
);
PADDLE_ENFORCE_NOT_NULL
(
ctx
.
InputVar
(
"Y"
),
"Input(Y) must be initialized."
);
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) must be initialized."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Y"
),
"Input(Y) must be initialized."
);
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
y
=
ctx
.
Input
<
Tensor
>
(
"Y"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
y_dims
=
ctx
->
GetInputDim
(
"Y"
);
PADDLE_ENFORCE_EQ
(
x
->
dims
(),
y
->
dims
()
);
PADDLE_ENFORCE_EQ
(
framework
::
arity
(
x
->
dims
()
),
2
,
PADDLE_ENFORCE_EQ
(
x
_dims
,
y_dims
);
PADDLE_ENFORCE_EQ
(
x_dims
.
size
(
),
2
,
"The rank of Input(X) must be 2 and the shape is "
"[batch_size, 1]."
);
PADDLE_ENFORCE_EQ
(
x
->
dims
()
[
1
],
1
,
PADDLE_ENFORCE_EQ
(
x
_dims
[
1
],
1
,
"Each row of Input(X) contains a real value, "
"so the 2nd dimension of Input(X) must be 1."
);
ctx
.
Output
<
Tensor
>
(
"Residual"
)
->
Resize
(
x
->
dims
());
ctx
.
Output
<
Tensor
>
(
"Out"
)
->
Resize
({
x
->
dims
()[
0
],
1
});
ctx
->
SetOutputDim
(
"Residual"
,
x_dims
);
ctx
->
SetOutputDim
(
"Out"
,
{
x_dims
[
0
],
1
});
ctx
->
ShareLoD
(
"X"
,
"Out"
);
}
};
...
...
@@ -55,7 +55,7 @@ class HuberLossOpMaker : public framework::OpProtoAndCheckerMaker {
"The target value of huber loss op."
"Y is a 2-D tensor with shape [batch_size, 1]."
);
AddOutput
(
"Residual"
,
"Intermediate tensor to cache residual value
of
Y and X."
"Intermediate tensor to cache residual value
between
Y and X."
"The shape is same as Input(X) and will be reused in backward."
)
.
AsIntermediate
();
AddOutput
(
"Out"
,
...
...
@@ -82,25 +82,30 @@ class HuberLossGradOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
const
framework
::
InferShapeContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
y
=
ctx
.
Input
<
Tensor
>
(
"Y"
);
auto
*
residual
=
ctx
.
Input
<
Tensor
>
(
"Residual"
);
auto
*
out_grad
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
x_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
*
y_grad
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"Y"
));
PADDLE_ENFORCE_NOT_NULL
(
x
,
"Input(X) should not be null."
);
PADDLE_ENFORCE_NOT_NULL
(
y
,
"Input(Y) should not be null."
);
PADDLE_ENFORCE_NOT_NULL
(
residual
,
"Input(Residual) should not be null."
);
PADDLE_ENFORCE_NOT_NULL
(
out_grad
,
"Input(Out@GRAD) should not be null."
);
PADDLE_ENFORCE_EQ
(
residual
->
dims
(),
x
->
dims
());
PADDLE_ENFORCE_EQ
(
out_grad
->
dims
(),
x
->
dims
());
if
(
x_grad
)
x_grad
->
Resize
(
x
->
dims
());
if
(
y_grad
)
y_grad
->
Resize
(
y
->
dims
());
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Y"
),
"Input(Y) should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Residual"
),
"Input(Residual) should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"Input(Out@GRAD) should not be null."
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
y_dims
=
ctx
->
GetInputDim
(
"Y"
);
auto
residual_dims
=
ctx
->
GetInputDim
(
"Residual"
);
auto
out_grad_dims
=
ctx
->
GetInputDim
(
framework
::
GradVarName
(
"Out"
));
PADDLE_ENFORCE_EQ
(
residual_dims
,
x_dims
);
PADDLE_ENFORCE_EQ
(
out_grad_dims
,
x_dims
);
auto
x_grad_name
=
framework
::
GradVarName
(
"X"
);
auto
y_grad_name
=
framework
::
GradVarName
(
"Y"
);
if
(
ctx
->
HasOutput
(
x_grad_name
))
{
ctx
->
SetOutputDim
(
x_grad_name
,
x_dims
);
}
if
(
ctx
->
HasOutput
(
y_grad_name
))
{
ctx
->
SetOutputDim
(
y_grad_name
,
y_dims
);
}
}
};
...
...
paddle/operators/huber_loss_op.h
浏览文件 @
5939a17c
...
...
@@ -42,14 +42,14 @@ struct HuberLossForward {
};
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
class
HuberLossKernel
:
public
framework
::
OpKernel
{
class
HuberLossKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in0
=
context
.
Input
<
Tensor
>
(
"X"
);
auto
*
in1
=
context
.
Input
<
Tensor
>
(
"Y"
);
auto
*
out0
=
context
.
Output
<
Tensor
>
(
"Residual"
);
auto
*
out1
=
context
.
Output
<
Tensor
>
(
"Out"
);
auto
delta
=
static_cast
<
T
>
(
context
.
op
().
Attr
<
AttrType
>
(
"delta"
));
auto
delta
=
static_cast
<
T
>
(
context
.
Attr
<
AttrType
>
(
"delta"
));
auto
place
=
context
.
GetEigenDevice
<
Place
>
();
auto
x
=
EigenVector
<
T
>::
Flatten
(
*
in0
);
...
...
@@ -65,11 +65,10 @@ class HuberLossKernel : public framework::OpKernel {
template
<
typename
T
>
struct
HuberLossBackward
{
HOSTDEVICE
HuberLossBackward
(
const
T
&
delta
,
bool
is_x
)
:
is_x
(
is_x
),
delta
(
delta
)
{}
HOSTDEVICE
HuberLossBackward
(
const
T
&
delta
,
T
sign
)
:
sign
(
sign
),
delta
(
delta
)
{}
HOSTDEVICE
T
operator
()(
const
T
&
val
)
const
{
T
sign
=
is_x
?
-
1.0
:
1.0
;
T
abs_val
=
std
::
abs
(
val
);
if
(
abs_val
<=
delta
)
{
return
sign
*
val
;
...
...
@@ -82,12 +81,12 @@ struct HuberLossBackward {
}
}
bool
is_x
;
T
sign
;
T
delta
;
};
template
<
typename
Place
,
typename
T
,
typename
AttrType
=
T
>
class
HuberLossGradKernel
:
public
framework
::
OpKernel
{
class
HuberLossGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
in0
=
context
.
Input
<
Tensor
>
(
"Residual"
);
...
...
@@ -104,14 +103,14 @@ class HuberLossGradKernel : public framework::OpKernel {
out0
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
x_grad
=
EigenVector
<
T
>::
Flatten
(
*
out0
);
x_grad
.
device
(
place
)
=
out_grad
*
residual
.
unaryExpr
(
HuberLossBackward
<
T
>
(
delta
,
true
));
out_grad
*
residual
.
unaryExpr
(
HuberLossBackward
<
T
>
(
delta
,
-
1.0
));
}
if
(
out1
)
{
out1
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
y_grad
=
EigenVector
<
T
>::
Flatten
(
*
out1
);
y_grad
.
device
(
place
)
=
out_grad
*
residual
.
unaryExpr
(
HuberLossBackward
<
T
>
(
delta
,
false
));
out_grad
*
residual
.
unaryExpr
(
HuberLossBackward
<
T
>
(
delta
,
1.0
));
}
}
};
...
...
python/paddle/v2/framework/tests/test_huber_loss_op.py
浏览文件 @
5939a17c
...
...
@@ -32,15 +32,15 @@ class TestHuberLossOp(OpTest):
self
.
check_output
()
def
test_check_grad_normal
(
self
):
self
.
check_grad
([
'X'
,
'Y'
],
'Out'
,
max_relative_error
=
0.0
5
)
self
.
check_grad
([
'X'
,
'Y'
],
'Out'
,
max_relative_error
=
0.0
08
)
def
test_check_grad_ingore_x
(
self
):
self
.
check_grad
(
[
'Y'
],
'Out'
,
max_relative_error
=
0.
5
,
no_grad_set
=
set
(
"residual"
))
[
'Y'
],
'Out'
,
max_relative_error
=
0.
008
,
no_grad_set
=
set
(
"residual"
))
def
test_check_grad_ingore_y
(
self
):
self
.
check_grad
(
[
'X'
],
'Out'
,
max_relative_error
=
0.
5
,
no_grad_set
=
set
(
'residual'
))
[
'X'
],
'Out'
,
max_relative_error
=
0.
008
,
no_grad_set
=
set
(
'residual'
))
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录