Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f58c8db6
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
f58c8db6
编写于
9月 26, 2019
作者:
A
Aurelius84
提交者:
GitHub
9月 26, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Require x.dims=label.dims in huber_loss (#20017)
* x.dims == y.dims test=develop * refine comment
上级
cde73a7b
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
32 addition
and
35 deletion
+32
-35
paddle/fluid/operators/huber_loss_op.cc
paddle/fluid/operators/huber_loss_op.cc
+11
-18
python/paddle/fluid/tests/unittests/test_huber_loss_op.py
python/paddle/fluid/tests/unittests/test_huber_loss_op.py
+21
-17
未找到文件。
paddle/fluid/operators/huber_loss_op.cc
浏览文件 @
f58c8db6
...
...
@@ -32,23 +32,16 @@ class HuberLossOp : public framework::OperatorWithKernel {
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
y_dims
=
ctx
->
GetInputDim
(
"Y"
);
int
rank
=
x_dims
.
size
();
if
(
rank
==
y_dims
.
size
())
{
PADDLE_ENFORCE_EQ
(
y_dims
[
rank
-
1
],
1U
,
"The last dimension of Input(Y) should be equal to 1."
);
}
else
{
PADDLE_ENFORCE_EQ
(
rank
,
y_dims
.
size
()
+
1
,
PADDLE_ENFORCE_EQ
(
x_dims
.
size
(),
y_dims
.
size
(),
"The rank of Input(X) should be equal to "
"the rank of Input(Y) plus 1."
);
}
"the rank of Input(Y)."
);
bool
contain_unknown_dim
=
framework
::
contain_unknown_dim
(
x_dims
)
||
framework
::
contain_unknown_dim
(
y_dims
);
if
(
ctx
->
IsRuntime
()
||
!
contain_unknown_dim
)
{
PADDLE_ENFORCE_EQ
(
framework
::
slice_ddim
(
x_dims
,
0
,
rank
-
1
),
framework
::
slice_ddim
(
y_dims
,
0
,
rank
-
1
),
"The Input(X) and Input(Label) should have the same "
"shape except the last dimension."
);
PADDLE_ENFORCE_EQ
(
x_dims
,
y_dims
,
"The Input(X) and Input(Label) should have the same shape."
);
}
auto
out_dims
=
y_dims
;
...
...
@@ -64,16 +57,16 @@ class HuberLossOpMaker : public framework::OpProtoAndCheckerMaker {
void
Make
()
override
{
AddInput
(
"X"
,
"The input value of huber loss op."
"X is a
2-D tensor with shape [batch_size, 1
]."
);
"X is a
N-D tensor with shape [N_1, N_2,..., N_n
]."
);
AddInput
(
"Y"
,
"The target value of huber loss op."
"Y is a
2-D tensor with shape [batch_size, 1
]."
);
"Y is a
N-D tensor with shape [N_1, N_2,..., N_n
]."
);
AddOutput
(
"Residual"
,
"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"
,
"The output
tensor with shape [batch_size, 1
] "
"The output
N-D tensor with shape [N_1, N_2,..., N_n
] "
"which represents the huber loss."
);
AddAttr
<
AttrType
>
(
"delta"
,
"Hyper parameter in huber loss."
);
AddComment
(
R"DOC(
...
...
@@ -81,7 +74,7 @@ HuberLoss Operator.
Huber loss is a loss function used in robust regression. We define X as the
input value and Y as the target value. Huber loss can evaluate the fitness of
X to Y. Different from MSE loss, Huber loss is more robust for outliers.
T
he
X to Y. Different from MSE loss, Huber loss is more robust for outliers.
If t
he
shape of X and Y are [batch_size, 1]. The equation is:
$$
...
...
python/paddle/fluid/tests/unittests/test_huber_loss_op.py
浏览文件 @
f58c8db6
...
...
@@ -30,27 +30,25 @@ def huber_loss_forward(val, delta):
class
TestHuberLossOp
(
OpTest
):
def
setUp
(
self
):
self
.
op_type
=
'huber_loss'
self
.
samples_num
=
64
self
.
delta
=
1.0
self
.
init_input
()
residual
=
self
.
inputs
[
'Y'
].
reshape
(
self
.
samples_num
,
1
)
-
self
.
inputs
[
'X'
].
reshape
(
self
.
samples_num
,
1
)
shape
=
self
.
set_shape
()
residual
=
self
.
inputs
[
'Y'
]
-
self
.
inputs
[
'X'
]
loss
=
np
.
vectorize
(
huber_loss_forward
)(
residual
,
self
.
delta
).
astype
(
'float32'
)
self
.
attrs
=
{
'delta'
:
self
.
delta
}
self
.
outputs
=
{
'Residual'
:
residual
,
'Out'
:
loss
.
reshape
((
self
.
samples_num
,
1
))
}
self
.
outputs
=
{
'Residual'
:
residual
,
'Out'
:
loss
.
reshape
(
shape
)}
def
init_input
(
self
):
shape
=
self
.
set_shape
()
self
.
inputs
=
{
'X'
:
np
.
random
.
uniform
(
0
,
1.
,
(
self
.
samples_num
,
1
)).
astype
(
'float32'
),
'Y'
:
np
.
random
.
uniform
(
0
,
1.
,
(
self
.
samples_num
,
1
)).
astype
(
'float32'
),
'X'
:
np
.
random
.
uniform
(
0
,
1.
,
shape
).
astype
(
'float32'
),
'Y'
:
np
.
random
.
uniform
(
0
,
1.
,
shape
).
astype
(
'float32'
),
}
def
set_shape
(
self
):
return
(
64
,
1
)
def
test_check_output
(
self
):
self
.
check_output
()
...
...
@@ -67,12 +65,18 @@ class TestHuberLossOp(OpTest):
def
TestHuberLossOp1
(
TestHuberLossOp
):
def
init_input
(
self
):
self
.
inputs
=
{
'X'
:
np
.
random
.
uniform
(
0
,
1.
,
(
self
.
samples_num
,
1
)).
astype
(
'float32'
),
'Y'
:
np
.
random
.
uniform
(
0
,
1.
,
(
self
.
samples_num
)).
astype
(
'float32'
),
}
def
set_shape
(
self
):
return
(
64
)
def
TestHuberLossOp2
(
TestHuberLossOp
):
def
set_shape
(
self
):
return
(
6
,
6
)
def
TestHuberLossOp2
(
TestHuberLossOp
):
def
set_shape
(
self
):
return
(
6
,
6
,
1
)
if
__name__
==
'__main__'
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录